Skip to content
This repository has been archived by the owner on Jul 3, 2024. It is now read-only.

Commit

Permalink
merge: #187 from feature/173-no-project-file-differenciation-staging
Browse files Browse the repository at this point in the history
173 - No project/file differenciation
  • Loading branch information
0xSwapFeeder authored Nov 3, 2023
2 parents 8c757e6 + 7a497c4 commit 91c4f89
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 68 deletions.
6 changes: 3 additions & 3 deletions toolchains/solidity/core/crates/linter-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
name = "solidhunter"
description = "Fast solidity linter cli"
repository = "https://github.com/astrodevs-labs/osmium"
version = "0.0.3"
version = "0.1.0"
edition = "2021"
license = "GPL-3.0-or-later"

authors = ["AstroDevs-Labs"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
solidhunter-lib = { path = "../linter-lib", version = "0.0.3" }
solidhunter-lib = { path = "../linter-lib", version = "0.0.4" }
clap = { version = "4.0.29", features = ["derive"] }
colored = "2"
serde = { version = "1.0.149", features = ["derive"] }
Expand Down
85 changes: 32 additions & 53 deletions toolchains/solidity/core/crates/linter-cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use clap::Parser;
use clap::{arg, Parser};
use solidhunter_lib::errors::SolidHunterError;
use solidhunter_lib::linter::SolidLinter;
use solidhunter_lib::rules::rule_impl::create_rules_file;
Expand All @@ -8,21 +8,11 @@ use solidhunter_lib::types::LintResult;
#[command(author, version, about, long_about = None)]
struct Args {
#[arg(
short = 'p',
long = "path",
required = false,
default_value = ".",
help = "Specify project path"
help = "Path to the project to lint"
)]
project_path: Vec<String>,

#[arg(
short = 'f',
long = "file",
default_value = "",
help = "Specify a single file to lint"
)]
file_to_lint: String,

path: String,
#[arg(
short = 'e',
long = "exclude",
Expand All @@ -40,7 +30,7 @@ struct Args {

#[arg(
short = 'j',
long = "json_output",
long = "json",
default_value = "false",
help = "Outputs a json format instead"
)]
Expand All @@ -63,28 +53,17 @@ struct Args {
init: bool,
}

fn lint_folder(args: Args) -> Result<(), SolidHunterError> {
let mut linter: SolidLinter = SolidLinter::new();
linter.initialize_rules(&args.rules_file)?;
let mut result = Vec::new();
for path in args.project_path {
result.append(&mut linter.parse_folder(path));
}
for res in result {
print_result(res);
}
Ok(())
}

fn print_result(result: LintResult) {
match result {
Ok(diags) => {
for diag in diags {
println!("{}", &diag);
fn print_result(results: Vec<LintResult>) {
for result in results {
match result {
Ok(diags) => {
for diag in diags {
println!("{}", &diag);
}
}
Err(e) => {
println!("{}", e);
}
}
Err(e) => {
println!("{}", e);
}
}
}
Expand All @@ -105,7 +84,7 @@ fn main() -> Result<(), SolidHunterError> {

if args.verbose {
println!("Verbose output enabled");
println!("Project path: {:?}", args.project_path);
println!("Project path: {:?}", args.path);
println!("Exclude path: {:?}", args.ignore_path);
println!("Using rules file: {}", args.rules_file);
println!("Verbose output: {}", args.verbose);
Expand All @@ -118,30 +97,30 @@ fn main() -> Result<(), SolidHunterError> {
return Ok(());
}

if !args.to_json && args.file_to_lint.is_empty() {
lint_folder(args)?;
} else if !args.file_to_lint.is_empty() {
if args.path.is_empty() {
let mut linter: SolidLinter = SolidLinter::new();
linter.initialize_rules(&args.rules_file)?;

let result = linter.parse_file(args.file_to_lint);
let result = linter.parse_path(args.path);
if !args.to_json {
print_result(result);
} else {
match result {
Ok(diags) => {
let json = serde_json::to_string_pretty(&diags);
match json {
Ok(j) => {
println!("{}", j);
}
Err(e) => {
println!("{}", e);
for res in result {
match res {
Ok(diags) => {
let json = serde_json::to_string_pretty(&diags);
match json {
Ok(j) => {
println!("{}", j);
}
Err(e) => {
println!("{}", e);
}
}
}
}
Err(e) => {
println!("{}", e);
Err(e) => {
println!("{}", e);
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion toolchains/solidity/core/crates/linter-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "solidhunter-lib"
description = "Solidhunter/Osmium solidity linter library"
repository = "https://github.com/astrodevs-labs/osmium"
version = "0.0.3"
version = "0.0.4"
edition = "2021"
authors = ["Astrodevs Labs"]
license = "GPL-3.0-or-later"
Expand Down
19 changes: 9 additions & 10 deletions toolchains/solidity/core/crates/linter-lib/src/linter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::types::*;
use std::fs;

use glob::glob;
use std::path::Path;

#[derive(Debug, Clone)]
pub struct SolidFile {
Expand Down Expand Up @@ -93,16 +94,7 @@ impl SolidLinter {

pub fn parse_file(&mut self, filepath: String) -> LintResult {
let content = fs::read_to_string(filepath.clone())?;
let res = osmium_libs_solidity_ast_extractor::extract::extract_ast_from_content(&content)?;

self._add_file(filepath.as_str(), res, content.as_str());
let mut res: Vec<LintDiag> = Vec::new();

for rule in &self.rules {
let mut diags = rule.diagnose(&self.files[self.files.len() - 1], &self.files);
res.append(&mut diags);
}
Ok(res)
self.parse_content(filepath, content.as_str())
}

pub fn parse_content(&mut self, filepath: String, content: &str) -> LintResult {
Expand All @@ -127,6 +119,13 @@ impl SolidLinter {
}
result
}
pub fn parse_path(&mut self, path: String) -> Vec<LintResult> {
if Path::new(&path).is_file() {
vec![self.parse_file(path)]
} else {
self.parse_folder(path)
}
}

pub fn delete_file(&mut self, path: String) {
loop {
Expand Down
2 changes: 1 addition & 1 deletion toolchains/solidity/core/crates/linter-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ crate-type = ["cdylib", "rlib"]

[dependencies]
osmium-libs-lsp-handler = { path = "../../../../../libs/lsp-handler", version = "0.0.2" }
solidhunter-lib = { path = "../linter-lib", version = "0.0.3" }
solidhunter-lib = { path = "../linter-lib", version = "0.0.4" }
js-sys = "0.3.64"

0 comments on commit 91c4f89

Please sign in to comment.