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

185 - Ignore linting file #195

Merged
merged 15 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions toolchains/solidity/core/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions toolchains/solidity/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ rustdoc-args = ["--cfg", "docsrs"]
[workspace.dependencies]
# workspace crates
extension = { version = "0.1.0", path = "crates/extension", default-features = false }
solidhunter = { version = "0.1.0", path = "crates/solidhunter", default-features = false }
linter-cli = { version = "0.1.0", path = "crates/linter-cli", default-features = false }
solidhunter = { version = "0.2.1", path = "crates/solidhunter", default-features = false }
linter-cli = { version = "0.2.0", path = "crates/linter-cli", default-features = false }
linter-server = { version = "0.1.0", path = "crates/linter-server", default-features = false }
1 change: 1 addition & 0 deletions toolchains/solidity/core/crates/linter-cli/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.solidhunter.json
4 changes: 2 additions & 2 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.1.1"
version = "0.2.1"
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.1.0" }
solidhunter-lib = { path = "../linter-lib", version = "0.2.0" }
clap = { version = "4.0.29", features = ["derive"] }
colored = "2"
serde = { version = "1.0.149", features = ["derive"] }
Expand Down
1 change: 1 addition & 0 deletions toolchains/solidity/core/crates/linter-cli/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Options:
-i, --init Initialize rules file
-h, --help Print help information
-V, --version Print version information
-g, --ignore Specify ignore file
```

## Configuration
Expand Down
44 changes: 23 additions & 21 deletions toolchains/solidity/core/crates/linter-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,8 @@ use solidhunter_lib::types::LintResult;
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
#[arg(
required = false,
default_value = ".",
help = "Path to the project to lint"
)]
path: String,
#[arg(
short = 'e',
long = "exclude",
help = "Exclude part of the project path"
)]
ignore_path: Vec<String>,
#[arg(help = "Paths to the projects to lint")]
paths: Vec<String>,

#[arg(
short = 'r',
Expand Down Expand Up @@ -51,6 +41,9 @@ struct Args {
help = "Initialize rules file"
)]
init: bool,

#[arg(short = 'e', long = "exclude", help = "Specify excluded files")]
exclude: Option<Vec<String>>,
}

fn print_result(results: Vec<LintResult>) {
Expand All @@ -67,7 +60,7 @@ fn print_result(results: Vec<LintResult>) {
}

fn main() -> Result<(), SolidHunterError> {
let args = Args::parse();
let mut args = Args::parse();

if !args.to_json {
println!();
Expand All @@ -82,10 +75,10 @@ fn main() -> Result<(), SolidHunterError> {

if args.verbose {
println!("Verbose output enabled");
println!("Project path: {:?}", args.path);
println!("Exclude path: {:?}", args.ignore_path);
println!("Project path: {:?}", args.paths);
println!("Using rules file: {}", args.rules_file);
println!("Verbose output: {}", args.verbose);
println!("Excluded files: {:?}", args.exclude);
}

if args.init {
Expand All @@ -95,15 +88,24 @@ fn main() -> Result<(), SolidHunterError> {
return Ok(());
}

if !args.path.is_empty() {
let mut linter: SolidLinter = SolidLinter::new();
linter.initialize_rules(&args.rules_file)?;
if args.paths.is_empty() {
args.paths.push(String::from("."));
}

let mut linter: SolidLinter = SolidLinter::new();
linter.initialize_rules(&args.rules_file)?;
linter.initialize_excluded_files(args.exclude.as_ref(), &args.paths)?;

let result = linter.parse_path(&args.path);
let mut results = vec![];
for path in &args.paths {
let result = linter.parse_path(path);
results.push(result);
}
for path_result in results {
if !args.to_json {
print_result(result);
print_result(path_result);
} else {
for res in result {
for res in path_result {
match res {
Ok(diags) => {
let json = serde_json::to_string_pretty(&diags);
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.1.0"
version = "0.2.0"
edition = "2021"
authors = ["Astrodevs Labs"]
license = "GPL-3.0-or-later"
Expand Down
63 changes: 63 additions & 0 deletions toolchains/solidity/core/crates/linter-lib/src/ignore.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use crate::errors::SolidHunterError;
use glob::glob;
use std::path::Path;

fn parse_line(line: &str, path: &Path) -> Vec<String> {
let mut files = Vec::new();
let line = line.replace("./", "");
if let Some(parent) = path.parent() {
if let Some(filepath) = parent.join(line).to_str() {
if let Ok(entries) = glob(filepath) {
for entry in entries.flatten() {
files.push(entry.into_os_string().into_string().unwrap())
}
}
}
}

files
}

fn parse_solihunterignore(filepath: &String) -> Result<Vec<String>, SolidHunterError> {
let mut excluded_files = Vec::new();
let content = std::fs::read_to_string(filepath)?;

for line in content.lines() {
excluded_files.append(&mut parse_line(line, Path::new(filepath)));
}

Ok(excluded_files)
}

fn get_solidhunterignore_paths(filepath: &String) -> Result<Vec<String>, SolidHunterError> {
let mut ignored_files = Vec::new();

if let Ok(entries) = glob(&format!("{}/**/.solidhunterignore", filepath)) {
for entry in entries.flatten() {
ignored_files.push(entry.into_os_string().into_string().unwrap())
}
}

Ok(ignored_files)
}

pub fn get_excluded_files(filepaths: &Vec<String>) -> Result<Vec<String>, SolidHunterError> {
let mut excluded_files = Vec::new();

for filepath in filepaths {
let path = Path::new(filepath);

if path.is_file() {
continue;
}

let solidhunterignore_paths = get_solidhunterignore_paths(filepath)?;

for solidhunterignore_path in solidhunterignore_paths {
if let Ok(mut excluded) = parse_solihunterignore(&solidhunterignore_path) {
excluded_files.append(&mut excluded);
}
}
}
Ok(excluded_files)
}
1 change: 1 addition & 0 deletions toolchains/solidity/core/crates/linter-lib/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod errors;
mod ignore;
pub mod linter;
pub mod rules;
pub mod types;
33 changes: 28 additions & 5 deletions toolchains/solidity/core/crates/linter-lib/src/linter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::rules::types::*;
use crate::types::*;
use std::fs;

use crate::ignore::get_excluded_files;
use glob::glob;
use std::path::Path;

Expand All @@ -20,6 +21,7 @@ pub struct SolidLinter {
files: Vec<SolidFile>,
rule_factory: RuleFactory,
rules: Vec<Box<dyn RuleType>>,
excluded_files: Vec<String>,
}

impl Default for SolidLinter {
Expand All @@ -34,6 +36,7 @@ impl SolidLinter {
files: Vec::new(),
rule_factory: RuleFactory::default(),
rules: vec![],
excluded_files: Vec::new(),
}
}

Expand All @@ -43,6 +46,7 @@ impl SolidLinter {
files: Vec::new(),
rule_factory: RuleFactory::default(),
rules: Vec::new(),
excluded_files: Vec::new(),
};

for rule in default_rules {
Expand All @@ -60,6 +64,22 @@ impl SolidLinter {
Ok(())
}

pub fn initialize_excluded_files(
&mut self,
excluded_filepaths: Option<&Vec<String>>,
filepaths: &Vec<String>,
) -> Result<(), SolidHunterError> {
if let Some(excluded) = excluded_filepaths {
for path in excluded {
self.excluded_files.push(path.clone())
}
}
self.excluded_files
.append(&mut get_excluded_files(filepaths)?);

Ok(())
}

fn _file_exists(&self, path: &str) -> bool {
for file in &self.files {
if file.path == path {
Expand Down Expand Up @@ -92,9 +112,12 @@ impl SolidLinter {
}
}

pub fn parse_file(&mut self, filepath: &str) -> LintResult {
let content = fs::read_to_string(filepath)?;
self.parse_content(filepath, content.as_str())
pub fn parse_file(&mut self, filepath: String) -> LintResult {
let content = fs::read_to_string(filepath.clone())?;
if self.excluded_files.contains(&filepath) {
return Ok(FileDiags::new(content, Vec::new()));
}
self.parse_content(&filepath, content.as_str())
}

pub fn parse_content(&mut self, filepath: &str, content: &str) -> LintResult {
Expand All @@ -114,14 +137,14 @@ impl SolidLinter {
let mut result: Vec<LintResult> = Vec::new();
if let Ok(entries) = glob(&(folder.to_owned() + "/**/*.sol")) {
for entry in entries.flatten() {
result.push(self.parse_file(&entry.into_os_string().into_string().unwrap()));
result.push(self.parse_file(entry.into_os_string().into_string().unwrap()));
}
}
result
}
pub fn parse_path(&mut self, path: &str) -> Vec<LintResult> {
if Path::new(&path).is_file() {
vec![self.parse_file(path)]
vec![self.parse_file(path.to_string())]
} else {
self.parse_folder(path)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "solidhunter", "rules": [
{
"id": "const-name-snakecase",
"severity": "WARNING"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test/test.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pragma solidity 0.8.0;

contract Test {
string public constant symbol = "TEST";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pragma solidity 0.8.0;

contract Test {
string public constant symbol = "TEST";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
./test2.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pragma solidity 0.8.0;

contract Test {
string public constant symbol = "TEST";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pragma solidity 0.8.0;

contract Test {
string public constant symbol = "TEST";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pragma solidity 0.8.0;

contract Test {
string public constant symbol = "TEST";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pragma solidity 0.8.0;

contract Test {
string public constant symbol = "TEST";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pragma solidity 0.8.0;

contract Test {
string public constant symbol = "TEST";
}
Loading