From fe3771bfeee2aff81c136ad7f59d5f95330d33d4 Mon Sep 17 00:00:00 2001 From: Astrodevs CI Date: Wed, 30 Aug 2023 19:53:23 +0000 Subject: [PATCH 01/59] chore: create branch feature/49-solidity-linter-staging --- remove-me-34219c48b31d404ab611.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 remove-me-34219c48b31d404ab611.txt diff --git a/remove-me-34219c48b31d404ab611.txt b/remove-me-34219c48b31d404ab611.txt new file mode 100644 index 00000000..78564588 --- /dev/null +++ b/remove-me-34219c48b31d404ab611.txt @@ -0,0 +1 @@ +34219c48b31d404ab611 From d795be7e091f980879305bbf0d5a830829c7a2f2 Mon Sep 17 00:00:00 2001 From: Astrodevs CI Date: Wed, 30 Aug 2023 19:53:37 +0000 Subject: [PATCH 02/59] chore: create branch feature/49-solidity-linter/75-refactor-solidhunter-staging --- remove-me-ca154a86e5b94f5db084.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 remove-me-ca154a86e5b94f5db084.txt diff --git a/remove-me-ca154a86e5b94f5db084.txt b/remove-me-ca154a86e5b94f5db084.txt new file mode 100644 index 00000000..d92993f9 --- /dev/null +++ b/remove-me-ca154a86e5b94f5db084.txt @@ -0,0 +1 @@ +ca154a86e5b94f5db084 From e260003b74e21c2e84a0a113e19adcc9cda25821 Mon Sep 17 00:00:00 2001 From: 0xtekgrinder <0xtekgrinder@protonmail.com> Date: Mon, 4 Sep 2023 19:15:39 +0200 Subject: [PATCH 03/59] refactor: fix all lint errors and a few compiler warnings --- toolchains/solidity/linter/core/solidhunter-lib/src/lib.rs | 2 +- .../solidity/linter/core/solidhunter-lib/src/linter.rs | 2 +- .../solidhunter-lib/src/rules/naming/use_forbidden_name.rs | 6 +++--- .../core/solidhunter-lib/src/rules/order/import_on_top.rs | 2 +- .../linter/core/solidhunter-lib/src/rules/rule_impl.rs | 1 + .../solidity/linter/core/solidhunter-lib/src/types.rs | 1 - toolchains/solidity/linter/core/src/main.rs | 2 +- 7 files changed, 8 insertions(+), 8 deletions(-) diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/lib.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/lib.rs index a0791e93..f76099ce 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/lib.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/lib.rs @@ -1,4 +1,4 @@ -use crate::types::{Range}; +use crate::types::Range; use solc_wrapper::ast::ast::{CodeLocation, offset_from_location}; pub mod linter; diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs index 172c2117..ae939ef3 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs @@ -79,7 +79,7 @@ impl SolidLinter { pub fn parse_file(&mut self, filepath: String) -> LintResult{ let res = Solc::default().extract_ast_file(filepath.clone()); - + if res.is_err() { println!("{:?}", res); return Err(LintError::SolcError(res.err().unwrap())); diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/use_forbidden_name.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/use_forbidden_name.rs index d5563848..367ee0c2 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/use_forbidden_name.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/use_forbidden_name.rs @@ -41,8 +41,8 @@ impl RuleType for UseForbiddenName { res } - -} + +} impl UseForbiddenName { @@ -54,7 +54,7 @@ impl UseForbiddenName { }; Box::new(rule) } - + pub(crate) fn create_default() -> RuleEntry { RuleEntry { id: UseForbiddenName::RULE_ID.to_string(), diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/order/import_on_top.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/order/import_on_top.rs index 79b878e7..53763bd4 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/order/import_on_top.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/order/import_on_top.rs @@ -30,7 +30,7 @@ impl RuleType for ImportOnTop { SourceUnitChildNodes::ImportDirective(import) => { if i > last_import_location { let location = decode_location(&import.src, &file.content); - + res.push(LintDiag { range: Range { start: Position { line: location.0.line as u64, character: location.0.column as u64 }, diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs index 5ea5fa82..b34b2396 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs @@ -3,6 +3,7 @@ use crate::rules::types::*; /* // Untested +/* fn merge_rules(rules: &mut Vec, new_rules: &Vec) { let mut new_rules_map = HashMap::new(); for rule in new_rules { diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs index 7ad3fa87..e9fffcbe 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs @@ -74,7 +74,6 @@ pub struct Range { pub length: u64, } - #[derive(Clone, Serialize, Deserialize, Debug)] pub enum NumberOrString { Number(i32), diff --git a/toolchains/solidity/linter/core/src/main.rs b/toolchains/solidity/linter/core/src/main.rs index 90ffbfe3..8e71c76b 100644 --- a/toolchains/solidity/linter/core/src/main.rs +++ b/toolchains/solidity/linter/core/src/main.rs @@ -82,7 +82,7 @@ fn print_diag(diag: &solidhunter_lib::types::LintDiag) { } else if diag.range.start.line > 9 { padding = " ".to_string(); } else { - padding = " ".repeat(2).to_string(); + padding = " ".repeat(2); } let line = diag .source_file_content From f7389383c91f983d59f811d690c434660c6a6200 Mon Sep 17 00:00:00 2001 From: leon Date: Thu, 7 Sep 2023 19:02:54 +0200 Subject: [PATCH 04/59] refactor: rename LintError to SolidHunterError --- .../linter/core/solidhunter-lib/src/linter.rs | 41 +++++++++++-------- .../linter/core/solidhunter-lib/src/types.rs | 24 +++++------ 2 files changed, 35 insertions(+), 30 deletions(-) diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs index ae939ef3..91bc184c 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs @@ -1,8 +1,8 @@ -use std::fs; -use crate::types::*; -use crate::rules::types::*; use crate::rules::factory::RuleFactory; use crate::rules::rule_impl::{create_rules_file, parse_rules}; +use crate::rules::types::*; +use crate::types::*; +use std::fs; use glob::glob; use solc_wrapper::{Solc, SourceUnit}; @@ -16,12 +16,11 @@ pub struct SolidFile { pub struct SolidLinter { files: Vec, rule_factory: RuleFactory, - rules : Vec>, + rules: Vec>, } impl SolidLinter { - fn _create_rules(&mut self, rules_config:& String, _first: bool) - { + fn _create_rules(&mut self, rules_config: &String, _first: bool) { let res = parse_rules(rules_config.as_str()); match res { Ok(rules) => { @@ -37,8 +36,7 @@ impl SolidLinter { } } } - pub fn initalize(&mut self, rules_config: &String) - { + pub fn initalize(&mut self, rules_config: &String) { self.rule_factory.register_rules(); self._create_rules(&rules_config, true); } @@ -77,20 +75,25 @@ impl SolidLinter { self.files.push(file); } - pub fn parse_file(&mut self, filepath: String) -> LintResult{ + pub fn parse_file(&mut self, filepath: String) -> LintResult { let res = Solc::default().extract_ast_file(filepath.clone()); if res.is_err() { println!("{:?}", res); - return Err(LintError::SolcError(res.err().unwrap())); + return Err(SolidHunterError::SolcError(res.err().unwrap())); } if self.file_exists(filepath.as_str()) { self.update_file_ast(filepath.as_str(), res.expect("ast not found")); } else { - let content = fs::read_to_string(filepath.clone()).map_err(|e| LintError::IoError(e))?; - self.add_file(filepath.as_str(), res.expect("ast not found"), content.as_str()); + let content = + fs::read_to_string(filepath.clone()).map_err(|e| SolidHunterError::IoError(e))?; + self.add_file( + filepath.as_str(), + res.expect("ast not found"), + content.as_str(), + ); } - let mut res : Vec = Vec::new(); + let mut res: Vec = Vec::new(); for rule in &self.rules { let mut diags = rule.diagnose(&self.files[0], &self.files); @@ -99,21 +102,25 @@ impl SolidLinter { Ok(res) } - pub fn parse_content(&mut self, filepath: String, content : &String) -> LintResult { + pub fn parse_content(&mut self, filepath: String, content: &String) -> LintResult { let res = Solc::default().extract_ast_content(content.to_string()); if res.is_err() { println!("{:?}", res); - return Err(LintError::SolcError(res.err().unwrap())); + return Err(SolidHunterError::SolcError(res.err().unwrap())); } if self.file_exists(filepath.as_str()) { self.update_file_ast(filepath.as_str(), res.expect("ast not found")); } else { - self.add_file(filepath.as_str(), res.expect("ast not found"), content.as_str()); + self.add_file( + filepath.as_str(), + res.expect("ast not found"), + content.as_str(), + ); } - let mut res : Vec = Vec::new(); + let mut res: Vec = Vec::new(); for rule in &self.rules { let mut diags = rule.diagnose(&self.files[0], &self.files); diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs index e9fffcbe..d311f656 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs @@ -1,22 +1,22 @@ -use serde::{Serialize, Deserialize}; +use serde::{Deserialize, Serialize}; use thiserror::Error; #[derive(Error, Debug)] -pub enum LintError { - #[error("LintError: Solc error occured")] +pub enum SolidHunterError { + // Linter errors + #[error("SolidHunterError: Solc error occured")] SolcError(#[from] solc_wrapper::SolcError), - #[error("LintError: Something went wrong with the file")] + #[error("SolidHunterError: Something went wrong with the file")] IoError(#[from] std::io::Error), - #[error("LintError: Something went wrong")] + #[error("SolidHunterError: Something went wrong")] LinterError(String), + // } - -pub type LintResult = Result, LintError>; +pub type LintResult = Result, SolidHunterError>; #[derive(Clone, Serialize, Deserialize, Debug)] pub struct LintDiag { - /// The range at which the message applies. pub range: Range, @@ -29,7 +29,6 @@ pub struct LintDiag { /// The diagnostic's code. Can be omitted. pub code: Option, - #[serde(skip_serializing_if = "Option::is_none")] /// A human-readable string describing the source of this /// diagnostic, e.g. 'typescript' or 'super lint'. @@ -40,11 +39,10 @@ pub struct LintDiag { pub uri: Uri, - #[serde(rename="sourceFileContent")] - pub source_file_content: String + #[serde(rename = "sourceFileContent")] + pub source_file_content: String, } - //////////////////////////////////////////////////////////// /////////////////// RELATED TYPES: ///////////////////////// //////////////////////////////////////////////////////////// @@ -64,7 +62,7 @@ pub enum Severity { /// Reports an information. INFO = 3, /// Reports a hint. - HINT = 4 + HINT = 4, } #[derive(Clone, Serialize, Deserialize, Debug)] From 7f3438b5093f59143ab33fb06f7c8ff4d57ea904 Mon Sep 17 00:00:00 2001 From: leon Date: Thu, 7 Sep 2023 19:13:03 +0200 Subject: [PATCH 05/59] refactor: rename and move errors --- .../linter/core/solidhunter-lib/src/linter.rs | 4 ++-- .../core/solidhunter-lib/src/rules/rule_impl.rs | 12 +++++++----- .../core/solidhunter-lib/src/rules/types.rs | 16 ++++------------ .../linter/core/solidhunter-lib/src/types.rs | 9 +++++++-- 4 files changed, 20 insertions(+), 21 deletions(-) diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs index 91bc184c..6cc2a3f9 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs @@ -85,8 +85,8 @@ impl SolidLinter { if self.file_exists(filepath.as_str()) { self.update_file_ast(filepath.as_str(), res.expect("ast not found")); } else { - let content = - fs::read_to_string(filepath.clone()).map_err(|e| SolidHunterError::IoError(e))?; + let content = fs::read_to_string(filepath.clone()) + .map_err(|e| SolidHunterError::ParsingError(e))?; self.add_file( filepath.as_str(), res.expect("ast not found"), diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs index b34b2396..caedf3b2 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs @@ -1,7 +1,7 @@ use crate::rules::create_default_rules; use crate::rules::types::*; +use crate::types::*; -/* // Untested /* fn merge_rules(rules: &mut Vec, new_rules: &Vec) { @@ -31,7 +31,7 @@ pub fn create_rules_file(path: &str) { std::fs::write(path, serialized).unwrap(); } -type RulesResult = Result; +type RulesResult = Result; pub fn parse_rules(path: &str) -> RulesResult { /*let mut rules = Rules { @@ -42,7 +42,10 @@ pub fn parse_rules(path: &str) -> RulesResult { };*/ if !std::path::Path::new(&path).is_file() { - return Err(RulesError::IoError(std::io::Error::new(std::io::ErrorKind::NotFound, "Rules file not found"))); + return Err(SolidHunterError::IoError(std::io::Error::new( + std::io::ErrorKind::NotFound, + "Rules file not found", + ))); } let file = std::fs::read_to_string(path).unwrap(); let parsed: Rules = serde_json::from_str(&file).unwrap(); @@ -61,7 +64,6 @@ pub fn parse_rules(path: &str) -> RulesResult { Ok(parsed) } - // create rules /* #[macro_export] @@ -85,4 +87,4 @@ macro_rules! create_rule { } } }; -}*/ \ No newline at end of file +}*/ diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/types.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/types.rs index fec52ecf..722e547f 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/types.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/types.rs @@ -1,11 +1,9 @@ -use serde::{Serialize, Deserialize}; use crate::linter::SolidFile; use crate::types::*; - +use serde::{Deserialize, Serialize}; #[derive(Serialize, Deserialize, Debug)] -pub struct RuleEntry -{ +pub struct RuleEntry { pub id: String, pub severity: Severity, pub data: Vec, @@ -16,15 +14,9 @@ pub struct Rules { pub name: String, pub includes: Vec, pub plugins: Vec, - pub rules: Vec -} - -#[derive(Debug)] -pub enum RulesError { - IoError(std::io::Error), + pub rules: Vec, } pub trait RuleType: Send + Sync + 'static { - fn diagnose(&self, file: &SolidFile, files: &Vec) -> Vec; -} \ No newline at end of file +} diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs index d311f656..c17dacb3 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs @@ -6,11 +6,16 @@ pub enum SolidHunterError { // Linter errors #[error("SolidHunterError: Solc error occured")] SolcError(#[from] solc_wrapper::SolcError), - #[error("SolidHunterError: Something went wrong with the file")] - IoError(#[from] std::io::Error), + #[error("SolidHunterError: Something went wrong with the file during parsing")] + ParsingError(#[from] std::io::Error), #[error("SolidHunterError: Something went wrong")] LinterError(String), // + + // RulesError + #[error("SolidHunterError: IO error occured with Rules")] + IoError(std::io::Error), + // } pub type LintResult = Result, SolidHunterError>; From 864cd347e7eb9006765d2f19aba76981bb97d09f Mon Sep 17 00:00:00 2001 From: 0xtekgrinder <0xtekgrinder@protonmail.com> Date: Wed, 6 Sep 2023 22:51:31 +0200 Subject: [PATCH 06/59] refactor(solidhunter): clean linter.rs --- .../linter/core/solc-wrapper/src/lib.rs | 1 - .../linter/core/solidhunter-lib/src/linter.rs | 68 ++++++++++--------- toolchains/solidity/linter/core/src/main.rs | 6 +- 3 files changed, 37 insertions(+), 38 deletions(-) diff --git a/toolchains/solidity/linter/core/solc-wrapper/src/lib.rs b/toolchains/solidity/linter/core/solc-wrapper/src/lib.rs index 320924d0..451ecf6b 100644 --- a/toolchains/solidity/linter/core/solc-wrapper/src/lib.rs +++ b/toolchains/solidity/linter/core/solc-wrapper/src/lib.rs @@ -15,7 +15,6 @@ pub use error::SolcError; use crate::solc::parsing_error::ParsingError; use crate::utils::{get_error_location, get_error_message}; - pub enum ExecuteResult { Ast(String), ParsingError(ParsingError), diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs index 6cc2a3f9..8ff681ac 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs @@ -19,8 +19,30 @@ pub struct SolidLinter { rules: Vec>, } +impl Default for SolidLinter { + + fn default() -> Self + { + let linter = SolidLinter::new(&String::new()); + linter + } +} + impl SolidLinter { - fn _create_rules(&mut self, rules_config: &String, _first: bool) { + + pub fn new(rules_config: &String) -> Self { + let mut linter = SolidLinter { + files: Vec::new(), + rule_factory: RuleFactory::new(), + rules: Vec::new(), + }; + linter.rule_factory.register_rules(); + linter._create_rules(&rules_config, true); + return linter; + } + + fn _create_rules(&mut self, rules_config: &String, first: bool) + { let res = parse_rules(rules_config.as_str()); match res { Ok(rules) => { @@ -30,26 +52,14 @@ impl SolidLinter { } Err(_) => { create_rules_file(rules_config.as_str()); - if _first { + if first { self._create_rules(rules_config, false); } } } } - pub fn initalize(&mut self, rules_config: &String) { - self.rule_factory.register_rules(); - self._create_rules(&rules_config, true); - } - pub fn new() -> SolidLinter { - let linter : SolidLinter = SolidLinter { - files: Vec::new(), - rule_factory: RuleFactory::new(), - rules: Vec::new(), - }; - return linter; - } - fn file_exists(&self, path: &str) -> bool { + fn _file_exists(&self, path: &str) -> bool { for file in &self.files { if file.path == path { return true; @@ -58,7 +68,7 @@ impl SolidLinter { false } - fn update_file_ast(&mut self, path: &str, ast: SourceUnit) { + fn _update_file_ast(&mut self, path: &str, ast: SourceUnit) { for file in &mut self.files { if file.path == path { file.data = ast.clone(); @@ -66,7 +76,7 @@ impl SolidLinter { } } - fn add_file(&mut self, path: &str, ast: SourceUnit, content: &str) { + fn _add_file(&mut self, path: &str, ast: SourceUnit, content: &str) { let file = SolidFile { data: ast, path: String::from(path), @@ -82,16 +92,11 @@ impl SolidLinter { println!("{:?}", res); return Err(SolidHunterError::SolcError(res.err().unwrap())); } - if self.file_exists(filepath.as_str()) { - self.update_file_ast(filepath.as_str(), res.expect("ast not found")); + if self._file_exists(filepath.as_str()) { + self._update_file_ast(filepath.as_str(), res.expect("ast not found")); } else { - let content = fs::read_to_string(filepath.clone()) - .map_err(|e| SolidHunterError::ParsingError(e))?; - self.add_file( - filepath.as_str(), - res.expect("ast not found"), - content.as_str(), - ); + let content = fs::read_to_string(filepath.clone()).map_err(|e| LintError::IoError(e))?; + self._add_file(filepath.as_str(), res.expect("ast not found"), content.as_str()); } let mut res: Vec = Vec::new(); @@ -110,14 +115,10 @@ impl SolidLinter { return Err(SolidHunterError::SolcError(res.err().unwrap())); } - if self.file_exists(filepath.as_str()) { - self.update_file_ast(filepath.as_str(), res.expect("ast not found")); + if self._file_exists(filepath.as_str()) { + self._update_file_ast(filepath.as_str(), res.expect("ast not found")); } else { - self.add_file( - filepath.as_str(), - res.expect("ast not found"), - content.as_str(), - ); + self._add_file(filepath.as_str(), res.expect("ast not found"), content.as_str()); } let mut res: Vec = Vec::new(); @@ -140,6 +141,7 @@ impl SolidLinter { } result } + pub fn delete_file(&mut self, path: String) { loop { let idx = self.files.iter().position(|x| x.path == path); diff --git a/toolchains/solidity/linter/core/src/main.rs b/toolchains/solidity/linter/core/src/main.rs index 8e71c76b..81eab428 100644 --- a/toolchains/solidity/linter/core/src/main.rs +++ b/toolchains/solidity/linter/core/src/main.rs @@ -106,8 +106,7 @@ fn print_diag(diag: &solidhunter_lib::types::LintDiag) { } fn lint_folder(args: Args) { - let mut linter: SolidLinter = SolidLinter::new(); - linter.initalize(&args.rules_file); + let mut linter: SolidLinter = SolidLinter::new(&args.rules_file); let mut result = Vec::new(); for path in args.project_path { result.append(&mut linter.parse_folder(path)); @@ -158,8 +157,7 @@ fn main() { if !args.to_json && args.file_to_lint.is_empty() { lint_folder(args); } else if !args.file_to_lint.is_empty() { - let mut linter: SolidLinter = SolidLinter::new(); - linter.initalize(&args.rules_file); + let mut linter: SolidLinter = SolidLinter::new(&args.rules_file); let result = linter.parse_file(args.file_to_lint); if !args.to_json { From 97dfb1459ef5a2519f12826bd17fc62207e63729 Mon Sep 17 00:00:00 2001 From: 0xtekgrinder <0xtekgrinder@protonmail.com> Date: Thu, 7 Sep 2023 19:35:12 +0200 Subject: [PATCH 07/59] refactor(solidhunter): refactor LintDiag and a few other things --- toolchains/solidity/linter/core/Cargo.toml | 2 - .../linter/core/solidhunter-lib/Cargo.toml | 2 - .../linter/core/solidhunter-lib/src/lib.rs | 12 ----- .../best_practises/function_max_lines.rs | 4 +- .../core/solidhunter-lib/src/rules/factory.rs | 4 +- .../linter/core/solidhunter-lib/src/types.rs | 35 +++++++++++++ toolchains/solidity/linter/core/src/main.rs | 50 ++----------------- 7 files changed, 42 insertions(+), 67 deletions(-) diff --git a/toolchains/solidity/linter/core/Cargo.toml b/toolchains/solidity/linter/core/Cargo.toml index 89fee01d..468a65a1 100644 --- a/toolchains/solidity/linter/core/Cargo.toml +++ b/toolchains/solidity/linter/core/Cargo.toml @@ -5,8 +5,6 @@ edition = "2021" authors = ["Astrodevs Labs"] license = "GPL-3.0-or-later" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - [dependencies] clap = { version = "4.0.29", features = ["derive"] } colored = "2" diff --git a/toolchains/solidity/linter/core/solidhunter-lib/Cargo.toml b/toolchains/solidity/linter/core/solidhunter-lib/Cargo.toml index 3e4aa694..e128998e 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/Cargo.toml +++ b/toolchains/solidity/linter/core/solidhunter-lib/Cargo.toml @@ -5,8 +5,6 @@ edition = "2021" authors = ["Astrodevs Labs"] license = "GPL-3.0-or-later" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - [dependencies] clap = { version = "4.0.29", features = ["derive"] } colored = "2" diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/lib.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/lib.rs index f76099ce..aa12c366 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/lib.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/lib.rs @@ -1,19 +1,7 @@ -use crate::types::Range; -use solc_wrapper::ast::ast::{CodeLocation, offset_from_location}; - pub mod linter; pub mod types; pub mod rules; -pub fn offset_from_range(content: &str, range: &Range) -> usize { - let loc = CodeLocation { - line: range.start.line as usize, - column: range.start.character as usize, - length: range.length as usize, - }; - offset_from_location(content, &loc) -} - #[cfg(test)] mod tests { use super::*; diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/function_max_lines.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/function_max_lines.rs index bd122394..0b9cdc24 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/function_max_lines.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/function_max_lines.rs @@ -117,13 +117,13 @@ impl FunctionMaxLines { }; Box::new(rule) } - + pub fn create_default() -> RuleEntry { RuleEntry { id: FunctionMaxLines::RULE_ID.to_string(), severity: Severity::WARNING, data: vec![DEFAULT_MAX_LINES.to_string()] - + } } } diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/factory.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/factory.rs index 5c060655..90962a44 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/factory.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/factory.rs @@ -14,12 +14,12 @@ impl RuleFactory { _rules: Vec::new(), } } - + pub fn register_rules(&mut self) { self._buildables = create_rules() } - + pub fn create_rule(&self, rule: RuleEntry) -> Box { let rule_type = self._buildables.get(&rule.id); diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs index c17dacb3..0660e8d3 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs @@ -1,5 +1,7 @@ use serde::{Deserialize, Serialize}; use thiserror::Error; +use std::fmt; +use colored::Colorize; #[derive(Error, Debug)] pub enum SolidHunterError { @@ -48,6 +50,39 @@ pub struct LintDiag { pub source_file_content: String, } +fn severity_to_string(severity: Option) -> String { + match severity { + Some(Severity::ERROR) => "error".to_string().red(), + Some(Severity::WARNING) => "warning".to_string().yellow(), + Some(Severity::INFO) => "info".to_string().blue(), + Some(Severity::HINT) => "hint".to_string().green(), + _ => "error".to_string().red(), + } + .to_string() +} + +impl fmt::Display for LintDiag { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let padding: String; + if self.range.start.line > 99 { + padding = " ".repeat(0); + } else if self.range.start.line > 9 { + padding = " ".to_string(); + } else { + padding = " ".repeat(2); + } + let line = self + .source_file_content + .lines() + .nth((self.range.start.line - 1) as usize) + .unwrap(); + + write!(f, "\n{}: {}\n --> {}:{}:{}\n |\n{}{}|{}\n |{}{}", severity_to_string(self.severity), self.message, self.uri, self.range.start.line, self.range.start.character, self.range.start.line, padding, line, " ".repeat(self.range.start.character as usize), + "^".repeat(self.range.length as usize)) + } +} + + //////////////////////////////////////////////////////////// /////////////////// RELATED TYPES: ///////////////////////// //////////////////////////////////////////////////////////// diff --git a/toolchains/solidity/linter/core/src/main.rs b/toolchains/solidity/linter/core/src/main.rs index 81eab428..16cc95ea 100644 --- a/toolchains/solidity/linter/core/src/main.rs +++ b/toolchains/solidity/linter/core/src/main.rs @@ -1,9 +1,6 @@ use clap::Parser; -use colored::Colorize; use solidhunter_lib::linter::SolidLinter; - -use solidhunter_lib::rules::rule_impl::create_rules_file; -use solidhunter_lib::types::Severity; +use solidhunter_lib::rules::rule_impl::{create_rules_file}; #[derive(Parser, Debug)] #[command(author, version, about, long_about = None)] @@ -64,47 +61,6 @@ struct Args { init: bool, } -pub fn severity_to_string(severity: Option) -> String { - match severity { - Some(Severity::ERROR) => "error".to_string().red(), - Some(Severity::WARNING) => "warning".to_string().yellow(), - Some(Severity::INFO) => "info".to_string().blue(), - Some(Severity::HINT) => "hint".to_string().green(), - _ => "error".to_string().red(), - } - .to_string() -} - -fn print_diag(diag: &solidhunter_lib::types::LintDiag) { - let padding: String; - if diag.range.start.line > 99 { - padding = "".to_string(); - } else if diag.range.start.line > 9 { - padding = " ".to_string(); - } else { - padding = " ".repeat(2); - } - let line = diag - .source_file_content - .lines() - .nth((diag.range.start.line - 1) as usize) - .unwrap(); - - println!("\n{}: {}", severity_to_string(diag.severity), diag.message); - println!( - " --> {}:{}:{}", - diag.uri, diag.range.start.line, diag.range.start.character, - ); - println!(" |"); - //TODO: add code to print - println!("{}{}|{}", diag.range.start.line, padding, line); - println!( - " |{}{}", - " ".repeat(diag.range.start.character as usize), - "^".repeat(diag.range.length as usize) - ); -} - fn lint_folder(args: Args) { let mut linter: SolidLinter = SolidLinter::new(&args.rules_file); let mut result = Vec::new(); @@ -115,7 +71,7 @@ fn lint_folder(args: Args) { match res { Ok(diags) => { for diag in diags { - print_diag(&diag); + println!("{}", &diag); } } Err(e) => { @@ -164,7 +120,7 @@ fn main() { match result { Ok(diags) => { for diag in diags { - print_diag(&diag); + println!("{}", &diag); } } Err(e) => { From 9b2df104ae58dde642d00b1c1b9e8ca1e591d934 Mon Sep 17 00:00:00 2001 From: EnergyCube Date: Thu, 7 Sep 2023 19:43:45 +0200 Subject: [PATCH 08/59] refactor: renamed invalid error enum name --- toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs index 8ff681ac..e6c78f98 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs @@ -95,7 +95,7 @@ impl SolidLinter { if self._file_exists(filepath.as_str()) { self._update_file_ast(filepath.as_str(), res.expect("ast not found")); } else { - let content = fs::read_to_string(filepath.clone()).map_err(|e| LintError::IoError(e))?; + let content = fs::read_to_string(filepath.clone()).map_err(|e| SolidHunterError::IoError(e))?; self._add_file(filepath.as_str(), res.expect("ast not found"), content.as_str()); } let mut res: Vec = Vec::new(); From 0ffd4888c519ccf37174139b3cc62f2474f14f9b Mon Sep 17 00:00:00 2001 From: 0xtekgrinder <0xtekgrinder@protonmail.com> Date: Thu, 7 Sep 2023 20:01:31 +0200 Subject: [PATCH 09/59] refactor(solidhunter): clean up the Factory and apply a default impl --- .../linter/core/solidhunter-lib/src/linter.rs | 3 +-- .../core/solidhunter-lib/src/rules/factory.rs | 14 +++++++++----- .../core/solidhunter-lib/src/rules/rule_impl.rs | 7 +++---- toolchains/solidity/linter/core/src/main.rs | 2 +- 4 files changed, 14 insertions(+), 12 deletions(-) diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs index e6c78f98..1a8f1285 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs @@ -33,10 +33,9 @@ impl SolidLinter { pub fn new(rules_config: &String) -> Self { let mut linter = SolidLinter { files: Vec::new(), - rule_factory: RuleFactory::new(), + rule_factory: RuleFactory::default(), rules: Vec::new(), }; - linter.rule_factory.register_rules(); linter._create_rules(&rules_config, true); return linter; } diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/factory.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/factory.rs index 90962a44..33d9ddcd 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/factory.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/factory.rs @@ -7,6 +7,15 @@ pub struct RuleFactory { _rules : Vec>, } +impl Default for RuleFactory { + fn default() -> Self { + RuleFactory { + _buildables: create_rules(), + _rules: Vec::new(), + } + } +} + impl RuleFactory { pub fn new() -> RuleFactory { RuleFactory { @@ -15,11 +24,6 @@ impl RuleFactory { } } - pub fn register_rules(&mut self) - { - self._buildables = create_rules() - } - pub fn create_rule(&self, rule: RuleEntry) -> Box { let rule_type = self._buildables.get(&rule.id); diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs index caedf3b2..53aa647e 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs @@ -31,10 +31,9 @@ pub fn create_rules_file(path: &str) { std::fs::write(path, serialized).unwrap(); } -type RulesResult = Result; - -pub fn parse_rules(path: &str) -> RulesResult { - /*let mut rules = Rules { +pub fn parse_rules(path: &str) -> Result { + /* + let rules = Rules { name: String::new(), includes: Vec::new(), plugins: Vec::new(), diff --git a/toolchains/solidity/linter/core/src/main.rs b/toolchains/solidity/linter/core/src/main.rs index 16cc95ea..92aba6bc 100644 --- a/toolchains/solidity/linter/core/src/main.rs +++ b/toolchains/solidity/linter/core/src/main.rs @@ -1,6 +1,6 @@ use clap::Parser; use solidhunter_lib::linter::SolidLinter; -use solidhunter_lib::rules::rule_impl::{create_rules_file}; +use solidhunter_lib::rules::rule_impl::create_rules_file; #[derive(Parser, Debug)] #[command(author, version, about, long_about = None)] From c6dc87cd21181ffa22052b5947fbf9e69fd44d10 Mon Sep 17 00:00:00 2001 From: EnergyCube Date: Thu, 7 Sep 2023 20:05:19 +0200 Subject: [PATCH 10/59] refactor(solidhunter): SolidHunterError moved to errors.rs & fixed warnings & styled lib --- .../linter/core/solidhunter-lib/src/errors.rs | 16 ++++ .../linter/core/solidhunter-lib/src/lib.rs | 6 +- .../linter/core/solidhunter-lib/src/linter.rs | 24 +++-- .../best_practises/function_max_lines.rs | 43 +++++---- .../src/rules/best_practises/line_maxlen.rs | 27 +++--- .../rules/best_practises/max_states_count.rs | 33 +++---- .../src/rules/best_practises/mod.rs | 17 ++-- .../src/rules/best_practises/reason_string.rs | 87 +++++++++---------- .../core/solidhunter-lib/src/rules/factory.rs | 11 ++- .../src/rules/miscellaneous/mod.rs | 6 +- .../src/rules/miscellaneous/quotes.rs | 27 +++--- .../core/solidhunter-lib/src/rules/mod.rs | 10 +-- .../rules/naming/contract_name_pascalcase.rs | 39 +++++---- .../src/rules/naming/func_name_camelcase.rs | 47 ++++++---- .../rules/naming/func_param_name_camelcase.rs | 50 +++++++---- .../solidhunter-lib/src/rules/naming/mod.rs | 26 ++++-- .../src/rules/naming/use_forbidden_name.rs | 34 ++++---- .../src/rules/order/import_on_top.rs | 24 ++--- .../solidhunter-lib/src/rules/order/mod.rs | 8 +- .../solidhunter-lib/src/rules/rule_impl.rs | 1 - .../linter/core/solidhunter-lib/src/types.rs | 38 ++++---- 21 files changed, 323 insertions(+), 251 deletions(-) create mode 100644 toolchains/solidity/linter/core/solidhunter-lib/src/errors.rs diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/errors.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/errors.rs new file mode 100644 index 00000000..6373641d --- /dev/null +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/errors.rs @@ -0,0 +1,16 @@ +use thiserror::Error; + +#[derive(Error, Debug)] +pub enum SolidHunterError { + // Linter errors + #[error("SolidHunterError: Solc error occured")] + SolcError(#[from] solc_wrapper::SolcError), + #[error("SolidHunterError: Something went wrong with the file during parsing")] + ParsingError(#[from] std::io::Error), + #[error("SolidHunterError: Something went wrong")] + LinterError(String), + + // RulesError + #[error("SolidHunterError: IO error occured with Rules")] + IoError(std::io::Error), +} diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/lib.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/lib.rs index aa12c366..d2ebe5f7 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/lib.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/lib.rs @@ -1,12 +1,12 @@ +pub mod errors; pub mod linter; -pub mod types; pub mod rules; +pub mod types; #[cfg(test)] mod tests { use super::*; #[test] - fn it_works() { - } + fn it_works() {} } diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs index 1a8f1285..51734a82 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs @@ -1,3 +1,4 @@ +use crate::errors::SolidHunterError; use crate::rules::factory::RuleFactory; use crate::rules::rule_impl::{create_rules_file, parse_rules}; use crate::rules::types::*; @@ -20,16 +21,13 @@ pub struct SolidLinter { } impl Default for SolidLinter { - - fn default() -> Self - { + fn default() -> Self { let linter = SolidLinter::new(&String::new()); linter } } impl SolidLinter { - pub fn new(rules_config: &String) -> Self { let mut linter = SolidLinter { files: Vec::new(), @@ -40,8 +38,7 @@ impl SolidLinter { return linter; } - fn _create_rules(&mut self, rules_config: &String, first: bool) - { + fn _create_rules(&mut self, rules_config: &String, first: bool) { let res = parse_rules(rules_config.as_str()); match res { Ok(rules) => { @@ -94,8 +91,13 @@ impl SolidLinter { if self._file_exists(filepath.as_str()) { self._update_file_ast(filepath.as_str(), res.expect("ast not found")); } else { - let content = fs::read_to_string(filepath.clone()).map_err(|e| SolidHunterError::IoError(e))?; - self._add_file(filepath.as_str(), res.expect("ast not found"), content.as_str()); + let content = + fs::read_to_string(filepath.clone()).map_err(|e| SolidHunterError::IoError(e))?; + self._add_file( + filepath.as_str(), + res.expect("ast not found"), + content.as_str(), + ); } let mut res: Vec = Vec::new(); @@ -117,7 +119,11 @@ impl SolidLinter { if self._file_exists(filepath.as_str()) { self._update_file_ast(filepath.as_str(), res.expect("ast not found")); } else { - self._add_file(filepath.as_str(), res.expect("ast not found"), content.as_str()); + self._add_file( + filepath.as_str(), + res.expect("ast not found"), + content.as_str(), + ); } let mut res: Vec = Vec::new(); diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/function_max_lines.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/function_max_lines.rs index 0b9cdc24..7a7fcd29 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/function_max_lines.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/function_max_lines.rs @@ -11,11 +11,10 @@ pub const DEFAULT_MAX_LINES: usize = 20; pub struct FunctionMaxLines { number_max_lines: usize, - _data: RuleEntry + _data: RuleEntry, } impl RuleType for FunctionMaxLines { - fn diagnose(&self, _file: &SolidFile, _files: &Vec) -> Vec { let mut res = Vec::new(); @@ -30,7 +29,7 @@ impl RuleType for FunctionMaxLines { source: None, message: DEFAULT_MESSAGE.to_string(), uri: _file.path.clone(), - source_file_content: _file.content.clone() + source_file_content: _file.content.clone(), }); } } @@ -38,17 +37,24 @@ impl RuleType for FunctionMaxLines { } } - // returns a struct containing the line number of the start and end of the function if it is too long -fn check_function_lines(_file: &SolidFile, function: Box, nb_max_line: usize) -> Option { +fn check_function_lines( + _file: &SolidFile, + function: Box, + nb_max_line: usize, +) -> Option { let mut res: Option = None; let function_copy_name_location = &function.src; let (_start, _) = decode_location(function_copy_name_location.as_str(), _file.content.as_str()); - let index = function_copy_name_location.split(":").collect::>()[0].parse::().unwrap(); - let mut function_lines:usize = 0; - let mut left_bracket:usize = 0; - let mut right_bracket:usize = 0; - let mut last_bracket_line:usize = 0; + let index = function_copy_name_location + .split(":") + .collect::>()[0] + .parse::() + .unwrap(); + let mut function_lines: usize = 0; + let mut left_bracket: usize = 0; + let mut right_bracket: usize = 0; + let mut last_bracket_line: usize = 0; for (_, c) in _file.content.chars().enumerate().skip(index) { if c == '{' { @@ -75,13 +81,15 @@ fn check_function_lines(_file: &SolidFile, function: Box, nb line: last_bracket_line as u64, character: 1, }, - length: _file.content.lines().nth(_start.line - 1)?.len() as u64 + length: _file.content.lines().nth(_start.line - 1)?.len() as u64, }); } res } -fn get_all_functions_from_ast(ast_nodes: &Vec) -> Vec> { +fn get_all_functions_from_ast( + ast_nodes: &Vec, +) -> Vec> { let mut res = Vec::new(); for node in ast_nodes { @@ -100,20 +108,18 @@ fn get_all_functions_from_ast(ast_nodes: &Vec) -> Vec Box { let max_number_lines = match data.data[0].parse::() { Ok(v) => v, - Err(_) => DEFAULT_MAX_LINES + Err(_) => DEFAULT_MAX_LINES, }; - let rule = FunctionMaxLines { + let rule = FunctionMaxLines { number_max_lines: max_number_lines, - _data: data + _data: data, }; Box::new(rule) } @@ -122,8 +128,7 @@ impl FunctionMaxLines { RuleEntry { id: FunctionMaxLines::RULE_ID.to_string(), severity: Severity::WARNING, - data: vec![DEFAULT_MAX_LINES.to_string()] - + data: vec![DEFAULT_MAX_LINES.to_string()], } } } diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/line_maxlen.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/line_maxlen.rs index 5b2f35a9..dff8cd56 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/line_maxlen.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/line_maxlen.rs @@ -4,11 +4,10 @@ use crate::types::*; pub struct LineMaxLen { max_len: usize, - data: RuleEntry + data: RuleEntry, } impl RuleType for LineMaxLen { - fn diagnose(&self, file: &SolidFile, _files: &Vec) -> Vec { let mut res = Vec::new(); let mut line_idx = 1; @@ -17,31 +16,35 @@ impl RuleType for LineMaxLen { if line.len() > self.max_len { res.push(LintDiag { range: Range { - start: Position { line: line_idx, character: self.max_len as u64}, - end: Position { line: line_idx, character: line.len() as u64 }, - length: (line.len() - self.max_len) as u64 + start: Position { + line: line_idx, + character: self.max_len as u64, + }, + end: Position { + line: line_idx, + character: line.len() as u64, + }, + length: (line.len() - self.max_len) as u64, }, message: format!("Line is too long: {}", line.len()), severity: Some(self.data.severity), code: None, source: None, uri: file.path.clone(), - source_file_content: file.content.clone() + source_file_content: file.content.clone(), }); } line_idx += 1; } res } - - } impl LineMaxLen { pub(crate) fn create(data: RuleEntry) -> Box { - let rule = LineMaxLen { + let rule = LineMaxLen { max_len: data.data[0].parse::().unwrap(), - data + data, }; Box::new(rule) } @@ -50,7 +53,7 @@ impl LineMaxLen { RuleEntry { id: "line-max-len".to_string(), severity: Severity::WARNING, - data: vec!["80".to_string()] + data: vec!["80".to_string()], } } -} \ No newline at end of file +} diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/max_states_count.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/max_states_count.rs index 76a7a046..bbd424cd 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/max_states_count.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/max_states_count.rs @@ -1,16 +1,14 @@ use crate::linter::SolidFile; -use solc_wrapper::*; use crate::rules::types::*; use crate::types::*; - +use solc_wrapper::*; pub struct MaxStatesCount { max_states: usize, - data: RuleEntry + data: RuleEntry, } impl RuleType for MaxStatesCount { - fn diagnose(&self, file: &SolidFile, _files: &Vec) -> Vec { let mut res = Vec::new(); @@ -20,28 +18,34 @@ impl RuleType for MaxStatesCount { for node in file.data.nodes.iter() { let contract = match node { SourceUnitChildNodes::ContractDefinition(contract) => contract, - _ => continue + _ => continue, }; for node_var in contract.nodes.iter() { let var = match node_var { ContractDefinitionChildNodes::VariableDeclaration(var) => var, - _ => continue + _ => continue, }; count += 1; if count > self.max_states { let location = decode_location(&var.src, &file.content); res.push(LintDiag { range: Range { - start: Position { line: location.0.line as u64, character: location.0.column as u64}, - end: Position { line: location.1.line as u64, character: location.1.column as u64 }, - length: location.0.length as u64 + start: Position { + line: location.0.line as u64, + character: location.0.column as u64, + }, + end: Position { + line: location.1.line as u64, + character: location.1.column as u64, + }, + length: location.0.length as u64, }, message: format!("Too many states: {}", count), severity: Some(self.data.severity), code: None, source: None, uri: file.path.clone(), - source_file_content: file.content.clone() + source_file_content: file.content.clone(), }); } } @@ -51,13 +55,12 @@ impl RuleType for MaxStatesCount { } impl MaxStatesCount { - pub(crate) const RULE_ID: &'static str = "max-states-count"; pub(crate) fn create(data: RuleEntry) -> Box { - let rule = MaxStatesCount { + let rule = MaxStatesCount { max_states: data.data[0].parse::().unwrap(), - data + data, }; Box::new(rule) } @@ -66,7 +69,7 @@ impl MaxStatesCount { RuleEntry { id: MaxStatesCount::RULE_ID.to_string(), severity: Severity::WARNING, - data: vec!["15".to_string()] + data: vec!["15".to_string()], } } -} \ No newline at end of file +} diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/mod.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/mod.rs index 62398701..845683f9 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/mod.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/mod.rs @@ -1,18 +1,18 @@ -use std::collections::HashMap; use crate::rules::types::{RuleEntry, RuleType}; +use std::collections::HashMap; #[macro_use] pub mod line_maxlen; -pub mod max_states_count; pub mod function_max_lines; +pub mod max_states_count; pub mod reason_string; // List all rules +use crate::rules::best_practises::function_max_lines::FunctionMaxLines; use crate::rules::best_practises::line_maxlen::LineMaxLen; use crate::rules::best_practises::max_states_count::MaxStatesCount; use crate::rules::best_practises::reason_string::ReasonString; -use crate::rules::best_practises::function_max_lines::FunctionMaxLines; use crate::rules::RuleBuilder; pub fn create_default_rules() -> Vec { @@ -27,12 +27,15 @@ pub fn create_default_rules() -> Vec { } pub fn create_rules() -> HashMap Box> { - let mut rules : HashMap = HashMap::new(); + let mut rules: HashMap = HashMap::new(); - rules.insert( "line-max-len".to_string(), LineMaxLen::create); + rules.insert("line-max-len".to_string(), LineMaxLen::create); rules.insert(MaxStatesCount::RULE_ID.to_string(), MaxStatesCount::create); - rules.insert(FunctionMaxLines::RULE_ID.to_string(), FunctionMaxLines::create); + rules.insert( + FunctionMaxLines::RULE_ID.to_string(), + FunctionMaxLines::create, + ); rules.insert(reason_string::RULE_ID.to_string(), ReasonString::create); rules -} \ No newline at end of file +} diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/reason_string.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/reason_string.rs index 8181c676..767bb3f4 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/reason_string.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/reason_string.rs @@ -1,9 +1,9 @@ -use solc_wrapper::{NodeType, Expression, decode_location}; -use solc_wrapper::ast::utils::{get_all_nodes_by_type, self}; +use solc_wrapper::ast::utils::{self, get_all_nodes_by_type}; +use solc_wrapper::{decode_location, Expression, NodeType}; use crate::linter::SolidFile; use crate::rules::types::{RuleEntry, RuleType}; -use crate::types::{LintDiag, Range, Position, Severity}; +use crate::types::{LintDiag, Position, Range, Severity}; pub const RULE_ID: &str = "reason-string"; const DEFAULT_SEVERITY: Severity = Severity::WARNING; @@ -11,27 +11,24 @@ const DEFAULT_SEVERITY: Severity = Severity::WARNING; // Specific const DEFAULT_LENGTH: u32 = 32; - pub struct ReasonString { max_length: u32, - data: RuleEntry + data: RuleEntry, } impl RuleType for ReasonString { - fn diagnose(&self, file: &SolidFile, _files: &Vec) -> Vec { let mut res = Vec::new(); let nodes = get_all_nodes_by_type(file.data.clone(), NodeType::FunctionCall); for i in &nodes { match i { - utils::Nodes::FunctionCall(j) => { - match &j.expression { - Expression::Identifier(v) => { - if v.name == "require" { - if j.arguments.len() != 2 { - let location = decode_location(&j.src, &file.content); - let diag = LintDiag { + utils::Nodes::FunctionCall(j) => match &j.expression { + Expression::Identifier(v) => { + if v.name == "require" { + if j.arguments.len() != 2 { + let location = decode_location(&j.src, &file.content); + let diag = LintDiag { range: Range { start: Position { line: location.0.line as u64, character: location.0.column as u64 }, end: Position { line: location.1.line as u64, character: location.1.column as u64 }, @@ -44,14 +41,17 @@ impl RuleType for ReasonString { uri: file.path.clone(), source_file_content: file.content.clone(), }; - res.push(diag); - } else { - for nj in &j.arguments { - match nj { - Expression::Literal(z) => { - if z.value.clone().unwrap().len() > self.max_length as usize { - let location = decode_location(&z.src, &file.content); - let diag = LintDiag { + res.push(diag); + } else { + for nj in &j.arguments { + match nj { + Expression::Literal(z) => { + if z.value.clone().unwrap().len() + > self.max_length as usize + { + let location = + decode_location(&z.src, &file.content); + let diag = LintDiag { range: Range { start: Position { line: location.0.line as u64, character: location.0.column as u64 }, end: Position { line: location.1.line as u64, character: location.1.column as u64 }, @@ -64,17 +64,17 @@ impl RuleType for ReasonString { uri: file.path.clone(), source_file_content: file.content.clone(), }; - res.push(diag); - } + res.push(diag); } - _ => {} } + _ => {} } } - } else if v.name == "revert" { - if j.arguments.len() == 0 { - let location = decode_location(&j.src, &file.content); - let diag = LintDiag { + } + } else if v.name == "revert" { + if j.arguments.len() == 0 { + let location = decode_location(&j.src, &file.content); + let diag = LintDiag { range: Range { start: Position { line: location.0.line as u64, character: location.0.column as u64 }, end: Position { line: location.1.line as u64, character: location.1.column as u64 }, @@ -87,13 +87,14 @@ impl RuleType for ReasonString { uri: file.path.clone(), source_file_content: file.content.clone(), }; - res.push(diag); - } else { - match &j.arguments[0] { - Expression::Literal(z) => { - if z.value.clone().unwrap().len() > self.max_length as usize { - let location = decode_location(&z.src, &file.content); - let diag = LintDiag { + res.push(diag); + } else { + match &j.arguments[0] { + Expression::Literal(z) => { + if z.value.clone().unwrap().len() > self.max_length as usize + { + let location = decode_location(&z.src, &file.content); + let diag = LintDiag { range: Range { start: Position { line: location.0.line as u64, character: location.0.column as u64 }, end: Position { line: location.1.line as u64, character: location.1.column as u64 }, @@ -106,30 +107,28 @@ impl RuleType for ReasonString { uri: file.path.clone(), source_file_content: file.content.clone(), }; - res.push(diag); - } + res.push(diag); } - _ => {} } + _ => {} } } } - _ => {} } - } + _ => {} + }, _ => {} } } res } - } impl ReasonString { pub fn create(data: RuleEntry) -> Box { - let rule = ReasonString { + let rule = ReasonString { max_length: data.data[0].parse::().unwrap(), - data + data, }; Box::new(rule) } @@ -138,7 +137,7 @@ impl ReasonString { RuleEntry { id: RULE_ID.to_string(), severity: DEFAULT_SEVERITY, - data: vec![DEFAULT_LENGTH.to_string()] + data: vec![DEFAULT_LENGTH.to_string()], } } } diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/factory.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/factory.rs index 33d9ddcd..0e94596a 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/factory.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/factory.rs @@ -1,10 +1,10 @@ -use std::collections::HashMap; -use crate::rules::types::*; use crate::rules::create_rules; +use crate::rules::types::*; +use std::collections::HashMap; pub struct RuleFactory { _buildables: HashMap Box>, - _rules : Vec>, + _rules: Vec>, } impl Default for RuleFactory { @@ -24,12 +24,11 @@ impl RuleFactory { } } - pub fn create_rule(&self, rule: RuleEntry) -> Box - { + pub fn create_rule(&self, rule: RuleEntry) -> Box { let rule_type = self._buildables.get(&rule.id); if rule_type.is_none() { panic!("Rule {} not found", &rule.id); } rule_type.unwrap()(rule) } -} \ No newline at end of file +} diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/miscellaneous/mod.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/miscellaneous/mod.rs index 09c71213..2db97300 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/miscellaneous/mod.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/miscellaneous/mod.rs @@ -1,5 +1,5 @@ -use std::collections::HashMap; use crate::rules::types::{RuleEntry, RuleType}; +use std::collections::HashMap; #[macro_use] pub mod quotes; @@ -18,9 +18,9 @@ pub fn create_default_rules() -> Vec { } pub fn create_rules() -> HashMap Box> { - let mut rules : HashMap = HashMap::new(); + let mut rules: HashMap = HashMap::new(); rules.insert("quotes".to_string(), Quotes::create); rules -} \ No newline at end of file +} diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/miscellaneous/quotes.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/miscellaneous/quotes.rs index 3b759e66..59bec4a7 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/miscellaneous/quotes.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/miscellaneous/quotes.rs @@ -2,13 +2,11 @@ use crate::linter::SolidFile; use crate::rules::types::*; use crate::types::*; - pub struct Quotes { - data: RuleEntry + data: RuleEntry, } impl RuleType for Quotes { - fn diagnose(&self, file: &SolidFile, _files: &Vec) -> Vec { let mut res = Vec::new(); let mut line_idx = 1; @@ -18,8 +16,14 @@ impl RuleType for Quotes { if c == '\'' && line.chars().nth(idx - 1).unwrap_or(' ') != '\\' { res.push(LintDiag { range: Range { - start: Position { line: line_idx, character: idx as u64}, - end: Position { line: line_idx, character: idx as u64 }, + start: Position { + line: line_idx, + character: idx as u64, + }, + end: Position { + line: line_idx, + character: idx as u64, + }, length: 1 as u64, }, message: format!("Use double quotes instead of single quote"), @@ -27,7 +31,7 @@ impl RuleType for Quotes { code: None, source: None, uri: file.path.clone(), - source_file_content: file.content.clone() + source_file_content: file.content.clone(), }); } }); @@ -35,14 +39,11 @@ impl RuleType for Quotes { } res } - -} +} impl Quotes { pub(crate) fn create(data: RuleEntry) -> Box { - let rule = Quotes { - data - }; + let rule = Quotes { data }; Box::new(rule) } @@ -50,7 +51,7 @@ impl Quotes { RuleEntry { id: "quotes".to_string(), severity: Severity::ERROR, - data: vec![] + data: vec![], } } -} \ No newline at end of file +} diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/mod.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/mod.rs index f78c49ec..30c3eef0 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/mod.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/mod.rs @@ -1,9 +1,9 @@ -use std::collections::HashMap; use crate::rules::types::{RuleEntry, RuleType}; +use std::collections::HashMap; -pub mod types; -pub mod rule_impl; pub mod factory; +pub mod rule_impl; +pub mod types; // List all rules pub mod best_practises; @@ -24,7 +24,7 @@ pub fn create_default_rules() -> Vec { type RuleBuilder = fn(RuleEntry) -> Box; -pub fn add_rules(rules : &mut HashMap, to_add: HashMap) { +pub fn add_rules(rules: &mut HashMap, to_add: HashMap) { for (key, value) in to_add { rules.insert(key, value); } @@ -39,4 +39,4 @@ pub fn create_rules() -> HashMap Box> { add_rules(&mut rules, miscellaneous::create_rules()); rules -} \ No newline at end of file +} diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/contract_name_pascalcase.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/contract_name_pascalcase.rs index cb643366..dbbc0742 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/contract_name_pascalcase.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/contract_name_pascalcase.rs @@ -4,27 +4,36 @@ use crate::types::*; use solc_wrapper::{decode_location, SourceUnitChildNodes}; pub struct ContractNamePascalCase { - data: RuleEntry + data: RuleEntry, } impl RuleType for ContractNamePascalCase { - fn diagnose(&self, file: &SolidFile, _files: &Vec) -> Vec { - let mut res = Vec::new(); for node in &file.data.nodes { match node { SourceUnitChildNodes::ContractDefinition(contract) => { - if (contract.name.chars().nth(0).unwrap() >= 'a' && contract.name.chars().nth(0).unwrap() <= 'z') || - contract.name.contains("_") || - contract.name.contains("-") { + if (contract.name.chars().nth(0).unwrap() >= 'a' + && contract.name.chars().nth(0).unwrap() <= 'z') + || contract.name.contains("_") + || contract.name.contains("-") + { //Untested - let location = decode_location(contract.name_location.as_ref().unwrap(), &file.content); + let location = decode_location( + contract.name_location.as_ref().unwrap(), + &file.content, + ); res.push(LintDiag { range: Range { - start: Position { line: location.0.line as u64, character: location.0.column as u64 }, - end: Position { line: location.1.line as u64, character: location.1.column as u64 }, + start: Position { + line: location.0.line as u64, + character: location.0.column as u64, + }, + end: Position { + line: location.1.line as u64, + character: location.1.column as u64, + }, length: location.0.length as u64, }, message: format!("Contract name need to be in pascal case"), @@ -36,7 +45,9 @@ impl RuleType for ContractNamePascalCase { }); } } - _ => { continue; } + _ => { + continue; + } } } res @@ -45,9 +56,7 @@ impl RuleType for ContractNamePascalCase { impl ContractNamePascalCase { pub(crate) fn create(data: RuleEntry) -> Box { - let rule = ContractNamePascalCase { - data - }; + let rule = ContractNamePascalCase { data }; Box::new(rule) } @@ -55,7 +64,7 @@ impl ContractNamePascalCase { RuleEntry { id: "contract-name-pascalcase".to_string(), severity: Severity::WARNING, - data: vec![] + data: vec![], } } -} \ No newline at end of file +} diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_name_camelcase.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_name_camelcase.rs index c92dd551..41c9df3d 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_name_camelcase.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_name_camelcase.rs @@ -1,16 +1,16 @@ use crate::linter::SolidFile; use crate::rules::types::*; use crate::types::*; -use solc_wrapper::{ContractDefinitionChildNodes, decode_location, FunctionDefinitionKind, SourceUnitChildNodes}; +use solc_wrapper::{ + decode_location, ContractDefinitionChildNodes, FunctionDefinitionKind, SourceUnitChildNodes, +}; pub struct FuncNameCamelCase { - data: RuleEntry + data: RuleEntry, } impl RuleType for FuncNameCamelCase { - fn diagnose(&self, file: &SolidFile, _files: &Vec) -> Vec { - let mut res = Vec::new(); for node in &file.data.nodes { @@ -20,16 +20,27 @@ impl RuleType for FuncNameCamelCase { match node { ContractDefinitionChildNodes::FunctionDefinition(function) => { if function.kind != FunctionDefinitionKind::Constructor - && (!(function.name.chars().nth(0).unwrap_or(' ') >= 'a' && function.name.chars().nth(0).unwrap_or(' ') <= 'z') + && (!(function.name.chars().nth(0).unwrap_or(' ') >= 'a' + && function.name.chars().nth(0).unwrap_or(' ') <= 'z') || function.name.contains('_') - || function.name.contains('-')) { + || function.name.contains('-')) + { //Untested - let location = decode_location(function.name_location.as_ref().unwrap(), &file.content); + let location = decode_location( + function.name_location.as_ref().unwrap(), + &file.content, + ); res.push(LintDiag { range: Range { - start: Position { line: location.0.line as u64, character: location.0.column as u64 }, - end: Position { line: location.1.line as u64, character: location.1.column as u64 }, - length: location.0.length as u64 + start: Position { + line: location.0.line as u64, + character: location.0.column as u64, + }, + end: Position { + line: location.1.line as u64, + character: location.1.column as u64, + }, + length: location.0.length as u64, }, message: format!("Function name need to be in camel case"), severity: Some(self.data.severity), @@ -40,11 +51,15 @@ impl RuleType for FuncNameCamelCase { }); } } - _ => { continue; } + _ => { + continue; + } } } } - _ => { continue; } + _ => { + continue; + } } } res @@ -53,9 +68,7 @@ impl RuleType for FuncNameCamelCase { impl FuncNameCamelCase { pub(crate) fn create(data: RuleEntry) -> Box { - let rule = FuncNameCamelCase { - data - }; + let rule = FuncNameCamelCase { data }; Box::new(rule) } @@ -63,7 +76,7 @@ impl FuncNameCamelCase { RuleEntry { id: "func-name-camelcase".to_string(), severity: Severity::WARNING, - data: vec![] + data: vec![], } } -} \ No newline at end of file +} diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_param_name_camelcase.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_param_name_camelcase.rs index 948520b3..9eef6df9 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_param_name_camelcase.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_param_name_camelcase.rs @@ -1,16 +1,14 @@ use crate::linter::SolidFile; use crate::rules::types::*; use crate::types::*; -use solc_wrapper::{ContractDefinitionChildNodes, decode_location, SourceUnitChildNodes}; +use solc_wrapper::{decode_location, ContractDefinitionChildNodes, SourceUnitChildNodes}; pub struct FuncParamNameCamelcase { - data: RuleEntry + data: RuleEntry, } impl RuleType for FuncParamNameCamelcase { - fn diagnose(&self, file: &SolidFile, _files: &Vec) -> Vec { - let mut res = Vec::new(); for node in &file.data.nodes { @@ -20,18 +18,31 @@ impl RuleType for FuncParamNameCamelcase { match node { ContractDefinitionChildNodes::FunctionDefinition(function) => { for parameter in &function.parameters.parameters { - if !(parameter.name.chars().nth(0).unwrap() >= 'a' && parameter.name.chars().nth(0).unwrap() <= 'z') || - parameter.name.contains("_") || - parameter.name.contains("-") { + if !(parameter.name.chars().nth(0).unwrap() >= 'a' + && parameter.name.chars().nth(0).unwrap() <= 'z') + || parameter.name.contains("_") + || parameter.name.contains("-") + { //Untested - let location = decode_location(parameter.name_location.as_ref().unwrap(), &file.content); + let location = decode_location( + parameter.name_location.as_ref().unwrap(), + &file.content, + ); res.push(LintDiag { range: Range { - start: Position { line: location.0.line as u64, character: location.0.column as u64 }, - end: Position { line: location.1.line as u64, character: location.1.column as u64 }, + start: Position { + line: location.0.line as u64, + character: location.0.column as u64, + }, + end: Position { + line: location.1.line as u64, + character: location.1.column as u64, + }, length: location.0.length as u64, }, - message: format!("Parameter name need to be in camel case"), + message: format!( + "Parameter name need to be in camel case" + ), severity: Some(self.data.severity), code: None, source: None, @@ -41,12 +52,15 @@ impl RuleType for FuncParamNameCamelcase { } } } - _ => { continue; } + _ => { + continue; + } } } - } - _ => { continue; } + _ => { + continue; + } } } res @@ -55,9 +69,7 @@ impl RuleType for FuncParamNameCamelcase { impl FuncParamNameCamelcase { pub(crate) fn create(data: RuleEntry) -> Box { - let rule = FuncParamNameCamelcase { - data - }; + let rule = FuncParamNameCamelcase { data }; Box::new(rule) } @@ -65,7 +77,7 @@ impl FuncParamNameCamelcase { RuleEntry { id: "func-param-name-camelcase".to_string(), severity: Severity::WARNING, - data: vec![] + data: vec![], } } -} \ No newline at end of file +} diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/mod.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/mod.rs index a066a16b..011499a8 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/mod.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/mod.rs @@ -1,10 +1,10 @@ -use std::collections::HashMap; use crate::rules::naming::contract_name_pascalcase::ContractNamePascalCase; use crate::rules::naming::func_name_camelcase::FuncNameCamelCase; +use crate::rules::naming::func_param_name_camelcase::FuncParamNameCamelcase; use crate::rules::naming::use_forbidden_name::UseForbiddenName; use crate::rules::types::{RuleEntry, RuleType}; -use crate::rules::naming::func_param_name_camelcase::FuncParamNameCamelcase; use crate::rules::RuleBuilder; +use std::collections::HashMap; #[macro_use] pub(crate) mod func_param_name_camelcase; @@ -14,7 +14,6 @@ pub(crate) mod use_forbidden_name; // List all rules - pub fn create_default_rules() -> Vec { let mut rules = Vec::new(); @@ -27,12 +26,21 @@ pub fn create_default_rules() -> Vec { } pub fn create_rules() -> HashMap Box> { - let mut rules : HashMap = HashMap::new(); - - rules.insert( "func-param-name-camelcase".to_string(), FuncParamNameCamelcase::create); - rules.insert( "contract-name-pascalcase".to_string(), ContractNamePascalCase::create); - rules.insert( "func-name-camelcase".to_string(), FuncNameCamelCase::create); - rules.insert( UseForbiddenName::RULE_ID.to_string(), UseForbiddenName::create); + let mut rules: HashMap = HashMap::new(); + + rules.insert( + "func-param-name-camelcase".to_string(), + FuncParamNameCamelcase::create, + ); + rules.insert( + "contract-name-pascalcase".to_string(), + ContractNamePascalCase::create, + ); + rules.insert("func-name-camelcase".to_string(), FuncNameCamelCase::create); + rules.insert( + UseForbiddenName::RULE_ID.to_string(), + UseForbiddenName::create, + ); rules } diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/use_forbidden_name.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/use_forbidden_name.rs index 367ee0c2..c2a3cf08 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/use_forbidden_name.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/use_forbidden_name.rs @@ -1,15 +1,14 @@ use crate::linter::SolidFile; -use solc_wrapper::*; -use solc_wrapper::ast::utils::{get_all_nodes_by_type, Nodes}; use crate::rules::types::*; use crate::types::*; +use solc_wrapper::ast::utils::{get_all_nodes_by_type, Nodes}; +use solc_wrapper::*; pub struct UseForbiddenName { - data: RuleEntry + data: RuleEntry, } impl RuleType for UseForbiddenName { - fn diagnose(&self, file: &SolidFile, _files: &Vec) -> Vec { let mut res = Vec::new(); let blacklist = vec!['I', 'l', 'O']; @@ -19,39 +18,40 @@ impl RuleType for UseForbiddenName { for node in nodes { let var = match node { Nodes::VariableDeclaration(var) => var, - _ => continue + _ => continue, }; if var.name.len() == 1 && blacklist.contains(&var.name.chars().next().unwrap()) { let location = decode_location(&var.src, &file.content); res.push(LintDiag { range: Range { - start: Position { line: location.0.line as u64, character: location.0.column as u64}, - end: Position { line: location.1.line as u64, character: location.1.column as u64 }, - length: location.0.length as u64 + start: Position { + line: location.0.line as u64, + character: location.0.column as u64, + }, + end: Position { + line: location.1.line as u64, + character: location.1.column as u64, + }, + length: location.0.length as u64, }, message: format!("Forbidden variable name: {}", var.name), severity: Some(self.data.severity), code: None, source: None, uri: file.path.clone(), - source_file_content: file.content.clone() + source_file_content: file.content.clone(), }); } } res } - - } impl UseForbiddenName { - - pub const RULE_ID : &'static str = "use-forbidden-name"; + pub const RULE_ID: &'static str = "use-forbidden-name"; pub(crate) fn create(data: RuleEntry) -> Box { - let rule = UseForbiddenName { - data - }; + let rule = UseForbiddenName { data }; Box::new(rule) } @@ -62,4 +62,4 @@ impl UseForbiddenName { data: vec![], } } -} \ No newline at end of file +} diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/order/import_on_top.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/order/import_on_top.rs index 53763bd4..11829a80 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/order/import_on_top.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/order/import_on_top.rs @@ -4,13 +4,11 @@ use crate::types::*; use solc_wrapper::{decode_location, SourceUnitChildNodes}; pub struct ImportOnTop { - data: RuleEntry + data: RuleEntry, } impl RuleType for ImportOnTop { - fn diagnose(&self, file: &SolidFile, _files: &Vec) -> Vec { - let mut res = Vec::new(); let mut last_import_location = 0; @@ -33,9 +31,15 @@ impl RuleType for ImportOnTop { res.push(LintDiag { range: Range { - start: Position { line: location.0.line as u64, character: location.0.column as u64 }, - end: Position { line: location.1.line as u64, character: location.1.column as u64 }, - length: location.0.length as u64 + start: Position { + line: location.0.line as u64, + character: location.0.column as u64, + }, + end: Position { + line: location.1.line as u64, + character: location.1.column as u64, + }, + length: location.0.length as u64, }, message: format!("Import must be on top in the file"), severity: Some(self.data.severity), @@ -56,9 +60,7 @@ impl RuleType for ImportOnTop { impl ImportOnTop { pub(crate) fn create(data: RuleEntry) -> Box { - let rule = ImportOnTop { - data - }; + let rule = ImportOnTop { data }; Box::new(rule) } @@ -66,7 +68,7 @@ impl ImportOnTop { RuleEntry { id: "import-on-top".to_string(), severity: Severity::WARNING, - data: vec![] + data: vec![], } } -} \ No newline at end of file +} diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/order/mod.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/order/mod.rs index 0fa313d3..8956cfb6 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/order/mod.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/order/mod.rs @@ -1,5 +1,5 @@ -use std::collections::HashMap; use crate::rules::types::{RuleEntry, RuleType}; +use std::collections::HashMap; #[macro_use] pub(crate) mod import_on_top; @@ -18,9 +18,9 @@ pub fn create_default_rules() -> Vec { } pub fn create_rules() -> HashMap Box> { - let mut rules : HashMap = HashMap::new(); + let mut rules: HashMap = HashMap::new(); - rules.insert( "import-on-top".to_string(), ImportOnTop::create); + rules.insert("import-on-top".to_string(), ImportOnTop::create); rules -} \ No newline at end of file +} diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs index 53aa647e..c183992f 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs @@ -1,6 +1,5 @@ use crate::rules::create_default_rules; use crate::rules::types::*; -use crate::types::*; // Untested /* diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs index 0660e8d3..0db55003 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs @@ -1,24 +1,7 @@ +use crate::errors::SolidHunterError; +use colored::Colorize; use serde::{Deserialize, Serialize}; -use thiserror::Error; use std::fmt; -use colored::Colorize; - -#[derive(Error, Debug)] -pub enum SolidHunterError { - // Linter errors - #[error("SolidHunterError: Solc error occured")] - SolcError(#[from] solc_wrapper::SolcError), - #[error("SolidHunterError: Something went wrong with the file during parsing")] - ParsingError(#[from] std::io::Error), - #[error("SolidHunterError: Something went wrong")] - LinterError(String), - // - - // RulesError - #[error("SolidHunterError: IO error occured with Rules")] - IoError(std::io::Error), - // -} pub type LintResult = Result, SolidHunterError>; @@ -77,12 +60,23 @@ impl fmt::Display for LintDiag { .nth((self.range.start.line - 1) as usize) .unwrap(); - write!(f, "\n{}: {}\n --> {}:{}:{}\n |\n{}{}|{}\n |{}{}", severity_to_string(self.severity), self.message, self.uri, self.range.start.line, self.range.start.character, self.range.start.line, padding, line, " ".repeat(self.range.start.character as usize), - "^".repeat(self.range.length as usize)) + write!( + f, + "\n{}: {}\n --> {}:{}:{}\n |\n{}{}|{}\n |{}{}", + severity_to_string(self.severity), + self.message, + self.uri, + self.range.start.line, + self.range.start.character, + self.range.start.line, + padding, + line, + " ".repeat(self.range.start.character as usize), + "^".repeat(self.range.length as usize) + ) } } - //////////////////////////////////////////////////////////// /////////////////// RELATED TYPES: ///////////////////////// //////////////////////////////////////////////////////////// From dac30495e4949562cc9a25ac4564213884af3f0a Mon Sep 17 00:00:00 2001 From: leon Date: Thu, 7 Sep 2023 20:15:02 +0200 Subject: [PATCH 11/59] refactor(solidity/linter/core): unified error types --- .../solidity/linter/core/solidhunter-lib/src/errors.rs | 2 +- .../solidity/linter/core/solidhunter-lib/src/linter.rs | 4 ++-- .../linter/core/solidhunter-lib/src/rules/rule_impl.rs | 7 +++---- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/errors.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/errors.rs index 6373641d..dae5d75e 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/errors.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/errors.rs @@ -12,5 +12,5 @@ pub enum SolidHunterError { // RulesError #[error("SolidHunterError: IO error occured with Rules")] - IoError(std::io::Error), + IoError(String), } diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs index 51734a82..8c569d7d 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs @@ -91,8 +91,8 @@ impl SolidLinter { if self._file_exists(filepath.as_str()) { self._update_file_ast(filepath.as_str(), res.expect("ast not found")); } else { - let content = - fs::read_to_string(filepath.clone()).map_err(|e| SolidHunterError::IoError(e))?; + let content = fs::read_to_string(filepath.clone()) + .map_err(|e| SolidHunterError::IoError(e.to_string()))?; self._add_file( filepath.as_str(), res.expect("ast not found"), diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs index c183992f..4fb6231e 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs @@ -40,10 +40,9 @@ pub fn parse_rules(path: &str) -> Result { };*/ if !std::path::Path::new(&path).is_file() { - return Err(SolidHunterError::IoError(std::io::Error::new( - std::io::ErrorKind::NotFound, - "Rules file not found", - ))); + return Err(SolidHunterError::IoError( + "Rules file not found".to_string(), + )); } let file = std::fs::read_to_string(path).unwrap(); let parsed: Rules = serde_json::from_str(&file).unwrap(); From 7a522cd55bf806a3abd3b6b0bf8a20931a98749b Mon Sep 17 00:00:00 2001 From: 0xtekgrinder <0xtekgrinder@protonmail.com> Date: Thu, 7 Sep 2023 22:46:43 +0200 Subject: [PATCH 12/59] refactor(solidhunter): remove usless unwrap --- toolchains/solidity/linter/core/solidhunter-lib/src/errors.rs | 2 ++ toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs | 4 ++-- .../linter/core/solidhunter-lib/src/rules/rule_impl.rs | 4 ++-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/errors.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/errors.rs index dae5d75e..b76e67a8 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/errors.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/errors.rs @@ -7,6 +7,8 @@ pub enum SolidHunterError { SolcError(#[from] solc_wrapper::SolcError), #[error("SolidHunterError: Something went wrong with the file during parsing")] ParsingError(#[from] std::io::Error), + #[error("SolidHunterError: Serde error occured")] + SerdeError(#[from] serde_json::Error), #[error("SolidHunterError: Something went wrong")] LinterError(String), diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs index 8c569d7d..66e1b773 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs @@ -86,7 +86,7 @@ impl SolidLinter { if res.is_err() { println!("{:?}", res); - return Err(SolidHunterError::SolcError(res.err().unwrap())); + return Err(SolidHunterError::SolcError(res.unwrap_err())); } if self._file_exists(filepath.as_str()) { self._update_file_ast(filepath.as_str(), res.expect("ast not found")); @@ -113,7 +113,7 @@ impl SolidLinter { if res.is_err() { println!("{:?}", res); - return Err(SolidHunterError::SolcError(res.err().unwrap())); + return Err(SolidHunterError::SolcError(res.unwrap_err())); } if self._file_exists(filepath.as_str()) { diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs index 4fb6231e..b2e78534 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs @@ -44,8 +44,8 @@ pub fn parse_rules(path: &str) -> Result { "Rules file not found".to_string(), )); } - let file = std::fs::read_to_string(path).unwrap(); - let parsed: Rules = serde_json::from_str(&file).unwrap(); + let file = std::fs::read_to_string(path)?; + let parsed: Rules = serde_json::from_str(&file)?; /* // Danger zone From 54ef01e037fcefd25185ceaa434d1cbebaa83e1e Mon Sep 17 00:00:00 2001 From: 0xSwapFeeder Date: Fri, 8 Sep 2023 17:18:56 -0400 Subject: [PATCH 13/59] chore(solidity/linter): cleaned and linted code --- .../linter/core/solc-wrapper/src/ast/ast.rs | 4 +- .../linter/core/solc-wrapper/src/utils.rs | 2 +- .../linter/core/solidhunter-lib/src/lib.rs | 1 - .../src/rules/miscellaneous/quotes.rs | 2 +- .../solidhunter-lib/src/rules/rule_impl.rs | 61 +------------------ toolchains/solidity/linter/core/src/main.rs | 32 +++++----- 6 files changed, 19 insertions(+), 83 deletions(-) diff --git a/toolchains/solidity/linter/core/solc-wrapper/src/ast/ast.rs b/toolchains/solidity/linter/core/solc-wrapper/src/ast/ast.rs index de66585c..b1ea0e5b 100644 --- a/toolchains/solidity/linter/core/solc-wrapper/src/ast/ast.rs +++ b/toolchains/solidity/linter/core/solc-wrapper/src/ast/ast.rs @@ -1571,7 +1571,7 @@ mod tests { assert_eq!(res.src, "33:23:0".to_string()); assert_eq!(res.literals, vec!["solidity".to_string(), "0.8".to_string(), - ".16".to_string()] as Vec); + ".16".to_string()]); Ok(assert_eq!(res.node_type, NodeType::PragmaDirective)) } @@ -2045,7 +2045,7 @@ mod tests { assert_eq!(res.id, 21); assert_eq!(res.src, "236:36:0".to_string()); - assert_eq!(res.names, vec!["value".to_string()] as Vec); + assert_eq!(res.names, vec!["value".to_string()]); assert_eq!(res.node_type, NodeType::FunctionCallOptions); Ok(()) } diff --git a/toolchains/solidity/linter/core/solc-wrapper/src/utils.rs b/toolchains/solidity/linter/core/solc-wrapper/src/utils.rs index bad6ebd2..0f62f9e3 100644 --- a/toolchains/solidity/linter/core/solc-wrapper/src/utils.rs +++ b/toolchains/solidity/linter/core/solc-wrapper/src/utils.rs @@ -65,7 +65,7 @@ pub fn get_error_location(stderr: &str) -> Result { file, line: line.parse().unwrap(), //unwrap is safe due to the regex that matches only number column: column.parse().unwrap(), //unwrap is safe due to the regex that matches only number - length: length + length } ) } \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/lib.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/lib.rs index d2ebe5f7..0ba72c7b 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/lib.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/lib.rs @@ -5,7 +5,6 @@ pub mod types; #[cfg(test)] mod tests { - use super::*; #[test] fn it_works() {} diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/miscellaneous/quotes.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/miscellaneous/quotes.rs index 59bec4a7..2b45091f 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/miscellaneous/quotes.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/miscellaneous/quotes.rs @@ -24,7 +24,7 @@ impl RuleType for Quotes { line: line_idx, character: idx as u64, }, - length: 1 as u64, + length: 1u64, }, message: format!("Use double quotes instead of single quote"), severity: Some(self.data.severity), diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs index b2e78534..025a219e 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs @@ -1,22 +1,7 @@ +use crate::errors::SolidHunterError; use crate::rules::create_default_rules; use crate::rules::types::*; -// Untested -/* -fn merge_rules(rules: &mut Vec, new_rules: &Vec) { - let mut new_rules_map = HashMap::new(); - for rule in new_rules { - new_rules_map.insert(rule.id.clone(), rule); - } - - for rule in rules { - if let Some(new_rule) = new_rules_map.get(&rule.id) { - rule.severity = new_rule.severity.clone(); - rule.data = new_rule.data.clone(); - } - } -} -*/ pub fn create_rules_file(path: &str) { let rules = Rules { @@ -31,14 +16,6 @@ pub fn create_rules_file(path: &str) { } pub fn parse_rules(path: &str) -> Result { - /* - let rules = Rules { - name: String::new(), - includes: Vec::new(), - plugins: Vec::new(), - rules: Vec::new(), - };*/ - if !std::path::Path::new(&path).is_file() { return Err(SolidHunterError::IoError( "Rules file not found".to_string(), @@ -47,41 +24,5 @@ pub fn parse_rules(path: &str) -> Result { let file = std::fs::read_to_string(path)?; let parsed: Rules = serde_json::from_str(&file)?; - /* - // Danger zone - for include in parsed.includes { - let include_rules = parse_rules(include.as_str()); - merge_rules(&mut rules.rules, &include_rules.unwrap().rules); - } - - merge_rules(&mut rules.rules, &parsed.rules); - // End of danger zone - */ - Ok(parsed) } - -// create rules -/* -#[macro_export] -macro_rules! create_rule { - ($rule_name:ident, $rule_id:expr, $default_severity:expr, $custom_data:expr, $message:expr) => { - pub struct $rule_name { - id: String, - message: String, - severity: RuleSeverity, - data: Vec, - } - - impl $rule_name { - pub fn new(severity: Severity, data: Vec) -> $rule_name { - $rule_name { - id: $rule_id.to_string(), - message: $message.to_string(), - severity, - data, - } - } - } - }; -}*/ diff --git a/toolchains/solidity/linter/core/src/main.rs b/toolchains/solidity/linter/core/src/main.rs index 92aba6bc..2531a2f9 100644 --- a/toolchains/solidity/linter/core/src/main.rs +++ b/toolchains/solidity/linter/core/src/main.rs @@ -1,6 +1,7 @@ use clap::Parser; use solidhunter_lib::linter::SolidLinter; use solidhunter_lib::rules::rule_impl::create_rules_file; +use solidhunter_lib::types::LintResult; #[derive(Parser, Debug)] #[command(author, version, about, long_about = None)] @@ -68,16 +69,20 @@ fn lint_folder(args: Args) { result.append(&mut linter.parse_folder(path)); } for res in result { - match res { - Ok(diags) => { - for diag in diags { - println!("{}", &diag); - } - } - Err(e) => { - println!("{}", e); + print_result(res); + } +} + +fn print_result(result: LintResult) { + match result { + Ok(diags) => { + for diag in diags { + println!("{}", &diag); } } + Err(e) => { + println!("{}", e); + } } } @@ -117,16 +122,7 @@ fn main() { let result = linter.parse_file(args.file_to_lint); if !args.to_json { - match result { - Ok(diags) => { - for diag in diags { - println!("{}", &diag); - } - } - Err(e) => { - println!("{}", e); - } - } + print_result(result); } else { match result { Ok(diags) => { From 40eb1d8aea83775baa646f42447671d70f5ed709 Mon Sep 17 00:00:00 2001 From: 0xMemoryGrinder <35138272+0xMemoryGrinder@users.noreply.github.com> Date: Fri, 8 Sep 2023 23:08:00 -0400 Subject: [PATCH 14/59] refactor(solidity/linter/core): added missing indentation --- toolchains/solidity/linter/core/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/toolchains/solidity/linter/core/src/main.rs b/toolchains/solidity/linter/core/src/main.rs index 2531a2f9..439254e2 100644 --- a/toolchains/solidity/linter/core/src/main.rs +++ b/toolchains/solidity/linter/core/src/main.rs @@ -122,7 +122,7 @@ fn main() { let result = linter.parse_file(args.file_to_lint); if !args.to_json { - print_result(result); + print_result(result); } else { match result { Ok(diags) => { From 38b541ba123c00eca3fe1198ca0d5c68bc6d5b72 Mon Sep 17 00:00:00 2001 From: 0xSwapFeeder Date: Sat, 9 Sep 2023 11:01:35 -0400 Subject: [PATCH 15/59] chore(solidity/linter): extracted linter diag creation from naming and miscellaneous rules --- .../src/rules/miscellaneous/quotes.rs | 44 ++++++++++------- .../rules/naming/contract_name_pascalcase.rs | 45 +++++++++-------- .../src/rules/naming/func_name_camelcase.rs | 46 ++++++++--------- .../rules/naming/func_param_name_camelcase.rs | 49 ++++++++++--------- .../src/rules/naming/use_forbidden_name.rs | 43 +++++++++------- 5 files changed, 125 insertions(+), 102 deletions(-) diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/miscellaneous/quotes.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/miscellaneous/quotes.rs index 2b45091f..1e0a2a88 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/miscellaneous/quotes.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/miscellaneous/quotes.rs @@ -1,3 +1,4 @@ +use solc_wrapper::{CodeLocation, decode_location}; use crate::linter::SolidFile; use crate::rules::types::*; use crate::types::*; @@ -7,6 +8,29 @@ pub struct Quotes { } impl RuleType for Quotes { + + fn create_diag(&self, file: SolidFile, idx: usize, line_idx: u32) { + LintDiag { + range: Range { + start: Position { + line: line_idx, + character: idx as u64, + }, + end: Position { + line: line_idx, + character: idx as u64, + }, + length: 1u64, + }, + message: format!("Use double quotes instead of single quote"), + severity: Some(self.data.severity), + code: None, + source: None, + uri: file.path.clone(), + source_file_content: file.content.clone(), + } + } + fn diagnose(&self, file: &SolidFile, _files: &Vec) -> Vec { let mut res = Vec::new(); let mut line_idx = 1; @@ -14,25 +38,7 @@ impl RuleType for Quotes { for line in file.content.lines() { line.chars().enumerate().for_each(|(idx, c)| { if c == '\'' && line.chars().nth(idx - 1).unwrap_or(' ') != '\\' { - res.push(LintDiag { - range: Range { - start: Position { - line: line_idx, - character: idx as u64, - }, - end: Position { - line: line_idx, - character: idx as u64, - }, - length: 1u64, - }, - message: format!("Use double quotes instead of single quote"), - severity: Some(self.data.severity), - code: None, - source: None, - uri: file.path.clone(), - source_file_content: file.content.clone(), - }); + res.push(self.create_diag(file, idx, line_idx)); } }); line_idx += 1; diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/contract_name_pascalcase.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/contract_name_pascalcase.rs index dbbc0742..48231353 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/contract_name_pascalcase.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/contract_name_pascalcase.rs @@ -1,13 +1,36 @@ use crate::linter::SolidFile; use crate::rules::types::*; use crate::types::*; -use solc_wrapper::{decode_location, SourceUnitChildNodes}; +use solc_wrapper::{CodeLocation, decode_location, SourceUnitChildNodes}; pub struct ContractNamePascalCase { data: RuleEntry, } impl RuleType for ContractNamePascalCase { + + fn create_diag(&self, location: (CodeLocation, CodeLocation), file: SolidFile) { + LintDiag { + range: Range { + start: Position { + line: location.0.line as u64, + character: location.0.column as u64, + }, + end: Position { + line: location.1.line as u64, + character: location.1.column as u64, + }, + length: location.0.length as u64, + }, + message: format!("Contract name need to be in pascal case"), + severity: Some(self.data.severity), + code: None, + source: None, + uri: file.path.clone(), + source_file_content: file.content.clone(), + } + } + fn diagnose(&self, file: &SolidFile, _files: &Vec) -> Vec { let mut res = Vec::new(); @@ -24,25 +47,7 @@ impl RuleType for ContractNamePascalCase { contract.name_location.as_ref().unwrap(), &file.content, ); - res.push(LintDiag { - range: Range { - start: Position { - line: location.0.line as u64, - character: location.0.column as u64, - }, - end: Position { - line: location.1.line as u64, - character: location.1.column as u64, - }, - length: location.0.length as u64, - }, - message: format!("Contract name need to be in pascal case"), - severity: Some(self.data.severity), - code: None, - source: None, - uri: file.path.clone(), - source_file_content: file.content.clone(), - }); + res.push(self.create_diag(location, file)); } } _ => { diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_name_camelcase.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_name_camelcase.rs index 41c9df3d..5a9e8b24 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_name_camelcase.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_name_camelcase.rs @@ -1,15 +1,35 @@ use crate::linter::SolidFile; use crate::rules::types::*; use crate::types::*; -use solc_wrapper::{ - decode_location, ContractDefinitionChildNodes, FunctionDefinitionKind, SourceUnitChildNodes, -}; +use solc_wrapper::{decode_location, ContractDefinitionChildNodes, FunctionDefinitionKind, SourceUnitChildNodes, CodeLocation}; pub struct FuncNameCamelCase { data: RuleEntry, } impl RuleType for FuncNameCamelCase { + fn create_diag(&self, location: (CodeLocation, CodeLocation), file: SolidFile) { + LintDiag { + range: Range { + start: Position { + line: location.0.line as u64, + character: location.0.column as u64, + }, + end: Position { + line: location.1.line as u64, + character: location.1.column as u64, + }, + length: location.0.length as u64, + }, + message: format!("Function name need to be in camel case"), + severity: Some(self.data.severity), + code: None, + source: None, + uri: file.path.clone(), + source_file_content: file.content.clone(), + } + } + fn diagnose(&self, file: &SolidFile, _files: &Vec) -> Vec { let mut res = Vec::new(); @@ -30,25 +50,7 @@ impl RuleType for FuncNameCamelCase { function.name_location.as_ref().unwrap(), &file.content, ); - res.push(LintDiag { - range: Range { - start: Position { - line: location.0.line as u64, - character: location.0.column as u64, - }, - end: Position { - line: location.1.line as u64, - character: location.1.column as u64, - }, - length: location.0.length as u64, - }, - message: format!("Function name need to be in camel case"), - severity: Some(self.data.severity), - code: None, - source: None, - uri: file.path.clone(), - source_file_content: file.content.clone(), - }); + res.push(self.create_diag(location, file)); } } _ => { diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_param_name_camelcase.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_param_name_camelcase.rs index 9eef6df9..4b4c4fa2 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_param_name_camelcase.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_param_name_camelcase.rs @@ -1,13 +1,38 @@ use crate::linter::SolidFile; use crate::rules::types::*; use crate::types::*; -use solc_wrapper::{decode_location, ContractDefinitionChildNodes, SourceUnitChildNodes}; +use solc_wrapper::{decode_location, ContractDefinitionChildNodes, SourceUnitChildNodes, CodeLocation}; pub struct FuncParamNameCamelcase { data: RuleEntry, } impl RuleType for FuncParamNameCamelcase { + + fn create_diag(&self, location: (CodeLocation, CodeLocation), file: SolidFile) { + LintDiag { + range: Range { + start: Position { + line: location.0.line as u64, + character: location.0.column as u64, + }, + end: Position { + line: location.1.line as u64, + character: location.1.column as u64, + }, + length: location.0.length as u64, + }, + message: format!( + "Parameter name need to be in camel case" + ), + severity: Some(self.data.severity), + code: None, + source: None, + uri: file.path.clone(), + source_file_content: file.content.clone(), + } + } + fn diagnose(&self, file: &SolidFile, _files: &Vec) -> Vec { let mut res = Vec::new(); @@ -28,27 +53,7 @@ impl RuleType for FuncParamNameCamelcase { parameter.name_location.as_ref().unwrap(), &file.content, ); - res.push(LintDiag { - range: Range { - start: Position { - line: location.0.line as u64, - character: location.0.column as u64, - }, - end: Position { - line: location.1.line as u64, - character: location.1.column as u64, - }, - length: location.0.length as u64, - }, - message: format!( - "Parameter name need to be in camel case" - ), - severity: Some(self.data.severity), - code: None, - source: None, - uri: file.path.clone(), - source_file_content: file.content.clone(), - }); + res.push(self.create_diag(location, file)); } } } diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/use_forbidden_name.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/use_forbidden_name.rs index c2a3cf08..8e296fde 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/use_forbidden_name.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/use_forbidden_name.rs @@ -9,6 +9,29 @@ pub struct UseForbiddenName { } impl RuleType for UseForbiddenName { + + fn create_diag(&self, location: (CodeLocation, CodeLocation), var: Box, file: SolidFile) { + LintDiag { + range: Range { + start: Position { + line: location.0.line as u64, + character: location.0.column as u64, + }, + end: Position { + line: location.1.line as u64, + character: location.1.column as u64, + }, + length: location.0.length as u64, + }, + message: format!("Forbidden variable name: {}", var.name), + severity: Some(self.data.severity), + code: None, + source: None, + uri: file.path.clone(), + source_file_content: file.content.clone(), + } + } + fn diagnose(&self, file: &SolidFile, _files: &Vec) -> Vec { let mut res = Vec::new(); let blacklist = vec!['I', 'l', 'O']; @@ -22,25 +45,7 @@ impl RuleType for UseForbiddenName { }; if var.name.len() == 1 && blacklist.contains(&var.name.chars().next().unwrap()) { let location = decode_location(&var.src, &file.content); - res.push(LintDiag { - range: Range { - start: Position { - line: location.0.line as u64, - character: location.0.column as u64, - }, - end: Position { - line: location.1.line as u64, - character: location.1.column as u64, - }, - length: location.0.length as u64, - }, - message: format!("Forbidden variable name: {}", var.name), - severity: Some(self.data.severity), - code: None, - source: None, - uri: file.path.clone(), - source_file_content: file.content.clone(), - }); + res.push(self.create_diag(location, var, file)); } } res From 7092cccc5af0b8923805ac280fba6c5e7824b60b Mon Sep 17 00:00:00 2001 From: EnergyCube Date: Sat, 9 Sep 2023 19:03:23 +0200 Subject: [PATCH 16/59] chore(solidity/linter): extracted linter diag creation from best practises and order rules --- .../src/rules/best_practises/line_maxlen.rs | 43 +++++----- .../rules/best_practises/max_states_count.rs | 44 +++++----- .../src/rules/best_practises/reason_string.rs | 83 ++++++------------- .../src/rules/order/import_on_top.rs | 45 +++++----- 4 files changed, 101 insertions(+), 114 deletions(-) diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/line_maxlen.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/line_maxlen.rs index dff8cd56..50ddb2fd 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/line_maxlen.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/line_maxlen.rs @@ -8,31 +8,36 @@ pub struct LineMaxLen { } impl RuleType for LineMaxLen { + + fn create_diag(&self, file: &SolidFile, line_idx: usize, line: &str) -> LintDiag { + LintDiag { + range: Range { + start: Position { + line: line_idx as u64, + character: self.max_len as u64, + }, + end: Position { + line: line_idx as u64, + character: line.len() as u64, + }, + length: (line.len() - self.max_len) as u64, + }, + message: format!("Line is too long: {}", line.len()), + severity: Some(self.data.severity), + code: None, + source: None, + uri: file.path.clone(), + source_file_content: file.content.clone(), + } + } + fn diagnose(&self, file: &SolidFile, _files: &Vec) -> Vec { let mut res = Vec::new(); let mut line_idx = 1; for line in file.content.lines() { if line.len() > self.max_len { - res.push(LintDiag { - range: Range { - start: Position { - line: line_idx, - character: self.max_len as u64, - }, - end: Position { - line: line_idx, - character: line.len() as u64, - }, - length: (line.len() - self.max_len) as u64, - }, - message: format!("Line is too long: {}", line.len()), - severity: Some(self.data.severity), - code: None, - source: None, - uri: file.path.clone(), - source_file_content: file.content.clone(), - }); + res.push(self.create_diag(file, line_idx, line)); } line_idx += 1; } diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/max_states_count.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/max_states_count.rs index bbd424cd..3c225219 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/max_states_count.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/max_states_count.rs @@ -9,6 +9,30 @@ pub struct MaxStatesCount { } impl RuleType for MaxStatesCount { + + fn create_diag(&self, file: &SolidFile, var: &ContractDefinitionChildNodes, + location: (CodeLocation, CodeLocation)) -> LintDiag { + LintDiag { + range: Range { + start: Position { + line: location.0.line as u64, + character: location.0.column as u64, + }, + end: Position { + line: location.1.line as u64, + character: location.1.column as u64, + }, + length: location.0.length as u64, + }, + message: format!("Too many states: {}", count), + severity: Some(self.data.severity), + code: None, + source: None, + uri: file.path.clone(), + source_file_content: file.content.clone(), + } + } + fn diagnose(&self, file: &SolidFile, _files: &Vec) -> Vec { let mut res = Vec::new(); @@ -28,25 +52,7 @@ impl RuleType for MaxStatesCount { count += 1; if count > self.max_states { let location = decode_location(&var.src, &file.content); - res.push(LintDiag { - range: Range { - start: Position { - line: location.0.line as u64, - character: location.0.column as u64, - }, - end: Position { - line: location.1.line as u64, - character: location.1.column as u64, - }, - length: location.0.length as u64, - }, - message: format!("Too many states: {}", count), - severity: Some(self.data.severity), - code: None, - source: None, - uri: file.path.clone(), - source_file_content: file.content.clone(), - }); + res.push(self.create_diag(file, var, location)); } } } diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/reason_string.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/reason_string.rs index 767bb3f4..fe3670a0 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/reason_string.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/reason_string.rs @@ -17,6 +17,29 @@ pub struct ReasonString { } impl RuleType for ReasonString { + + fn create_diag(&self, file: &SolidFile, location: (CodeLocation, CodeLocation), message: String) -> LintDiag { + LintDiag { + range: Range { + start: Position { + line: location.0.line as u64, + character: location.0.column as u64, + }, + end: Position { + line: location.1.line as u64, + character: location.1.column as u64, + }, + length: location.0.length as u64, + }, + message: message, + severity: Some(self.data.severity), + code: None, + source: None, + uri: file.path.clone(), + source_file_content: file.content.clone(), + } + } + fn diagnose(&self, file: &SolidFile, _files: &Vec) -> Vec { let mut res = Vec::new(); @@ -28,20 +51,7 @@ impl RuleType for ReasonString { if v.name == "require" { if j.arguments.len() != 2 { let location = decode_location(&j.src, &file.content); - let diag = LintDiag { - range: Range { - start: Position { line: location.0.line as u64, character: location.0.column as u64 }, - end: Position { line: location.1.line as u64, character: location.1.column as u64 }, - length: location.0.length as u64 - }, - message: format!("reason-string: A require statement must have a reason string"), - severity: Some(self.data.severity), - code: None, - source: None, - uri: file.path.clone(), - source_file_content: file.content.clone(), - }; - res.push(diag); + res.push(self.create_diag(file, location, format!("reason-string: A require statement must have a reason string"))); } else { for nj in &j.arguments { match nj { @@ -51,20 +61,7 @@ impl RuleType for ReasonString { { let location = decode_location(&z.src, &file.content); - let diag = LintDiag { - range: Range { - start: Position { line: location.0.line as u64, character: location.0.column as u64 }, - end: Position { line: location.1.line as u64, character: location.1.column as u64 }, - length: location.0.length as u64 - }, - message: format!("reason-string: A revert statement must have a reason string of length less than {}", self.max_length), - severity: Some(self.data.severity), - code: None, - source: None, - uri: file.path.clone(), - source_file_content: file.content.clone(), - }; - res.push(diag); + res.push(self.create_diag(file, location, format!("reason-string: A revert statement must have a reason string of length less than {}", self.max_length))); } } _ => {} @@ -74,40 +71,14 @@ impl RuleType for ReasonString { } else if v.name == "revert" { if j.arguments.len() == 0 { let location = decode_location(&j.src, &file.content); - let diag = LintDiag { - range: Range { - start: Position { line: location.0.line as u64, character: location.0.column as u64 }, - end: Position { line: location.1.line as u64, character: location.1.column as u64 }, - length: location.0.length as u64 - }, - message: format!("reason-string: A revert statement must have a reason string"), - severity: Some(self.data.severity), - code: None, - source: None, - uri: file.path.clone(), - source_file_content: file.content.clone(), - }; - res.push(diag); + res.push(self.create_diag(file, location, format!("reason-string: A revert statement must have a reason string"))); } else { match &j.arguments[0] { Expression::Literal(z) => { if z.value.clone().unwrap().len() > self.max_length as usize { let location = decode_location(&z.src, &file.content); - let diag = LintDiag { - range: Range { - start: Position { line: location.0.line as u64, character: location.0.column as u64 }, - end: Position { line: location.1.line as u64, character: location.1.column as u64 }, - length: location.0.length as u64 - }, - message: format!("reason-string: A revert statement must have a reason string of length less than {}", self.max_length), - severity: Some(self.data.severity), - code: None, - source: None, - uri: file.path.clone(), - source_file_content: file.content.clone(), - }; - res.push(diag); + res.push(self.create_diag(file, location, format!("reason-string: A revert statement must have a reason string of length less than {}", self.max_length))); } } _ => {} diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/order/import_on_top.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/order/import_on_top.rs index 11829a80..7b297ea7 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/order/import_on_top.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/order/import_on_top.rs @@ -8,6 +8,30 @@ pub struct ImportOnTop { } impl RuleType for ImportOnTop { + + fn create_diag(&self, file: &SolidFile, import: &SourceUnitChildNodes, + location: (CodeLocation, CodeLocation)) -> LintDiag { + LintDiag { + range: Range { + start: Position { + line: location.0.line as u64, + character: location.0.column as u64, + }, + end: Position { + line: location.1.line as u64, + character: location.1.column as u64, + }, + length: location.0.length as u64, + }, + message: String("Import must be on top in the file"), + severity: Some(self.data.severity), + code: None, + source: None, + uri: file.path.clone(), + source_file_content: file.content.clone(), + } + } + fn diagnose(&self, file: &SolidFile, _files: &Vec) -> Vec { let mut res = Vec::new(); let mut last_import_location = 0; @@ -28,26 +52,7 @@ impl RuleType for ImportOnTop { SourceUnitChildNodes::ImportDirective(import) => { if i > last_import_location { let location = decode_location(&import.src, &file.content); - - res.push(LintDiag { - range: Range { - start: Position { - line: location.0.line as u64, - character: location.0.column as u64, - }, - end: Position { - line: location.1.line as u64, - character: location.1.column as u64, - }, - length: location.0.length as u64, - }, - message: format!("Import must be on top in the file"), - severity: Some(self.data.severity), - code: None, - source: None, - uri: file.path.clone(), - source_file_content: file.content.clone(), - }); + res.push(self.generate_diagnostic(file, import)); } } _ => {} From 3ca620c58f64f5957d5a4d46c1f8b555d48bde89 Mon Sep 17 00:00:00 2001 From: 0xtekgrinder <72015889+0xtekgrinder@users.noreply.github.com> Date: Sat, 9 Sep 2023 23:19:45 -0400 Subject: [PATCH 17/59] refactor(solidhunter): rename errors file into error --- .../linter/core/solidhunter-lib/src/{errors.rs => error.rs} | 0 toolchains/solidity/linter/core/solidhunter-lib/src/lib.rs | 2 +- toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs | 2 +- .../solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs | 2 +- toolchains/solidity/linter/core/solidhunter-lib/src/types.rs | 2 +- 5 files changed, 4 insertions(+), 4 deletions(-) rename toolchains/solidity/linter/core/solidhunter-lib/src/{errors.rs => error.rs} (100%) diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/errors.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/error.rs similarity index 100% rename from toolchains/solidity/linter/core/solidhunter-lib/src/errors.rs rename to toolchains/solidity/linter/core/solidhunter-lib/src/error.rs diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/lib.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/lib.rs index 0ba72c7b..87a653a1 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/lib.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/lib.rs @@ -1,4 +1,4 @@ -pub mod errors; +pub mod error; pub mod linter; pub mod rules; pub mod types; diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs index 66e1b773..415ad961 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs @@ -1,4 +1,4 @@ -use crate::errors::SolidHunterError; +use crate::error::SolidHunterError; use crate::rules::factory::RuleFactory; use crate::rules::rule_impl::{create_rules_file, parse_rules}; use crate::rules::types::*; diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs index 025a219e..98b0692d 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs @@ -1,4 +1,4 @@ -use crate::errors::SolidHunterError; +use crate::error::SolidHunterError; use crate::rules::create_default_rules; use crate::rules::types::*; diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs index 0db55003..a360625b 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs @@ -1,4 +1,4 @@ -use crate::errors::SolidHunterError; +use crate::error::SolidHunterError; use colored::Colorize; use serde::{Deserialize, Serialize}; use std::fmt; From f58af1e961d96e49662f1711614918a1c80fab33 Mon Sep 17 00:00:00 2001 From: 0xMemoryGrinder <35138272+0xMemoryGrinder@users.noreply.github.com> Date: Sun, 10 Sep 2023 01:44:18 -0400 Subject: [PATCH 18/59] fix(solidity/linter/core): fixed all compilation issues --- .../src/rules/best_practises/line_maxlen.rs | 6 ++++-- .../rules/best_practises/max_states_count.rs | 11 ++++++----- .../src/rules/best_practises/reason_string.rs | 8 +++++--- .../src/rules/miscellaneous/quotes.rs | 9 +++++---- .../rules/naming/contract_name_pascalcase.rs | 8 +++++--- .../src/rules/naming/func_name_camelcase.rs | 8 ++++++-- .../rules/naming/func_param_name_camelcase.rs | 8 +++++--- .../src/rules/naming/use_forbidden_name.rs | 10 +++++++--- .../src/rules/order/import_on_top.rs | 17 ++++++++++------- 9 files changed, 53 insertions(+), 32 deletions(-) diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/line_maxlen.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/line_maxlen.rs index 50ddb2fd..d208edd5 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/line_maxlen.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/line_maxlen.rs @@ -7,8 +7,7 @@ pub struct LineMaxLen { data: RuleEntry, } -impl RuleType for LineMaxLen { - +impl LineMaxLen { fn create_diag(&self, file: &SolidFile, line_idx: usize, line: &str) -> LintDiag { LintDiag { range: Range { @@ -30,7 +29,10 @@ impl RuleType for LineMaxLen { source_file_content: file.content.clone(), } } +} +impl RuleType for LineMaxLen { + fn diagnose(&self, file: &SolidFile, _files: &Vec) -> Vec { let mut res = Vec::new(); let mut line_idx = 1; diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/max_states_count.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/max_states_count.rs index 3c225219..08cde0ec 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/max_states_count.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/max_states_count.rs @@ -8,10 +8,8 @@ pub struct MaxStatesCount { data: RuleEntry, } -impl RuleType for MaxStatesCount { - - fn create_diag(&self, file: &SolidFile, var: &ContractDefinitionChildNodes, - location: (CodeLocation, CodeLocation)) -> LintDiag { +impl MaxStatesCount { + fn create_diag(&self, file: &SolidFile, location: (CodeLocation, CodeLocation), count: usize) -> LintDiag { LintDiag { range: Range { start: Position { @@ -32,6 +30,9 @@ impl RuleType for MaxStatesCount { source_file_content: file.content.clone(), } } +} + +impl RuleType for MaxStatesCount { fn diagnose(&self, file: &SolidFile, _files: &Vec) -> Vec { let mut res = Vec::new(); @@ -52,7 +53,7 @@ impl RuleType for MaxStatesCount { count += 1; if count > self.max_states { let location = decode_location(&var.src, &file.content); - res.push(self.create_diag(file, var, location)); + res.push(self.create_diag(file, location, count)); } } } diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/reason_string.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/reason_string.rs index fe3670a0..5eb664a8 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/reason_string.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/reason_string.rs @@ -1,5 +1,5 @@ use solc_wrapper::ast::utils::{self, get_all_nodes_by_type}; -use solc_wrapper::{decode_location, Expression, NodeType}; +use solc_wrapper::{decode_location, Expression, NodeType, CodeLocation}; use crate::linter::SolidFile; use crate::rules::types::{RuleEntry, RuleType}; @@ -16,8 +16,7 @@ pub struct ReasonString { data: RuleEntry, } -impl RuleType for ReasonString { - +impl ReasonString { fn create_diag(&self, file: &SolidFile, location: (CodeLocation, CodeLocation), message: String) -> LintDiag { LintDiag { range: Range { @@ -39,6 +38,9 @@ impl RuleType for ReasonString { source_file_content: file.content.clone(), } } +} + +impl RuleType for ReasonString { fn diagnose(&self, file: &SolidFile, _files: &Vec) -> Vec { let mut res = Vec::new(); diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/miscellaneous/quotes.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/miscellaneous/quotes.rs index 1e0a2a88..fa572dc8 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/miscellaneous/quotes.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/miscellaneous/quotes.rs @@ -1,4 +1,3 @@ -use solc_wrapper::{CodeLocation, decode_location}; use crate::linter::SolidFile; use crate::rules::types::*; use crate::types::*; @@ -7,9 +6,8 @@ pub struct Quotes { data: RuleEntry, } -impl RuleType for Quotes { - - fn create_diag(&self, file: SolidFile, idx: usize, line_idx: u32) { +impl Quotes { + fn create_diag(&self, file: &SolidFile, idx: usize, line_idx: u64) -> LintDiag { LintDiag { range: Range { start: Position { @@ -30,6 +28,9 @@ impl RuleType for Quotes { source_file_content: file.content.clone(), } } +} + +impl RuleType for Quotes { fn diagnose(&self, file: &SolidFile, _files: &Vec) -> Vec { let mut res = Vec::new(); diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/contract_name_pascalcase.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/contract_name_pascalcase.rs index 48231353..f798a934 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/contract_name_pascalcase.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/contract_name_pascalcase.rs @@ -7,9 +7,8 @@ pub struct ContractNamePascalCase { data: RuleEntry, } -impl RuleType for ContractNamePascalCase { - - fn create_diag(&self, location: (CodeLocation, CodeLocation), file: SolidFile) { +impl ContractNamePascalCase { + fn create_diag(&self, location: (CodeLocation, CodeLocation), file: &SolidFile) -> LintDiag { LintDiag { range: Range { start: Position { @@ -30,6 +29,9 @@ impl RuleType for ContractNamePascalCase { source_file_content: file.content.clone(), } } +} + +impl RuleType for ContractNamePascalCase { fn diagnose(&self, file: &SolidFile, _files: &Vec) -> Vec { let mut res = Vec::new(); diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_name_camelcase.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_name_camelcase.rs index 5a9e8b24..ab0c6cc7 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_name_camelcase.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_name_camelcase.rs @@ -7,8 +7,8 @@ pub struct FuncNameCamelCase { data: RuleEntry, } -impl RuleType for FuncNameCamelCase { - fn create_diag(&self, location: (CodeLocation, CodeLocation), file: SolidFile) { +impl FuncNameCamelCase { + fn create_diag(&self, location: (CodeLocation, CodeLocation), file: &SolidFile) -> LintDiag { LintDiag { range: Range { start: Position { @@ -29,6 +29,10 @@ impl RuleType for FuncNameCamelCase { source_file_content: file.content.clone(), } } +} + +impl RuleType for FuncNameCamelCase { + fn diagnose(&self, file: &SolidFile, _files: &Vec) -> Vec { let mut res = Vec::new(); diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_param_name_camelcase.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_param_name_camelcase.rs index 4b4c4fa2..19faa8a4 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_param_name_camelcase.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_param_name_camelcase.rs @@ -7,9 +7,8 @@ pub struct FuncParamNameCamelcase { data: RuleEntry, } -impl RuleType for FuncParamNameCamelcase { - - fn create_diag(&self, location: (CodeLocation, CodeLocation), file: SolidFile) { +impl FuncParamNameCamelcase { + fn create_diag(&self, location: (CodeLocation, CodeLocation), file: &SolidFile) -> LintDiag { LintDiag { range: Range { start: Position { @@ -32,6 +31,9 @@ impl RuleType for FuncParamNameCamelcase { source_file_content: file.content.clone(), } } +} + +impl RuleType for FuncParamNameCamelcase { fn diagnose(&self, file: &SolidFile, _files: &Vec) -> Vec { let mut res = Vec::new(); diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/use_forbidden_name.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/use_forbidden_name.rs index 8e296fde..38ecb51b 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/use_forbidden_name.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/use_forbidden_name.rs @@ -8,9 +8,8 @@ pub struct UseForbiddenName { data: RuleEntry, } -impl RuleType for UseForbiddenName { - - fn create_diag(&self, location: (CodeLocation, CodeLocation), var: Box, file: SolidFile) { +impl UseForbiddenName { + fn create_diag(&self, location: (CodeLocation, CodeLocation), var: Box, file: &SolidFile) -> LintDiag { LintDiag { range: Range { start: Position { @@ -31,6 +30,11 @@ impl RuleType for UseForbiddenName { source_file_content: file.content.clone(), } } +} + +impl RuleType for UseForbiddenName { + + fn diagnose(&self, file: &SolidFile, _files: &Vec) -> Vec { let mut res = Vec::new(); diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/order/import_on_top.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/order/import_on_top.rs index 7b297ea7..d5bc85b4 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/order/import_on_top.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/order/import_on_top.rs @@ -1,16 +1,14 @@ use crate::linter::SolidFile; use crate::rules::types::*; use crate::types::*; -use solc_wrapper::{decode_location, SourceUnitChildNodes}; +use solc_wrapper::{decode_location, SourceUnitChildNodes, CodeLocation}; pub struct ImportOnTop { data: RuleEntry, } -impl RuleType for ImportOnTop { - - fn create_diag(&self, file: &SolidFile, import: &SourceUnitChildNodes, - location: (CodeLocation, CodeLocation)) -> LintDiag { +impl ImportOnTop { + fn create_diag(&self, file: &SolidFile, location: (CodeLocation, CodeLocation)) -> LintDiag { LintDiag { range: Range { start: Position { @@ -23,7 +21,7 @@ impl RuleType for ImportOnTop { }, length: location.0.length as u64, }, - message: String("Import must be on top in the file"), + message: String::from("Import must be on top in the file"), severity: Some(self.data.severity), code: None, source: None, @@ -31,6 +29,11 @@ impl RuleType for ImportOnTop { source_file_content: file.content.clone(), } } +} + +impl RuleType for ImportOnTop { + + fn diagnose(&self, file: &SolidFile, _files: &Vec) -> Vec { let mut res = Vec::new(); @@ -52,7 +55,7 @@ impl RuleType for ImportOnTop { SourceUnitChildNodes::ImportDirective(import) => { if i > last_import_location { let location = decode_location(&import.src, &file.content); - res.push(self.generate_diagnostic(file, import)); + res.push(self.create_diag(file, location)); } } _ => {} From 644b521b68f902c76c0a22ad9abbce6dedd3ee15 Mon Sep 17 00:00:00 2001 From: 0xtekgrinder <0xtekgrinder@protonmail.com> Date: Tue, 12 Sep 2023 08:20:43 +0200 Subject: [PATCH 19/59] tests(solidhunter): add test engine --- toolchains/solidity/linter/core/.gitignore | 3 +- .../linter/core/solidhunter-lib/src/linter.rs | 4 +- .../best_practises/function_max_lines.rs | 1 + .../src/rules/best_practises/line_maxlen.rs | 2 +- .../rules/best_practises/max_states_count.rs | 9 +- .../src/rules/best_practises/reason_string.rs | 11 +- .../src/rules/miscellaneous/quotes.rs | 2 +- .../rules/naming/contract_name_pascalcase.rs | 154 +++++++-------- .../src/rules/naming/func_name_camelcase.rs | 178 ++++++++--------- .../rules/naming/func_param_name_camelcase.rs | 180 +++++++++--------- .../src/rules/naming/use_forbidden_name.rs | 11 +- .../src/rules/order/import_on_top.rs | 162 ++++++++-------- .../solidhunter-lib/src/rules/rule_impl.rs | 1 - .../linter/core/solidhunter-lib/src/types.rs | 8 + .../testdata/LineMaxLen/.solidhunter.json | 65 +++++++ .../testdata/LineMaxLen/findings.csv | 1 + .../core/solidhunter-lib/tests/linter.rs | 81 ++++++++ 17 files changed, 522 insertions(+), 351 deletions(-) create mode 100644 toolchains/solidity/linter/core/solidhunter-lib/testdata/LineMaxLen/.solidhunter.json create mode 100644 toolchains/solidity/linter/core/solidhunter-lib/testdata/LineMaxLen/findings.csv create mode 100644 toolchains/solidity/linter/core/solidhunter-lib/tests/linter.rs diff --git a/toolchains/solidity/linter/core/.gitignore b/toolchains/solidity/linter/core/.gitignore index 4940fa07..9f672f43 100644 --- a/toolchains/solidity/linter/core/.gitignore +++ b/toolchains/solidity/linter/core/.gitignore @@ -10,4 +10,5 @@ target # These are backup files generated by rustfmt **/*.rs.bk -.solidhunter.json \ No newline at end of file +.solidhunter.json +!/solidhunter-lib/**/**/.solidhunter.json diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs index 415ad961..fd326bd1 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs @@ -140,7 +140,9 @@ impl SolidLinter { if let Ok(entries) = glob(&*(folder + "/**/*.sol")) { for entry in entries { if let Ok(path) = entry { - result.push(self.parse_file(String::from(path.into_os_string().into_string().unwrap()))); + result.push( + self.parse_file(String::from(path.into_os_string().into_string().unwrap())), + ); } } } diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/function_max_lines.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/function_max_lines.rs index 7a7fcd29..b2b5febd 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/function_max_lines.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/function_max_lines.rs @@ -23,6 +23,7 @@ impl RuleType for FunctionMaxLines { let _report = check_function_lines(_file, function, self.number_max_lines); if let Some(report) = _report { res.push(LintDiag { + id: Self::RULE_ID.to_string(), range: report, severity: Some(Severity::WARNING), code: None, diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/line_maxlen.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/line_maxlen.rs index d208edd5..b4d2be23 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/line_maxlen.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/line_maxlen.rs @@ -21,6 +21,7 @@ impl LineMaxLen { }, length: (line.len() - self.max_len) as u64, }, + id: "line-max-len".to_string(), message: format!("Line is too long: {}", line.len()), severity: Some(self.data.severity), code: None, @@ -32,7 +33,6 @@ impl LineMaxLen { } impl RuleType for LineMaxLen { - fn diagnose(&self, file: &SolidFile, _files: &Vec) -> Vec { let mut res = Vec::new(); let mut line_idx = 1; diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/max_states_count.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/max_states_count.rs index 08cde0ec..793ef258 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/max_states_count.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/max_states_count.rs @@ -9,8 +9,14 @@ pub struct MaxStatesCount { } impl MaxStatesCount { - fn create_diag(&self, file: &SolidFile, location: (CodeLocation, CodeLocation), count: usize) -> LintDiag { + fn create_diag( + &self, + file: &SolidFile, + location: (CodeLocation, CodeLocation), + count: usize, + ) -> LintDiag { LintDiag { + id: Self::RULE_ID.to_string(), range: Range { start: Position { line: location.0.line as u64, @@ -33,7 +39,6 @@ impl MaxStatesCount { } impl RuleType for MaxStatesCount { - fn diagnose(&self, file: &SolidFile, _files: &Vec) -> Vec { let mut res = Vec::new(); diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/reason_string.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/reason_string.rs index 5eb664a8..7ad81518 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/reason_string.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/reason_string.rs @@ -1,5 +1,5 @@ use solc_wrapper::ast::utils::{self, get_all_nodes_by_type}; -use solc_wrapper::{decode_location, Expression, NodeType, CodeLocation}; +use solc_wrapper::{decode_location, CodeLocation, Expression, NodeType}; use crate::linter::SolidFile; use crate::rules::types::{RuleEntry, RuleType}; @@ -17,8 +17,14 @@ pub struct ReasonString { } impl ReasonString { - fn create_diag(&self, file: &SolidFile, location: (CodeLocation, CodeLocation), message: String) -> LintDiag { + fn create_diag( + &self, + file: &SolidFile, + location: (CodeLocation, CodeLocation), + message: String, + ) -> LintDiag { LintDiag { + id: RULE_ID.to_string(), range: Range { start: Position { line: location.0.line as u64, @@ -41,7 +47,6 @@ impl ReasonString { } impl RuleType for ReasonString { - fn diagnose(&self, file: &SolidFile, _files: &Vec) -> Vec { let mut res = Vec::new(); diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/miscellaneous/quotes.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/miscellaneous/quotes.rs index fa572dc8..e1dabdad 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/miscellaneous/quotes.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/miscellaneous/quotes.rs @@ -9,6 +9,7 @@ pub struct Quotes { impl Quotes { fn create_diag(&self, file: &SolidFile, idx: usize, line_idx: u64) -> LintDiag { LintDiag { + id: "quotes".to_string(), range: Range { start: Position { line: line_idx, @@ -31,7 +32,6 @@ impl Quotes { } impl RuleType for Quotes { - fn diagnose(&self, file: &SolidFile, _files: &Vec) -> Vec { let mut res = Vec::new(); let mut line_idx = 1; diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/contract_name_pascalcase.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/contract_name_pascalcase.rs index f798a934..2f5463bf 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/contract_name_pascalcase.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/contract_name_pascalcase.rs @@ -1,77 +1,77 @@ -use crate::linter::SolidFile; -use crate::rules::types::*; -use crate::types::*; -use solc_wrapper::{CodeLocation, decode_location, SourceUnitChildNodes}; - -pub struct ContractNamePascalCase { - data: RuleEntry, -} - -impl ContractNamePascalCase { - fn create_diag(&self, location: (CodeLocation, CodeLocation), file: &SolidFile) -> LintDiag { - LintDiag { - range: Range { - start: Position { - line: location.0.line as u64, - character: location.0.column as u64, - }, - end: Position { - line: location.1.line as u64, - character: location.1.column as u64, - }, - length: location.0.length as u64, - }, - message: format!("Contract name need to be in pascal case"), - severity: Some(self.data.severity), - code: None, - source: None, - uri: file.path.clone(), - source_file_content: file.content.clone(), - } - } -} - -impl RuleType for ContractNamePascalCase { - - fn diagnose(&self, file: &SolidFile, _files: &Vec) -> Vec { - let mut res = Vec::new(); - - for node in &file.data.nodes { - match node { - SourceUnitChildNodes::ContractDefinition(contract) => { - if (contract.name.chars().nth(0).unwrap() >= 'a' - && contract.name.chars().nth(0).unwrap() <= 'z') - || contract.name.contains("_") - || contract.name.contains("-") - { - //Untested - let location = decode_location( - contract.name_location.as_ref().unwrap(), - &file.content, - ); - res.push(self.create_diag(location, file)); - } - } - _ => { - continue; - } - } - } - res - } -} - -impl ContractNamePascalCase { - pub(crate) fn create(data: RuleEntry) -> Box { - let rule = ContractNamePascalCase { data }; - Box::new(rule) - } - - pub(crate) fn create_default() -> RuleEntry { - RuleEntry { - id: "contract-name-pascalcase".to_string(), - severity: Severity::WARNING, - data: vec![], - } - } -} +use crate::linter::SolidFile; +use crate::rules::types::*; +use crate::types::*; +use solc_wrapper::{decode_location, CodeLocation, SourceUnitChildNodes}; + +pub struct ContractNamePascalCase { + data: RuleEntry, +} + +impl ContractNamePascalCase { + fn create_diag(&self, location: (CodeLocation, CodeLocation), file: &SolidFile) -> LintDiag { + LintDiag { + id: "contract-name-pascalcase".to_string(), + range: Range { + start: Position { + line: location.0.line as u64, + character: location.0.column as u64, + }, + end: Position { + line: location.1.line as u64, + character: location.1.column as u64, + }, + length: location.0.length as u64, + }, + message: format!("Contract name need to be in pascal case"), + severity: Some(self.data.severity), + code: None, + source: None, + uri: file.path.clone(), + source_file_content: file.content.clone(), + } + } +} + +impl RuleType for ContractNamePascalCase { + fn diagnose(&self, file: &SolidFile, _files: &Vec) -> Vec { + let mut res = Vec::new(); + + for node in &file.data.nodes { + match node { + SourceUnitChildNodes::ContractDefinition(contract) => { + if (contract.name.chars().nth(0).unwrap() >= 'a' + && contract.name.chars().nth(0).unwrap() <= 'z') + || contract.name.contains("_") + || contract.name.contains("-") + { + //Untested + let location = decode_location( + contract.name_location.as_ref().unwrap(), + &file.content, + ); + res.push(self.create_diag(location, file)); + } + } + _ => { + continue; + } + } + } + res + } +} + +impl ContractNamePascalCase { + pub(crate) fn create(data: RuleEntry) -> Box { + let rule = ContractNamePascalCase { data }; + Box::new(rule) + } + + pub(crate) fn create_default() -> RuleEntry { + RuleEntry { + id: "contract-name-pascalcase".to_string(), + severity: Severity::WARNING, + data: vec![], + } + } +} diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_name_camelcase.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_name_camelcase.rs index ab0c6cc7..cb43efcc 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_name_camelcase.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_name_camelcase.rs @@ -1,88 +1,90 @@ -use crate::linter::SolidFile; -use crate::rules::types::*; -use crate::types::*; -use solc_wrapper::{decode_location, ContractDefinitionChildNodes, FunctionDefinitionKind, SourceUnitChildNodes, CodeLocation}; - -pub struct FuncNameCamelCase { - data: RuleEntry, -} - -impl FuncNameCamelCase { - fn create_diag(&self, location: (CodeLocation, CodeLocation), file: &SolidFile) -> LintDiag { - LintDiag { - range: Range { - start: Position { - line: location.0.line as u64, - character: location.0.column as u64, - }, - end: Position { - line: location.1.line as u64, - character: location.1.column as u64, - }, - length: location.0.length as u64, - }, - message: format!("Function name need to be in camel case"), - severity: Some(self.data.severity), - code: None, - source: None, - uri: file.path.clone(), - source_file_content: file.content.clone(), - } - } -} - -impl RuleType for FuncNameCamelCase { - - - fn diagnose(&self, file: &SolidFile, _files: &Vec) -> Vec { - let mut res = Vec::new(); - - for node in &file.data.nodes { - match node { - SourceUnitChildNodes::ContractDefinition(contract) => { - for node in &contract.nodes { - match node { - ContractDefinitionChildNodes::FunctionDefinition(function) => { - if function.kind != FunctionDefinitionKind::Constructor - && (!(function.name.chars().nth(0).unwrap_or(' ') >= 'a' - && function.name.chars().nth(0).unwrap_or(' ') <= 'z') - || function.name.contains('_') - || function.name.contains('-')) - { - //Untested - let location = decode_location( - function.name_location.as_ref().unwrap(), - &file.content, - ); - res.push(self.create_diag(location, file)); - } - } - _ => { - continue; - } - } - } - } - _ => { - continue; - } - } - } - res - } -} - -impl FuncNameCamelCase { - pub(crate) fn create(data: RuleEntry) -> Box { - let rule = FuncNameCamelCase { data }; - Box::new(rule) - } - - pub(crate) fn create_default() -> RuleEntry { - RuleEntry { - id: "func-name-camelcase".to_string(), - severity: Severity::WARNING, - data: vec![], - } - } -} +use crate::linter::SolidFile; +use crate::rules::types::*; +use crate::types::*; +use solc_wrapper::{ + decode_location, CodeLocation, ContractDefinitionChildNodes, FunctionDefinitionKind, + SourceUnitChildNodes, +}; + +pub struct FuncNameCamelCase { + data: RuleEntry, +} + +impl FuncNameCamelCase { + fn create_diag(&self, location: (CodeLocation, CodeLocation), file: &SolidFile) -> LintDiag { + LintDiag { + id: "func-name-camelcase".to_string(), + range: Range { + start: Position { + line: location.0.line as u64, + character: location.0.column as u64, + }, + end: Position { + line: location.1.line as u64, + character: location.1.column as u64, + }, + length: location.0.length as u64, + }, + message: format!("Function name need to be in camel case"), + severity: Some(self.data.severity), + code: None, + source: None, + uri: file.path.clone(), + source_file_content: file.content.clone(), + } + } +} + +impl RuleType for FuncNameCamelCase { + fn diagnose(&self, file: &SolidFile, _files: &Vec) -> Vec { + let mut res = Vec::new(); + + for node in &file.data.nodes { + match node { + SourceUnitChildNodes::ContractDefinition(contract) => { + for node in &contract.nodes { + match node { + ContractDefinitionChildNodes::FunctionDefinition(function) => { + if function.kind != FunctionDefinitionKind::Constructor + && (!(function.name.chars().nth(0).unwrap_or(' ') >= 'a' + && function.name.chars().nth(0).unwrap_or(' ') <= 'z') + || function.name.contains('_') + || function.name.contains('-')) + { + //Untested + let location = decode_location( + function.name_location.as_ref().unwrap(), + &file.content, + ); + res.push(self.create_diag(location, file)); + } + } + _ => { + continue; + } + } + } + } + _ => { + continue; + } + } + } + res + } +} + +impl FuncNameCamelCase { + pub(crate) fn create(data: RuleEntry) -> Box { + let rule = FuncNameCamelCase { data }; + Box::new(rule) + } + + pub(crate) fn create_default() -> RuleEntry { + RuleEntry { + id: "func-name-camelcase".to_string(), + severity: Severity::WARNING, + data: vec![], + } + } +} diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_param_name_camelcase.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_param_name_camelcase.rs index 19faa8a4..5c7779ac 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_param_name_camelcase.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_param_name_camelcase.rs @@ -1,90 +1,90 @@ -use crate::linter::SolidFile; -use crate::rules::types::*; -use crate::types::*; -use solc_wrapper::{decode_location, ContractDefinitionChildNodes, SourceUnitChildNodes, CodeLocation}; - -pub struct FuncParamNameCamelcase { - data: RuleEntry, -} - -impl FuncParamNameCamelcase { - fn create_diag(&self, location: (CodeLocation, CodeLocation), file: &SolidFile) -> LintDiag { - LintDiag { - range: Range { - start: Position { - line: location.0.line as u64, - character: location.0.column as u64, - }, - end: Position { - line: location.1.line as u64, - character: location.1.column as u64, - }, - length: location.0.length as u64, - }, - message: format!( - "Parameter name need to be in camel case" - ), - severity: Some(self.data.severity), - code: None, - source: None, - uri: file.path.clone(), - source_file_content: file.content.clone(), - } - } -} - -impl RuleType for FuncParamNameCamelcase { - - fn diagnose(&self, file: &SolidFile, _files: &Vec) -> Vec { - let mut res = Vec::new(); - - for node in &file.data.nodes { - match node { - SourceUnitChildNodes::ContractDefinition(contract) => { - for node in &contract.nodes { - match node { - ContractDefinitionChildNodes::FunctionDefinition(function) => { - for parameter in &function.parameters.parameters { - if !(parameter.name.chars().nth(0).unwrap() >= 'a' - && parameter.name.chars().nth(0).unwrap() <= 'z') - || parameter.name.contains("_") - || parameter.name.contains("-") - { - //Untested - let location = decode_location( - parameter.name_location.as_ref().unwrap(), - &file.content, - ); - res.push(self.create_diag(location, file)); - } - } - } - _ => { - continue; - } - } - } - } - _ => { - continue; - } - } - } - res - } -} - -impl FuncParamNameCamelcase { - pub(crate) fn create(data: RuleEntry) -> Box { - let rule = FuncParamNameCamelcase { data }; - Box::new(rule) - } - - pub(crate) fn create_default() -> RuleEntry { - RuleEntry { - id: "func-param-name-camelcase".to_string(), - severity: Severity::WARNING, - data: vec![], - } - } -} +use crate::linter::SolidFile; +use crate::rules::types::*; +use crate::types::*; +use solc_wrapper::{ + decode_location, CodeLocation, ContractDefinitionChildNodes, SourceUnitChildNodes, +}; + +pub struct FuncParamNameCamelcase { + data: RuleEntry, +} + +impl FuncParamNameCamelcase { + fn create_diag(&self, location: (CodeLocation, CodeLocation), file: &SolidFile) -> LintDiag { + LintDiag { + id: "func-param-name-camelcase".to_string(), + range: Range { + start: Position { + line: location.0.line as u64, + character: location.0.column as u64, + }, + end: Position { + line: location.1.line as u64, + character: location.1.column as u64, + }, + length: location.0.length as u64, + }, + message: format!("Parameter name need to be in camel case"), + severity: Some(self.data.severity), + code: None, + source: None, + uri: file.path.clone(), + source_file_content: file.content.clone(), + } + } +} + +impl RuleType for FuncParamNameCamelcase { + fn diagnose(&self, file: &SolidFile, _files: &Vec) -> Vec { + let mut res = Vec::new(); + + for node in &file.data.nodes { + match node { + SourceUnitChildNodes::ContractDefinition(contract) => { + for node in &contract.nodes { + match node { + ContractDefinitionChildNodes::FunctionDefinition(function) => { + for parameter in &function.parameters.parameters { + if !(parameter.name.chars().nth(0).unwrap() >= 'a' + && parameter.name.chars().nth(0).unwrap() <= 'z') + || parameter.name.contains("_") + || parameter.name.contains("-") + { + //Untested + let location = decode_location( + parameter.name_location.as_ref().unwrap(), + &file.content, + ); + res.push(self.create_diag(location, file)); + } + } + } + _ => { + continue; + } + } + } + } + _ => { + continue; + } + } + } + res + } +} + +impl FuncParamNameCamelcase { + pub(crate) fn create(data: RuleEntry) -> Box { + let rule = FuncParamNameCamelcase { data }; + Box::new(rule) + } + + pub(crate) fn create_default() -> RuleEntry { + RuleEntry { + id: "func-param-name-camelcase".to_string(), + severity: Severity::WARNING, + data: vec![], + } + } +} diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/use_forbidden_name.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/use_forbidden_name.rs index 38ecb51b..acc8d4d8 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/use_forbidden_name.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/use_forbidden_name.rs @@ -9,8 +9,14 @@ pub struct UseForbiddenName { } impl UseForbiddenName { - fn create_diag(&self, location: (CodeLocation, CodeLocation), var: Box, file: &SolidFile) -> LintDiag { + fn create_diag( + &self, + location: (CodeLocation, CodeLocation), + var: Box, + file: &SolidFile, + ) -> LintDiag { LintDiag { + id: "use-forbidden-name".to_string(), range: Range { start: Position { line: location.0.line as u64, @@ -33,9 +39,6 @@ impl UseForbiddenName { } impl RuleType for UseForbiddenName { - - - fn diagnose(&self, file: &SolidFile, _files: &Vec) -> Vec { let mut res = Vec::new(); let blacklist = vec!['I', 'l', 'O']; diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/order/import_on_top.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/order/import_on_top.rs index d5bc85b4..b9ea19ae 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/order/import_on_top.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/order/import_on_top.rs @@ -1,82 +1,80 @@ -use crate::linter::SolidFile; -use crate::rules::types::*; -use crate::types::*; -use solc_wrapper::{decode_location, SourceUnitChildNodes, CodeLocation}; - -pub struct ImportOnTop { - data: RuleEntry, -} - -impl ImportOnTop { - fn create_diag(&self, file: &SolidFile, location: (CodeLocation, CodeLocation)) -> LintDiag { - LintDiag { - range: Range { - start: Position { - line: location.0.line as u64, - character: location.0.column as u64, - }, - end: Position { - line: location.1.line as u64, - character: location.1.column as u64, - }, - length: location.0.length as u64, - }, - message: String::from("Import must be on top in the file"), - severity: Some(self.data.severity), - code: None, - source: None, - uri: file.path.clone(), - source_file_content: file.content.clone(), - } - } -} - -impl RuleType for ImportOnTop { - - - - fn diagnose(&self, file: &SolidFile, _files: &Vec) -> Vec { - let mut res = Vec::new(); - let mut last_import_location = 0; - - for i in 1..file.data.nodes.len() { - match &file.data.nodes[i] { - SourceUnitChildNodes::ImportDirective(_) => { - last_import_location = i; - } - _ => { - break; - } - } - } - - for i in 1..file.data.nodes.len() { - match &file.data.nodes[i] { - SourceUnitChildNodes::ImportDirective(import) => { - if i > last_import_location { - let location = decode_location(&import.src, &file.content); - res.push(self.create_diag(file, location)); - } - } - _ => {} - } - } - - res - } -} - -impl ImportOnTop { - pub(crate) fn create(data: RuleEntry) -> Box { - let rule = ImportOnTop { data }; - Box::new(rule) - } - - pub(crate) fn create_default() -> RuleEntry { - RuleEntry { - id: "import-on-top".to_string(), - severity: Severity::WARNING, - data: vec![], - } - } -} +use crate::linter::SolidFile; +use crate::rules::types::*; +use crate::types::*; +use solc_wrapper::{decode_location, CodeLocation, SourceUnitChildNodes}; + +pub struct ImportOnTop { + data: RuleEntry, +} + +impl ImportOnTop { + fn create_diag(&self, file: &SolidFile, location: (CodeLocation, CodeLocation)) -> LintDiag { + LintDiag { + id: "import-on-top".to_string(), + range: Range { + start: Position { + line: location.0.line as u64, + character: location.0.column as u64, + }, + end: Position { + line: location.1.line as u64, + character: location.1.column as u64, + }, + length: location.0.length as u64, + }, + message: String::from("Import must be on top in the file"), + severity: Some(self.data.severity), + code: None, + source: None, + uri: file.path.clone(), + source_file_content: file.content.clone(), + } + } +} + +impl RuleType for ImportOnTop { + fn diagnose(&self, file: &SolidFile, _files: &Vec) -> Vec { + let mut res = Vec::new(); + let mut last_import_location = 0; + + for i in 1..file.data.nodes.len() { + match &file.data.nodes[i] { + SourceUnitChildNodes::ImportDirective(_) => { + last_import_location = i; + } + _ => { + break; + } + } + } + + for i in 1..file.data.nodes.len() { + match &file.data.nodes[i] { + SourceUnitChildNodes::ImportDirective(import) => { + if i > last_import_location { + let location = decode_location(&import.src, &file.content); + res.push(self.create_diag(file, location)); + } + } + _ => {} + } + } + + res + } +} + +impl ImportOnTop { + pub(crate) fn create(data: RuleEntry) -> Box { + let rule = ImportOnTop { data }; + Box::new(rule) + } + + pub(crate) fn create_default() -> RuleEntry { + RuleEntry { + id: "import-on-top".to_string(), + severity: Severity::WARNING, + data: vec![], + } + } +} diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs index 98b0692d..5f2fbac4 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs @@ -2,7 +2,6 @@ use crate::error::SolidHunterError; use crate::rules::create_default_rules; use crate::rules::types::*; - pub fn create_rules_file(path: &str) { let rules = Rules { name: "solidhunter".to_string(), diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs index a360625b..0662777c 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs @@ -27,6 +27,8 @@ pub struct LintDiag { /// The diagnostic's message. pub message: String, + pub id: String, + pub uri: Uri, #[serde(rename = "sourceFileContent")] @@ -81,6 +83,12 @@ impl fmt::Display for LintDiag { /////////////////// RELATED TYPES: ///////////////////////// //////////////////////////////////////////////////////////// +impl PartialEq for Position { + fn eq(&self, other: &Self) -> bool { + self.line == other.line && self.character == other.character + } +} + #[derive(Clone, Serialize, Deserialize, Debug)] pub struct Position { pub line: u64, diff --git a/toolchains/solidity/linter/core/solidhunter-lib/testdata/LineMaxLen/.solidhunter.json b/toolchains/solidity/linter/core/solidhunter-lib/testdata/LineMaxLen/.solidhunter.json new file mode 100644 index 00000000..b4a09fbf --- /dev/null +++ b/toolchains/solidity/linter/core/solidhunter-lib/testdata/LineMaxLen/.solidhunter.json @@ -0,0 +1,65 @@ +{ + "name": "solidhunter", + "includes": [], + "plugins": [], + "rules": [ + { + "id": "line-max-len", + "severity": "WARNING", + "data": [ + "80" + ] + }, + { + "id": "max-states-count", + "severity": "WARNING", + "data": [ + "15" + ] + }, + { + "id": "function-max-lines", + "severity": "WARNING", + "data": [ + "20" + ] + }, + { + "id": "reason-string", + "severity": "WARNING", + "data": [ + "32" + ] + }, + { + "id": "quotes", + "severity": "ERROR", + "data": [] + }, + { + "id": "func-param-name-camelcase", + "severity": "WARNING", + "data": [] + }, + { + "id": "contract-name-pascalcase", + "severity": "WARNING", + "data": [] + }, + { + "id": "func-name-camelcase", + "severity": "WARNING", + "data": [] + }, + { + "id": "use-forbidden-name", + "severity": "WARNING", + "data": [] + }, + { + "id": "import-on-top", + "severity": "WARNING", + "data": [] + } + ] +} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solidhunter-lib/testdata/LineMaxLen/findings.csv b/toolchains/solidity/linter/core/solidhunter-lib/testdata/LineMaxLen/findings.csv new file mode 100644 index 00000000..35a86c33 --- /dev/null +++ b/toolchains/solidity/linter/core/solidhunter-lib/testdata/LineMaxLen/findings.csv @@ -0,0 +1 @@ +line-max-len:4:80:12 \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solidhunter-lib/tests/linter.rs b/toolchains/solidity/linter/core/solidhunter-lib/tests/linter.rs new file mode 100644 index 00000000..e4ceae98 --- /dev/null +++ b/toolchains/solidity/linter/core/solidhunter-lib/tests/linter.rs @@ -0,0 +1,81 @@ +use solidhunter_lib::linter::SolidLinter; +use solidhunter_lib::types::Position; +use std::{fs, path::PathBuf}; + +struct Finding { + start: Position, + length: u64, + id: String, +} + +fn test_directory(base_name: &str) { + let mut source = String::new(); + let mut config = String::new(); + let mut expected_findings: Vec = Vec::new(); + + for path in fs::read_dir( + PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join("testdata") + .join(base_name), + ) + .unwrap() + { + let path = path.unwrap().path(); + + if let Some(filename) = path.file_name().and_then(|name| name.to_str()) { + if filename == "file.sol" { + source = path.to_str().unwrap().to_string(); + } else if filename == ".solidhunter.json" { + config = path.to_str().unwrap().to_string(); + } else if filename == "findings.csv" { + for line in fs::read_to_string(path).unwrap().lines() { + let splitted_line: Vec<&str> = line.split(':').collect(); + expected_findings.push(Finding { + start: Position { + line: splitted_line[1].parse::().unwrap(), + character: splitted_line[2].parse::().unwrap(), + }, + length: splitted_line[3].parse::().unwrap(), + id: splitted_line[0].to_string(), + }); + } + } + } + } + + test_linter(&config, &source, &expected_findings); +} + +fn test_linter(config: &str, source: &str, expected_findings: &Vec) { + println!("{}", config); + let mut linter: SolidLinter = SolidLinter::new(&String::from(config)); + + let result = linter.parse_file(String::from(source)); + match result { + Ok(diags) => { + assert_eq!(diags.len(), expected_findings.len()); + for (i, el) in diags.iter().enumerate() { + assert_eq!(el.id, expected_findings[i].id); + assert_eq!(el.range.start, expected_findings[i].start); + assert_eq!(el.range.length, expected_findings[i].length); + } + } + Err(e) => { + panic!("{}", e); + } + } +} + +macro_rules! test_directories { + ($($dir:ident),+ $(,)?) => {$( + #[allow(non_snake_case)] + #[test] + fn $dir() { + test_directory(stringify!($dir)); + } + )+}; +} + +test_directories! { + LineMaxLen +} From 2d59a35753108844fe28249d89acb0001f5ff92d Mon Sep 17 00:00:00 2001 From: 0xtekgrinder <0xtekgrinder@protonmail.com> Date: Tue, 12 Sep 2023 08:51:43 +0200 Subject: [PATCH 20/59] refactor(solidhunter): clean solidhunter-lib --- .../linter/core/solidhunter-lib/src/linter.rs | 25 ++++++++----------- .../best_practises/function_max_lines.rs | 10 +++----- .../src/rules/best_practises/mod.rs | 14 +++++------ .../src/rules/best_practises/reason_string.rs | 8 +++--- .../src/rules/miscellaneous/mod.rs | 6 +---- .../rules/naming/contract_name_pascalcase.rs | 6 ++--- .../src/rules/naming/func_name_camelcase.rs | 2 +- .../rules/naming/func_param_name_camelcase.rs | 6 ++--- .../solidhunter-lib/src/rules/naming/mod.rs | 14 +++++------ .../src/rules/naming/use_forbidden_name.rs | 2 +- .../solidhunter-lib/src/rules/order/mod.rs | 6 +---- 11 files changed, 40 insertions(+), 59 deletions(-) diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs index fd326bd1..794f519d 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs @@ -22,8 +22,7 @@ pub struct SolidLinter { impl Default for SolidLinter { fn default() -> Self { - let linter = SolidLinter::new(&String::new()); - linter + SolidLinter::new(&String::new()) } } @@ -34,8 +33,8 @@ impl SolidLinter { rule_factory: RuleFactory::default(), rules: Vec::new(), }; - linter._create_rules(&rules_config, true); - return linter; + linter._create_rules(rules_config, true); + linter } fn _create_rules(&mut self, rules_config: &String, first: bool) { @@ -84,9 +83,9 @@ impl SolidLinter { pub fn parse_file(&mut self, filepath: String) -> LintResult { let res = Solc::default().extract_ast_file(filepath.clone()); - if res.is_err() { + if let Err(res) = res { println!("{:?}", res); - return Err(SolidHunterError::SolcError(res.unwrap_err())); + return Err(SolidHunterError::SolcError(res)); } if self._file_exists(filepath.as_str()) { self._update_file_ast(filepath.as_str(), res.expect("ast not found")); @@ -111,9 +110,9 @@ impl SolidLinter { pub fn parse_content(&mut self, filepath: String, content: &String) -> LintResult { let res = Solc::default().extract_ast_content(content.to_string()); - if res.is_err() { + if let Err(res) = res { println!("{:?}", res); - return Err(SolidHunterError::SolcError(res.unwrap_err())); + return Err(SolidHunterError::SolcError(res)); } if self._file_exists(filepath.as_str()) { @@ -137,13 +136,9 @@ impl SolidLinter { pub fn parse_folder(&mut self, folder: String) -> Vec { let mut result: Vec = Vec::new(); - if let Ok(entries) = glob(&*(folder + "/**/*.sol")) { - for entry in entries { - if let Ok(path) = entry { - result.push( - self.parse_file(String::from(path.into_os_string().into_string().unwrap())), - ); - } + if let Ok(entries) = glob(&(folder + "/**/*.sol")) { + for entry in entries.flatten() { + result.push(self.parse_file(entry.into_os_string().into_string().unwrap())); } } result diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/function_max_lines.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/function_max_lines.rs index b2b5febd..14b57627 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/function_max_lines.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/function_max_lines.rs @@ -20,7 +20,7 @@ impl RuleType for FunctionMaxLines { let functions = get_all_functions_from_ast(&_file.data.nodes); for function in functions { - let _report = check_function_lines(_file, function, self.number_max_lines); + let _report = check_function_lines(_file, Box::new(function), self.number_max_lines); if let Some(report) = _report { res.push(LintDiag { id: Self::RULE_ID.to_string(), @@ -48,7 +48,7 @@ fn check_function_lines( let function_copy_name_location = &function.src; let (_start, _) = decode_location(function_copy_name_location.as_str(), _file.content.as_str()); let index = function_copy_name_location - .split(":") + .split(':') .collect::>()[0] .parse::() .unwrap(); @@ -88,9 +88,7 @@ fn check_function_lines( res } -fn get_all_functions_from_ast( - ast_nodes: &Vec, -) -> Vec> { +fn get_all_functions_from_ast(ast_nodes: &Vec) -> Vec { let mut res = Vec::new(); for node in ast_nodes { @@ -103,7 +101,7 @@ fn get_all_functions_from_ast( ContractDefinitionChildNodes::FunctionDefinition(function) => function, _ => continue, }; - res.push(function.clone()); + res.push(*function.clone()); } } res diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/mod.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/mod.rs index 845683f9..7ab23cfb 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/mod.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/mod.rs @@ -16,14 +16,12 @@ use crate::rules::best_practises::reason_string::ReasonString; use crate::rules::RuleBuilder; pub fn create_default_rules() -> Vec { - let mut rules = Vec::new(); - - rules.push(LineMaxLen::create_default()); - rules.push(MaxStatesCount::create_default()); - rules.push(FunctionMaxLines::create_default()); - rules.push(ReasonString::create_default()); - - rules + vec![ + LineMaxLen::create_default(), + MaxStatesCount::create_default(), + FunctionMaxLines::create_default(), + ReasonString::create_default(), + ] } pub fn create_rules() -> HashMap Box> { diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/reason_string.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/reason_string.rs index 7ad81518..e5a67338 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/reason_string.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/reason_string.rs @@ -36,7 +36,7 @@ impl ReasonString { }, length: location.0.length as u64, }, - message: message, + message, severity: Some(self.data.severity), code: None, source: None, @@ -58,7 +58,7 @@ impl RuleType for ReasonString { if v.name == "require" { if j.arguments.len() != 2 { let location = decode_location(&j.src, &file.content); - res.push(self.create_diag(file, location, format!("reason-string: A require statement must have a reason string"))); + res.push(self.create_diag(file, location, "reason-string: A require statement must have a reason string".to_string())); } else { for nj in &j.arguments { match nj { @@ -76,9 +76,9 @@ impl RuleType for ReasonString { } } } else if v.name == "revert" { - if j.arguments.len() == 0 { + if j.arguments.is_empty() { let location = decode_location(&j.src, &file.content); - res.push(self.create_diag(file, location, format!("reason-string: A revert statement must have a reason string"))); + res.push(self.create_diag(file, location, "reason-string: A revert statement must have a reason string".to_string())); } else { match &j.arguments[0] { Expression::Literal(z) => { diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/miscellaneous/mod.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/miscellaneous/mod.rs index 2db97300..a2ff21c0 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/miscellaneous/mod.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/miscellaneous/mod.rs @@ -10,11 +10,7 @@ use crate::rules::miscellaneous::quotes::Quotes; use crate::rules::RuleBuilder; pub fn create_default_rules() -> Vec { - let mut rules = Vec::new(); - - rules.push(Quotes::create_default()); - - rules + vec![Quotes::create_default()] } pub fn create_rules() -> HashMap Box> { diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/contract_name_pascalcase.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/contract_name_pascalcase.rs index 2f5463bf..e85e5aa1 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/contract_name_pascalcase.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/contract_name_pascalcase.rs @@ -22,7 +22,7 @@ impl ContractNamePascalCase { }, length: location.0.length as u64, }, - message: format!("Contract name need to be in pascal case"), + message: "Contract name need to be in pascal case".to_string(), severity: Some(self.data.severity), code: None, source: None, @@ -41,8 +41,8 @@ impl RuleType for ContractNamePascalCase { SourceUnitChildNodes::ContractDefinition(contract) => { if (contract.name.chars().nth(0).unwrap() >= 'a' && contract.name.chars().nth(0).unwrap() <= 'z') - || contract.name.contains("_") - || contract.name.contains("-") + || contract.name.contains('_') + || contract.name.contains('-') { //Untested let location = decode_location( diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_name_camelcase.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_name_camelcase.rs index cb43efcc..4c99c920 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_name_camelcase.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_name_camelcase.rs @@ -25,7 +25,7 @@ impl FuncNameCamelCase { }, length: location.0.length as u64, }, - message: format!("Function name need to be in camel case"), + message: "Function name need to be in camel case".to_string(), severity: Some(self.data.severity), code: None, source: None, diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_param_name_camelcase.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_param_name_camelcase.rs index 5c7779ac..1b06ca49 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_param_name_camelcase.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_param_name_camelcase.rs @@ -24,7 +24,7 @@ impl FuncParamNameCamelcase { }, length: location.0.length as u64, }, - message: format!("Parameter name need to be in camel case"), + message: "Parameter name need to be in camel case".to_string(), severity: Some(self.data.severity), code: None, source: None, @@ -47,8 +47,8 @@ impl RuleType for FuncParamNameCamelcase { for parameter in &function.parameters.parameters { if !(parameter.name.chars().nth(0).unwrap() >= 'a' && parameter.name.chars().nth(0).unwrap() <= 'z') - || parameter.name.contains("_") - || parameter.name.contains("-") + || parameter.name.contains('_') + || parameter.name.contains('-') { //Untested let location = decode_location( diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/mod.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/mod.rs index 011499a8..6c29aec3 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/mod.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/mod.rs @@ -15,14 +15,12 @@ pub(crate) mod use_forbidden_name; // List all rules pub fn create_default_rules() -> Vec { - let mut rules = Vec::new(); - - rules.push(FuncParamNameCamelcase::create_default()); - rules.push(ContractNamePascalCase::create_default()); - rules.push(FuncNameCamelCase::create_default()); - rules.push(UseForbiddenName::create_default()); - - rules + vec![ + FuncParamNameCamelcase::create_default(), + ContractNamePascalCase::create_default(), + FuncNameCamelCase::create_default(), + UseForbiddenName::create_default(), + ] } pub fn create_rules() -> HashMap Box> { diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/use_forbidden_name.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/use_forbidden_name.rs index acc8d4d8..982caf52 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/use_forbidden_name.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/use_forbidden_name.rs @@ -41,7 +41,7 @@ impl UseForbiddenName { impl RuleType for UseForbiddenName { fn diagnose(&self, file: &SolidFile, _files: &Vec) -> Vec { let mut res = Vec::new(); - let blacklist = vec!['I', 'l', 'O']; + let blacklist = ['I', 'l', 'O']; let nodes = get_all_nodes_by_type(file.data.clone(), NodeType::VariableDeclaration); diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/order/mod.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/order/mod.rs index 8956cfb6..a235f67d 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/order/mod.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/order/mod.rs @@ -10,11 +10,7 @@ use crate::rules::order::import_on_top::ImportOnTop; use crate::rules::RuleBuilder; pub fn create_default_rules() -> Vec { - let mut rules = Vec::new(); - - rules.push(ImportOnTop::create_default()); - - rules + vec![ImportOnTop::create_default()] } pub fn create_rules() -> HashMap Box> { From e8ade81d11325c4fbbc3afdf22df9e7594fe24bf Mon Sep 17 00:00:00 2001 From: 0xtekgrinder <0xtekgrinder@protonmail.com> Date: Tue, 12 Sep 2023 09:05:40 +0200 Subject: [PATCH 21/59] revert: SolidHunter::IoError --- toolchains/solidity/linter/core/solidhunter-lib/src/error.rs | 2 +- toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs | 2 +- .../solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/error.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/error.rs index b76e67a8..49e0d7a3 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/error.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/error.rs @@ -14,5 +14,5 @@ pub enum SolidHunterError { // RulesError #[error("SolidHunterError: IO error occured with Rules")] - IoError(String), + IoError(std::io::Error), } diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs index 794f519d..159a6616 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs @@ -91,7 +91,7 @@ impl SolidLinter { self._update_file_ast(filepath.as_str(), res.expect("ast not found")); } else { let content = fs::read_to_string(filepath.clone()) - .map_err(|e| SolidHunterError::IoError(e.to_string()))?; + .map_err(|e| SolidHunterError::IoError(e))?; self._add_file( filepath.as_str(), res.expect("ast not found"), diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs index 5f2fbac4..ef52aa07 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs @@ -17,7 +17,7 @@ pub fn create_rules_file(path: &str) { pub fn parse_rules(path: &str) -> Result { if !std::path::Path::new(&path).is_file() { return Err(SolidHunterError::IoError( - "Rules file not found".to_string(), + std::io::Error::new(std::io::ErrorKind::NotFound, "Rules file not found") )); } let file = std::fs::read_to_string(path)?; From 6e1b2cf4feab34570c1e1cd707c97d4c51b2d842 Mon Sep 17 00:00:00 2001 From: 0xtekgrinder <0xtekgrinder@protonmail.com> Date: Wed, 13 Sep 2023 06:18:41 +0200 Subject: [PATCH 22/59] feat(solidhunter): update test engine to search for the finding --- .../testdata/LineMaxLen/file.sol | 5 +++++ .../testdata/LineMaxLen/findings.csv | 2 +- .../core/solidhunter-lib/tests/linter.rs | 20 +++++++++++++------ 3 files changed, 20 insertions(+), 7 deletions(-) create mode 100644 toolchains/solidity/linter/core/solidhunter-lib/testdata/LineMaxLen/file.sol diff --git a/toolchains/solidity/linter/core/solidhunter-lib/testdata/LineMaxLen/file.sol b/toolchains/solidity/linter/core/solidhunter-lib/testdata/LineMaxLen/file.sol new file mode 100644 index 00000000..b75e6422 --- /dev/null +++ b/toolchains/solidity/linter/core/solidhunter-lib/testdata/LineMaxLen/file.sol @@ -0,0 +1,5 @@ +pragma solidity 0.8.0; + +contract Test { + event ThisIsATooLongEventNameToBeAllowedByTheLinterSoItShouldThrowAnErrorPrettySoon(); +} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solidhunter-lib/testdata/LineMaxLen/findings.csv b/toolchains/solidity/linter/core/solidhunter-lib/testdata/LineMaxLen/findings.csv index 35a86c33..66932d7e 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/testdata/LineMaxLen/findings.csv +++ b/toolchains/solidity/linter/core/solidhunter-lib/testdata/LineMaxLen/findings.csv @@ -1 +1 @@ -line-max-len:4:80:12 \ No newline at end of file +line-max-len:4:80:10 \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solidhunter-lib/tests/linter.rs b/toolchains/solidity/linter/core/solidhunter-lib/tests/linter.rs index e4ceae98..85cbc3c5 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/tests/linter.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/tests/linter.rs @@ -47,18 +47,26 @@ fn test_directory(base_name: &str) { } fn test_linter(config: &str, source: &str, expected_findings: &Vec) { - println!("{}", config); let mut linter: SolidLinter = SolidLinter::new(&String::from(config)); let result = linter.parse_file(String::from(source)); match result { Ok(diags) => { - assert_eq!(diags.len(), expected_findings.len()); - for (i, el) in diags.iter().enumerate() { - assert_eq!(el.id, expected_findings[i].id); - assert_eq!(el.range.start, expected_findings[i].start); - assert_eq!(el.range.length, expected_findings[i].length); + assert_eq!(diags.len(), expected_findings.len(), "Wrong number of findings for {}", source); + let mut found = false; + + for (_, diag) in diags.iter().enumerate() { + for (_, expected_finding) in expected_findings.iter().enumerate() { + if (diag.range.start == expected_finding.start) + && (diag.range.length == expected_finding.length) + && (diag.id == expected_finding.id) + { + found = true; + break; + } + } } + assert_eq!(found, true, "Can't find the diagnostic for {}", source); } Err(e) => { panic!("{}", e); From 511e80cabdd887face98e483e06b45a48dbd84d4 Mon Sep 17 00:00:00 2001 From: 0xtekgrinder <0xtekgrinder@protonmail.com> Date: Wed, 13 Sep 2023 06:20:42 +0200 Subject: [PATCH 23/59] chore(solidhunter): remove removeme file --- remove-me-ca154a86e5b94f5db084.txt | 1 - .../solidity/linter/core/solidhunter-lib/src/lib.rs | 9 +-------- 2 files changed, 1 insertion(+), 9 deletions(-) delete mode 100644 remove-me-ca154a86e5b94f5db084.txt diff --git a/remove-me-ca154a86e5b94f5db084.txt b/remove-me-ca154a86e5b94f5db084.txt deleted file mode 100644 index d92993f9..00000000 --- a/remove-me-ca154a86e5b94f5db084.txt +++ /dev/null @@ -1 +0,0 @@ -ca154a86e5b94f5db084 diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/lib.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/lib.rs index 87a653a1..460aae37 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/lib.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/lib.rs @@ -1,11 +1,4 @@ pub mod error; pub mod linter; pub mod rules; -pub mod types; - -#[cfg(test)] -mod tests { - - #[test] - fn it_works() {} -} +pub mod types; \ No newline at end of file From cbf13510bad8d5b028e8e5aa3e5bd2003386bb83 Mon Sep 17 00:00:00 2001 From: Astrodevs CI Date: Wed, 13 Sep 2023 23:00:22 +0000 Subject: [PATCH 24/59] chore: create branch feature/49-solidity-linter/69-use-ast-crate-in-solidhunter-staging --- remove-me-1b9390d0431343b6a1aa.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 remove-me-1b9390d0431343b6a1aa.txt diff --git a/remove-me-1b9390d0431343b6a1aa.txt b/remove-me-1b9390d0431343b6a1aa.txt new file mode 100644 index 00000000..3313bfdf --- /dev/null +++ b/remove-me-1b9390d0431343b6a1aa.txt @@ -0,0 +1 @@ +1b9390d0431343b6a1aa From 06e4dfa1930b603e31392bcee7d2d8d3c54aac11 Mon Sep 17 00:00:00 2001 From: 0xtekgrinder <0xtekgrinder@protonmail.com> Date: Thu, 14 Sep 2023 19:41:50 -0400 Subject: [PATCH 25/59] refactor(ast-extractor): make the lib easier to use with errors aside and expose syn-solidity --- libs/ast-extractor/src/errors.rs | 11 +++++++ libs/ast-extractor/src/extract.rs | 32 +++++++------------ libs/ast-extractor/src/lib.rs | 4 +++ .../src/{retriever.rs => retriever/mod.rs} | 5 --- 4 files changed, 27 insertions(+), 25 deletions(-) create mode 100644 libs/ast-extractor/src/errors.rs rename libs/ast-extractor/src/{retriever.rs => retriever/mod.rs} (74%) diff --git a/libs/ast-extractor/src/errors.rs b/libs/ast-extractor/src/errors.rs new file mode 100644 index 00000000..c8192a38 --- /dev/null +++ b/libs/ast-extractor/src/errors.rs @@ -0,0 +1,11 @@ +use syn::Error; +use thiserror::Error; +use proc_macro2::LexError; + +#[derive(Error, Debug)] +pub enum ExtractError { + #[error("Tokenization error: {0}")] + Tokenize(#[from] LexError), + #[error("Parsing error")] + Parse(#[from] Error), +} \ No newline at end of file diff --git a/libs/ast-extractor/src/extract.rs b/libs/ast-extractor/src/extract.rs index 0e462890..7c76aabe 100644 --- a/libs/ast-extractor/src/extract.rs +++ b/libs/ast-extractor/src/extract.rs @@ -1,23 +1,15 @@ -use proc_macro2::{LexError, TokenStream}; /** * extract.rs * Extract AST from solidity source code * author: 0xMemoryGrinder -*/ -use std::str::FromStr; -use syn::Error; -use thiserror::Error; + */ -#[derive(Error, Debug)] -pub enum ExtractError { - #[error("Tokenization error: {0}")] - Tokenize(#[from] LexError), - #[error("Parsing error")] - Parse(#[from] Error), -} +use crate::errors::ExtractError; +use proc_macro2::TokenStream; +use std::str::FromStr; -pub fn extract_ast_from(source: String) -> Result { - let tokens = TokenStream::from_str(source.as_str())?; +pub fn extract_ast_from_content(content: String) -> Result { + let tokens = TokenStream::from_str(content.as_str())?; let ast = syn_solidity::parse2(tokens)?; Ok(ast) } @@ -29,20 +21,20 @@ mod tests { use std::path::PathBuf; #[test] - fn test_extract_ast_from_good() { + fn test_extract_ast_from_content_good() { let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); path.push("tests"); path.push("files"); path.push("good.sol"); let source = fs::read_to_string(path).unwrap(); - let res = extract_ast_from(source); + let res = extract_ast_from_content(source); assert!(res.is_ok()); } #[test] - fn test_extract_ast_from_invalid_token() { + fn test_extract_ast_from_content_invalid_token() { let source = String::from("contract test { function test() public | uint a = 1 } }"); - let result = extract_ast_from(source); + let result = extract_ast_from_content(source); assert!(result.is_err()); assert_eq!( result.unwrap_err().to_string(), @@ -51,9 +43,9 @@ mod tests { } #[test] - fn test_extract_ast_from_missing_semicolumn() { + fn test_extract_ast_from_content_missing_semicolumn() { let source = String::from("contract test { function test() public { uint a = 1 } }"); - let result = extract_ast_from(source); + let result = extract_ast_from_content(source); assert!(result.is_err()); assert_eq!(result.unwrap_err().to_string(), "Parsing error"); } diff --git a/libs/ast-extractor/src/lib.rs b/libs/ast-extractor/src/lib.rs index 0bc0b9b5..3e452dc2 100644 --- a/libs/ast-extractor/src/lib.rs +++ b/libs/ast-extractor/src/lib.rs @@ -1,2 +1,6 @@ pub mod extract; pub mod retriever; +pub mod errors; + +// Expose syn_solidity crate +pub use syn_solidity::*; \ No newline at end of file diff --git a/libs/ast-extractor/src/retriever.rs b/libs/ast-extractor/src/retriever/mod.rs similarity index 74% rename from libs/ast-extractor/src/retriever.rs rename to libs/ast-extractor/src/retriever/mod.rs index 97e6cd01..6b71da32 100644 --- a/libs/ast-extractor/src/retriever.rs +++ b/libs/ast-extractor/src/retriever/mod.rs @@ -1,8 +1,3 @@ -/** - * retriever.rs - * Module for all AST utils functions - * author: 0xMemoryGrinder -*/ mod contract; pub use contract::*; From 6294a4ca48f5cd5d44406fe14bafb1efd304080f Mon Sep 17 00:00:00 2001 From: 0xtekgrinder <0xtekgrinder@protonmail.com> Date: Thu, 14 Sep 2023 19:42:39 -0400 Subject: [PATCH 26/59] =?UTF-8?q?refactor(solidhunter):=20remove=20solc=5F?= =?UTF-8?q?wrapp=C3=AAr=20and=20replace=20it=20with=20ast=5Fextractor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- toolchains/solidity/linter/core/Cargo.lock | 1870 ++------------- .../linter/core/solc-wrapper/.gitignore | 1 - .../linter/core/solc-wrapper/Cargo.toml | 19 - .../linter/core/solc-wrapper/src/ast/ast.rs | 2100 ----------------- .../linter/core/solc-wrapper/src/ast/error.rs | 7 - .../linter/core/solc-wrapper/src/ast/mod.rs | 4 - .../linter/core/solc-wrapper/src/ast/parse.rs | 7 - .../linter/core/solc-wrapper/src/ast/utils.rs | 731 ------ .../linter/core/solc-wrapper/src/error.rs | 28 - .../linter/core/solc-wrapper/src/lib.rs | 145 -- .../core/solc-wrapper/src/solc/command.rs | 66 - .../core/solc-wrapper/src/solc/error.rs | 19 - .../linter/core/solc-wrapper/src/solc/mod.rs | 3 - .../solc-wrapper/src/solc/parsing_error.rs | 29 - .../linter/core/solc-wrapper/src/utils.rs | 71 - .../core/solc-wrapper/src/version/error.rs | 19 - .../core/solc-wrapper/src/version/mod.rs | 2 - .../core/solc-wrapper/src/version/version.rs | 87 - .../tests/files/ast/Assignment.json | 23 - .../tests/files/ast/BinaryOperation.json | 42 - .../solc-wrapper/tests/files/ast/Block.json | 6 - .../solc-wrapper/tests/files/ast/Break.json | 5 - .../tests/files/ast/Conditional.json | 61 - .../tests/files/ast/Continue.json | 5 - .../tests/files/ast/ContractDefinition.json | 13 - .../tests/files/ast/DoWhileStatement.json | 33 - .../tests/files/ast/ElementaryTypeName.json | 8 - .../ast/ElementaryTypeNameExpression.json | 17 - .../tests/files/ast/EmitStatement.json | 42 - .../tests/files/ast/EnumDefinition.json | 24 - .../tests/files/ast/EnumValue.json | 7 - .../tests/files/ast/ErrorDefinition.json | 15 - .../tests/files/ast/EventDefinition.json | 20 - .../tests/files/ast/ExpressionStatement.json | 29 - .../tests/files/ast/ForStatement.json | 148 -- .../tests/files/ast/FunctionCall.json | 39 - .../tests/files/ast/FunctionCallOptions.json | 57 - .../tests/files/ast/FunctionDefinition.json | 31 - .../tests/files/ast/FunctionTypeName.json | 72 - .../tests/files/ast/Identifier.json | 12 - .../tests/files/ast/IfStatement.json | 167 -- .../tests/files/ast/ImportDirective.json | 10 - .../tests/files/ast/IndexAccess.json | 32 - .../tests/files/ast/IndexRangeAccess.json | 31 - .../tests/files/ast/InheritanceSpecifier.json | 14 - .../tests/files/ast/InlineAssembly.json | 12 - .../solc-wrapper/tests/files/ast/Literal.json | 9 - .../tests/files/ast/MemberAccess.json | 16 - .../tests/files/ast/ModifierDefinition.json | 74 - .../tests/files/ast/ModifierInvocation.json | 14 - .../tests/files/ast/NewExpression.json | 25 - .../tests/files/ast/ParameterList.json | 27 - .../tests/files/ast/PlaceholderStatement.json | 5 - .../tests/files/ast/PragmaDirective.json | 10 - .../solc-wrapper/tests/files/ast/Return.json | 13 - .../tests/files/ast/RevertStatement.json | 36 - .../tests/files/ast/SourceUnit.json | 8 - .../tests/files/ast/StructDefinition.json | 66 - .../files/ast/StructuredDocumentation.json | 6 - .../tests/files/ast/TryStatement.json | 244 -- .../tests/files/ast/TupleExpression.json | 44 - .../tests/files/ast/TypeDescriptions.json | 4 - .../tests/files/ast/UnaryOperation.json | 22 - .../tests/files/ast/UncheckedBlock.json | 34 - .../ast/UserDefinedValueTypeDefinition.json | 16 - .../tests/files/ast/UsingForDirective.json | 31 - .../tests/files/ast/VariableDeclaration.json | 20 - .../tests/files/ast/WhileStatement.json | 33 - .../linter/core/solidhunter-lib/Cargo.toml | 2 +- .../linter/core/solidhunter-lib/src/error.rs | 4 +- .../linter/core/solidhunter-lib/src/linter.rs | 78 +- 71 files changed, 244 insertions(+), 6780 deletions(-) delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/.gitignore delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/Cargo.toml delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/src/ast/ast.rs delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/src/ast/error.rs delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/src/ast/mod.rs delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/src/ast/parse.rs delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/src/ast/utils.rs delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/src/error.rs delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/src/lib.rs delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/src/solc/command.rs delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/src/solc/error.rs delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/src/solc/mod.rs delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/src/solc/parsing_error.rs delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/src/utils.rs delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/src/version/error.rs delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/src/version/mod.rs delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/src/version/version.rs delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/Assignment.json delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/BinaryOperation.json delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/Block.json delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/Break.json delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/Conditional.json delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/Continue.json delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/ContractDefinition.json delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/DoWhileStatement.json delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/ElementaryTypeName.json delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/ElementaryTypeNameExpression.json delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/EmitStatement.json delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/EnumDefinition.json delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/EnumValue.json delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/ErrorDefinition.json delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/EventDefinition.json delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/ExpressionStatement.json delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/ForStatement.json delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/FunctionCall.json delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/FunctionCallOptions.json delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/FunctionDefinition.json delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/FunctionTypeName.json delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/Identifier.json delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/IfStatement.json delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/ImportDirective.json delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/IndexAccess.json delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/IndexRangeAccess.json delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/InheritanceSpecifier.json delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/InlineAssembly.json delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/Literal.json delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/MemberAccess.json delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/ModifierDefinition.json delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/ModifierInvocation.json delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/NewExpression.json delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/ParameterList.json delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/PlaceholderStatement.json delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/PragmaDirective.json delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/Return.json delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/RevertStatement.json delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/SourceUnit.json delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/StructDefinition.json delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/StructuredDocumentation.json delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/TryStatement.json delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/TupleExpression.json delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/TypeDescriptions.json delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/UnaryOperation.json delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/UncheckedBlock.json delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/UserDefinedValueTypeDefinition.json delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/UsingForDirective.json delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/VariableDeclaration.json delete mode 100644 toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/WhileStatement.json diff --git a/toolchains/solidity/linter/core/Cargo.lock b/toolchains/solidity/linter/core/Cargo.lock index d9d49b76..6d557624 100644 --- a/toolchains/solidity/linter/core/Cargo.lock +++ b/toolchains/solidity/linter/core/Cargo.lock @@ -3,1795 +3,393 @@ version = 3 [[package]] -name = "adler" -version = "1.0.2" +name = "anstream" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" - -[[package]] -name = "aes" -version = "0.7.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e8b47f52ea9bae42228d07ec09eb676433d7c4ed1ebdf0f1d1c29ed446f1ab8" -dependencies = [ - "cfg-if", - "cipher", - "cpufeatures", - "opaque-debug", -] - -[[package]] -name = "aho-corasick" -version = "0.7.20" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cc936419f96fa211c1b9166887b38e5e40b19958e5b895be7c1f93adec7071ac" -dependencies = [ - "memchr", -] - -[[package]] -name = "anyhow" -version = "1.0.66" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "216261ddc8289130e551ddcd5ce8a064710c0d064a4d2895c67151c92b5443f6" - -[[package]] -name = "arrayvec" -version = "0.7.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8da52d66c7071e2e3fa2a1e5c6d088fec47b593032b254f5e980de8ea54454d6" - -[[package]] -name = "atty" -version = "0.2.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" -dependencies = [ - "hermit-abi 0.1.19", - "libc", - "winapi", -] - -[[package]] -name = "auto_impl" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a8c1df849285fbacd587de7818cc7d13be6cd2cbcd47a04fb1801b0e2706e33" -dependencies = [ - "proc-macro-error", - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "autocfg" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" - -[[package]] -name = "base64" -version = "0.13.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8" - -[[package]] -name = "base64ct" -version = "1.5.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b645a089122eccb6111b4f81cbc1a49f5900ac4666bb93ac027feaecf15607bf" - -[[package]] -name = "binstall-zip" -version = "0.6.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a7d6964f4bcc7bf2b07f47bc20245b08997b5be470e450b5b511d72164151894" -dependencies = [ - "aes", - "byteorder", - "bzip2", - "constant_time_eq", - "crc32fast", - "crossbeam-utils", - "flate2", - "hmac", - "pbkdf2", - "sha1", - "time", - "zstd", -] - -[[package]] -name = "bitflags" -version = "1.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" - -[[package]] -name = "block-buffer" -version = "0.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4152116fd6e9dadb291ae18fc1ec3575ed6d84c29642d97890f4b4a3417297e4" -dependencies = [ - "generic-array", -] - -[[package]] -name = "block-buffer" -version = "0.10.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "69cce20737498f97b993470a6e536b8523f0af7892a4f928cceb1ac5e52ebe7e" -dependencies = [ - "generic-array", -] - -[[package]] -name = "bumpalo" -version = "3.11.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "572f695136211188308f16ad2ca5c851a712c464060ae6974944458eb83880ba" - -[[package]] -name = "byteorder" -version = "1.4.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" - -[[package]] -name = "bytes" -version = "1.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dfb24e866b15a1af2a1b663f10c6b6b8f397a84aadb828f12e5b289ec23a3a3c" - -[[package]] -name = "bzip2" -version = "0.4.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6afcd980b5f3a45017c57e57a2fcccbb351cc43a356ce117ef760ef8052b89b0" -dependencies = [ - "bzip2-sys", - "libc", -] - -[[package]] -name = "bzip2-sys" -version = "0.1.11+1.0.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "736a955f3fa7875102d57c82b8cac37ec45224a07fd32d58f9f7a186b6cd4cdc" -dependencies = [ - "cc", - "libc", - "pkg-config", -] - -[[package]] -name = "cc" -version = "1.0.77" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e9f73505338f7d905b19d18738976aae232eb46b8efc15554ffc56deb5d9ebe4" -dependencies = [ - "jobserver", -] - -[[package]] -name = "cfg-if" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" - -[[package]] -name = "cipher" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ee52072ec15386f770805afd189a01c8841be8696bed250fa2f13c4c0d6dfb7" -dependencies = [ - "generic-array", -] - -[[package]] -name = "clap" -version = "3.2.23" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "71655c45cb9845d3270c9d6df84ebe72b4dad3c2ba3f7023ad47c144e4e473a5" -dependencies = [ - "atty", - "bitflags", - "clap_derive 3.2.18", - "clap_lex 0.2.4", - "indexmap", - "once_cell", - "strsim", - "termcolor", - "textwrap", -] - -[[package]] -name = "clap" -version = "4.0.29" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4d63b9e9c07271b9957ad22c173bae2a4d9a81127680962039296abcd2f8251d" -dependencies = [ - "bitflags", - "clap_derive 4.0.21", - "clap_lex 0.3.0", - "is-terminal", - "once_cell", - "strsim", - "termcolor", -] - -[[package]] -name = "clap_derive" -version = "3.2.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ea0c8bce528c4be4da13ea6fead8965e95b6073585a2f05204bd8f4119f82a65" -dependencies = [ - "heck", - "proc-macro-error", - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "clap_derive" -version = "4.0.21" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0177313f9f02afc995627906bbd8967e2be069f5261954222dac78290c2b9014" -dependencies = [ - "heck", - "proc-macro-error", - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "clap_lex" -version = "0.2.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2850f2f5a82cbf437dd5af4d49848fbdfc27c157c3d010345776f952765261c5" -dependencies = [ - "os_str_bytes", -] - -[[package]] -name = "clap_lex" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0d4198f73e42b4936b35b5bb248d81d2b595ecb170da0bac7655c54eedfa8da8" -dependencies = [ - "os_str_bytes", -] - -[[package]] -name = "colored" -version = "2.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b3616f750b84d8f0de8a58bda93e08e2a81ad3f523089b05f1dffecab48c6cbd" -dependencies = [ - "atty", - "lazy_static", - "winapi", -] - -[[package]] -name = "console" -version = "0.14.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3993e6445baa160675931ec041a5e03ca84b9c6e32a056150d3aa2bdda0a1f45" -dependencies = [ - "encode_unicode", - "lazy_static", - "libc", - "regex", - "terminal_size", - "unicode-width", - "winapi", -] - -[[package]] -name = "console" -version = "0.15.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c050367d967ced717c04b65d8c619d863ef9292ce0c5760028655a2fb298718c" -dependencies = [ - "encode_unicode", - "lazy_static", - "libc", - "terminal_size", - "winapi", -] - -[[package]] -name = "constant_time_eq" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc" - -[[package]] -name = "cpufeatures" -version = "0.2.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "28d997bd5e24a5928dd43e46dc529867e207907fe0b239c3477d924f7f2ca320" -dependencies = [ - "libc", -] - -[[package]] -name = "crc32fast" -version = "1.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b540bd8bc810d3885c6ea91e2018302f68baba2129ab3e88f32389ee9370880d" -dependencies = [ - "cfg-if", -] - -[[package]] -name = "crossbeam-utils" -version = "0.8.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4fb766fa798726286dbbb842f174001dab8abc7b627a1dd86e0b7222a95d929f" -dependencies = [ - "cfg-if", -] - -[[package]] -name = "crypto-common" -version = "0.1.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" -dependencies = [ - "generic-array", - "typenum", -] - -[[package]] -name = "dialoguer" -version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c9dd058f8b65922819fabb4a41e7d1964e56344042c26efbccd465202c23fa0c" -dependencies = [ - "console 0.14.1", - "lazy_static", - "tempfile", - "zeroize", -] - -[[package]] -name = "digest" -version = "0.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d3dd60d1080a57a05ab032377049e0591415d2b31afd7028356dbf3cc6dcb066" -dependencies = [ - "generic-array", -] - -[[package]] -name = "digest" -version = "0.10.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8168378f4e5023e7218c89c891c0fd8ecdb5e5e4f18cb78f38cf245dd021e76f" -dependencies = [ - "block-buffer 0.10.3", - "crypto-common", - "subtle", -] - -[[package]] -name = "either" -version = "1.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797" - -[[package]] -name = "encode_unicode" -version = "0.3.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f" - -[[package]] -name = "encoding_rs" -version = "0.8.31" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9852635589dc9f9ea1b6fe9f05b50ef208c85c834a562f0c6abb1c475736ec2b" -dependencies = [ - "cfg-if", -] - -[[package]] -name = "errno" -version = "0.2.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f639046355ee4f37944e44f60642c6f3a7efa3cf6b78c78a0d989a8ce6c396a1" -dependencies = [ - "errno-dragonfly", - "libc", - "winapi", -] - -[[package]] -name = "errno-dragonfly" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf" -dependencies = [ - "cc", - "libc", -] - -[[package]] -name = "fastrand" -version = "1.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a7a407cfaa3385c4ae6b23e84623d48c2798d06e3e6a1878f7f59f17b3f86499" -dependencies = [ - "instant", -] - -[[package]] -name = "flate2" -version = "1.0.25" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a8a2db397cb1c8772f31494cb8917e48cd1e64f0fa7efac59fbd741a0a8ce841" -dependencies = [ - "crc32fast", - "miniz_oxide", -] - -[[package]] -name = "fnv" -version = "1.0.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" - -[[package]] -name = "form_urlencoded" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a9c384f161156f5260c24a097c56119f9be8c798586aecc13afbcbe7b7e26bf8" -dependencies = [ - "percent-encoding", -] - -[[package]] -name = "fs2" -version = "0.4.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9564fc758e15025b46aa6643b1b77d047d1a56a1aea6e01002ac0c7026876213" -dependencies = [ - "libc", - "winapi", -] - -[[package]] -name = "futures-channel" -version = "0.3.25" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "52ba265a92256105f45b719605a571ffe2d1f0fea3807304b522c1d778f79eed" -dependencies = [ - "futures-core", -] - -[[package]] -name = "futures-core" -version = "0.3.25" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "04909a7a7e4633ae6c4a9ab280aeb86da1236243a77b694a49eacd659a4bd3ac" - -[[package]] -name = "futures-io" -version = "0.3.25" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "00f5fb52a06bdcadeb54e8d3671f8888a39697dcb0b81b23b55174030427f4eb" - -[[package]] -name = "futures-sink" -version = "0.3.25" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "39c15cf1a4aa79df40f1bb462fb39676d0ad9e366c2a33b590d7c66f4f81fcf9" - -[[package]] -name = "futures-task" -version = "0.3.25" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2ffb393ac5d9a6eaa9d3fdf37ae2776656b706e200c8e16b1bdb227f5198e6ea" - -[[package]] -name = "futures-util" -version = "0.3.25" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "197676987abd2f9cadff84926f410af1c183608d36641465df73ae8211dc65d6" -dependencies = [ - "futures-core", - "futures-io", - "futures-task", - "memchr", - "pin-project-lite", - "pin-utils", - "slab", -] - -[[package]] -name = "generic-array" -version = "0.14.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bff49e947297f3312447abdca79f45f4738097cc82b06e72054d2223f601f1b9" -dependencies = [ - "typenum", - "version_check", -] - -[[package]] -name = "glob" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574" - -[[package]] -name = "h2" -version = "0.3.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5f9f29bc9dda355256b2916cf526ab02ce0aeaaaf2bad60d65ef3f12f11dd0f4" -dependencies = [ - "bytes", - "fnv", - "futures-core", - "futures-sink", - "futures-util", - "http", - "indexmap", - "slab", - "tokio", - "tokio-util", - "tracing", -] - -[[package]] -name = "hashbrown" -version = "0.12.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" - -[[package]] -name = "heck" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9" - -[[package]] -name = "hermit-abi" -version = "0.1.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" -dependencies = [ - "libc", -] - -[[package]] -name = "hermit-abi" -version = "0.2.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ee512640fe35acbfb4bb779db6f0d80704c2cacfa2e39b601ef3e3f47d1ae4c7" -dependencies = [ - "libc", -] - -[[package]] -name = "hex" -version = "0.4.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" - -[[package]] -name = "hmac" -version = "0.12.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e" -dependencies = [ - "digest 0.10.6", -] - -[[package]] -name = "home" -version = "0.5.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "747309b4b440c06d57b0b25f2aee03ee9b5e5397d288c60e21fc709bb98a7408" -dependencies = [ - "winapi", -] - -[[package]] -name = "http" -version = "0.2.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "75f43d41e26995c17e71ee126451dd3941010b0514a81a9d11f3b341debc2399" -dependencies = [ - "bytes", - "fnv", - "itoa", -] - -[[package]] -name = "http-body" -version = "0.4.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d5f38f16d184e36f2408a55281cd658ecbd3ca05cce6d6510a176eca393e26d1" -dependencies = [ - "bytes", - "http", - "pin-project-lite", -] - -[[package]] -name = "httparse" -version = "1.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d897f394bad6a705d5f4104762e116a75639e470d80901eed05a860a95cb1904" - -[[package]] -name = "httpdate" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c4a1e36c821dbe04574f602848a19f742f4fb3c98d40449f11bcad18d6b17421" - -[[package]] -name = "hyper" -version = "0.14.23" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "034711faac9d2166cb1baf1a2fb0b60b1f277f8492fd72176c17f3515e1abd3c" -dependencies = [ - "bytes", - "futures-channel", - "futures-core", - "futures-util", - "h2", - "http", - "http-body", - "httparse", - "httpdate", - "itoa", - "pin-project-lite", - "socket2", - "tokio", - "tower-service", - "tracing", - "want", -] - -[[package]] -name = "hyper-rustls" -version = "0.23.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1788965e61b367cd03a62950836d5cd41560c3577d90e40e0819373194d1661c" -dependencies = [ - "http", - "hyper", - "rustls", - "tokio", - "tokio-rustls", -] - -[[package]] -name = "idna" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e14ddfc70884202db2244c223200c204c2bda1bc6e0998d11b5e024d657209e6" -dependencies = [ - "unicode-bidi", - "unicode-normalization", -] - -[[package]] -name = "indexmap" -version = "1.9.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1885e79c1fc4b10f0e172c475f458b7f7b93061064d98c3293e98c5ba0c8b399" -dependencies = [ - "autocfg", - "hashbrown", -] - -[[package]] -name = "indicatif" -version = "0.16.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2d207dc617c7a380ab07ff572a6e52fa202a2a8f355860ac9c38e23f8196be1b" -dependencies = [ - "console 0.15.2", - "lazy_static", - "number_prefix", - "regex", -] - -[[package]] -name = "instant" -version = "0.1.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c" -dependencies = [ - "cfg-if", -] - -[[package]] -name = "io-lifetimes" -version = "1.0.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "46112a93252b123d31a119a8d1a1ac19deac4fac6e0e8b0df58f0d4e5870e63c" -dependencies = [ - "libc", - "windows-sys", -] - -[[package]] -name = "ipnet" -version = "2.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "11b0d96e660696543b251e58030cf9787df56da39dab19ad60eae7353040917e" - -[[package]] -name = "is-terminal" -version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "927609f78c2913a6f6ac3c27a4fe87f43e2a35367c0c4b0f8265e8f49a104330" -dependencies = [ - "hermit-abi 0.2.6", - "io-lifetimes", - "rustix", - "windows-sys", -] - -[[package]] -name = "itertools" -version = "0.10.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473" -dependencies = [ - "either", -] - -[[package]] -name = "itoa" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4217ad341ebadf8d8e724e264f13e593e0648f5b3e94b3896a5df283be015ecc" - -[[package]] -name = "jobserver" -version = "0.1.25" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "068b1ee6743e4d11fb9c6a1e6064b3693a1b600e7f5f5988047d98b3dc9fb90b" -dependencies = [ - "libc", -] - -[[package]] -name = "js-sys" -version = "0.3.60" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49409df3e3bf0856b916e2ceaca09ee28e6871cf7d9ce97a692cacfdb2a25a47" -dependencies = [ - "wasm-bindgen", -] - -[[package]] -name = "lazy_static" -version = "1.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" - -[[package]] -name = "libc" -version = "0.2.138" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "db6d7e329c562c5dfab7a46a2afabc8b987ab9a4834c9d1ca04dc54c1546cef8" - -[[package]] -name = "linux-raw-sys" -version = "0.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f9f08d8963a6c613f4b1a78f4f4a4dbfadf8e6545b2d72861731e4858b8b47f" - -[[package]] -name = "lock_api" -version = "0.4.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "435011366fe56583b16cf956f9df0095b405b82d76425bc8981c0e22e60ec4df" -dependencies = [ - "autocfg", - "scopeguard", -] - -[[package]] -name = "log" -version = "0.4.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" -dependencies = [ - "cfg-if", -] - -[[package]] -name = "memchr" -version = "2.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" - -[[package]] -name = "mime" -version = "0.3.16" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d" - -[[package]] -name = "miniz_oxide" -version = "0.6.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b275950c28b37e794e8c55d88aeb5e139d0ce23fdbbeda68f8d7174abdf9e8fa" -dependencies = [ - "adler", -] - -[[package]] -name = "mio" -version = "0.8.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e5d732bc30207a6423068df043e3d02e0735b155ad7ce1a6f76fe2baa5b158de" -dependencies = [ - "libc", - "log", - "wasi", - "windows-sys", -] - -[[package]] -name = "num_cpus" -version = "1.14.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f6058e64324c71e02bc2b150e4f3bc8286db6c83092132ffa3f6b1eab0f9def5" -dependencies = [ - "hermit-abi 0.1.19", - "libc", -] - -[[package]] -name = "number_prefix" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "830b246a0e5f20af87141b25c173cd1b609bd7779a4617d6ec582abaf90870f3" - -[[package]] -name = "once_cell" -version = "1.16.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "86f0b0d4bf799edbc74508c1e8bf170ff5f41238e5f8225603ca7caaae2b7860" - -[[package]] -name = "opaque-debug" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" - -[[package]] -name = "open-fastrlp" -version = "0.1.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "786393f80485445794f6043fd3138854dd109cc6c4bd1a6383db304c9ce9b9ce" -dependencies = [ - "arrayvec", - "auto_impl", - "bytes", -] - -[[package]] -name = "os_str_bytes" -version = "6.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b7820b9daea5457c9f21c69448905d723fbd21136ccf521748f23fd49e723ee" - -[[package]] -name = "parking_lot" -version = "0.12.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f" -dependencies = [ - "lock_api", - "parking_lot_core", -] - -[[package]] -name = "parking_lot_core" -version = "0.9.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ff9f3fef3968a3ec5945535ed654cb38ff72d7495a25619e2247fb15a2ed9ba" -dependencies = [ - "cfg-if", - "libc", - "redox_syscall", - "smallvec", - "windows-sys", -] - -[[package]] -name = "password-hash" -version = "0.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7676374caaee8a325c9e7a2ae557f216c5563a171d6997b0ef8a65af35147700" -dependencies = [ - "base64ct", - "rand_core", - "subtle", -] - -[[package]] -name = "pbkdf2" -version = "0.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "83a0692ec44e4cf1ef28ca317f14f8f07da2d95ec3fa01f86e4467b725e60917" -dependencies = [ - "digest 0.10.6", - "hmac", - "password-hash", - "sha2 0.10.6", -] - -[[package]] -name = "percent-encoding" -version = "2.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "478c572c3d73181ff3c2539045f6eb99e5491218eae919370993b890cdbdd98e" - -[[package]] -name = "pin-project-lite" -version = "0.2.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e0a7ae3ac2f1173085d398531c705756c94a4c56843785df85a60c1a0afac116" - -[[package]] -name = "pin-utils" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" - -[[package]] -name = "pkg-config" -version = "0.3.26" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ac9a59f73473f1b8d852421e59e64809f025994837ef743615c6d0c5b305160" - -[[package]] -name = "proc-macro-error" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" -dependencies = [ - "proc-macro-error-attr", - "proc-macro2", - "quote", - "syn", - "version_check", -] - -[[package]] -name = "proc-macro-error-attr" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" -dependencies = [ - "proc-macro2", - "quote", - "version_check", -] - -[[package]] -name = "proc-macro2" -version = "1.0.47" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ea3d908b0e36316caf9e9e2c4625cdde190a7e6f440d794667ed17a1855e725" -dependencies = [ - "unicode-ident", -] - -[[package]] -name = "quote" -version = "1.0.21" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179" -dependencies = [ - "proc-macro2", -] - -[[package]] -name = "rand" -version = "0.8.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" -dependencies = [ - "rand_core", -] - -[[package]] -name = "rand_core" -version = "0.6.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" - -[[package]] -name = "redox_syscall" -version = "0.2.16" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a" -dependencies = [ - "bitflags", -] - -[[package]] -name = "regex" -version = "1.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e076559ef8e241f2ae3479e36f97bd5741c0330689e217ad51ce2c76808b868a" -dependencies = [ - "aho-corasick", - "memchr", - "regex-syntax", -] - -[[package]] -name = "regex-syntax" -version = "0.6.28" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848" - -[[package]] -name = "remove_dir_all" -version = "0.5.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7" -dependencies = [ - "winapi", -] - -[[package]] -name = "reqwest" -version = "0.11.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "68cc60575865c7831548863cc02356512e3f1dc2f3f82cb837d7fc4cc8f3c97c" -dependencies = [ - "base64", - "bytes", - "encoding_rs", - "futures-core", - "futures-util", - "h2", - "http", - "http-body", - "hyper", - "hyper-rustls", - "ipnet", - "js-sys", - "log", - "mime", - "once_cell", - "percent-encoding", - "pin-project-lite", - "rustls", - "rustls-pemfile", - "serde", - "serde_json", - "serde_urlencoded", - "tokio", - "tokio-rustls", - "tower-service", - "url", - "wasm-bindgen", - "wasm-bindgen-futures", - "web-sys", - "webpki-roots", - "winreg", -] - -[[package]] -name = "ring" -version = "0.16.20" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3053cf52e236a3ed746dfc745aa9cacf1b791d846bdaf412f60a8d7d6e17c8fc" -dependencies = [ - "cc", - "libc", - "once_cell", - "spin", - "untrusted", - "web-sys", - "winapi", -] - -[[package]] -name = "rustix" -version = "0.36.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a3807b5d10909833d3e9acd1eb5fb988f79376ff10fce42937de71a449c4c588" -dependencies = [ - "bitflags", - "errno", - "io-lifetimes", - "libc", - "linux-raw-sys", - "windows-sys", -] - -[[package]] -name = "rustls" -version = "0.20.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "539a2bfe908f471bfa933876bd1eb6a19cf2176d375f82ef7f99530a40e48c2c" -dependencies = [ - "log", - "ring", - "sct", - "webpki", -] - -[[package]] -name = "rustls-pemfile" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0864aeff53f8c05aa08d86e5ef839d3dfcf07aeba2db32f12db0ef716e87bd55" -dependencies = [ - "base64", -] - -[[package]] -name = "ryu" -version = "1.0.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09" - -[[package]] -name = "scopeguard" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" - -[[package]] -name = "sct" -version = "0.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d53dcdb7c9f8158937a7981b48accfd39a43af418591a5d008c7b22b5e1b7ca4" -dependencies = [ - "ring", - "untrusted", -] - -[[package]] -name = "semver" -version = "1.0.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e25dfac463d778e353db5be2449d1cce89bd6fd23c9f1ea21310ce6e5a1b29c4" -dependencies = [ - "serde", -] - -[[package]] -name = "serde" -version = "1.0.150" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e326c9ec8042f1b5da33252c8a37e9ffbd2c9bef0155215b6e6c80c790e05f91" -dependencies = [ - "serde_derive", -] - -[[package]] -name = "serde_derive" -version = "1.0.150" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "42a3df25b0713732468deadad63ab9da1f1fd75a48a15024b50363f128db627e" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "serde_json" -version = "1.0.89" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "020ff22c755c2ed3f8cf162dbb41a7268d934702f3ed3631656ea597e08fc3db" -dependencies = [ - "itoa", - "ryu", - "serde", -] - -[[package]] -name = "serde_urlencoded" -version = "0.7.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d3491c14715ca2294c4d6a88f15e84739788c1d030eed8c110436aafdaa2f3fd" -dependencies = [ - "form_urlencoded", - "itoa", - "ryu", - "serde", -] - -[[package]] -name = "sha1" -version = "0.10.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f04293dc80c3993519f2d7f6f511707ee7094fe0c6d3406feb330cdb3540eba3" +checksum = "b1f58811cfac344940f1a400b6e6231ce35171f614f26439e80f8c1465c5cc0c" dependencies = [ - "cfg-if", - "cpufeatures", - "digest 0.10.6", + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "utf8parse", ] [[package]] -name = "sha2" -version = "0.9.9" +name = "anstyle" +version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4d58a1e1bf39749807d89cf2d98ac2dfa0ff1cb3faa38fbb64dd88ac8013d800" -dependencies = [ - "block-buffer 0.9.0", - "cfg-if", - "cpufeatures", - "digest 0.9.0", - "opaque-debug", -] +checksum = "b84bf0a05bbb2a83e5eb6fa36bb6e87baa08193c35ff52bbf6b38d8af2890e46" [[package]] -name = "sha2" -version = "0.10.6" +name = "anstyle-parse" +version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "82e6b795fe2e3b1e845bafcb27aa35405c4d47cdfc92af5fc8d3002f76cebdc0" +checksum = "938874ff5980b03a87c5524b3ae5b59cf99b1d6bc836848df7bc5ada9643c333" dependencies = [ - "cfg-if", - "cpufeatures", - "digest 0.10.6", + "utf8parse", ] [[package]] -name = "signal-hook-registry" -version = "1.4.0" +name = "anstyle-query" +version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e51e73328dc4ac0c7ccbda3a494dfa03df1de2f46018127f60c693f2648455b0" +checksum = "5ca11d4be1bab0c8bc8734a9aa7bf4ee8316d462a08c6ac5052f888fef5b494b" dependencies = [ - "libc", + "windows-sys", ] [[package]] -name = "slab" -version = "0.4.7" +name = "anstyle-wincon" +version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4614a76b2a8be0058caa9dbbaf66d988527d86d003c11a94fbd335d7661edcef" +checksum = "58f54d10c6dfa51283a066ceab3ec1ab78d13fae00aa49243a45e4571fb79dfd" dependencies = [ - "autocfg", + "anstyle", + "windows-sys", ] [[package]] -name = "smallvec" -version = "1.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0" - -[[package]] -name = "socket2" -version = "0.4.7" +name = "anyhow" +version = "1.0.75" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "02e2d2db9033d13a1567121ddd7a095ee144db4e1ca1b1bda3419bc0da294ebd" -dependencies = [ - "libc", - "winapi", -] +checksum = "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6" [[package]] -name = "solc-wrapper" +name = "ast-extractor" version = "0.1.0" -dependencies = [ - "anyhow", - "hex", - "home", - "once_cell", - "open-fastrlp", - "regex", - "semver", - "serde", - "serde_json", - "svm-rs", - "thiserror", -] - -[[package]] -name = "solidhunter" -version = "0.0.1" -dependencies = [ - "anyhow", - "clap 4.0.29", - "colored", - "glob", - "serde", - "serde_json", - "solidhunter-lib", - "thiserror", -] - -[[package]] -name = "solidhunter-lib" -version = "0.0.1" -dependencies = [ - "anyhow", - "clap 4.0.29", - "colored", - "glob", - "serde", - "serde_json", - "solc-wrapper", - "thiserror", -] - -[[package]] -name = "spin" -version = "0.5.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d" - -[[package]] -name = "strsim" -version = "0.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" - -[[package]] -name = "subtle" -version = "2.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6bdef32e8150c2a081110b42772ffe7d7c9032b606bc226c8260fd97e0976601" - -[[package]] -name = "svm-rs" -version = "0.2.18" -source = "git+https://github.com/roynalnaruto/svm-rs#ad9434b9fe1aee6e87cbbebb4eb15f6182663f52" -dependencies = [ - "anyhow", - "binstall-zip", - "cfg-if", - "clap 3.2.23", - "console 0.14.1", - "dialoguer", - "fs2", - "hex", - "home", - "indicatif", - "itertools", - "once_cell", - "rand", - "reqwest", - "semver", - "serde", - "serde_json", - "sha2 0.9.9", - "tempfile", - "thiserror", - "tokio", - "tracing", - "url", -] - -[[package]] -name = "syn" -version = "1.0.105" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "60b9b43d45702de4c839cb9b51d9f529c5dd26a4aff255b42b1ebc03e88ee908" dependencies = [ "proc-macro2", "quote", - "unicode-ident", + "syn", + "syn-solidity", + "thiserror", ] [[package]] -name = "tempfile" -version = "3.3.0" +name = "bitflags" +version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5cdb1ef4eaeeaddc8fbd371e5017057064af0911902ef36b39801f67cc6d79e4" -dependencies = [ - "cfg-if", - "fastrand", - "libc", - "redox_syscall", - "remove_dir_all", - "winapi", -] +checksum = "b4682ae6287fcf752ecaabbfcc7b6f9b72aa33933dc23a554d853aea8eea8635" [[package]] -name = "termcolor" -version = "1.1.3" +name = "cc" +version = "1.0.83" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755" +checksum = "f1174fb0b6ec23863f8b971027804a42614e347eafb0a95bf0b12cdae21fc4d0" dependencies = [ - "winapi-util", + "libc", ] [[package]] -name = "terminal_size" -version = "0.1.17" +name = "clap" +version = "4.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "633c1a546cee861a1a6d0dc69ebeca693bf4296661ba7852b9d21d159e0506df" +checksum = "84ed82781cea27b43c9b106a979fe450a13a31aab0500595fb3fc06616de08e6" dependencies = [ - "libc", - "winapi", + "clap_builder", + "clap_derive", ] [[package]] -name = "textwrap" -version = "0.16.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "222a222a5bfe1bba4a77b45ec488a741b3cb8872e5e499451fd7d0129c9c7c3d" - -[[package]] -name = "thiserror" -version = "1.0.37" +name = "clap_builder" +version = "4.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "10deb33631e3c9018b9baf9dcbbc4f737320d2b576bac10f6aefa048fa407e3e" +checksum = "2bb9faaa7c2ef94b2743a21f5a29e6f0010dff4caa69ac8e9d6cf8b6fa74da08" dependencies = [ - "thiserror-impl", + "anstream", + "anstyle", + "clap_lex", + "strsim", ] [[package]] -name = "thiserror-impl" -version = "1.0.37" +name = "clap_derive" +version = "4.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "982d17546b47146b28f7c22e3d08465f6b8903d0ea13c1660d9d84a6e7adcdbb" +checksum = "0862016ff20d69b84ef8247369fabf5c008a7417002411897d40ee1f4532b873" dependencies = [ + "heck", "proc-macro2", "quote", "syn", ] [[package]] -name = "time" -version = "0.3.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a561bf4617eebd33bca6434b988f39ed798e527f51a1e797d0ee4f61c0a38376" -dependencies = [ - "itoa", - "serde", - "time-core", - "time-macros", -] - -[[package]] -name = "time-core" -version = "0.1.0" +name = "clap_lex" +version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2e153e1f1acaef8acc537e68b44906d2db6436e2b35ac2c6b42640fff91f00fd" +checksum = "cd7cc57abe963c6d3b9d8be5b06ba7c8957a930305ca90304f24ef040aa6f961" [[package]] -name = "time-macros" -version = "0.2.6" +name = "colorchoice" +version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d967f99f534ca7e495c575c62638eebc2898a8c84c119b89e250477bc4ba16b2" -dependencies = [ - "time-core", -] +checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" [[package]] -name = "tinyvec" -version = "1.6.0" +name = "colored" +version = "2.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87cc5ceb3875bb20c2890005a4e226a4651264a5c75edb2421b52861a0a0cb50" +checksum = "2674ec482fbc38012cf31e6c42ba0177b431a0cb6f15fe40efa5aab1bda516f6" dependencies = [ - "tinyvec_macros", + "is-terminal", + "lazy_static", + "windows-sys", ] [[package]] -name = "tinyvec_macros" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c" - -[[package]] -name = "tokio" -version = "1.23.0" +name = "errno" +version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eab6d665857cc6ca78d6e80303a02cea7a7851e85dfbd77cbdc09bd129f1ef46" +checksum = "136526188508e25c6fef639d7927dfb3e0e3084488bf202267829cf7fc23dbdd" dependencies = [ - "autocfg", - "bytes", + "errno-dragonfly", "libc", - "memchr", - "mio", - "num_cpus", - "parking_lot", - "pin-project-lite", - "signal-hook-registry", - "socket2", - "tokio-macros", "windows-sys", ] [[package]] -name = "tokio-macros" -version = "1.8.2" +name = "errno-dragonfly" +version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d266c00fde287f55d3f1c3e96c500c362a2b8c695076ec180f27918820bc6df8" +checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf" dependencies = [ - "proc-macro2", - "quote", - "syn", + "cc", + "libc", ] [[package]] -name = "tokio-rustls" -version = "0.23.4" +name = "glob" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c43ee83903113e03984cb9e5cebe6c04a5116269e900e3ddba8f068a62adda59" -dependencies = [ - "rustls", - "tokio", - "webpki", -] +checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" [[package]] -name = "tokio-util" -version = "0.7.4" +name = "heck" +version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0bb2e075f03b3d66d8d8785356224ba688d2906a371015e225beeb65ca92c740" -dependencies = [ - "bytes", - "futures-core", - "futures-sink", - "pin-project-lite", - "tokio", - "tracing", -] +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" [[package]] -name = "tower-service" +name = "hermit-abi" version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6bc1c9ce2b5135ac7f93c72918fc37feb872bdc6a5533a8b85eb4b86bfdae52" - -[[package]] -name = "tracing" -version = "0.1.37" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ce8c33a8d48bd45d624a6e523445fd21ec13d3653cd51f681abf67418f54eb8" -dependencies = [ - "cfg-if", - "pin-project-lite", - "tracing-attributes", - "tracing-core", -] +checksum = "443144c8cdadd93ebf52ddb4056d257f5b52c04d3c804e657d19eb73fc33668b" [[package]] -name = "tracing-attributes" -version = "0.1.23" +name = "is-terminal" +version = "0.4.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4017f8f45139870ca7e672686113917c71c7a6e02d4924eda67186083c03081a" +checksum = "cb0889898416213fab133e1d33a0e5858a48177452750691bde3666d0fdbaf8b" dependencies = [ - "proc-macro2", - "quote", - "syn", + "hermit-abi", + "rustix", + "windows-sys", ] [[package]] -name = "tracing-core" -version = "0.1.30" +name = "itoa" +version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "24eb03ba0eab1fd845050058ce5e616558e8f8d8fca633e6b163fe25c797213a" -dependencies = [ - "once_cell", -] +checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38" [[package]] -name = "try-lock" -version = "0.2.3" +name = "lazy_static" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] -name = "typenum" -version = "1.16.0" +name = "libc" +version = "0.2.148" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "497961ef93d974e23eb6f433eb5fe1b7930b659f06d12dec6fc44a8f554c0bba" +checksum = "9cdc71e17332e86d2e1d38c1f99edcb6288ee11b815fb1a4b049eaa2114d369b" [[package]] -name = "unicode-bidi" -version = "0.3.8" +name = "linux-raw-sys" +version = "0.4.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "099b7128301d285f79ddd55b9a83d5e6b9e97c92e0ea0daebee7263e932de992" +checksum = "1a9bad9f94746442c783ca431b22403b519cd7fbeed0533fdd6328b2f2212128" [[package]] -name = "unicode-ident" -version = "1.0.5" +name = "paste" +version = "1.0.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ceab39d59e4c9499d4e5a8ee0e2735b891bb7308ac83dfb4e80cad195c9f6f3" +checksum = "de3145af08024dea9fa9914f381a17b8fc6034dfb00f3a84013f7ff43f29ed4c" [[package]] -name = "unicode-normalization" -version = "0.1.22" +name = "proc-macro2" +version = "1.0.67" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c5713f0fc4b5db668a2ac63cdb7bb4469d8c9fed047b1d0292cc7b0ce2ba921" +checksum = "3d433d9f1a3e8c1263d9456598b16fec66f4acc9a74dacffd35c7bb09b3a1328" dependencies = [ - "tinyvec", + "unicode-ident", ] [[package]] -name = "unicode-width" -version = "0.1.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b" - -[[package]] -name = "untrusted" -version = "0.7.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a156c684c91ea7d62626509bce3cb4e1d9ed5c4d978f7b4352658f96a4c26b4a" - -[[package]] -name = "url" -version = "2.3.1" +name = "quote" +version = "1.0.33" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0d68c799ae75762b8c3fe375feb6600ef5602c883c5d21eb51c09f22b83c4643" +checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" dependencies = [ - "form_urlencoded", - "idna", - "percent-encoding", + "proc-macro2", ] [[package]] -name = "version_check" -version = "0.9.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" - -[[package]] -name = "want" -version = "0.3.0" +name = "rustix" +version = "0.38.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1ce8a968cb1cd110d136ff8b819a556d6fb6d919363c61534f6860c7eb172ba0" +checksum = "d7db8590df6dfcd144d22afd1b83b36c21a18d7cbc1dc4bb5295a8712e9eb662" dependencies = [ - "log", - "try-lock", + "bitflags", + "errno", + "libc", + "linux-raw-sys", + "windows-sys", ] [[package]] -name = "wasi" -version = "0.11.0+wasi-snapshot-preview1" +name = "ryu" +version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" +checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741" [[package]] -name = "wasm-bindgen" -version = "0.2.83" +name = "serde" +version = "1.0.188" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eaf9f5aceeec8be17c128b2e93e031fb8a4d469bb9c4ae2d7dc1888b26887268" +checksum = "cf9e0fcba69a370eed61bcf2b728575f726b50b55cba78064753d708ddc7549e" dependencies = [ - "cfg-if", - "wasm-bindgen-macro", + "serde_derive", ] [[package]] -name = "wasm-bindgen-backend" -version = "0.2.83" +name = "serde_derive" +version = "1.0.188" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4c8ffb332579b0557b52d268b91feab8df3615f265d5270fec2a8c95b17c1142" +checksum = "4eca7ac642d82aa35b60049a6eccb4be6be75e599bd2e9adb5f875a737654af2" dependencies = [ - "bumpalo", - "log", - "once_cell", "proc-macro2", "quote", "syn", - "wasm-bindgen-shared", ] [[package]] -name = "wasm-bindgen-futures" -version = "0.4.33" +name = "serde_json" +version = "1.0.107" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "23639446165ca5a5de86ae1d8896b737ae80319560fbaa4c2887b7da6e7ebd7d" +checksum = "6b420ce6e3d8bd882e9b243c6eed35dbc9a6110c9769e74b584e0d68d1f20c65" dependencies = [ - "cfg-if", - "js-sys", - "wasm-bindgen", - "web-sys", + "itoa", + "ryu", + "serde", ] [[package]] -name = "wasm-bindgen-macro" -version = "0.2.83" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "052be0f94026e6cbc75cdefc9bae13fd6052cdcaf532fa6c45e7ae33a1e6c810" +name = "solidhunter" +version = "0.0.1" dependencies = [ - "quote", - "wasm-bindgen-macro-support", + "anyhow", + "clap", + "colored", + "glob", + "serde", + "serde_json", + "solidhunter-lib", + "thiserror", ] [[package]] -name = "wasm-bindgen-macro-support" -version = "0.2.83" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07bc0c051dc5f23e307b13285f9d75df86bfdf816c5721e573dec1f9b8aa193c" +name = "solidhunter-lib" +version = "0.0.1" dependencies = [ - "proc-macro2", - "quote", - "syn", - "wasm-bindgen-backend", - "wasm-bindgen-shared", + "anyhow", + "ast-extractor", + "clap", + "colored", + "glob", + "serde", + "serde_json", + "thiserror", ] [[package]] -name = "wasm-bindgen-shared" -version = "0.2.83" +name = "strsim" +version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1c38c045535d93ec4f0b4defec448e4291638ee608530863b1e2ba115d4fff7f" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" [[package]] -name = "web-sys" -version = "0.3.60" +name = "syn" +version = "2.0.33" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bcda906d8be16e728fd5adc5b729afad4e444e106ab28cd1c7256e54fa61510f" +checksum = "9caece70c63bfba29ec2fed841a09851b14a235c60010fa4de58089b6c025668" dependencies = [ - "js-sys", - "wasm-bindgen", + "proc-macro2", + "quote", + "unicode-ident", ] [[package]] -name = "webpki" -version = "0.22.0" +name = "syn-solidity" +version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f095d78192e208183081cc07bc5515ef55216397af48b873e5edcd72637fa1bd" +checksum = "397f229dc34c7b8231b6ef85502f9ca4e3425b8625e6d403bb74779e6b1917b5" dependencies = [ - "ring", - "untrusted", + "paste", + "proc-macro2", + "quote", + "syn", ] [[package]] -name = "webpki-roots" -version = "0.22.6" +name = "thiserror" +version = "1.0.48" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6c71e40d7d2c34a5106301fb632274ca37242cd0c9d3e64dbece371a40a2d87" +checksum = "9d6d7a740b8a666a7e828dd00da9c0dc290dff53154ea77ac109281de90589b7" dependencies = [ - "webpki", + "thiserror-impl", ] [[package]] -name = "winapi" -version = "0.3.9" +name = "thiserror-impl" +version = "1.0.48" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +checksum = "49922ecae66cc8a249b77e68d1d0623c1b2c514f0060c27cdc68bd62a1219d35" dependencies = [ - "winapi-i686-pc-windows-gnu", - "winapi-x86_64-pc-windows-gnu", + "proc-macro2", + "quote", + "syn", ] [[package]] -name = "winapi-i686-pc-windows-gnu" -version = "0.4.0" +name = "unicode-ident" +version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" [[package]] -name = "winapi-util" -version = "0.1.5" +name = "utf8parse" +version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" -dependencies = [ - "winapi", -] +checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" [[package]] -name = "winapi-x86_64-pc-windows-gnu" -version = "0.4.0" +name = "windows-sys" +version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets", +] [[package]] -name = "windows-sys" -version = "0.42.0" +name = "windows-targets" +version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a3e1820f08b8513f676f7ab6c1f99ff312fb97b553d30ff4dd86f9f15728aa7" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" dependencies = [ "windows_aarch64_gnullvm", "windows_aarch64_msvc", @@ -1804,86 +402,42 @@ dependencies = [ [[package]] name = "windows_aarch64_gnullvm" -version = "0.42.0" +version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41d2aa71f6f0cbe00ae5167d90ef3cfe66527d6f613ca78ac8024c3ccab9a19e" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" [[package]] name = "windows_aarch64_msvc" -version = "0.42.0" +version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd0f252f5a35cac83d6311b2e795981f5ee6e67eb1f9a7f64eb4500fbc4dcdb4" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" [[package]] name = "windows_i686_gnu" -version = "0.42.0" +version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fbeae19f6716841636c28d695375df17562ca208b2b7d0dc47635a50ae6c5de7" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" [[package]] name = "windows_i686_msvc" -version = "0.42.0" +version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "84c12f65daa39dd2babe6e442988fc329d6243fdce47d7d2d155b8d874862246" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" [[package]] name = "windows_x86_64_gnu" -version = "0.42.0" +version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bf7b1b21b5362cbc318f686150e5bcea75ecedc74dd157d874d754a2ca44b0ed" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" [[package]] name = "windows_x86_64_gnullvm" -version = "0.42.0" +version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09d525d2ba30eeb3297665bd434a54297e4170c7f1a44cad4ef58095b4cd2028" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" [[package]] name = "windows_x86_64_msvc" -version = "0.42.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f40009d85759725a34da6d89a94e63d7bdc50a862acf0dbc7c8e488f1edcb6f5" - -[[package]] -name = "winreg" -version = "0.10.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "80d0f4e272c85def139476380b12f9ac60926689dd2e01d4923222f40580869d" -dependencies = [ - "winapi", -] - -[[package]] -name = "zeroize" -version = "1.5.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c394b5bd0c6f669e7275d9c20aa90ae064cb22e75a1cad54e1b34088034b149f" - -[[package]] -name = "zstd" -version = "0.11.2+zstd.1.5.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "20cc960326ece64f010d2d2107537f26dc589a6573a316bd5b1dba685fa5fde4" -dependencies = [ - "zstd-safe", -] - -[[package]] -name = "zstd-safe" -version = "5.0.2+zstd.1.5.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d2a5585e04f9eea4b2a3d1eca508c4dee9592a89ef6f450c11719da0726f4db" -dependencies = [ - "libc", - "zstd-sys", -] - -[[package]] -name = "zstd-sys" -version = "2.0.4+zstd.1.5.2" +version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4fa202f2ef00074143e219d15b62ffc317d17cc33909feac471c044087cad7b0" -dependencies = [ - "cc", - "libc", -] +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" diff --git a/toolchains/solidity/linter/core/solc-wrapper/.gitignore b/toolchains/solidity/linter/core/solc-wrapper/.gitignore deleted file mode 100644 index ffa3bbd2..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/.gitignore +++ /dev/null @@ -1 +0,0 @@ -Cargo.lock \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solc-wrapper/Cargo.toml b/toolchains/solidity/linter/core/solc-wrapper/Cargo.toml deleted file mode 100644 index 9735382b..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/Cargo.toml +++ /dev/null @@ -1,19 +0,0 @@ -[package] -name = "solc-wrapper" -version = "0.1.0" -edition = "2021" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -svm-rs = { git = "https://github.com/roynalnaruto/svm-rs", features = ["blocking"] } -home = "0.5.4" -semver = { version = "1.0.14", features = ["serde"] } -regex = "1.7.0" -once_cell = "1.16.0" -thiserror = "1.0" -serde_json = "1.0" -serde = { version = "1.0", features = ["derive"] } -open-fastrlp = "0.1.4" -hex = "0.4.3" -anyhow = "1.0" \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solc-wrapper/src/ast/ast.rs b/toolchains/solidity/linter/core/solc-wrapper/src/ast/ast.rs deleted file mode 100644 index b1ea0e5b..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/src/ast/ast.rs +++ /dev/null @@ -1,2100 +0,0 @@ -use std::collections::HashMap; -use serde::{Serialize, Deserialize}; - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub enum StateMutability { - #[serde(rename = "payable")] - Payable, - - #[serde(rename = "pure")] - Pure, - - #[serde(rename = "nonpayable")] - NonPayable, - - #[serde(rename = "view")] - View, -} - -pub type SourceLocation = String; - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub enum Mutability { - #[serde(rename = "mutable")] - Mutable, - - #[serde(rename = "immutable")] - Immutable, - - #[serde(rename = "constant")] - Constant, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub enum StorageLocation { - #[serde(rename = "default")] - Default, - - #[serde(rename = "storage")] - Storage, - - #[serde(rename = "memory")] - Memory, - - #[serde(rename = "calldata")] - Calldata, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub enum Visibility { - #[serde(rename = "public")] - Public, - - #[serde(rename = "external")] - External, - - #[serde(rename = "internal")] - Internal, - - #[serde(rename = "private")] - Private, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub enum AssignmentOperator { - #[serde(rename = "=")] - Equal, - - #[serde(rename = "+=")] - PlusEqual, - - #[serde(rename = "-=")] - MinusEqual, - - #[serde(rename = "*=")] - StarEqual, - - #[serde(rename = "/=")] - SlashEqual, - - #[serde(rename = "%=")] - PercentEqual, - - #[serde(rename = "|=")] - PipeEqual, - - #[serde(rename = "&=")] - AmpersandEqual, - - #[serde(rename = "^=")] - CaretEqual, - - #[serde(rename = ">>=")] - RightShiftEqual, - - #[serde(rename = "<<=")] - LeftShiftEqual, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub enum BinaryOperator { - #[serde(rename = "+")] - Plus, - - #[serde(rename = "-")] - Minus, - - #[serde(rename = "*")] - Star, - - #[serde(rename = "/")] - Slash, - - #[serde(rename = "%")] - Percent, - - #[serde(rename = "**")] - DoubleStar, - - #[serde(rename = "&&")] - DoubleAmpersand, - - #[serde(rename = "||")] - DoublePipe, - - #[serde(rename = "!=")] - ExclamationEqual, - - #[serde(rename = "==")] - DoubleEqual, - - #[serde(rename = "<")] - LessThan, - - #[serde(rename = "<=")] - LessThanOrEqual, - - #[serde(rename = ">")] - GreaterThan, - - #[serde(rename = ">=")] - GreaterThanOrEqual, - - #[serde(rename = "^")] - Caret, - - #[serde(rename = "&")] - Ampersand, - - #[serde(rename = "|")] - Pipe, - - #[serde(rename = "<<")] - LeftShift, - - #[serde(rename = ">>")] - RightShift, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub enum UnaryOperator { - #[serde(rename = "!")] - Exclamation, - - #[serde(rename = "-")] - Minus, - - #[serde(rename = "+")] - Plus, - - #[serde(rename = "++")] - DoublePlus, - - #[serde(rename = "--")] - DoubleMinus, - - #[serde(rename = "delete")] - Delete, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub enum FunctionCallKind { - #[serde(rename = "functionCall")] - FunctionCall, - - #[serde(rename = "structConstructorCall")] - StructConstructorCall, - - #[serde(rename = "typeConversion")] - TypeConversion, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub enum LiteralKind { - #[serde(rename = "number")] - Number, - - #[serde(rename = "string")] - String, - - #[serde(rename = "hexString")] - HexString, - - #[serde(rename = "unicodeString")] - UnicodeString, - - #[serde(rename = "bool")] - Bool, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub enum FunctionDefinitionKind { - #[serde(rename = "function")] - Function, - - #[serde(rename = "constructor")] - Constructor, - - #[serde(rename = "fallback")] - Fallback, - - #[serde(rename = "receive")] - Receive, - - #[serde(rename = "modifier")] - Modifier, - - #[serde(rename = "event")] - Event, - - #[serde(rename = "freeFunction")] - FreeFunction -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -#[serde(untagged)] -pub enum TypeName { - ArrayTypeName(Box), - ElementaryTypeName(Box), - FunctionTypeName(Box), - Mapping(Box), - UserDefinedTypeName(Box), -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -#[serde(untagged)] -pub enum Expression { - Assignment(Box), - BinaryOperation(Box), - Conditional(Box), - ElementaryTypeNameExpression(Box), - FunctionCall(Box), - FunctionCallOptions(Box), - Identifier(Box), - IdentifierPath(Box), - IndexAccess(Box), - IndexRangeAccess(Box), - Literal(Box), - MemberAccess(Box), - NewExpression(Box), - TupleExpression(Box), - UnaryOperation(Box), -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -#[serde(untagged)] -pub enum Statement { - VariableDeclarationStatement(Box), - ForStatement(Box), - IfStatement(Box), - DoWhileStatement(Box), - Return(Box), - TryStatement(Box), - WhileStatement(Box), - UncheckedBlock(Box), - EmitStatement(Box), - RevertStatement(Box), - ExpressionStatement(Box), - Block(Box), - Continue(Box), - Break(Box), - PlaceholderStatement(Box), -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub enum NodeType { - // Expressions - Assignment, - BinaryOperation, - Conditional, - ElementaryTypeNameExpression, - FunctionCall, - FunctionCallOptions, - Identifier, - IdentifierPath, - IndexAccess, - IndexRangeAccess, - Literal, - MemberAccess, - NewExpression, - TupleExpression, - UnaryOperation, - - // Statements - Block, - Break, - Continue, - DoWhileStatement, - EmitStatement, - ExpressionStatement, - ForStatement, - IfStatement, - InlineAssembly, - PlaceholderStatement, - Return, - RevertStatement, - TryStatement, - UncheckedBlock, - VariableDeclarationStatement, - VariableDeclaration, - WhileStatement, - - // Definitions - ContractDefinition, - FunctionDefinition, - EventDefinition, - ErrorDefinition, - ModifierDefinition, - StructDefinition, - EnumDefinition, - EnumValue, - UserDefinedValueTypeDefinition, - - // Directives - PragmaDirective, - ImportDirective, - UsingForDirective, - - // Misc - SourceUnit, - InheritanceSpecifier, - ElementaryTypeName, - FunctionTypeName, - ParameterList, - TryCatchClause, - ModifierInvocation, - StructuredDocumentation, - UserDefinedTypeName, - OverrideSpecifier, - Mapping, - - /// An unknown AST node type. - Other(String), -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct EnumDefinition { - pub id: usize, - pub src: SourceLocation, - pub name: String, - #[serde(rename = "nameLocation", skip_serializing_if = "Option::is_none")] - pub name_location: Option, - #[serde(rename = "canonicalName")] - pub canonical_name: String, - pub members: Vec, - #[serde(rename = "nodeType")] - pub node_type: NodeType, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct EnumValue { - pub id: usize, - pub src: SourceLocation, - pub name: String, - #[serde(rename = "nameLocation", skip_serializing_if = "Option::is_none")] - pub name_location: Option, - #[serde(rename = "nodeType")] - pub node_type: NodeType, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct TypeDescriptions { - #[serde(rename = "typeIdentifier", skip_serializing_if = "Option::is_none")] - pub type_identifier: Option, - #[serde(rename = "typeString", skip_serializing_if = "Option::is_none")] - pub type_string: Option -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct StructuredDocumentation { - pub id: usize, - pub src: SourceLocation, - pub text: String, - #[serde(rename = "nodeType")] - pub node_type: NodeType, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct StructureFunction { - pub function: IdentifierPath -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -#[serde(untagged)] -pub enum SourceUnitChildNodes { - ContractDefinition(Box), - StructDefinition(Box), - EnumDefinition(Box), - ErrorDefinition(Box), - PragmaDirective(Box), - ImportDirective(Box), - UsingForDirective(Box), - Other(String) -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct SourceUnit { - pub id: usize, - pub src: SourceLocation, - #[serde(rename = "absolutePath")] - pub absolute_path: String, - #[serde(rename = "exportedSymbols")] - pub exported_symbols: Option>>, - #[serde(rename = "license", skip_serializing_if = "Option::is_none")] - pub license: Option, - #[serde(rename = "nodes")] - pub nodes: Vec, - #[serde(rename = "nodeType")] - pub node_type: NodeType, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -#[serde(untagged)] -pub enum ContractDefinitionChildNodes { - FunctionDefinition(Box), - ModifierDefinition(Box), - StructDefinition(Box), - UserDefinedValueTypeDefinition(Box), - VariableDeclaration(Box), - EnumDefinition(Box), - ErrorDefinition(Box), - EventDefinition(Box), - UsingForDirective(Box), -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub enum ContractKind { - #[serde(rename = "contract")] - Contract, - #[serde(rename = "interface")] - Interface, - #[serde(rename = "library")] - Library, - Other(String) -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct ContractDefinition { - pub id: usize, - pub src: SourceLocation, - pub name: String, - #[serde(rename = "nameLocation", skip_serializing_if = "Option::is_none")] - pub name_location: Option, - #[serde(rename = "abstract")] - pub is_abstract: bool, - #[serde(rename = "baseContracts")] - pub base_contracts: Vec, - #[serde(rename = "canonicalName", skip_serializing_if = "Option::is_none")] - pub canonical_name: Option, - #[serde(rename = "contractDependencies")] - pub contract_dependencies: Vec, - #[serde(rename = "contractKind")] - pub contract_kind: ContractKind, - #[serde(rename = "documentation", skip_serializing_if = "Option::is_none")] - pub documentation: Option, - #[serde(rename = "fullyImplemented")] - pub is_fully_implemented: Option, - #[serde(rename = "linearizedBaseContracts")] - pub linearized_base_contracts: Option>, - #[serde(rename = "nodes")] - pub nodes: Vec, - #[serde(rename = "scope")] - pub scope: Option, - #[serde(rename = "usedErrors")] - pub used_errors: Vec, - #[serde(rename = "nodeType")] - pub node_type: NodeType, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -#[serde(untagged)] -pub enum BaseName { - UserDefinedTypeName(UserDefinedTypeName), - IdentifierPath(IdentifierPath), -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct InheritanceSpecifier { - pub id: usize, - pub src: SourceLocation, - #[serde(rename = "arguments")] - pub arguments: Option>, - #[serde(rename = "baseName")] - pub base_name: BaseName, - #[serde(rename = "nodeType")] - pub node_type: NodeType, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct Assignment { - pub id: usize, - pub src: SourceLocation, - #[serde(rename = "argumentTypes", skip_serializing_if = "Option::is_none")] - pub argument_types: Option>, - #[serde(rename = "isConstant")] - pub is_constant: Option, - #[serde(rename = "isLValue")] - pub is_l_value: Option, - #[serde(rename = "isPure")] - pub is_pure: Option, - #[serde(rename = "lValueRequested")] - pub l_value_requested: Option, - #[serde(rename = "typeDescriptions")] - pub type_descriptions: TypeDescriptions, - #[serde(rename = "leftHandSide")] - pub left_hand_side: Expression, - pub operator: AssignmentOperator, - #[serde(rename = "rightHandSide")] - pub right_hand_side: Expression, - #[serde(rename = "nodeType")] - pub node_type: NodeType, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct BinaryOperation { - pub id: usize, - pub src: SourceLocation, - #[serde(rename = "argumentTypes", skip_serializing_if = "Option::is_none")] - pub argument_types: Option>, - #[serde(rename = "isConstant")] - pub is_constant: bool, - #[serde(rename = "isLValue")] - pub is_l_value: bool, - #[serde(rename = "isPure")] - pub is_pure: bool, - #[serde(rename = "lValueRequested")] - pub l_value_requested: bool, - #[serde(rename = "typeDescriptions")] - pub type_descriptions: TypeDescriptions, - #[serde(rename = "commonType")] - pub common_type: TypeDescriptions, - #[serde(rename = "leftExpression")] - pub left_expression: Expression, - pub operator: BinaryOperator, - #[serde(rename = "rightExpression")] - pub right_expression: Expression, - #[serde(rename = "nodeType")] - pub node_type: NodeType, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct Conditional { - pub id: usize, - pub src: SourceLocation, - #[serde(rename = "argumentTypes", skip_serializing_if = "Option::is_none")] - pub argument_types: Option>, - #[serde(rename = "isConstant")] - pub is_constant: bool, - #[serde(rename = "isLValue")] - pub is_l_value: bool, - #[serde(rename = "isPure")] - pub is_pure: bool, - #[serde(rename = "lValueRequested")] - pub l_value_requested: bool, - #[serde(rename = "typeDescriptions")] - pub type_descriptions: TypeDescriptions, - #[serde(rename = "condition")] - pub condition: Expression, - #[serde(rename = "falseExpression")] - pub false_expression: Expression, - #[serde(rename = "trueExpression")] - pub true_expression: Expression, - #[serde(rename = "nodeType")] - pub node_type: NodeType, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct ElementaryTypeNameExpression { - pub id: usize, - pub src: SourceLocation, - #[serde(rename = "argumentTypes", skip_serializing_if = "Option::is_none")] - pub argument_types: Option>, - #[serde(rename = "isConstant")] - pub is_constant: Option, - #[serde(rename = "isLValue")] - pub is_l_value: Option, - #[serde(rename = "isPure")] - pub is_pure: Option, - #[serde(rename = "lValueRequested")] - pub l_value_requested: Option, - #[serde(rename = "typeDescriptions")] - pub type_descriptions: TypeDescriptions, - #[serde(rename = "typeName")] - pub type_name: ElementaryTypeName, - #[serde(rename = "nodeType")] - pub node_type: NodeType, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct ElementaryTypeName { - pub id: usize, - pub src: SourceLocation, - #[serde(rename = "typeDescriptions")] - pub type_descriptions: TypeDescriptions, - pub name: String, - #[serde(rename = "stateMutability")] - pub state_mutability: Option, - #[serde(rename = "nodeType")] - pub node_type: NodeType, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct FunctionCall { - pub id: usize, - pub src: SourceLocation, - #[serde(rename = "argumentTypes", skip_serializing_if = "Option::is_none")] - pub argument_types: Option>, - #[serde(rename = "isConstant")] - pub is_constant: Option, - #[serde(rename = "isLValue")] - pub is_l_value: Option, - #[serde(rename = "isPure")] - pub is_pure: Option, - #[serde(rename = "lValueRequested")] - pub l_value_requested: Option, - #[serde(rename = "typeDescriptions")] - pub type_descriptions: TypeDescriptions, - #[serde(rename = "arguments")] - pub arguments: Vec, - #[serde(rename = "expression")] - pub expression: Expression, - pub kind: Option, - pub names: Vec, - #[serde(rename = "tryCall")] - pub try_call: bool, - #[serde(rename = "nodeType")] - pub node_type: NodeType, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct FunctionCallOptions { - pub id: usize, - pub src: SourceLocation, - #[serde(rename = "argumentTypes", skip_serializing_if = "Option::is_none")] - pub argument_types: Option>, - #[serde(rename = "isConstant")] - pub is_constant: Option, - #[serde(rename = "isLValue")] - pub is_l_value: Option, - #[serde(rename = "isPure")] - pub is_pure: Option, - #[serde(rename = "lValueRequested")] - pub l_value_requested: Option, - #[serde(rename = "typeDescriptions")] - pub type_descriptions: TypeDescriptions, - pub expression: Expression, - pub names: Vec, - pub options: Vec, - #[serde(rename = "nodeType")] - pub node_type: NodeType, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct Identifier { - pub id: usize, - pub src: SourceLocation, - #[serde(rename = "argumentTypes", skip_serializing_if = "Option::is_none")] - pub argument_types: Option>, - pub name: String, - #[serde(rename = "overloadedDeclarations")] - pub overloaded_declarations: Vec, - #[serde(rename = "referencedDeclaration")] - pub referenced_declaration: Option, - #[serde(rename = "typeDescriptions")] - pub type_descriptions: TypeDescriptions, - #[serde(rename = "nodeType")] - pub node_type: NodeType, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct IndexAccess { - pub id: usize, - pub src: SourceLocation, - #[serde(rename = "argumentTypes", skip_serializing_if = "Option::is_none")] - pub argument_types: Option>, - #[serde(rename = "isConstant")] - pub is_constant: Option, - #[serde(rename = "isLValue")] - pub is_l_value: Option, - #[serde(rename = "isPure")] - pub is_pure: Option, - #[serde(rename = "lValueRequested")] - pub l_value_requested: Option, - #[serde(rename = "typeDescriptions")] - pub type_descriptions: TypeDescriptions, - #[serde(rename = "baseExpression")] - pub base_expression: Expression, - #[serde(rename = "indexExpression", skip_serializing_if = "Option::is_none")] - pub index_expression: Option, - #[serde(rename = "nodeType")] - pub node_type: NodeType, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct IndexRangeAccess { - pub id: usize, - pub src: SourceLocation, - #[serde(rename = "argumentTypes", skip_serializing_if = "Option::is_none")] - pub argument_types: Option>, - #[serde(rename = "isConstant")] - pub is_constant: Option, - #[serde(rename = "isLValue")] - pub is_l_value: Option, - #[serde(rename = "isPure")] - pub is_pure: Option, - #[serde(rename = "lValueRequested")] - pub l_value_requested: Option, - #[serde(rename = "typeDescriptions")] - pub type_descriptions: TypeDescriptions, - #[serde(rename = "baseExpression")] - pub base_expression: Expression, - #[serde(rename = "endExpression", skip_serializing_if = "Option::is_none")] - pub end_expression: Option, - #[serde(rename = "startExpression", skip_serializing_if = "Option::is_none")] - pub start_expression: Option, - #[serde(rename = "nodeType")] - pub node_type: NodeType, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct Literal { - pub id: usize, - pub src: SourceLocation, - #[serde(rename = "argumentTypes", skip_serializing_if = "Option::is_none")] - pub argument_types: Option>, - #[serde(rename = "isConstant")] - pub is_constant: Option, - #[serde(rename = "isLValue")] - pub is_l_value: Option, - #[serde(rename = "isPure")] - pub is_pure: Option, - #[serde(rename = "lValueRequested")] - pub l_value_requested: Option, - #[serde(rename = "typeDescriptions")] - pub type_descriptions: TypeDescriptions, - #[serde(rename = "hexValue")] - pub hex_value: String, - pub kind: LiteralKind, - //#[serde(rename = "subdenomination", skip_serializing_if = "Option::is_none")] - //subdenomination: Option, - pub value: Option, - #[serde(rename = "nodeType")] - pub node_type: NodeType, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct MemberAccess { - pub id: usize, - pub src: SourceLocation, - #[serde(rename = "argumentTypes", skip_serializing_if = "Option::is_none")] - pub argument_types: Option>, - #[serde(rename = "isConstant")] - pub is_constant: Option, - #[serde(rename = "isLValue")] - pub is_l_value: Option, - #[serde(rename = "isPure")] - pub is_pure: Option, - #[serde(rename = "lValueRequested")] - pub l_value_requested: Option, - #[serde(rename = "typeDescriptions")] - pub type_descriptions: TypeDescriptions, - #[serde(rename = "expression")] - pub expression: Expression, - #[serde(rename = "memberLocation")] - pub member_location: SourceLocation, - #[serde(rename = "memberName")] - pub member_name: String, - #[serde(rename = "referencedDeclaration")] - pub referenced_declaration: Option, - #[serde(rename = "nodeType")] - pub node_type: NodeType, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct NewExpression { - pub id: usize, - pub src: SourceLocation, - #[serde(rename = "argumentTypes", skip_serializing_if = "Option::is_none")] - pub argument_types: Option>, - #[serde(rename = "isConstant")] - pub is_constant: Option, - #[serde(rename = "isLValue")] - pub is_l_value: Option, - #[serde(rename = "isPure")] - pub is_pure: Option, - #[serde(rename = "lValueRequested")] - pub l_value_requested: Option, - #[serde(rename = "typeDescriptions")] - pub type_descriptions: TypeDescriptions, - #[serde(rename = "typeName")] - pub type_name: TypeName, - #[serde(rename = "nodeType")] - pub node_type: NodeType, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct ArrayTypeName { - pub id: usize, - pub src: SourceLocation, - #[serde(rename = "typeDescriptions")] - pub type_descriptions: TypeDescriptions, - #[serde(rename = "baseType")] - pub base_type: TypeName, - pub length: Option, - #[serde(rename = "nodeType")] - pub node_type: NodeType, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct FunctionTypeName { - id : usize, - pub src: SourceLocation, - #[serde(rename = "typeDescriptions")] - pub type_descriptions: TypeDescriptions, - #[serde(rename = "parameterTypes")] - pub parameter_types: ParameterList, - #[serde(rename = "returnParameterTypes")] - pub return_parameter_types: ParameterList, - #[serde(rename = "stateMutability")] - pub state_mutability: StateMutability, - pub visibility: Visibility, - #[serde(rename = "nodeType")] - pub node_type: NodeType, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct ParameterList { - pub id: usize, - pub src: SourceLocation, - pub parameters: Vec, - #[serde(rename = "nodeType")] - pub node_type: NodeType, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct VariableDeclaration { - pub id: usize, - pub src: SourceLocation, - pub name: String, - #[serde(rename = "nameLocation")] - pub name_location: Option, - #[serde(rename = "baseFunctions", skip_serializing_if = "Option::is_none")] - pub base_functions: Option>, - #[serde(rename = "constant")] - pub is_constant: bool, - pub documentation: Option, - #[serde(rename = "functionSelector")] - pub function_selector: Option, - pub indexed: Option, - pub mutability: Mutability, - pub overrides: Option, - pub scope: Option, - #[serde(rename = "stateVariable")] - pub state_variable: bool, - #[serde(rename = "storageLocation")] - pub storage_location: StorageLocation, - #[serde(rename = "typeDescriptions")] - pub type_descriptions: TypeDescriptions, - #[serde(rename = "typeName", skip_serializing_if = "Option::is_none")] - pub type_name: Option, - #[serde(rename = "value", skip_serializing_if = "Option::is_none")] - pub value: Option, - pub visibility: Visibility, - #[serde(rename = "nodeType")] - pub node_type: NodeType, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -#[serde(untagged)] -pub enum OverridesEnum { - UserDefinedTypeName(Vec), - Identifier(Vec), -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct OverrideSpecifier { - pub id: usize, - pub src: SourceLocation, - pub overrides: OverridesEnum, - #[serde(rename = "nodeType")] - pub node_type: NodeType, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct UserDefinedTypeName { - pub id: usize, - pub src: SourceLocation, - #[serde(rename = "typeDescriptions")] - pub type_descriptions: TypeDescriptions, - //#[serde(rename = "contractScope")] - //contract_scope: Option, - pub name: Option, - #[serde(rename = "pathNode")] - pub path_node: Option, - #[serde(rename = "referencedDeclaration")] - pub referenced_declaration: Option, - #[serde(rename = "nodeType")] - pub node_type: NodeType, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct IdentifierPath { - pub id: usize, - pub src: SourceLocation, - pub name: String, - #[serde(rename = "nameLocations")] - pub name_locations: Option>, - #[serde(rename = "referencedDeclaration")] - pub referenced_declaration: Option, - #[serde(rename = "nodeType")] - pub node_type: NodeType, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct Mapping { - pub id: usize, - pub src: SourceLocation, - #[serde(rename = "typeDescriptions")] - pub type_descriptions: TypeDescriptions, - #[serde(rename = "keyType")] - pub key_type: TypeName, - #[serde(rename = "valueType")] - pub value_type: TypeName, - #[serde(rename = "nodeType")] - pub node_type: NodeType, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct TupleExpression { - pub id: usize, - pub src: SourceLocation, - #[serde(rename = "argumentTypes")] - pub argument_types: Option>, - #[serde(rename = "isConstant")] - pub is_constant: Option, - #[serde(rename = "isLValue")] - pub is_l_value: Option, - #[serde(rename = "isPure")] - pub is_pure: Option, - #[serde(rename = "lValueRequested")] - pub l_value_requested: Option, - #[serde(rename = "typeDescriptions")] - pub type_descriptions: TypeDescriptions, - pub components: Vec, - #[serde(rename = "isInlineArray")] - pub is_inline_array: bool, - #[serde(rename = "nodeType")] - pub node_type: NodeType, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct UnaryOperation { - pub id: usize, - pub src: SourceLocation, - #[serde(rename = "argumentTypes")] - pub argument_types: Option>, - #[serde(rename = "isConstant")] - pub is_constant: Option, - #[serde(rename = "isLValue")] - pub is_l_value: Option, - #[serde(rename = "isPure")] - pub is_pure: Option, - #[serde(rename = "lValueRequested")] - pub l_value_requested: Option, - #[serde(rename = "typeDescriptions")] - pub type_descriptions: TypeDescriptions, - #[serde(rename = "operator")] - pub operator: UnaryOperator, - pub prefix: bool, - #[serde(rename = "subExpression")] - pub sub_expression: Expression, - #[serde(rename = "nodeType")] - pub node_type: NodeType, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct ErrorDefinition { - pub id: usize, - pub src: SourceLocation, - pub name: String, - #[serde(rename = "nameLocation")] - pub name_location: String, - pub documentation: Option, - #[serde(rename = "errorSelector")] - pub error_selector: Option, - pub parameters: Option, - #[serde(rename = "nodeType")] - pub node_type: NodeType, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct EventDefinition { - pub id: usize, - pub src: SourceLocation, - pub name: String, - #[serde(rename = "nameLocation")] - pub name_location: Option, - pub anonymous: bool, - #[serde(rename = "eventSelector")] - pub event_selector: Option, - pub documentation: Option, - pub parameters: Option, - #[serde(rename = "nodeType")] - pub node_type: NodeType, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct FunctionDefinition { - pub id: usize, - pub src: SourceLocation, - pub name: String, - #[serde(rename = "nameLocation")] - pub name_location: Option, - #[serde(rename = "baseFunctions")] - pub base_functions: Option>, - pub body: Option, - #[serde(rename = "documentation")] - pub documentation: Option, - #[serde(rename = "functionSelector")] - pub function_selector: Option, - pub implemented: bool, - pub kind: FunctionDefinitionKind, - pub modifiers: Vec, - pub overrides: Option, - pub parameters: ParameterList, - #[serde(rename = "returnParameters")] - pub return_parameters: ParameterList, - #[serde(rename = "scope")] - pub scope: Option, - #[serde(rename = "stateMutability")] - pub state_mutability: StateMutability, - #[serde(rename = "virtual")] - pub is_virtual: bool, - pub visibility: Option, - #[serde(rename = "nodeType")] - pub node_type: NodeType, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct Block { - pub id: usize, - pub src: SourceLocation, - pub documentation: Option, - pub statements: Option>, - #[serde(rename = "nodeType")] - pub node_type: NodeType, -} - - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct Break { - pub id: usize, - pub src: SourceLocation, - pub documentation: Option, - #[serde(rename = "nodeType")] - pub node_type: NodeType, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct Continue { - pub id: usize, - pub src: SourceLocation, - pub documentation: Option, - #[serde(rename = "nodeType")] - pub node_type: NodeType, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -#[serde(untagged)] -pub enum Body { - Block(Box), - Statement(Box), -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct DoWhileStatement { - pub id: usize, - pub src: SourceLocation, - pub documentation: Option, - #[serde(rename = "condition")] - pub condition: Expression, - #[serde(rename = "body")] - pub body: Body, - #[serde(rename = "nodeType")] - pub node_type: NodeType, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct EmitStatement { - pub id: usize, - pub src: SourceLocation, - pub documentation: Option, - #[serde(rename = "eventCall")] - pub event_call: FunctionCall, - #[serde(rename = "nodeType")] - pub node_type: NodeType, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct ExpressionStatement { - pub id: usize, - pub src: SourceLocation, - pub documentation: Option, - pub expression: Expression, - #[serde(rename = "nodeType")] - pub node_type: NodeType, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -#[serde(untagged)] -pub enum InitializationExpression { - ExpressionStatement(ExpressionStatement), - VariableDeclarationStatement(VariableDeclarationStatement) -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct ForStatement { - pub id: usize, - pub src: SourceLocation, - pub documentation: Option, - pub body: Body, - pub condition: Option, - #[serde(rename = "initializationExpression")] - pub initialization_expression: Option, - #[serde(rename = "loopExpression")] - pub loop_expression: Option, - #[serde(rename = "nodeType")] - pub node_type: NodeType, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct VariableDeclarationStatement { - pub id: usize, - pub src: SourceLocation, - pub documentation: Option, - pub assignments: Vec>, - #[serde(rename = "declarations")] - pub declarations: Vec>, - #[serde(rename = "initialValue")] - pub initial_value: Option, - #[serde(rename = "nodeType")] - pub node_type: NodeType, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct IfStatement { - pub id: usize, - pub src: SourceLocation, - pub documentation: Option, - #[serde(rename = "condition")] - pub condition: Expression, - #[serde(rename = "trueBody")] - pub true_body: Body, - #[serde(rename = "falseBody")] - pub false_body: Option, - #[serde(rename = "nodeType")] - pub node_type: NodeType, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct PlaceholderStatement { - pub id: usize, - pub src: SourceLocation, - pub documentation: Option, - #[serde(rename = "nodeType")] - pub node_type: NodeType, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct Return { - pub id: usize, - pub src: SourceLocation, - pub documentation: Option, - pub expression: Option, - #[serde(rename = "functionReturnParameters")] - pub function_return_parameters: Option, - #[serde(rename = "nodeType")] - pub node_type: NodeType, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct RevertStatement { - pub id: usize, - pub src: SourceLocation, - pub documentation: Option, - #[serde(rename = "errorCall")] - pub error_call: Statement, - #[serde(rename = "nodeType")] - pub node_type: NodeType, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct TryStatement { - pub id: usize, - pub src: SourceLocation, - pub documentation: Option, - pub clauses: Vec, - #[serde(rename = "externalCall")] - pub external_call: Expression, - #[serde(rename = "nodeType")] - pub node_type: NodeType, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct TryCatchClause { - pub id: usize, - pub src: SourceLocation, - pub block: Block, - #[serde(rename = "errorName")] - pub error_name: String, - pub parameters: Option, - #[serde(rename = "nodeType")] - pub node_type: NodeType, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct UncheckedBlock { - pub id: usize, - pub src: SourceLocation, - pub documentation: Option, - pub statements: Vec, - #[serde(rename = "nodeType")] - pub node_type: NodeType, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct WhileStatement { - pub id: usize, - pub src: SourceLocation, - pub documentation: Option, - pub body: Statement, - pub condition: Expression, - #[serde(rename = "nodeType")] - pub node_type: NodeType, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -#[serde(untagged)] -pub enum ModifierName { - Identifier(Identifier), - IdentifierPath(IdentifierPath), -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub enum ModifierInvocationKind { - #[serde(rename = "modifierInvocation")] - ModifierInvocation, - #[serde(rename = "baseConstructorSpecifier")] - BaseConstructorSpecifier, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct ModifierInvocation { - pub id: usize, - pub src: SourceLocation, - pub arguments: Option, - pub kind: Option, - #[serde(rename = "modifierName")] - pub modifier_name: ModifierName, - #[serde(rename = "nodeType")] - pub node_type: NodeType, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct ModifierDefinition { - pub id: usize, - pub src: SourceLocation, - pub name: String, - #[serde(rename = "nameLocation")] - pub name_location: Option, - #[serde(rename = "baseModifiers")] - pub base_modifiers: Option>, - pub body: Statement, - pub documentation: Option, - pub overrides: Option, - pub parameters: ParameterList, - #[serde(rename = "virtual")] - pub is_virtual: bool, - pub visibility: Visibility, - #[serde(rename = "nodeType")] - pub node_type: NodeType, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct StructDefinition { - pub id: usize, - pub src: SourceLocation, - pub name: String, - #[serde(rename = "nameLocation")] - pub name_location: Option, - #[serde(rename = "canonicalName")] - pub canonical_name: String, - pub members: Vec, - pub scope: usize, - pub visibility: Visibility, - #[serde(rename = "nodeType")] - pub node_type: NodeType, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct UserDefinedValueTypeDefinition { - pub id: usize, - pub src: SourceLocation, - pub name: String, - #[serde(rename = "nameLocation")] - pub name_location: Option, - #[serde(rename = "canonicalName")] - pub canonical_name: Option, - #[serde(rename = "underlyingType")] - pub underlying_type: TypeName, - #[serde(rename = "nodeType")] - pub node_type: NodeType, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct UsingForDirective { - pub id: usize, - pub src: SourceLocation, - #[serde(rename = "functionList")] - pub function_list: Option>, - pub function: Option, - pub global: Option, - #[serde(rename = "libraryName")] - pub library_name: Option, - #[serde(rename = "typeName")] - pub type_name: Option, - #[serde(rename = "nodeType")] - pub node_type: NodeType, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct SymbolAlias { - pub foreign: Identifier, - pub local: Option, - pub name_location: Option, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct ImportDirective { - pub id: usize, - pub src: SourceLocation, - #[serde(rename = "absolutePath")] - pub absolute_path: String, - pub file: String, - #[serde(rename = "nameLocation")] - pub name_location: Option, - pub scope: Option, - #[serde(rename = "sourceUnit")] - pub source_unit: Option, - #[serde(rename = "symbolAliases")] - pub symbol_aliases: Vec, - #[serde(rename = "unitAlias")] - pub unit_alias: String, - #[serde(rename = "nodeType")] - pub node_type: NodeType, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct PragmaDirective { - pub id: usize, - pub src: SourceLocation, - pub literals: Vec, - #[serde(rename = "nodeType")] - pub node_type: NodeType, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub enum EvmVersion { - #[serde(rename = "homestead")] - Homestead, - #[serde(rename = "tangerineWhistle")] - TangerineWhistle, - #[serde(rename = "spuriousDragon")] - SpuriousDragon, - #[serde(rename = "byzantium")] - Byzantium, - #[serde(rename = "constantinople")] - Constantinople, - #[serde(rename = "petersburg")] - Petersburg, - #[serde(rename = "istanbul")] - Istanbul, - #[serde(rename = "berlin")] - Berlin, - #[serde(rename = "london")] - London, -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub enum Suffix { - #[serde(rename = "slot")] - Slot, - #[serde(rename = "offset")] - Offset -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct ExternalReference { - pub declaration: usize, - #[serde(rename = "isOffset")] - pub is_offset: bool, - #[serde(rename = "isSlot")] - pub is_slot: bool, - pub src: SourceLocation, - #[serde(rename = "valueSize")] - pub value_size: usize, - pub suffix: Option -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct InlineAssembly { - pub id: usize, - pub src: SourceLocation, - pub documentation: Option, - #[serde(rename = "evmVersion")] - pub evm_version: EvmVersion, - #[serde(rename = "externalReferences")] - pub external_references: Vec, - pub flags: Option>, - #[serde(rename = "nodeType")] - pub node_type: NodeType -} - -#[derive(Debug, Clone, PartialEq, Eq)] -pub struct CodeLocation { - pub line: usize, - pub column: usize, - pub length: usize, -} - -pub fn get_line_from_offset(content: &str, offset: usize) -> (usize, usize) { - let mut nb_line = 1; - let mut tmp = offset; - - for line in content.split('\n') { - if line.len() < tmp { - tmp -= line.len() + 1; - nb_line += 1; - continue; - } - return (nb_line, tmp); - } - return (0, 0); -} - -pub fn decode_begin_location(src: &str, content: &str) -> CodeLocation { - let mut split = src.split(':'); - let offset = split.next().unwrap().parse().unwrap(); - let (line, column) = get_line_from_offset(&content, offset); - let length = split.next().unwrap().parse().unwrap(); - CodeLocation { line, column, length } -} - -pub fn decode_end_location(src: &str, content: &str) -> CodeLocation { - let mut split = src.split(':'); - let offset = split.next().unwrap().parse().unwrap(); - let (line, _column) = get_line_from_offset(&content, offset); - let length = split.next().unwrap().parse().unwrap(); - let extract = content[offset..offset + length].to_string(); - let (diff_line, new_column) = get_line_from_offset(&extract, length); - CodeLocation { - line: line + diff_line, - column: new_column, - length - } -} - -pub fn offset_from_location(content: &str, location: &CodeLocation) -> usize { - let mut offset = 0; - - for (i, line) in content.split('\n').enumerate() { - if i == location.line - 1 { - return offset + location.column; - } - offset += line.len() + 1; - } - offset -} - -pub fn decode_location(src: &str, content: &str) -> (CodeLocation, CodeLocation) { - (decode_begin_location(src, content), decode_end_location(src, content)) -} - -#[cfg(test)] -mod tests { - use std::fs; - use super::*; - - #[test] - fn test_correct_EnumValue_parsing() -> Result<(), String> { - let ast = fs::read_to_string("../solc-wrapper/tests/files/ast/EnumValue.json").expect("Could not find test data file"); - let res = serde_json::from_str::(&ast).map_err(|_| "Error deserializing EnumValue".to_string())?; - assert_eq!(res.name, "item1"); - assert_eq!(res.name_location, Some("72:5:0".to_string())); - Ok(assert_eq!(res.node_type, NodeType::EnumValue)) - } - - #[test] - fn test_correct_enum_definition_parsing() -> Result<(), String> { - let ast = fs::read_to_string("../solc-wrapper/tests/files/ast/EnumDefinition.json").expect("Could not find test data file"); - let res = serde_json::from_str::(&ast).map_err(|_| "Error deserializing EnumDefinition".to_string())?; - assert_eq!(res.canonical_name, "Test"); - assert_eq!(res.name_location, Some("66:4:0".to_string())); - Ok(assert_eq!(res.node_type, NodeType::EnumDefinition)) - } - - #[test] - fn test_correct_event_definition_parsing() -> Result<(), String> { - let ast = fs::read_to_string("../solc-wrapper/tests/files/ast/EventDefinition.json").expect("Could not find test data file"); - let res = serde_json::from_str::(&ast).map_err(|_| "Error deserializing EventDefinition".to_string())?; - assert_eq!(res.anonymous, false); - assert_eq!(res.id, 67); - assert_eq!(res.name, "MintingLocked".to_string()); - assert_eq!(res.src, "1582:45:0".to_string()); - assert_eq!(res.name_location, Some("1588:13:0".to_string())); - Ok(assert_eq!(res.node_type, NodeType::EventDefinition)) - } - - #[test] - fn test_correct_return_parsing() -> Result<(), String> { - let ast = fs::read_to_string("../solc-wrapper/tests/files/ast/Return.json").expect("Could not find test data file"); - let res = serde_json::from_str::(&ast).map_err(|_| "Error deserializing Return".to_string())?; - assert_eq!(res.id, 296); - assert_eq!(res.src, "5761:19:0".to_string()); - Ok(assert_eq!(res.node_type, NodeType::Return)) - } - - #[test] - fn test_correct_emit_statement_parsing() -> Result<(), String> { - let ast = fs::read_to_string("../solc-wrapper/tests/files/ast/EmitStatement.json").expect("Could not find test data file"); - let res = serde_json::from_str::(&ast).map_err(|_| "Error deserializing EmitStatement".to_string())?; - assert_eq!(res.id, 287); - assert_eq!(res.src, "5537:33:0".to_string()); - Ok(assert_eq!(res.node_type, NodeType::EmitStatement)) - } - - #[test] - fn test_correct_modifier_invocation_parsing() -> Result<(), String> { - let ast = fs::read_to_string("../solc-wrapper/tests/files/ast/ModifierInvocation.json").expect("Could not find test data file"); - let res = serde_json::from_str::(&ast).map_err(|_| "Error deserializing ModifierInvocation".to_string())?; - - assert_eq!(res.id, 274); - assert_eq!(res.src, "5445:13:0".to_string()); - Ok(assert_eq!(res.node_type, NodeType::ModifierInvocation)) - } - - #[test] - fn test_correct_pragma_directive_parsing() -> Result<(), String> { - let ast = fs::read_to_string("../solc-wrapper/tests/files/ast/PragmaDirective.json").expect("Could not find test data file"); - let res = serde_json::from_str::(&ast).map_err(|_| "Error deserializing PragmaDirective".to_string())?; - assert_eq!(res.id, 1); - assert_eq!(res.src, "33:23:0".to_string()); - assert_eq!(res.literals, vec!["solidity".to_string(), - "0.8".to_string(), - ".16".to_string()]); - Ok(assert_eq!(res.node_type, NodeType::PragmaDirective)) - } - - #[test] - fn test_correct_tuple_expression_parsing() -> Result<(), String> { - let ast = fs::read_to_string("../solc-wrapper/tests/files/ast/TupleExpression.json").expect("Could not find test data file"); - let res = serde_json::from_str::(&ast).map_err(|_| "Error deserializing TupleExpression".to_string())?; - assert_eq!(res.id, 16); - assert_eq!(res.src, "150:12:0".to_string()); - assert_eq!(res.is_inline_array, false); - Ok(assert_eq!(res.node_type, NodeType::TupleExpression)) - } - - #[test] - fn test_correct_using_for_directive_parsing() -> Result<(), String> { - let ast = fs::read_to_string("../solc-wrapper/tests/files/ast/UsingForDirective.json").expect("Could not find test data file"); - let res = serde_json::from_str::(&ast).map_err(|_| "Error deserializing UsingForDirective".to_string())?; - assert_eq!(res.id, 31); - assert_eq!(res.global, Some(false)); - assert_eq!(res.src, "1007:36:0".to_string()); - Ok(assert_eq!(res.node_type, NodeType::UsingForDirective)) - } - - #[test] - fn test_correct_modifier_definition_parsing() -> Result<(), String> { - let ast = fs::read_to_string("../solc-wrapper/tests/files/ast/ModifierDefinition.json").expect("Could not find test data file"); - let res = serde_json::from_str::(&ast).map_err(|_| "Error deserializing ModifierDefinition".to_string())?; - assert_eq!(res.id, 92); - assert_eq!(res.name, "metadataNotLocked".to_string()); - assert_eq!(res.name_location, Some("1995:17:0".to_string())); - assert_eq!(res.src, "1986:104:0".to_string()); - assert_eq!(res.is_virtual, false); - assert_eq!(res.visibility, Visibility::Internal); - Ok(assert_eq!(res.node_type, NodeType::ModifierDefinition)) - } - - #[test] - fn test_correct_parameter_list_parsing() -> Result<(), String> { - let ast = fs::read_to_string("../solc-wrapper/tests/files/ast/ParameterList.json").expect("Could not find test data file"); - let res = serde_json::from_str::(&ast).map_err(|_| "Error deserializing ParameterList".to_string())?; - assert_eq!(res.id, 197); - assert_eq!(res.src, "3904:30:0".to_string()); - Ok(assert_eq!(res.node_type, NodeType::ParameterList)) - } - - #[test] - fn test_correct_assignment_list_parsing() -> Result<(), String> { - let ast = fs::read_to_string("../solc-wrapper/tests/files/ast/Assignment.json").expect("Could not find test data file"); - let res = serde_json::from_str::(&ast).map_err(|_| "Error deserializing Assignment".to_string())?; - - assert_eq!(res.id, 141); - assert_eq!(res.src, "2849:35:0".to_string()); - assert_eq!(res.operator, AssignmentOperator::Equal); - Ok(assert_eq!(res.node_type, NodeType::Assignment)) - } - - #[test] - fn test_correct_inheritance_specifier_parsing() -> Result<(), String> { - let ast = fs::read_to_string("../solc-wrapper/tests/files/ast/InheritanceSpecifier.json").expect("Could not find test data file"); - let res = serde_json::from_str::(&ast).map_err(|_| "Error deserializing InheritanceSpecifier".to_string())?; - - assert_eq!(res.id, 13); - assert_eq!(res.src, "828:16:0".to_string()); - Ok(assert_eq!(res.node_type, NodeType::InheritanceSpecifier)) - } - - #[test] - fn test_correct_variable_declaration_parsing() -> Result<(), String> { - let ast = fs::read_to_string("../solc-wrapper/tests/files/ast/VariableDeclaration.json").expect("Could not find test data file"); - let res = serde_json::from_str::(&ast).map_err(|_| "Error deserializing VariableDeclaration".to_string())?; - assert_eq!(res.is_constant, false); - assert_eq!(res.id, 293); - assert_eq!(res.mutability, Mutability::Mutable); - assert_eq!(res.src, "5736:13:0".to_string()); - assert_eq!(res.name_location, Some("-1:-1:-1".to_string())); - assert_eq!(res.state_variable, false); - assert_eq!(res.storage_location, StorageLocation::Memory); - assert_eq!(res.visibility, Visibility::Internal); - Ok(assert_eq!(res.node_type, NodeType::VariableDeclaration)) - } - - #[test] - fn test_correct_if_statement_parsing() -> Result<(), String> { - let ast = fs::read_to_string("../solc-wrapper/tests/files/ast/IfStatement.json").expect("Could not find test data file"); - let res = serde_json::from_str::(&ast).map_err(|_| "Error deserializing IfStatement".to_string())?; - assert_eq!(res.id, 19); - assert_eq!(res.src, "145:103:0".to_string()); - Ok(assert_eq!(res.node_type, NodeType::IfStatement)) - } - - #[test] - fn test_correct_binary_operation_parsing() -> Result<(), String> { - let ast = fs::read_to_string("../solc-wrapper/tests/files/ast/BinaryOperation.json").expect("Could not find test data file"); - let res = serde_json::from_str::(&ast).map_err(|_| "Error deserializing TypeDescriptions".to_string())?; - - assert_eq!(res.id, 18); - assert_eq!(res.src, "203:5:0".to_string()); - assert_eq!(res.argument_types, None); - assert_eq!(res.is_constant, false); - assert_eq!(res.is_l_value, false); - assert_eq!(res.is_pure, false); - assert_eq!(res.l_value_requested, false); - assert_eq!(res.operator, BinaryOperator::Ampersand); - Ok(assert_eq!(res.node_type, NodeType::BinaryOperation)) - } - - #[test] - fn test_correct_unary_operation_parsing() -> Result<(), String> { - let ast = fs::read_to_string("../solc-wrapper/tests/files/ast/UnaryOperation.json").expect("Could not find test data file"); - let res = serde_json::from_str::(&ast).map_err(|_| "Error deserializing UnaryOperation".to_string())?; - - assert_eq!(res.id, 7); - assert_eq!(res.src, "104:6:0".to_string()); - assert_eq!(res.operator, UnaryOperator::DoublePlus); - assert_eq!(res.prefix, false); - Ok(assert_eq!(res.node_type, NodeType::UnaryOperation)) - } - - #[test] - fn test_correct_unchecked_block_parsing() -> Result<(), String> { - let ast = fs::read_to_string("../solc-wrapper/tests/files/ast/UncheckedBlock.json").expect("Could not find test data file"); - let res = serde_json::from_str::(&ast).map_err(|_| "Error deserializing UncheckedBlock".to_string())?; - - assert_eq!(res.id, 9); - assert_eq!(res.src, "104:21:0".to_string()); - Ok(assert_eq!(res.node_type, NodeType::UncheckedBlock)) - } - - #[test] - fn test_correct_identifier_parsing() -> Result<(), String> { - let ast = fs::read_to_string("../solc-wrapper/tests/files/ast/Identifier.json").expect("Could not find test data file"); - let res = serde_json::from_str::(&ast).map_err(|_| "Error deserializing TypeDescriptions".to_string())?; - - assert_eq!(res.id, 16); - assert_eq!(res.src, "203:1:0".to_string()); - assert_eq!(res.name, "a".to_string()); - assert_eq!(res.overloaded_declarations, vec![] as Vec); - assert_eq!(res.referenced_declaration, Some(7)); - Ok(assert_eq!(res.node_type, NodeType::Identifier)) - } - - #[test] - fn test_correct_conditional_parsing() -> Result<(), String> { - let ast = fs::read_to_string("../solc-wrapper/tests/files/ast/Conditional.json").expect("Could not find test data file"); - let res = serde_json::from_str::(&ast).map_err(|_| "Error deserializing Conditional".to_string())?; - - assert_eq!(res.id, 10); - assert_eq!(res.src, "158:20:0".to_string()); - assert_eq!(res.is_constant, false); - assert_eq!(res.is_l_value, false); - assert_eq!(res.is_pure, true); - assert_eq!(res.l_value_requested, false); - assert_eq!(res.node_type, NodeType::Conditional); - Ok(()) - } - - #[test] - fn test_correct_type_descriptions_parsing() -> Result<(), String> { - let ast = fs::read_to_string("../solc-wrapper/tests/files/ast/TypeDescriptions.json").expect("Could not find test data file"); - let res = serde_json::from_str::(&ast).map_err(|_| "Error deserializing Conditional".to_string())?; - - assert_eq!(res.type_identifier, Some("t_bool".to_string())); - assert_eq!(res.type_string, Some("bool".to_string())); - Ok(()) - } - - #[test] - fn test_correct_elementary_type_name_parsing() -> Result<(), String> { - let ast = fs::read_to_string("../solc-wrapper/tests/files/ast/ElementaryTypeName.json").expect("Could not find test data file"); - let res = serde_json::from_str::(&ast).map_err(|_| "Error deserializing ElementaryTypeName".to_string())?; - - assert_eq!(res.id, 64); - assert_eq!(res.src, "1602:7:0".to_string()); - assert_eq!(res.name, "address".to_string()); - assert_eq!(res.state_mutability, Some(StateMutability::NonPayable)); - assert_eq!(res.node_type, NodeType::ElementaryTypeName); - Ok(()) - } - - #[test] - fn test_correct_function_call_parsing() -> Result<(), String> { - let ast = fs::read_to_string("../solc-wrapper/tests/files/ast/FunctionCall.json").expect("Could not find test data file"); - let res = serde_json::from_str::(&ast).map_err(|_| "Error deserializing FunctionCall".to_string())?; - - assert_eq!(res.id, 78); - assert_eq!(res.src, "1850:44:0".to_string()); - assert_eq!(res.try_call, false); - assert_eq!(res.node_type, NodeType::FunctionCall); - Ok(()) - } - - #[test] - fn test_correct_literal_parsing() -> Result<(), String> { - let ast = fs::read_to_string("../solc-wrapper/tests/files/ast/Literal.json").expect("Could not find test data file"); - let res = serde_json::from_str::(&ast).map_err(|_| "Error deserializing Literal".to_string())?; - - assert_eq!(res.id, 77); - assert_eq!(res.src, "1874:19:0".to_string()); - assert_eq!(res.value, Some("Minting is locked".to_string())); - assert_eq!(res.hex_value, "4d696e74696e67206973206c6f636b6564".to_string()); - assert_eq!(res.kind, LiteralKind::String); - assert_eq!(res.node_type, NodeType::Literal); - Ok(()) - } - - #[test] - fn test_correct_member_access_parsing() -> Result<(), String> { - let ast = fs::read_to_string("../solc-wrapper/tests/files/ast/MemberAccess.json").expect("Could not find test data file"); - let res = serde_json::from_str::(&ast).map_err(|_| "Error deserializing Literal".to_string())?; - - assert_eq!(res.id, 176); - assert_eq!(res.src, "3535:23:0".to_string()); - assert_eq!(res.member_name, "current".to_string()); - assert_eq!(res.member_location, "3551:7:0".to_string()); - assert_eq!(res.node_type, NodeType::MemberAccess); - Ok(()) - } - - #[test] - fn test_correct_block_parsing() -> Result<(), String> { - let ast = fs::read_to_string("../solc-wrapper/tests/files/ast/Block.json").expect("Could not find test data file"); - let res = serde_json::from_str::(&ast).map_err(|_| "Error deserializing Block".to_string())?; - - assert_eq!(res.id, 192); - assert_eq!(res.src, "3511:148:0".to_string()); - assert_eq!(res.statements, Some(vec![] as Vec)); - assert_eq!(res.node_type, NodeType::Block); - Ok(()) - } - - #[test] - fn test_correct_expression_statement_parsing() -> Result<(), String> { - let ast = fs::read_to_string("../solc-wrapper/tests/files/ast/ExpressionStatement.json").expect("Could not find test data file"); - let res = serde_json::from_str::(&ast).map_err(|_| "Error deserializing Expression Statement".to_string())?; - - assert_eq!(res.id, 154); - assert_eq!(res.src, "2968:35:0".to_string()); - assert_eq!(res.node_type, NodeType::ExpressionStatement); - Ok(()) - } - - #[test] - fn test_correct_placeholder_statement_parsing() -> Result<(), String> { - let ast = fs::read_to_string("../solc-wrapper/tests/files/ast/PlaceholderStatement.json").expect("Could not find test data file"); - let res = serde_json::from_str::(&ast).map_err(|_| "Error deserializing PlaceholderStatement".to_string())?; - - assert_eq!(res.id, 80); - assert_eq!(res.src, "1904:1:0".to_string()); - assert_eq!(res.node_type, NodeType::PlaceholderStatement); - Ok(()) - } - - #[test] - fn test_correct_contract_definition_parsing() -> Result<(), String> { - let ast = fs::read_to_string("../solc-wrapper/tests/files/ast/ContractDefinition.json").expect("Could not find test data file"); - let res = serde_json::from_str::(&ast).map_err(|_| "Error deserializing ContractDefinition".to_string())?; - - assert_eq!(res.id, 427); - assert_eq!(res.src, "783:7774:0".to_string()); - assert_eq!(res.name_location, Some("792:28:0".to_string())); - assert_eq!(res.name, "StartonERC721MetaTransaction".to_string()); - assert_eq!(res.contract_kind, ContractKind::Contract); - assert_eq!(res.is_abstract, false); - assert_eq!(res.node_type, NodeType::ContractDefinition); - Ok(()) - } - - #[test] - fn test_correct_function_definition_parsing() -> Result<(), String> { - let ast = fs::read_to_string("../solc-wrapper/tests/files/ast/FunctionDefinition.json").expect("Could not find test data file"); - let res = serde_json::from_str::(&ast).map_err(|_| "Error deserializing FunctionDefinition".to_string())?; - - assert_eq!(res.id, 193); - assert_eq!(res.src, "3392:267:0".to_string()); - assert_eq!(res.name, "mint".to_string()); - assert_eq!(res.name_location, Some("3401:4:0".to_string())); - assert_eq!(res.implemented, true); - assert_eq!(res.kind, FunctionDefinitionKind::Function); - assert_eq!(res.visibility, Some(Visibility::Public)); - assert_eq!(res.is_virtual, false); - assert_eq!(res.state_mutability, StateMutability::NonPayable); - assert_eq!(res.node_type, NodeType::FunctionDefinition); - Ok(()) - } - - #[test] - fn test_correct_import_directive_parsing() -> Result<(), String> { - let ast = fs::read_to_string("../solc-wrapper/tests/files/ast/ImportDirective.json").expect("Could not find test data file"); - let res = serde_json::from_str::(&ast).map_err(|_| "Error deserializing ImportDirective".to_string())?; - - assert_eq!(res.id, 2); - assert_eq!(res.src, "58:78:0".to_string()); - assert_eq!(res.name_location, Some("-1:-1:-1".to_string())); - assert_eq!(res.file, "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol".to_string()); - assert_eq!(res.absolute_path, "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol".to_string()); - assert_eq!(res.unit_alias, "".to_string()); - assert_eq!(res.symbol_aliases, vec![] as Vec); - assert_eq!(res.node_type, NodeType::ImportDirective); - Ok(()) - } - - #[test] - fn test_correct_source_unit_parsing() -> Result<(), String> { - let ast = fs::read_to_string("../solc-wrapper/tests/files/ast/SourceUnit.json").expect("Could not find test data file"); - let res = serde_json::from_str::(&ast).map_err(|_| "Error deserializing SourceUnit".to_string())?; - - assert_eq!(res.id, 428); - assert_eq!(res.src, "33:8524:0".to_string()); - assert_eq!(res.absolute_path, "wow.sol".to_string()); - assert_eq!(res.license, Some("MIT".to_string())); - assert_eq!(res.node_type, NodeType::SourceUnit); - Ok(()) - } - - #[test] - fn test_correct_structured_documentation_parsing() -> Result<(), String> { - let ast = fs::read_to_string("../solc-wrapper/tests/files/ast/StructuredDocumentation.json").expect("Could not find test data file"); - let res = serde_json::from_str::(&ast).map_err(|_| "Error deserializing StructuredDocumentation".to_string())?; - - assert_eq!(res.id, 63); - assert_eq!(res.src, "1522:55:0".to_string()); - assert_eq!(res.text, "@notice Event emitted when the minting is locked ".to_string()); - assert_eq!(res.node_type, NodeType::StructuredDocumentation); - Ok(()) - } - - #[test] - fn test_correct_break_parsing() -> Result<(), String> { - let ast = fs::read_to_string("../solc-wrapper/tests/files/ast/Break.json").expect("Could not find test data file"); - let res = serde_json::from_str::(&ast).map_err(|_| "Error deserializing Break".to_string())?; - - assert_eq!(res.id, 7); - assert_eq!(res.src, "172:5:0".to_string()); - assert_eq!(res.node_type, NodeType::Break); - Ok(()) - } - - #[test] - fn test_correct_continue_parsing() -> Result<(), String> { - let ast = fs::read_to_string("../solc-wrapper/tests/files/ast/Continue.json").expect("Could not find test data file"); - let res = serde_json::from_str::(&ast).map_err(|_| "Error deserializing Continue".to_string())?; - - assert_eq!(res.id, 7); - assert_eq!(res.src, "172:8:0".to_string()); - assert_eq!(res.node_type, NodeType::Continue); - Ok(()) - } - - #[test] - fn test_correct_while_statement_parsing() -> Result<(), String> { - let ast = fs::read_to_string("../solc-wrapper/tests/files/ast/WhileStatement.json").expect("Could not find test data file"); - let res = serde_json::from_str::(&ast).map_err(|_| "Error deserializing WhileStatement".to_string())?; - - assert_eq!(res.id, 9); - assert_eq!(res.src, "145:46:0".to_string()); - assert_eq!(res.node_type, NodeType::WhileStatement); - Ok(()) - } - - #[test] - fn test_correct_do_while_statement_parsing() -> Result<(), String> { - let ast = fs::read_to_string("../solc-wrapper/tests/files/ast/DoWhileStatement.json").expect("Could not find test data file"); - let res = serde_json::from_str::(&ast).map_err(|_| "Error deserializing DoWhileStatement".to_string())?; - - assert_eq!(res.id, 9); - assert_eq!(res.src, "145:50:0".to_string()); - assert_eq!(res.node_type, NodeType::DoWhileStatement); - Ok(()) - } - - #[test] - fn test_correct_for_statement_parsing() -> Result<(), String> { - let ast = fs::read_to_string("../solc-wrapper/tests/files/ast/ForStatement.json").expect("Could not find test data file"); - let res = serde_json::from_str::(&ast).map_err(|_| "Error deserializing ForStatement".to_string())?; - - assert_eq!(res.id, 18); - assert_eq!(res.src, "145:60:0".to_string()); - assert_eq!(res.node_type, NodeType::ForStatement); - Ok(()) - } - - #[test] - fn test_correct_struct_definition_parsing() -> Result<(), String> { - let ast = fs::read_to_string("../solc-wrapper/tests/files/ast/StructDefinition.json").expect("Could not find test data file"); - let res = serde_json::from_str::(&ast).map_err(|_| "Error deserializing StructDefinition".to_string())?; - - assert_eq!(res.id, 6); - assert_eq!(res.src, "62:36:0".to_string()); - assert_eq!(res.name_location, Some("69:1:0".to_string())); - assert_eq!(res.name, "S".to_string()); - assert_eq!(res.scope, 27); - assert_eq!(res.visibility, Visibility::Public); - assert_eq!(res.canonical_name, "S".to_string()); - assert_eq!(res.node_type, NodeType::StructDefinition); - Ok(()) - } - - #[test] - fn test_correct_try_statement_parsing() -> Result<(), String> { - let ast = fs::read_to_string("../solc-wrapper/tests/files/ast/TryStatement.json").expect("Could not find test data file"); - let res = serde_json::from_str::(&ast).map_err(|_| "Error deserializing TryStatement".to_string())?; - - assert_eq!(res.id, 95); - assert_eq!(res.src, "976:155:0".to_string()); - assert_eq!(res.node_type, NodeType::TryStatement); - Ok(()) - } - - #[test] - fn test_correct_revert_statement_parsing() -> Result<(), String> { - let ast = fs::read_to_string("../solc-wrapper/tests/files/ast/RevertStatement.json").expect("Could not find test data file"); - let res = serde_json::from_str::(&ast).map_err(|_| "Error deserializing RevertStatement".to_string())?; - - assert_eq!(res.id, 8); - assert_eq!(res.src, "118:12:0".to_string()); - assert_eq!(res.node_type, NodeType::RevertStatement); - Ok(()) - } - - #[test] - fn test_correct_inline_assembly_parsing() -> Result<(), String> { - let ast = fs::read_to_string("../solc-wrapper/tests/files/ast/InlineAssembly.json").expect("Could not find test data file"); - let res = serde_json::from_str::(&ast).map_err(|_| "Error deserializing InlineAssembly".to_string())?; - - assert_eq!(res.id, 9); - assert_eq!(res.src, "176:50:0".to_string()); - assert_eq!(res.evm_version, EvmVersion::London); - assert_eq!(res.node_type, NodeType::InlineAssembly); - Ok(()) - } - - #[test] - fn test_correct_error_definition_parsing() -> Result<(), String> { - let ast = fs::read_to_string("../solc-wrapper/tests/files/ast/ErrorDefinition.json").expect("Could not find test data file"); - let res = serde_json::from_str::(&ast).map_err(|_| "Error deserializing ErrorDefinition".to_string())?; - - assert_eq!(res.id, 3); - assert_eq!(res.name, "No".to_string()); - assert_eq!(res.name_location, "92:2:0".to_string()); - assert_eq!(res.src, "86:11:0".to_string()); - assert_eq!(res.node_type, NodeType::ErrorDefinition); - Ok(()) - } - - #[test] - fn test_correct_index_access_parsing() -> Result<(), String> { - let ast = fs::read_to_string("../solc-wrapper/tests/files/ast/IndexAccess.json").expect("Could not find test data file"); - let res = serde_json::from_str::(&ast).map_err(|_| "Error deserializing IndexAccess".to_string())?; - - assert_eq!(res.id, 14); - assert_eq!(res.src, "203:10:0".to_string()); - assert_eq!(res.node_type, NodeType::IndexAccess); - Ok(()) - } - - #[test] - fn test_correct_new_expression_parsing() -> Result<(), String> { - let ast = fs::read_to_string("../solc-wrapper/tests/files/ast/NewExpression.json").expect("Could not find test data file"); - let res = serde_json::from_str::(&ast).map_err(|_| "Error deserializing NewExpression".to_string())?; - - assert_eq!(res.id, 7); - assert_eq!(res.src, "131:6:0".to_string()); - assert_eq!(res.node_type, NodeType::NewExpression); - Ok(()) - } - - #[test] - fn test_correct_function_call_options_parsing() -> Result<(), String> { - let ast = fs::read_to_string("../solc-wrapper/tests/files/ast/FunctionCallOptions.json").expect("Could not find test data file"); - let res = serde_json::from_str::(&ast).map_err(|_| "Error deserializing FunctionCallOptions".to_string())?; - - assert_eq!(res.id, 21); - assert_eq!(res.src, "236:36:0".to_string()); - assert_eq!(res.names, vec!["value".to_string()]); - assert_eq!(res.node_type, NodeType::FunctionCallOptions); - Ok(()) - } - - #[test] - fn test_correct_function_type_name_options_parsing() -> Result<(), String> { - let ast = fs::read_to_string("../solc-wrapper/tests/files/ast/FunctionTypeName.json").expect("Could not find test data file"); - let res = serde_json::from_str::(&ast).map_err(|_| "Error deserializing FunctionTypeName".to_string())?; - - assert_eq!(res.id, 11); - assert_eq!(res.src, "260:37:0".to_string()); - assert_eq!(res.state_mutability, StateMutability::Pure); - assert_eq!(res.visibility, Visibility::Internal); - assert_eq!(res.node_type, NodeType::FunctionTypeName); - Ok(()) - } - - #[test] - fn test_correct_user_defined_value_type_definition_parsing() -> Result<(), String> { - let ast = fs::read_to_string("../solc-wrapper/tests/files/ast/UserDefinedValueTypeDefinition.json").expect("Could not find test data file"); - let res = serde_json::from_str::(&ast).map_err(|_| "Error deserializing UserDefinedValueTypeDefinition".to_string())?; - - assert_eq!(res.id, 3); - assert_eq!(res.name, "UFixed256x18".to_string()); - assert_eq!(res.name_location, Some("160:12:0".to_string())); - assert_eq!(res.src, "155:29:0".to_string()); - assert_eq!(res.node_type, NodeType::UserDefinedValueTypeDefinition); - Ok(()) - } - - #[test] - fn test_correct_elementary_type_name_expression_parsing() -> Result<(), String> { - let ast = fs::read_to_string("../solc-wrapper/tests/files/ast/ElementaryTypeNameExpression.json").expect("Could not find test data file"); - let res = serde_json::from_str::(&ast).map_err(|_| "Error deserializing ElementaryTypeNameExpression".to_string())?; - - assert_eq!(res.id, 10); - assert_eq!(res.src, "156:7:0".to_string()); - assert_eq!(res.node_type, NodeType::ElementaryTypeNameExpression); - Ok(()) - } - - #[test] - fn test_correct_index_range_access_expression_parsing() -> Result<(), String> { - let ast = fs::read_to_string("../solc-wrapper/tests/files/ast/IndexRangeAccess.json").expect("Could not find test data file"); - let res = serde_json::from_str::(&ast).map_err(|_| "Error deserializing IndexRangeAccess".to_string())?; - - assert_eq!(res.id, 12); - assert_eq!(res.src, "174:8:0".to_string()); - assert_eq!(res.node_type, NodeType::IndexRangeAccess); - Ok(()) - } -} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solc-wrapper/src/ast/error.rs b/toolchains/solidity/linter/core/solc-wrapper/src/ast/error.rs deleted file mode 100644 index 2e16d955..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/src/ast/error.rs +++ /dev/null @@ -1,7 +0,0 @@ -use thiserror::Error; - -#[derive(Error, Debug)] -pub enum AstError { - #[error("AstError: cannot parse the json to AST")] - JsonParseFailed(#[from] serde_json::Error), -} diff --git a/toolchains/solidity/linter/core/solc-wrapper/src/ast/mod.rs b/toolchains/solidity/linter/core/solc-wrapper/src/ast/mod.rs deleted file mode 100644 index 6b8c7b6c..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/src/ast/mod.rs +++ /dev/null @@ -1,4 +0,0 @@ -pub mod parse; -pub mod ast; -pub mod error; -pub mod utils; \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solc-wrapper/src/ast/parse.rs b/toolchains/solidity/linter/core/solc-wrapper/src/ast/parse.rs deleted file mode 100644 index 6c03cd37..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/src/ast/parse.rs +++ /dev/null @@ -1,7 +0,0 @@ -use crate::ast::ast::SourceUnit; - -use super::error::AstError; - -pub fn parse_ast(json: &str) -> Result { - Ok(serde_json::from_str(json)?) -} diff --git a/toolchains/solidity/linter/core/solc-wrapper/src/ast/utils.rs b/toolchains/solidity/linter/core/solc-wrapper/src/ast/utils.rs deleted file mode 100644 index 3cbd39eb..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/src/ast/utils.rs +++ /dev/null @@ -1,731 +0,0 @@ -use crate::ast::ast::*; - -pub enum Nodes { - SourceUnit(SourceUnit), - ContractDefinition(Box), - Assignment(Box), - BinaryOperation(Box), - Conditional(Box), - ElementaryTypeNameExpression(Box), - FunctionCall(Box), - FunctionCallOptions(Box), - Identifier(Box), - IdentifierPath(Box), - IndexAccess(Box), - IndexRangeAccess(Box), - Literal(Box), - MemberAccess(Box), - NewExpression(Box), - TupleExpression(Box), - UnaryOperation(Box), - Block(Box), - Break(Box), - Continue(Box), - DoWhileStatement(Box), - EmitStatement(Box), - ExpressionStatement(Box), - ForStatement(Box), - IfStatement(Box), - PlaceholderStatement(Box), - Return(Box), - RevertStatement(Box), - TryStatement(Box), - TryCatchClause(Box), - UncheckedBlock(Box), - VariableDeclarationStatement(Box), - WhileStatement(Box), - FunctionDefinition(Box), - ModifierDefinition(Box), - StructDefinition(Box), - UserDefinedValueTypeDefinition(Box), - VariableDeclaration(Box), - EnumDefinition(Box), - EnumValue(Box), - ErrorDefinition(Box), - EventDefinition(Box), - UsingForDirective(Box), - UserDefinedTypeName(Box), - ImportDirective(Box), - PragmaDirective(Box), - FunctionTypeName(Box), - Mapping(Box), - ElementaryTypeName(Box), - ParameterList(Box), - OverrideSpecifier(Box), - InheritanceSpecifier(Box), - ModifierInvocation(Box) -} - -fn check_statement_node(node: Statement, nodes: &mut Vec, node_type: NodeType) { - match node { - Statement::Block(block) => { - check_block_node(block, nodes, node_type); - } - Statement::Break(break_statement) => { - check_break_node(break_statement, nodes, node_type); - } - Statement::Continue(continue_statement) => { - check_continue_node(continue_statement, nodes, node_type); - } - Statement::DoWhileStatement(do_while_statement) => { - check_do_while_statement_node(do_while_statement, nodes, node_type); - } - Statement::EmitStatement(emit_statement) => { - check_emit_statement_node(emit_statement, nodes, node_type); - } - Statement::ExpressionStatement(expression_statement) => { - check_expression_statement_node(expression_statement, nodes, node_type); - } - Statement::ForStatement(for_statement) => { - check_for_statement_node(for_statement, nodes, node_type); - } - Statement::IfStatement(if_statement) => { - check_if_statement_node(if_statement, nodes, node_type); - } - Statement::PlaceholderStatement(placeholder_statement) => { - check_placeholder_statement_node(placeholder_statement, nodes, node_type); - } - Statement::Return(return_statement) => { - check_return_node(return_statement, nodes, node_type); - } - Statement::RevertStatement(revert_statement) => { - check_revert_statement_node(revert_statement, nodes, node_type); - } - Statement::TryStatement(try_statement) => { - check_try_statement_node(try_statement, nodes, node_type); - } - Statement::UncheckedBlock(unchecked_block) => { - check_unchecked_block_node(unchecked_block, nodes, node_type); - } - Statement::VariableDeclarationStatement(variable_declaration_statement) => { - check_variable_declaration_statement_node(variable_declaration_statement, nodes, node_type); - } - Statement::WhileStatement(while_statement) => { - check_while_statement_node(while_statement, nodes, node_type); - } - } -} - -fn check_body_node(body: Body, nodes: &mut Vec, node_type: NodeType) { - match body { - Body::Block(block) => { - check_block_node(block, nodes, node_type); - } - Body::Statement(statement) => { - check_statement_node(*statement, nodes, node_type); - } - } -} - -fn check_function_type_name_node(node: Box, nodes: &mut Vec, node_type: NodeType) { - if node_type == NodeType::FunctionTypeName { - nodes.push(Nodes::FunctionTypeName(node.clone())); - } - check_parameter_list_node(Box::new(node.parameter_types), nodes, node_type.clone()); - check_parameter_list_node(Box::new(node.return_parameter_types), nodes, node_type); -} - -fn check_typename_node(node: TypeName, nodes: &mut Vec, node_type: NodeType) { - match node { - TypeName::ArrayTypeName(node) => { - check_typename_node(node.base_type, nodes, node_type); - }, - TypeName::ElementaryTypeName(node) => { - check_elementary_type_name_node(node, nodes, node_type); - }, - TypeName::FunctionTypeName(node) => { - check_function_type_name_node(node, nodes, node_type); - }, - TypeName::Mapping(node) => { - check_mapping_node(node, nodes, node_type); - }, - TypeName::UserDefinedTypeName(node) => { - nodes.push(Nodes::UserDefinedTypeName(node)); - }, - } -} - -fn check_mapping_node(node: Box, nodes: &mut Vec, node_type: NodeType) { - if node_type == NodeType::Mapping { - nodes.push(Nodes::Mapping(node.clone())); - } - check_typename_node(node.key_type, nodes, node_type.clone()); - check_typename_node(node.value_type, nodes, node_type.clone()); -} - -fn check_expression_node(node: Expression, nodes: &mut Vec, node_type: NodeType) { - match node { - Expression::Assignment(node) => { - check_assignment_node(node, nodes, node_type); - } - Expression::BinaryOperation(node) => { - check_binary_operation_node(node, nodes, node_type); - } - Expression::Conditional(node) => { - check_conditional_node(node, nodes, node_type); - } - Expression::ElementaryTypeNameExpression(node) => { - check_elementary_type_name_expression_node(node, nodes, node_type); - } - Expression::FunctionCall(node) => { - check_function_call_node(node, nodes, node_type); - } - Expression::FunctionCallOptions(node) => { - check_function_call_options_node(node, nodes, node_type); - } - Expression::Identifier(node) => { - check_identifier_node(node, nodes, node_type); - } - Expression::IdentifierPath(node) => { - check_identifier_path_node(node, nodes, node_type); - } - Expression::IndexAccess(node) => { - check_index_access_node(node, nodes, node_type); - } - Expression::IndexRangeAccess(node) => { - check_index_range_access_node(node, nodes, node_type); - } - Expression::Literal(node) => { - check_literal_node(node, nodes, node_type); - } - Expression::MemberAccess(node) => { - check_member_access_node(node, nodes, node_type); - } - Expression::NewExpression(node) => { - check_new_expression_node(node, nodes, node_type); - } - Expression::TupleExpression(node) => { - check_tuple_expression_node(node, nodes, node_type); - } - Expression::UnaryOperation(node) => { - check_unary_operation_node(node, nodes, node_type); - } - } -} - -fn check_parameter_list_node(node: Box, nodes: &mut Vec, node_type: NodeType) { - if node_type == NodeType::ParameterList { - nodes.push(Nodes::ParameterList(node.clone())); - } - for parameter in node.parameters { - check_variable_declaration_node(Box::new(parameter), nodes, node_type.clone()); - } -} - -fn check_assignment_node(node: Box, nodes: &mut Vec, node_type: NodeType) { - if node_type == NodeType::Assignment { - nodes.push(Nodes::Assignment(node.clone())); - } - check_expression_node(node.left_hand_side, nodes, node_type.clone()); - check_expression_node(node.right_hand_side, nodes, node_type); -} - -fn check_binary_operation_node(node: Box, nodes: &mut Vec, node_type: NodeType) { - if node_type == NodeType::BinaryOperation { - nodes.push(Nodes::BinaryOperation(node.clone())); - } - check_expression_node(node.left_expression, nodes, node_type.clone()); - check_expression_node(node.right_expression, nodes, node_type.clone()); -} - -fn check_conditional_node(node: Box, nodes: &mut Vec, node_type: NodeType) { - if node_type == NodeType::Conditional { - nodes.push(Nodes::Conditional(node.clone())); - } - check_expression_node(node.condition, nodes, node_type.clone()); - check_expression_node(node.true_expression, nodes, node_type.clone()); - check_expression_node(node.false_expression, nodes, node_type); -} - -fn check_elementary_type_name_expression_node(node: Box, nodes: &mut Vec, node_type: NodeType) { - if node_type == NodeType::ElementaryTypeNameExpression { - nodes.push(Nodes::ElementaryTypeNameExpression(node.clone())); - } - check_elementary_type_name_node(Box::new(node.type_name), nodes, node_type); -} - -fn check_elementary_type_name_node(node: Box, nodes: &mut Vec, node_type: NodeType) { - if node_type == NodeType::ElementaryTypeName { - nodes.push(Nodes::ElementaryTypeName(node.clone())); - } -} - -fn check_function_call_node(node: Box, nodes: &mut Vec, node_type: NodeType) { - if node_type == NodeType::FunctionCall { - nodes.push(Nodes::FunctionCall(node.clone())); - } - check_expression_node(node.expression, nodes, node_type.clone()); - for argument in node.arguments { - check_expression_node(argument, nodes, node_type.clone()); - } -} - -fn check_function_call_options_node(node: Box, nodes: &mut Vec, node_type: NodeType) { - if node_type == NodeType::FunctionCallOptions { - nodes.push(Nodes::FunctionCallOptions(node.clone())); - } - check_expression_node(node.expression, nodes, node_type.clone()); - for option in node.options { - check_expression_node(option, nodes, node_type.clone()); - } -} - -fn check_identifier_node(node: Box, nodes: &mut Vec, node_type: NodeType) { - if node_type == NodeType::Identifier { - nodes.push(Nodes::Identifier(node.clone())); - } -} - -fn check_identifier_path_node(node: Box, nodes: &mut Vec, node_type: NodeType) { - if node_type == NodeType::IdentifierPath { - nodes.push(Nodes::IdentifierPath(node.clone())); - } -} - -fn check_index_access_node(node: Box, nodes: &mut Vec, node_type: NodeType) { - if node_type == NodeType::IndexAccess { - nodes.push(Nodes::IndexAccess(node.clone())); - } - check_expression_node(node.base_expression, nodes, node_type.clone()); - if node.index_expression.is_some() { - check_expression_node(node.index_expression.unwrap(), nodes, node_type); - } -} - -fn check_index_range_access_node(node: Box, nodes: &mut Vec, node_type: NodeType) { - if node_type == NodeType::IndexRangeAccess { - nodes.push(Nodes::IndexRangeAccess(node.clone())); - } - check_expression_node(node.base_expression, nodes, node_type.clone()); - if node.start_expression.is_some() { - check_expression_node(node.start_expression.unwrap(), nodes, node_type.clone()); - } - if node.end_expression.is_some() { - check_expression_node(node.end_expression.unwrap(), nodes, node_type); - } -} - -fn check_literal_node(node: Box, nodes: &mut Vec, node_type: NodeType) { - if node_type == NodeType::Literal { - nodes.push(Nodes::Literal(node.clone())); - } -} - -fn check_member_access_node(node: Box, nodes: &mut Vec, node_type: NodeType) { - if node_type == NodeType::MemberAccess { - nodes.push(Nodes::MemberAccess(node.clone())); - } - check_expression_node(node.expression, nodes, node_type); -} - -fn check_new_expression_node(node: Box, nodes: &mut Vec, node_type: NodeType) { - if node_type == NodeType::NewExpression { - nodes.push(Nodes::NewExpression(node.clone())); - } -} - -fn check_tuple_expression_node(node: Box, nodes: &mut Vec, node_type: NodeType) { - if node_type == NodeType::TupleExpression { - nodes.push(Nodes::TupleExpression(node.clone())); - } - for expression in node.components { - check_expression_node(expression, nodes, node_type.clone()); - } -} - -fn check_unary_operation_node(node: Box, nodes: &mut Vec, node_type: NodeType) { - if node_type == NodeType::UnaryOperation { - nodes.push(Nodes::UnaryOperation(node.clone())); - } - check_expression_node(node.sub_expression, nodes, node_type); -} - -fn check_block_node(node: Box, nodes: &mut Vec, node_type: NodeType) { - if node_type == NodeType::Block { - nodes.push(Nodes::Block(node.clone())); - } - if node.statements.is_some() { - for statement in node.statements.unwrap() { - check_statement_node(statement, nodes, node_type.clone()); - } - } -} - -fn check_break_node(node: Box, nodes: &mut Vec, node_type: NodeType) { - if node_type == NodeType::Break { - nodes.push(Nodes::Break(node.clone())); - } -} - -fn check_continue_node(node: Box, nodes: &mut Vec, node_type: NodeType) { - if node_type == NodeType::Continue { - nodes.push(Nodes::Continue(node.clone())); - } -} - -fn check_do_while_statement_node(node: Box, nodes: &mut Vec, node_type: NodeType) { - if node_type == NodeType::DoWhileStatement { - nodes.push(Nodes::DoWhileStatement(node.clone())); - } - check_body_node(node.body, nodes, node_type.clone()); - check_expression_node(node.condition, nodes, node_type); -} - -fn check_emit_statement_node(node: Box, nodes: &mut Vec, node_type: NodeType) { - if node_type == NodeType::EmitStatement { - nodes.push(Nodes::EmitStatement(node.clone())); - } - check_function_call_node(Box::new(node.event_call), nodes, node_type); -} - -fn check_expression_statement_node(node: Box, nodes: &mut Vec, node_type: NodeType) { - if node_type == NodeType::ExpressionStatement { - nodes.push(Nodes::ExpressionStatement(node.clone())); - } - check_expression_node(node.expression, nodes, node_type); -} - -fn check_for_statement_node(node: Box, nodes: &mut Vec, node_type: NodeType) { - if node_type == NodeType::ForStatement { - nodes.push(Nodes::ForStatement(node.clone())); - } - check_body_node(node.body, nodes, node_type.clone()); - if node.condition.is_some() { - check_expression_node(node.condition.unwrap(), nodes, node_type.clone()); - } - if node.loop_expression.is_some() { - check_expression_statement_node(Box::new(node.loop_expression.unwrap()), nodes, node_type.clone()); - } - if node.initialization_expression.is_some() { - let initialization_expression = node.initialization_expression.unwrap(); - match initialization_expression { - InitializationExpression::VariableDeclarationStatement(variable_declaration_statement) => { - check_variable_declaration_statement_node(Box::new(variable_declaration_statement), nodes, node_type.clone()); - }, - InitializationExpression::ExpressionStatement(expression_statement) => { - check_expression_statement_node(Box::new(expression_statement), nodes, node_type.clone()); - }, - } - } -} - -fn check_if_statement_node(node: Box, nodes: &mut Vec, node_type: NodeType) { - if node_type == NodeType::IfStatement { - nodes.push(Nodes::IfStatement(node.clone())); - } - check_expression_node(node.condition, nodes, node_type.clone()); - check_body_node(node.true_body, nodes, node_type.clone()); - if node.false_body.is_some() { - check_body_node(node.false_body.unwrap(), nodes, node_type); - } -} - -fn check_placeholder_statement_node(node: Box, nodes: &mut Vec, node_type: NodeType) { - if node_type == NodeType::PlaceholderStatement { - nodes.push(Nodes::PlaceholderStatement(node.clone())); - } -} - -fn check_return_node(node: Box, nodes: &mut Vec, node_type: NodeType) { - if node_type == NodeType::Return { - nodes.push(Nodes::Return(node.clone())); - } - if node.expression.is_some() { - check_expression_node(node.expression.unwrap(), nodes, node_type); - } -} - -fn check_revert_statement_node(node: Box, nodes: &mut Vec, node_type: NodeType) { - if node_type == NodeType::RevertStatement { - nodes.push(Nodes::RevertStatement(node.clone())); - } - check_statement_node(node.error_call, nodes, node_type); -} - -fn check_try_statement_node(node: Box, nodes: &mut Vec, node_type: NodeType) { - if node_type == NodeType::TryStatement { - nodes.push(Nodes::TryStatement(node.clone())); - } - check_expression_node(node.external_call, nodes, node_type.clone()); - for catch_clause in node.clauses { - check_try_catch_clause_node(Box::new(catch_clause), nodes, node_type.clone()); - } -} - -fn check_try_catch_clause_node(node: Box, nodes: &mut Vec, node_type: NodeType) { - if node_type == NodeType::TryCatchClause { - nodes.push(Nodes::TryCatchClause(node.clone())); - } - check_block_node(Box::new(node.block), nodes, node_type); -} - -fn check_unchecked_block_node(node: Box, nodes: &mut Vec, node_type: NodeType) { - if node_type == NodeType::UncheckedBlock { - nodes.push(Nodes::UncheckedBlock(node.clone())); - } - for statement in node.statements { - check_statement_node(statement, nodes, node_type.clone()); - } -} - -fn check_variable_declaration_statement_node(node: Box, nodes: &mut Vec, node_type: NodeType) { - if node_type == NodeType::VariableDeclarationStatement { - nodes.push(Nodes::VariableDeclarationStatement(node.clone())); - } - for variable_declaration in node.declarations { - if variable_declaration.is_some() { - check_variable_declaration_node(Box::new(variable_declaration.unwrap()), nodes, node_type.clone()); - } - } -} - -fn check_while_statement_node(node: Box, nodes: &mut Vec, node_type: NodeType) { - if node_type == NodeType::WhileStatement { - nodes.push(Nodes::WhileStatement(node.clone())); - } - check_statement_node(node.body, nodes, node_type.clone()); - check_expression_node(node.condition, nodes, node_type); -} - -fn check_modifier_invocation_node(node: Box, nodes: &mut Vec, node_type: NodeType) { - if node_type == NodeType::ModifierInvocation { - nodes.push(Nodes::ModifierInvocation(node.clone())); - } - if node.arguments.is_some() { - check_expression_node(node.arguments.unwrap(), nodes, node_type.clone()); - } - match node.modifier_name { - ModifierName::Identifier(identifier) => { - check_identifier_node(Box::new(identifier), nodes, node_type); - }, - ModifierName::IdentifierPath(identifier_path) => { - check_identifier_path_node(Box::new(identifier_path), nodes, node_type); - } - }; -} - -fn check_function_definition_node(node: Box, nodes: &mut Vec, node_type: NodeType) { - if node_type == NodeType::FunctionDefinition { - nodes.push(Nodes::FunctionDefinition(node.clone())); - } - if node.body.is_some() { - check_block_node(Box::new(node.body.unwrap()), nodes, node_type.clone()); - } - for modifier in node.modifiers { - check_modifier_invocation_node(Box::new(modifier), nodes, node_type.clone()); - } - if node.overrides.is_some() { - check_override_specifier_node(Box::new(node.overrides.unwrap()), nodes, node_type.clone()); - } - check_parameter_list_node(Box::new(node.parameters), nodes, node_type.clone()); - check_parameter_list_node(Box::new(node.return_parameters), nodes, node_type); -} - -fn check_override_specifier_node(node: Box, nodes: &mut Vec, node_type: NodeType) { - if node_type == NodeType::OverrideSpecifier { - nodes.push(Nodes::OverrideSpecifier(node.clone())); - } - match node.overrides { - OverridesEnum::Identifier(identifier_list) => { - for identifier in identifier_list { - check_identifier_path_node(Box::new(identifier), nodes, node_type.clone()); - } - }, - OverridesEnum::UserDefinedTypeName(user_defined_type_name) => { - for user_defined_type in user_defined_type_name { - check_user_defined_type_name_node(Box::new(user_defined_type), nodes, node_type.clone()); - } - }, - } -} - -fn check_modifier_definition_node(node: Box, nodes: &mut Vec, node_type: NodeType) { - if node_type == NodeType::ModifierDefinition { - nodes.push(Nodes::ModifierDefinition(node.clone())); - } - check_statement_node(node.body, nodes, node_type.clone()); - if node.overrides.is_some() { - check_override_specifier_node(Box::new(node.overrides.unwrap()), nodes, node_type.clone()); - } - check_parameter_list_node(Box::new(node.parameters), nodes, node_type); -} - -fn check_struct_definition_node(node: Box, nodes: &mut Vec, node_type: NodeType) { - if node_type == NodeType::StructDefinition { - nodes.push(Nodes::StructDefinition(node.clone())); - } - for member in node.members { - check_variable_declaration_node(Box::new(member), nodes, node_type.clone()); - } -} - -fn check_user_defined_value_type_definition_node(node: Box, nodes: &mut Vec, node_type: NodeType) { - if node_type == NodeType::UserDefinedValueTypeDefinition { - nodes.push(Nodes::UserDefinedValueTypeDefinition(node.clone())); - } - check_typename_node(node.underlying_type, nodes, node_type); -} - -fn check_variable_declaration_node(node: Box, nodes: &mut Vec, node_type: NodeType) { - if node_type == NodeType::VariableDeclaration { - nodes.push(Nodes::VariableDeclaration(node.clone())); - } - if node.value.is_some() { - check_expression_node(node.value.unwrap(), nodes, node_type.clone()); - } - if node.type_name.is_some() { - check_typename_node(node.type_name.unwrap(), nodes, node_type); - } -} - -fn check_enum_definition_node(node: Box, nodes: &mut Vec, node_type: NodeType) { - if node_type == NodeType::EnumDefinition { - nodes.push(Nodes::EnumDefinition(node.clone())); - } - for member in node.members { - check_enum_value_node(Box::new(member), nodes, node_type.clone()); - } -} - -fn check_enum_value_node(node: Box, nodes: &mut Vec, node_type: NodeType) { - if node_type == NodeType::EnumValue { - nodes.push(Nodes::EnumValue(node.clone())); - } -} - -fn check_error_definition_node(node: Box, nodes: &mut Vec, node_type: NodeType) { - if node_type == NodeType::ErrorDefinition { - nodes.push(Nodes::ErrorDefinition(node.clone())); - } - if node.parameters.is_some() { - check_parameter_list_node(Box::new(node.parameters.unwrap()), nodes, node_type); - } -} - -fn check_event_definition_node(node: Box, nodes: &mut Vec, node_type: NodeType) { - if node_type == NodeType::EventDefinition { - nodes.push(Nodes::EventDefinition(node.clone())); - } - if node.parameters.is_some() { - check_parameter_list_node(Box::new(node.parameters.unwrap()), nodes, node_type); - } -} - -fn check_using_for_directive_node(node: Box, nodes: &mut Vec, node_type: NodeType) { - if node_type == NodeType::UsingForDirective { - nodes.push(Nodes::UsingForDirective(node.clone())); - } - if node.function_list.is_some() { - let function_list = node.function_list.unwrap(); - for function in function_list { - check_identifier_path_node(Box::new(function.function), nodes, node_type.clone()); - } - } - if node.function.is_some() { - check_identifier_path_node(Box::new(node.function.unwrap()), nodes, node_type.clone()); - } - if node.library_name.is_some() { - check_expression_node(node.library_name.unwrap(), nodes, node_type.clone()); - } - if node.type_name.is_some() { - check_typename_node(node.type_name.unwrap(), nodes, node_type); - } -} - -fn check_inheritance_specifier_node(node: Box, nodes: &mut Vec, node_type: NodeType) { - if node_type == NodeType::InheritanceSpecifier { - nodes.push(Nodes::InheritanceSpecifier(node.clone())); - } - if node.arguments.is_some() { - for argument in node.arguments.unwrap() { - check_expression_node(argument, nodes, node_type.clone()); - } - } -} - -fn check_contract_definition_node(node: Box, nodes: &mut Vec, node_type: NodeType) { - if node_type == NodeType::ContractDefinition { - nodes.push(Nodes::ContractDefinition(node.clone())); - } - for base in node.base_contracts { - check_inheritance_specifier_node(Box::new(base), nodes, node_type.clone()); - } - for node in node.nodes { - check_contract_definition_child_node(Box::new(node), nodes, node_type.clone()); - } -} - -fn check_contract_definition_child_node(node: Box, nodes: &mut Vec, node_type: NodeType) { - match *node { - ContractDefinitionChildNodes::UsingForDirective(node) => check_using_for_directive_node(node, nodes, node_type), - ContractDefinitionChildNodes::StructDefinition(node) => check_struct_definition_node(node, nodes, node_type), - ContractDefinitionChildNodes::EnumDefinition(node) => check_enum_definition_node(node, nodes, node_type), - ContractDefinitionChildNodes::EventDefinition(node) => check_event_definition_node(node, nodes, node_type), - ContractDefinitionChildNodes::FunctionDefinition(node) => check_function_definition_node(node, nodes, node_type), - ContractDefinitionChildNodes::ModifierDefinition(node) => check_modifier_definition_node(node, nodes, node_type), - ContractDefinitionChildNodes::ErrorDefinition(node) => check_error_definition_node(node, nodes, node_type), - ContractDefinitionChildNodes::UserDefinedValueTypeDefinition(node) => check_user_defined_value_type_definition_node(node, nodes, node_type), - ContractDefinitionChildNodes::VariableDeclaration(node) => check_variable_declaration_node(node, nodes, node_type), - } -} - - -fn check_user_defined_type_name_node(node: Box, nodes: &mut Vec, node_type: NodeType) { - if node_type == NodeType::UserDefinedTypeName { - nodes.push(Nodes::UserDefinedTypeName(node.clone())); - } - if node.path_node.is_some() { - check_identifier_path_node(Box::new(node.path_node.unwrap()), nodes, node_type); - } -} - -fn check_import_directive_node(node: Box, nodes: &mut Vec, node_type: NodeType) { - if node_type == NodeType::ImportDirective { - nodes.push(Nodes::ImportDirective(node.clone())); - } -} - -fn check_pragma_directive_node(node: Box, nodes: &mut Vec, node_type: NodeType) { - if node_type == NodeType::PragmaDirective { - nodes.push(Nodes::PragmaDirective(node.clone())); - } -} - -fn check_source_unit_child_node(ast: SourceUnitChildNodes, nodes: &mut Vec, node_type: NodeType) { - match ast { - SourceUnitChildNodes::ContractDefinition(node) => { - check_contract_definition_node(node, nodes, node_type); - } - SourceUnitChildNodes::EnumDefinition(node) => { - check_enum_definition_node(node, nodes, node_type); - } - SourceUnitChildNodes::ErrorDefinition(node) => { - check_error_definition_node(node, nodes, node_type); - } - SourceUnitChildNodes::ImportDirective(node) => { - check_import_directive_node(node, nodes, node_type); - } - SourceUnitChildNodes::PragmaDirective(node) => { - check_pragma_directive_node(node, nodes, node_type); - } - SourceUnitChildNodes::StructDefinition(node) => { - check_struct_definition_node(node, nodes, node_type); - } - SourceUnitChildNodes::UsingForDirective(node) => { - check_using_for_directive_node(node, nodes, node_type); - } - _ => {} - } -} - -pub fn get_all_nodes_by_type(ast: SourceUnit, node_type: NodeType) -> Vec { - let mut nodes = Vec::new(); - if ast.node_type == node_type { - nodes.push(Nodes::SourceUnit(ast)); - return nodes; - } - for node in ast.nodes { - check_source_unit_child_node(node, &mut nodes, node_type.clone()); - } - nodes -} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solc-wrapper/src/error.rs b/toolchains/solidity/linter/core/solc-wrapper/src/error.rs deleted file mode 100644 index 4ce0fcb0..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/src/error.rs +++ /dev/null @@ -1,28 +0,0 @@ -use thiserror::Error; -use crate::{solc::error::CommandError, version::error::SolcVersionError, ast::error::AstError}; -use anyhow; -use crate::solc::parsing_error::ParsingError; - -#[derive(Error, Debug)] -pub enum SolcError { - #[error("SolcError: Something went wrong with sevm")] - SevmFailed(#[from] SolcVersionError), - - #[error("SolcError: Error from solc")] - SolcFailed(#[from] CommandError), - - #[error("SolcError: Can't do the compuation")] - ComputationFailed, - - #[error("SolcError: Error from ast pasing")] - AstFailed(#[from] AstError), - - #[error("SolcError: Output is empty")] - OutputIsEmpty, - - #[error("SolcError: compiler returned an error without outputing AST")] - ParsingFailed(#[from] ParsingError), - - #[error(transparent)] - Other(#[from] anyhow::Error), -} diff --git a/toolchains/solidity/linter/core/solc-wrapper/src/lib.rs b/toolchains/solidity/linter/core/solc-wrapper/src/lib.rs deleted file mode 100644 index 451ecf6b..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/src/lib.rs +++ /dev/null @@ -1,145 +0,0 @@ -mod solc; - -pub mod ast; -pub use ast::ast::*; - -mod utils; -mod version; - -use solc::command::SolcCommand; -use version::version::SolcVersion; -use ast::parse::parse_ast; - -mod error; -pub use error::SolcError; -use crate::solc::parsing_error::ParsingError; -use crate::utils::{get_error_location, get_error_message}; - -pub enum ExecuteResult { - Ast(String), - ParsingError(ParsingError), -} - -pub struct Solc { - version: SolcVersion -} - -impl Default for Solc { - fn default() -> Self { - Solc::new() - } -} - -impl Solc { - pub fn new() -> Self { - Solc { version: SolcVersion::default() } - } - - fn skip_output_header(output: &str) -> &str { - let idx = output.find("{").expect("No { found"); - &output[idx..] - } - - fn check_stderr(stderr: &str) -> Result<(), ParsingError> { - if !stderr.contains("Error") { - return Ok(()); - } - let location = get_error_location(stderr); - let location = match location { - Ok(e) => e, - Err(_) => return Ok(()) - }; - let error = get_error_message(stderr); - let error = match error { - Ok(e) => e, - Err(_) => return Ok(()) - }; - - Err( - ParsingError { - error, - location - } - ) - } - - pub fn execute_on_file(&self, path: &str) -> Result { - let content = std::fs::read_to_string(path).map_err(|e| SolcError::Other(anyhow::Error::new(e)))?; - - let version = self.version.find_matching_version( content.as_str())?; - let version_path = self.version.find_version_and_install(&version)?; - - let output = SolcCommand::new(version_path) - .args(["--ast-compact-json", "--stop-after", "parsing", path]) - .execute()?; - let stderr = String::from_utf8(output.clone().stderr) - .map_err(|e| SolcError::Other(anyhow::Error::new(e)))?; - let output = match Solc::check_stderr(stderr.as_str()).map_err(|e| SolcError::ParsingFailed(e)) { - Ok(_) => output, - Err(e) => return Err(e) - }; - let res = String::from_utf8(output.stdout) - .map_err(|e| SolcError::Other(anyhow::Error::new(e)))?; - Ok(String::from(Self::skip_output_header(&res))) - } - - pub fn execute_on_content(&self, content: &str) -> Result { - let version = self.version.find_matching_version( content)?; - let version_path = self.version.find_version_and_install(&version)?; - - let output = SolcCommand::new(version_path) - .args(["--ast-compact-json", "--stop-after", "parsing", "-"]) - .execute_with_input(content)?; - let stderr = String::from_utf8(output.clone().stderr) - .map_err(|e| SolcError::Other(anyhow::Error::new(e)))?; - let output = match Solc::check_stderr(stderr.as_str()).map_err(|e| SolcError::ParsingFailed(e)) { - Ok(_) => output, - Err(e) => return Err(e) - }; - let res = String::from_utf8(output.stdout) - .map_err(|e| SolcError::Other(anyhow::Error::new(e)))?; - Ok(String::from(Self::skip_output_header(&res))) - } - - pub fn extract_ast_file(&self, filepath: String) -> Result { - let output = self.execute_on_file(filepath.as_str())?; - Ok(parse_ast(output.as_str())?) - } - - pub fn extract_ast_content(&self, content: String) -> Result { - let output = self.execute_on_content(&content)?; - Ok(parse_ast(output.as_str())?) - } - -} - - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_skip_output_header_already_formatted() { - let output = r#"{ - "contracts": {}, - "sources": {}, - "errors": [] - }"#; - assert_eq!(Solc::skip_output_header(output), output); - } - - #[test] - fn test_skip_output_header() { - let output = r#"ok ======= test ====== \n awesome { - "contracts": {}, - "sources": {}, - "errors": [] - }"#; - let expected = r#"{ - "contracts": {}, - "sources": {}, - "errors": [] - }"#; - assert_eq!(Solc::skip_output_header(output), expected); - } -} diff --git a/toolchains/solidity/linter/core/solc-wrapper/src/solc/command.rs b/toolchains/solidity/linter/core/solc-wrapper/src/solc/command.rs deleted file mode 100644 index 426253e8..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/src/solc/command.rs +++ /dev/null @@ -1,66 +0,0 @@ -use std::{process::Command}; -use std::io::Write; -use std::process::{Output, Stdio}; -use std::{path::PathBuf}; - -use super::error::{CommandError, CommandType}; - -pub struct SolcCommand { - args: Vec, - bin_path : PathBuf -} - -impl Default for SolcCommand { - fn default() -> Self { - SolcCommand::new("solc") - } -} - -impl SolcCommand { - - pub fn new(path: impl Into) -> Self { - SolcCommand { - args: Vec::new(), - bin_path: path.into() - } - } - - pub fn arg>(mut self, arg: T) -> Self { - self.args.push(arg.into()); - self - } - - pub fn args(mut self, args: I) -> Self - where - I: IntoIterator, - S: Into, - { - for arg in args { - self = self.arg(arg); - } - self - } - - pub fn execute(&self) -> Result { - Command::new(&self.bin_path) - .args(&self.args) - .stdout(Stdio::piped()) - .output() - .map_err(|e| CommandError { error: e.to_string(), command_type: CommandType::ParseFile }) - } - - pub fn execute_with_input(&self, input: &str) -> Result { - let mut cmd = Command::new(&self.bin_path) - .args(&self.args) - .stdin(Stdio::piped()) - .stdout(Stdio::piped()) - .spawn() - .map_err(|e| CommandError { error: e.to_string(), command_type: CommandType::ParseStdin })?; - - { - let child_stdin = cmd.stdin.as_mut().unwrap(); - child_stdin.write_all(input.as_bytes()).map_err(|e| CommandError { error: e.to_string(), command_type: CommandType::ParseStdin })?; - } - cmd.wait_with_output().map_err(|e| CommandError { error: e.to_string(), command_type: CommandType::ParseStdin }) - } -} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solc-wrapper/src/solc/error.rs b/toolchains/solidity/linter/core/solc-wrapper/src/solc/error.rs deleted file mode 100644 index 6efc974c..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/src/solc/error.rs +++ /dev/null @@ -1,19 +0,0 @@ -use thiserror::Error; - -#[derive(Debug)] -pub enum CommandType { - ParseFile, - ParseStdin, -} - -#[derive(Error, Debug)] -pub struct CommandError { - pub command_type: CommandType, - pub error: String, -} - -impl std::fmt::Display for CommandError { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - write!(f, "CommandError: {}", self.error) - } -} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solc-wrapper/src/solc/mod.rs b/toolchains/solidity/linter/core/solc-wrapper/src/solc/mod.rs deleted file mode 100644 index fb5cfd3e..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/src/solc/mod.rs +++ /dev/null @@ -1,3 +0,0 @@ -pub mod error; -pub mod command; -pub mod parsing_error; \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solc-wrapper/src/solc/parsing_error.rs b/toolchains/solidity/linter/core/solc-wrapper/src/solc/parsing_error.rs deleted file mode 100644 index 199776d8..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/src/solc/parsing_error.rs +++ /dev/null @@ -1,29 +0,0 @@ -use thiserror::Error; - - - -#[derive(Error, Debug)] -pub struct ErrorLocation { - pub file: String, - pub line: usize, - pub column: usize, - pub length: usize -} - -#[derive(Error, Debug)] -pub struct ParsingError { - pub error: String, - pub location: ErrorLocation, -} - -impl std::fmt::Display for ErrorLocation { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - write!(f, "ErrorLocation in {} at line {}, column {}", self.file, self.line, self.column) - } -} - -impl std::fmt::Display for ParsingError { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - write!(f, "ParsingError: {}", self.error) - } -} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solc-wrapper/src/utils.rs b/toolchains/solidity/linter/core/solc-wrapper/src/utils.rs deleted file mode 100644 index 0f62f9e3..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/src/utils.rs +++ /dev/null @@ -1,71 +0,0 @@ -use regex::{Match, Regex}; -use once_cell::sync::Lazy; -use crate::solc::parsing_error::{ErrorLocation}; - -pub static RE_SOL_ERROR_PRINT: Lazy = Lazy::new(|| Regex::new(r"Error: (.*)\n").unwrap()); -pub static RE_SOL_ERROR_FILE: Lazy = Lazy::new(|| Regex::new(r"--> (?P.*):(?P\d+):(?P\d+):").unwrap()); -pub static RE_SOL_PRAGMA_VERSION: Lazy = Lazy::new(|| Regex::new(r"pragma\s+solidity\s+(?P\^?\d+\.\d+(?:\.\d+)?);").unwrap()); - -pub fn find_version_pragma(contract: &str) -> Option { - RE_SOL_PRAGMA_VERSION.captures(contract)?.name("version") -} - -fn count_length(error: String) -> usize { - let mut count = 0; - let mut n = error.len() - 3; - - while n > 0 { - if error.chars().nth(n).unwrap() == '\n' { - return count; - } - if error.chars().nth(n).unwrap() == '^' { - count += 1; - } - n-=1; - } - return count; -} - -pub fn get_error_message(stderr: &str) -> Result { - let error = RE_SOL_ERROR_PRINT.captures(stderr); - let error = match error { - Some(error) => error.get(0), - None => return Err(()) - }; - let error = match error { - Some(error) => error.as_str(), - None => return Err(()) - }; - let error = error.to_string(); - Ok(error) -} - -pub fn get_error_location(stderr: &str) -> Result { - let error = RE_SOL_ERROR_FILE.captures(stderr); - let error = match error { - Some(error) => error, - None => return Err(()) - }; - let file = match error.name("file") { - Some(file) => file.as_str().to_string(), - None => return Err(()) - }; - let line = match error.name("line") { - Some(line) => line.as_str().to_string(), - None => return Err(()) - }; - let column = match error.name("column") { - Some(line) => line.as_str().to_string(), - None => return Err(()) - }; - let length = count_length(stderr.to_string()); - - Ok( - ErrorLocation { - file, - line: line.parse().unwrap(), //unwrap is safe due to the regex that matches only number - column: column.parse().unwrap(), //unwrap is safe due to the regex that matches only number - length - } - ) -} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solc-wrapper/src/version/error.rs b/toolchains/solidity/linter/core/solc-wrapper/src/version/error.rs deleted file mode 100644 index 00d14e80..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/src/version/error.rs +++ /dev/null @@ -1,19 +0,0 @@ -use svm_lib::SolcVmError; -use thiserror::Error; - -use semver::Error; - -#[derive(Error, Debug)] -pub enum SolcVersionError { - #[error("SolcVersionError: Something went wrong with sevm")] - SevmFailed(#[from] SolcVmError), - - #[error("SolcVersionError: Can't do the compuation")] - ComputationFailed, - - #[error("SolcVersionError: Version failed")] - VersionFailed(#[from] Error), - - #[error("SolcVersionError: Wrong version of solidity")] - WrongVersion, -} diff --git a/toolchains/solidity/linter/core/solc-wrapper/src/version/mod.rs b/toolchains/solidity/linter/core/solc-wrapper/src/version/mod.rs deleted file mode 100644 index b4e1bd83..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/src/version/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -pub mod error; -pub mod version; \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solc-wrapper/src/version/version.rs b/toolchains/solidity/linter/core/solc-wrapper/src/version/version.rs deleted file mode 100644 index fb22722b..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/src/version/version.rs +++ /dev/null @@ -1,87 +0,0 @@ -use crate::utils; - -use semver::{Version, VersionReq}; -use std::{path::PathBuf}; - -use super::error::SolcVersionError; - -pub struct SolcVersion { - global_version_path: PathBuf -} - -impl Default for SolcVersion { - fn default() -> Self { - SolcVersion::new(Self::get_global_version_path()) - } -} - -impl SolcVersion { - pub fn new(path: impl Into) -> Self { - SolcVersion { global_version_path: path.into() } - } - - pub fn find_matching_version(&self, source: &str) -> Result { - let version_req = Self::source_version_req(source)?; - - if self.global_version_path.is_file() { - let installed_versions = Self::list_installed_versions()?; - let version = installed_versions.iter().find(|v| version_req.matches(v)); - if !version.is_none() { - return version.cloned().ok_or(SolcVersionError::ComputationFailed); - } - } - let remote_versions = Self::list_remote_versions()?; - let version = remote_versions.iter().find(|v| version_req.matches(v)); - version.cloned().ok_or(SolcVersionError::ComputationFailed) - } - - pub fn list_installed_versions() -> Result, SolcVersionError> { - Ok(svm_lib::installed_versions()?) - } - - pub fn get_global_version_path() -> PathBuf { - svm_lib::global_version_path() - } - - pub fn list_remote_versions() -> Result, SolcVersionError> { - Ok(svm_lib::blocking_all_versions()?) - } - - pub fn install_version(version: &Version) -> Result { - Ok(svm_lib::blocking_install(version)?) - } - - pub fn find_version_and_install(&self, version: &Version) -> Result { - // TODO optimize the code to only have to run it once and outside this function possibly - if self.global_version_path.is_file() { - let versions = svm_lib::installed_versions()?; - if !versions.is_empty() && versions.contains(&version) { - return Ok(svm_lib::version_path(&version.to_string()).join("solc-".to_owned() + version.to_string().as_str())); - } - } - Self::install_version(version) - } - - pub fn source_version_req(source: &str) -> Result { - let version = - utils::find_version_pragma(source).ok_or(SolcVersionError::WrongVersion)?; - Self::version_req(version.as_str()) - } - - /// Returns the corresponding SemVer version requirement for the solidity version - pub fn version_req(version: &str) -> Result { - let version = version.replace(' ', ","); - - // Somehow, Solidity semver without an operator is considered to be "exact", - // but lack of operator automatically marks the operator as Caret, so we need - // to manually patch it? :shrug: - let exact = !matches!(&version[0..1], "*" | "^" | "=" | ">" | "<" | "~"); - let mut version = VersionReq::parse(&version)?; - if exact { - version.comparators[0].op = semver::Op::Exact; - } - - Ok(version) - } - -} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/Assignment.json b/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/Assignment.json deleted file mode 100644 index ff0e828a..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/Assignment.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "id": 141, - "leftHandSide": { - "id": 139, - "name": "_baseTokenURI", - "nodeType": "Identifier", - "overloadedDeclarations": [], - "src": "2849:13:0", - "typeDescriptions": {} - }, - "nodeType": "Assignment", - "operator": "=", - "rightHandSide": { - "id": 140, - "name": "initialBaseTokenURI", - "nodeType": "Identifier", - "overloadedDeclarations": [], - "src": "2865:19:0", - "typeDescriptions": {} - }, - "src": "2849:35:0", - "typeDescriptions": {} -} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/BinaryOperation.json b/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/BinaryOperation.json deleted file mode 100644 index 16df1b7f..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/BinaryOperation.json +++ /dev/null @@ -1,42 +0,0 @@ -{ - "commonType": { - "typeIdentifier": "t_uint256", - "typeString": "uint256" - }, - "id": 18, - "isConstant": false, - "isLValue": false, - "isPure": false, - "lValueRequested": false, - "leftExpression": { - "id": 16, - "name": "a", - "nodeType": "Identifier", - "overloadedDeclarations": [], - "referencedDeclaration": 7, - "src": "203:1:0", - "typeDescriptions": { - "typeIdentifier": "t_uint256", - "typeString": "uint256" - } - }, - "nodeType": "BinaryOperation", - "operator": "&", - "rightExpression": { - "id": 17, - "name": "b", - "nodeType": "Identifier", - "overloadedDeclarations": [], - "referencedDeclaration": 11, - "src": "207:1:0", - "typeDescriptions": { - "typeIdentifier": "t_uint256", - "typeString": "uint256" - } - }, - "src": "203:5:0", - "typeDescriptions": { - "typeIdentifier": "t_uint256", - "typeString": "uint256" - } -} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/Block.json b/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/Block.json deleted file mode 100644 index 1e5d02e4..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/Block.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "id": 192, - "nodeType": "Block", - "src": "3511:148:0", - "statements": [] -} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/Break.json b/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/Break.json deleted file mode 100644 index 714cc6c5..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/Break.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "id": 7, - "nodeType": "Break", - "src": "172:5:0" -} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/Conditional.json b/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/Conditional.json deleted file mode 100644 index 14695b37..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/Conditional.json +++ /dev/null @@ -1,61 +0,0 @@ -{ - "condition": { - "hexValue": "74727565", - "id": 7, - "isConstant": false, - "isLValue": false, - "isPure": true, - "kind": "bool", - "lValueRequested": false, - "nodeType": "Literal", - "src": "158:4:0", - "typeDescriptions": { - "typeIdentifier": "t_bool", - "typeString": "bool" - }, - "value": "true" - }, - "falseExpression": { - "hexValue": "66616c7365", - "id": 9, - "isConstant": false, - "isLValue": false, - "isPure": true, - "kind": "bool", - "lValueRequested": false, - "nodeType": "Literal", - "src": "173:5:0", - "typeDescriptions": { - "typeIdentifier": "t_bool", - "typeString": "bool" - }, - "value": "false" - }, - "id": 10, - "isConstant": false, - "isLValue": false, - "isPure": true, - "lValueRequested": false, - "nodeType": "Conditional", - "src": "158:20:0", - "trueExpression": { - "hexValue": "74727565", - "id": 8, - "isConstant": false, - "isLValue": false, - "isPure": true, - "kind": "bool", - "lValueRequested": false, - "nodeType": "Literal", - "src": "166:4:0", - "typeDescriptions": { - "typeIdentifier": "t_bool", - "typeString": "bool" - }, - "value": "true" - }, - "typeDescriptions": { - "typeIdentifier": "t_bool", - "typeString": "bool" - } -} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/Continue.json b/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/Continue.json deleted file mode 100644 index ec2f7f48..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/Continue.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "id": 7, - "nodeType": "Continue", - "src": "172:8:0" -} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/ContractDefinition.json b/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/ContractDefinition.json deleted file mode 100644 index be9b8e3b..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/ContractDefinition.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "abstract": false, - "baseContracts": [], - "contractDependencies": [], - "contractKind": "contract", - "id": 427, - "name": "StartonERC721MetaTransaction", - "nameLocation": "792:28:0", - "nodeType": "ContractDefinition", - "nodes": [], - "src": "783:7774:0", - "usedErrors": [] -} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/DoWhileStatement.json b/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/DoWhileStatement.json deleted file mode 100644 index 4eb3773d..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/DoWhileStatement.json +++ /dev/null @@ -1,33 +0,0 @@ -{ - "body": { - "id": 7, - "nodeType": "Block", - "src": "148:33:0", - "statements": [ - { - "id": 6, - "nodeType": "Continue", - "src": "162:8:0" - } - ] - }, - "condition": { - "hexValue": "74727565", - "id": 8, - "isConstant": false, - "isLValue": false, - "isPure": true, - "kind": "bool", - "lValueRequested": false, - "nodeType": "Literal", - "src": "189:4:0", - "typeDescriptions": { - "typeIdentifier": "t_bool", - "typeString": "bool" - }, - "value": "true" - }, - "id": 9, - "nodeType": "DoWhileStatement", - "src": "145:50:0" -} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/ElementaryTypeName.json b/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/ElementaryTypeName.json deleted file mode 100644 index ed5e54f9..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/ElementaryTypeName.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "id": 64, - "name": "address", - "nodeType": "ElementaryTypeName", - "src": "1602:7:0", - "stateMutability": "nonpayable", - "typeDescriptions": {} -} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/ElementaryTypeNameExpression.json b/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/ElementaryTypeNameExpression.json deleted file mode 100644 index cd4213f9..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/ElementaryTypeNameExpression.json +++ /dev/null @@ -1,17 +0,0 @@ -{ - "id":10, - "nodeType":"ElementaryTypeNameExpression", - "src":"156:7:0", - "typeDescriptions":{ - - }, - "typeName":{ - "id":9, - "name":"bytes32", - "nodeType":"ElementaryTypeName", - "src":"156:7:0", - "typeDescriptions":{ - - } - } -} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/EmitStatement.json b/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/EmitStatement.json deleted file mode 100644 index fbee8ec6..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/EmitStatement.json +++ /dev/null @@ -1,42 +0,0 @@ -{ - "eventCall": { - "arguments": [ - { - "arguments": [], - "expression": { - "id": 284, - "name": "_msgSender", - "nodeType": "Identifier", - "overloadedDeclarations": [], - "src": "5557:10:0", - "typeDescriptions": {} - }, - "id": 285, - "nameLocations": [], - "names": [], - "nodeType": "FunctionCall", - "src": "5557:12:0", - "tryCall": false, - "typeDescriptions": {} - } - ], - "expression": { - "id": 283, - "name": "MetadataLocked", - "nodeType": "Identifier", - "overloadedDeclarations": [], - "src": "5542:14:0", - "typeDescriptions": {} - }, - "id": 286, - "nameLocations": [], - "names": [], - "nodeType": "FunctionCall", - "src": "5542:28:0", - "tryCall": false, - "typeDescriptions": {} - }, - "id": 287, - "nodeType": "EmitStatement", - "src": "5537:33:0" -} diff --git a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/EnumDefinition.json b/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/EnumDefinition.json deleted file mode 100644 index a52239c5..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/EnumDefinition.json +++ /dev/null @@ -1,24 +0,0 @@ -{ - "canonicalName":"Test", - "id":4, - "members":[ - { - "id":2, - "name":"item1", - "nameLocation":"72:5:0", - "nodeType":"EnumValue", - "src":"72:5:0" - }, - { - "id":3, - "name":"item2", - "nameLocation":"79:5:0", - "nodeType":"EnumValue", - "src":"79:5:0" - } - ], - "name":"Test", - "nameLocation":"66:4:0", - "nodeType":"EnumDefinition", - "src":"61:25:0" -} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/EnumValue.json b/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/EnumValue.json deleted file mode 100644 index a3e3a12c..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/EnumValue.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "id":2, - "name": "item1", - "nameLocation": "72:5:0", - "nodeType": "EnumValue", - "src": "72:5:0" -} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/ErrorDefinition.json b/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/ErrorDefinition.json deleted file mode 100644 index d93579bb..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/ErrorDefinition.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "id":3, - "name":"No", - "nameLocation":"92:2:0", - "nodeType":"ErrorDefinition", - "parameters":{ - "id":2, - "nodeType":"ParameterList", - "parameters":[ - - ], - "src":"94:2:0" - }, - "src":"86:11:0" -} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/EventDefinition.json b/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/EventDefinition.json deleted file mode 100644 index f204ee06..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/EventDefinition.json +++ /dev/null @@ -1,20 +0,0 @@ -{ - "anonymous": false, - "documentation": { - "id": 63, - "nodeType": "StructuredDocumentation", - "src": "1522:55:0", - "text": "@notice Event emitted when the minting is locked " - }, - "id": 67, - "name": "MintingLocked", - "nameLocation": "1588:13:0", - "nodeType": "EventDefinition", - "parameters": { - "id": 66, - "nodeType": "ParameterList", - "parameters": [], - "src": "1601:25:0" - }, - "src": "1582:45:0" -} diff --git a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/ExpressionStatement.json b/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/ExpressionStatement.json deleted file mode 100644 index efd8766d..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/ExpressionStatement.json +++ /dev/null @@ -1,29 +0,0 @@ -{ - "expression": { - "id": 153, - "leftHandSide": { - "id": 151, - "name": "_isMetatadataChangingAllowed", - "nodeType": "Identifier", - "overloadedDeclarations": [], - "src": "2968:28:0", - "typeDescriptions": {} - }, - "nodeType": "Assignment", - "operator": "=", - "rightHandSide": { - "hexValue": "74727565", - "id": 152, - "kind": "bool", - "nodeType": "Literal", - "src": "2999:4:0", - "typeDescriptions": {}, - "value": "true" - }, - "src": "2968:35:0", - "typeDescriptions": {} - }, - "id": 154, - "nodeType": "ExpressionStatement", - "src": "2968:35:0" -} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/ForStatement.json b/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/ForStatement.json deleted file mode 100644 index 21dfa622..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/ForStatement.json +++ /dev/null @@ -1,148 +0,0 @@ -{ - "body": { - "id": 17, - "nodeType": "Block", - "src": "175:30:0", - "statements": [ - { - "id": 16, - "nodeType": "Break", - "src": "189:5:0" - } - ] - }, - "condition": { - "commonType": { - "typeIdentifier": "t_uint256", - "typeString": "uint256" - }, - "id": 12, - "isConstant": false, - "isLValue": false, - "isPure": false, - "lValueRequested": false, - "leftExpression": { - "id": 10, - "name": "i", - "nodeType": "Identifier", - "overloadedDeclarations": [], - "referencedDeclaration": 7, - "src": "162:1:0", - "typeDescriptions": { - "typeIdentifier": "t_uint256", - "typeString": "uint256" - } - }, - "nodeType": "BinaryOperation", - "operator": "<", - "rightExpression": { - "hexValue": "3130", - "id": 11, - "isConstant": false, - "isLValue": false, - "isPure": true, - "kind": "number", - "lValueRequested": false, - "nodeType": "Literal", - "src": "166:2:0", - "typeDescriptions": { - "typeIdentifier": "t_rational_10_by_1", - "typeString": "int_const 10" - }, - "value": "10" - }, - "src": "162:6:0", - "typeDescriptions": { - "typeIdentifier": "t_bool", - "typeString": "bool" - } - }, - "id": 18, - "initializationExpression": { - "assignments": [ - 7 - ], - "declarations": [ - { - "constant": false, - "id": 7, - "mutability": "mutable", - "name": "i", - "nameLocation": "155:1:0", - "nodeType": "VariableDeclaration", - "scope": 18, - "src": "150:6:0", - "stateVariable": false, - "storageLocation": "default", - "typeDescriptions": { - "typeIdentifier": "t_uint256", - "typeString": "uint256" - }, - "typeName": { - "id": 6, - "name": "uint", - "nodeType": "ElementaryTypeName", - "src": "150:4:0", - "typeDescriptions": { - "typeIdentifier": "t_uint256", - "typeString": "uint256" - } - }, - "visibility": "internal" - } - ], - "id": 9, - "initialValue": { - "hexValue": "30", - "id": 8, - "isConstant": false, - "isLValue": false, - "isPure": true, - "kind": "number", - "lValueRequested": false, - "nodeType": "Literal", - "src": "159:1:0", - "typeDescriptions": { - "typeIdentifier": "t_rational_0_by_1", - "typeString": "int_const 0" - }, - "value": "0" - }, - "nodeType": "VariableDeclarationStatement", - "src": "150:10:0" - }, - "loopExpression": { - "expression": { - "id": 14, - "isConstant": false, - "isLValue": false, - "isPure": false, - "lValueRequested": false, - "nodeType": "UnaryOperation", - "operator": "++", - "prefix": false, - "src": "170:3:0", - "subExpression": { - "id": 13, - "name": "i", - "nodeType": "Identifier", - "overloadedDeclarations": [], - "referencedDeclaration": 7, - "src": "170:1:0", - "typeDescriptions": { - "typeIdentifier": "t_uint256", - "typeString": "uint256" - } - }, - "typeDescriptions": { - "typeIdentifier": "t_uint256", - "typeString": "uint256" - } - }, - "id": 15, - "nodeType": "ExpressionStatement", - "src": "170:3:0" - }, - "nodeType": "ForStatement", - "src": "145:60:0" -} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/FunctionCall.json b/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/FunctionCall.json deleted file mode 100644 index 391d05f5..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/FunctionCall.json +++ /dev/null @@ -1,39 +0,0 @@ -{ - "arguments": [ - { - "hexValue": "4d696e74696e67206973206c6f636b6564", - "id": 76, - "name": "_isMintAllowed", - "nodeType": "Identifier", - "overloadedDeclarations": [], - "src": "1858:14:0", - "typeDescriptions": {}, - "kind": "string", - "value": "Minting is locked" - }, - { - "hexValue": "4d696e74696e67206973206c6f636b6564", - "id": 77, - "kind": "string", - "nodeType": "Literal", - "src": "1874:19:0", - "typeDescriptions": {}, - "value": "Minting is locked" - } - ], - "expression": { - "id": 75, - "name": "require", - "nodeType": "Identifier", - "overloadedDeclarations": [], - "src": "1850:7:0", - "typeDescriptions": {} - }, - "id": 78, - "nameLocations": [], - "names": [], - "nodeType": "FunctionCall", - "src": "1850:44:0", - "tryCall": false, - "typeDescriptions": {} -} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/FunctionCallOptions.json b/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/FunctionCallOptions.json deleted file mode 100644 index 64fe122e..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/FunctionCallOptions.json +++ /dev/null @@ -1,57 +0,0 @@ -{ - "expression":{ - "expression":{ - "id":16, - "name":"otherContract", - "nodeType":"Identifier", - "overloadedDeclarations":[ - - ], - "src":"236:13:0", - "typeDescriptions":{ - - } - }, - "id":18, - "memberLocation":"250:4:0", - "memberName":"call", - "nodeType":"MemberAccess", - "src":"236:18:0", - "typeDescriptions":{ - - } - }, - "id":21, - "names":[ - "value" - ], - "nodeType":"FunctionCallOptions", - "options":[ - { - "expression":{ - "id":19, - "name":"msg", - "nodeType":"Identifier", - "overloadedDeclarations":[ - - ], - "src":"262:3:0", - "typeDescriptions":{ - - } - }, - "id":20, - "memberLocation":"266:5:0", - "memberName":"value", - "nodeType":"MemberAccess", - "src":"262:9:0", - "typeDescriptions":{ - - } - } - ], - "src":"236:36:0", - "typeDescriptions":{ - - } -} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/FunctionDefinition.json b/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/FunctionDefinition.json deleted file mode 100644 index 3158d372..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/FunctionDefinition.json +++ /dev/null @@ -1,31 +0,0 @@ -{ - "body": { - "id": 192, - "nodeType": "Block", - "src": "3511:148:0", - "statements": [] - }, - "id": 193, - "implemented": true, - "kind": "function", - "modifiers": [], - "name": "mint", - "nameLocation": "3401:4:0", - "nodeType": "FunctionDefinition", - "parameters": { - "id": 166, - "nodeType": "ParameterList", - "parameters": [], - "src": "3405:31:0" - }, - "returnParameters": { - "id": 172, - "nodeType": "ParameterList", - "parameters": [], - "src": "3511:0:0" - }, - "src": "3392:267:0", - "stateMutability": "nonpayable", - "virtual": false, - "visibility": "public" -} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/FunctionTypeName.json b/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/FunctionTypeName.json deleted file mode 100644 index 2a593dc7..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/FunctionTypeName.json +++ /dev/null @@ -1,72 +0,0 @@ -{ - "id":11, - "nodeType":"FunctionTypeName", - "parameterTypes":{ - "id":7, - "nodeType":"ParameterList", - "parameters":[ - { - "constant":false, - "id":6, - "mutability":"mutable", - "name":"", - "nameLocation":"-1:-1:-1", - "nodeType":"VariableDeclaration", - "src":"270:4:0", - "stateVariable":false, - "storageLocation":"default", - "typeDescriptions":{ - - }, - "typeName":{ - "id":5, - "name":"uint", - "nodeType":"ElementaryTypeName", - "src":"270:4:0", - "typeDescriptions":{ - - } - }, - "visibility":"internal" - } - ], - "src":"269:6:0" - }, - "returnParameterTypes":{ - "id":10, - "nodeType":"ParameterList", - "parameters":[ - { - "constant":false, - "id":9, - "mutability":"mutable", - "name":"", - "nameLocation":"-1:-1:-1", - "nodeType":"VariableDeclaration", - "src":"290:4:0", - "stateVariable":false, - "storageLocation":"default", - "typeDescriptions":{ - - }, - "typeName":{ - "id":8, - "name":"uint", - "nodeType":"ElementaryTypeName", - "src":"290:4:0", - "typeDescriptions":{ - - } - }, - "visibility":"internal" - } - ], - "src":"289:6:0" - }, - "src":"260:37:0", - "stateMutability":"pure", - "typeDescriptions":{ - - }, - "visibility":"internal" -} diff --git a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/Identifier.json b/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/Identifier.json deleted file mode 100644 index b2a4cccb..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/Identifier.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "id": 16, - "name": "a", - "nodeType": "Identifier", - "overloadedDeclarations": [], - "referencedDeclaration": 7, - "src": "203:1:0", - "typeDescriptions": { - "typeIdentifier": "t_uint256", - "typeString": "uint256" - } -} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/IfStatement.json b/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/IfStatement.json deleted file mode 100644 index aee61e1c..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/IfStatement.json +++ /dev/null @@ -1,167 +0,0 @@ -{ - "condition":{ - "commonType":{ - "typeIdentifier":"t_uint8", - "typeString":"uint8" - }, - "id":8, - "isConstant":false, - "isLValue":false, - "isPure":true, - "lValueRequested":false, - "leftExpression":{ - "hexValue":"31", - "id":6, - "isConstant":false, - "isLValue":false, - "isPure":true, - "kind":"number", - "lValueRequested":false, - "nodeType":"Literal", - "src":"149:1:0", - "typeDescriptions":{ - "typeIdentifier":"t_rational_1_by_1", - "typeString":"int_const 1" - }, - "value":"1" - }, - "nodeType":"BinaryOperation", - "operator":"==", - "rightExpression":{ - "hexValue":"32", - "id":7, - "isConstant":false, - "isLValue":false, - "isPure":true, - "kind":"number", - "lValueRequested":false, - "nodeType":"Literal", - "src":"154:1:0", - "typeDescriptions":{ - "typeIdentifier":"t_rational_2_by_1", - "typeString":"int_const 2" - }, - "value":"2" - }, - "src":"149:6:0", - "typeDescriptions":{ - "typeIdentifier":"t_bool", - "typeString":"bool" - } - }, - "falseBody":{ - "id":18, - "nodeType":"Block", - "src":"205:43:0", - "statements":[ - { - "expression":{ - "id":16, - "isConstant":false, - "isLValue":false, - "isPure":false, - "lValueRequested":false, - "leftHandSide":{ - "id":14, - "name":"goStraight", - "nodeType":"Identifier", - "overloadedDeclarations":[ - - ], - "referencedDeclaration":3, - "src":"219:10:0", - "typeDescriptions":{ - "typeIdentifier":"t_bool", - "typeString":"bool" - } - }, - "nodeType":"Assignment", - "operator":"=", - "rightHandSide":{ - "hexValue":"66616c7365", - "id":15, - "isConstant":false, - "isLValue":false, - "isPure":true, - "kind":"bool", - "lValueRequested":false, - "nodeType":"Literal", - "src":"232:5:0", - "typeDescriptions":{ - "typeIdentifier":"t_bool", - "typeString":"bool" - }, - "value":"false" - }, - "src":"219:18:0", - "typeDescriptions":{ - "typeIdentifier":"t_bool", - "typeString":"bool" - } - }, - "id":17, - "nodeType":"ExpressionStatement", - "src":"219:18:0" - } - ] - }, - "id":19, - "nodeType":"IfStatement", - "src":"145:103:0", - "trueBody":{ - "id":13, - "nodeType":"Block", - "src":"157:42:0", - "statements":[ - { - "expression":{ - "id":11, - "isConstant":false, - "isLValue":false, - "isPure":false, - "lValueRequested":false, - "leftHandSide":{ - "id":9, - "name":"goStraight", - "nodeType":"Identifier", - "overloadedDeclarations":[ - - ], - "referencedDeclaration":3, - "src":"171:10:0", - "typeDescriptions":{ - "typeIdentifier":"t_bool", - "typeString":"bool" - } - }, - "nodeType":"Assignment", - "operator":"=", - "rightHandSide":{ - "hexValue":"74727565", - "id":10, - "isConstant":false, - "isLValue":false, - "isPure":true, - "kind":"bool", - "lValueRequested":false, - "nodeType":"Literal", - "src":"184:4:0", - "typeDescriptions":{ - "typeIdentifier":"t_bool", - "typeString":"bool" - }, - "value":"true" - }, - "src":"171:17:0", - "typeDescriptions":{ - "typeIdentifier":"t_bool", - "typeString":"bool" - } - }, - "id":12, - "nodeType":"ExpressionStatement", - "src":"171:17:0" - } - ] - } -} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/ImportDirective.json b/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/ImportDirective.json deleted file mode 100644 index 64f0a282..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/ImportDirective.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "absolutePath": "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol", - "file": "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol", - "id": 2, - "nameLocation": "-1:-1:-1", - "nodeType": "ImportDirective", - "src": "58:78:0", - "symbolAliases": [], - "unitAlias": "" -} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/IndexAccess.json b/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/IndexAccess.json deleted file mode 100644 index aa2ea6d6..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/IndexAccess.json +++ /dev/null @@ -1,32 +0,0 @@ -{ - "baseExpression":{ - "id":12, - "name":"pls", - "nodeType":"Identifier", - "overloadedDeclarations":[ - - ], - "src":"203:3:0", - "typeDescriptions":{ - - } - }, - "id":14, - "indexExpression":{ - "id":13, - "name":"index", - "nodeType":"Identifier", - "overloadedDeclarations":[ - - ], - "src":"207:5:0", - "typeDescriptions":{ - - } - }, - "nodeType":"IndexAccess", - "src":"203:10:0", - "typeDescriptions":{ - - } -} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/IndexRangeAccess.json b/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/IndexRangeAccess.json deleted file mode 100644 index 6799147e..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/IndexRangeAccess.json +++ /dev/null @@ -1,31 +0,0 @@ -{ - "baseExpression":{ - "id":10, - "name":"cheh", - "nodeType":"Identifier", - "overloadedDeclarations":[ - - ], - "src":"174:4:0", - "typeDescriptions":{ - - } - }, - "endExpression":{ - "hexValue":"33", - "id":11, - "kind":"number", - "nodeType":"Literal", - "src":"180:1:0", - "typeDescriptions":{ - - }, - "value":"3" - }, - "id":12, - "nodeType":"IndexRangeAccess", - "src":"174:8:0", - "typeDescriptions":{ - - } -} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/InheritanceSpecifier.json b/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/InheritanceSpecifier.json deleted file mode 100644 index 4d7ace84..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/InheritanceSpecifier.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "baseName": { - "id": 12, - "name": "ERC721Enumerable", - "nameLocations": [ - "828:16:0" - ], - "nodeType": "IdentifierPath", - "src": "828:16:0" - }, - "id": 13, - "nodeType": "InheritanceSpecifier", - "src": "828:16:0" -} diff --git a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/InlineAssembly.json b/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/InlineAssembly.json deleted file mode 100644 index 57caaa4e..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/InlineAssembly.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "AST":{ - - }, - "evmVersion":"london", - "externalReferences":[ - - ], - "id":9, - "nodeType":"InlineAssembly", - "src":"176:50:0" -} diff --git a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/Literal.json b/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/Literal.json deleted file mode 100644 index 2485266e..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/Literal.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "hexValue": "4d696e74696e67206973206c6f636b6564", - "id": 77, - "kind": "string", - "nodeType": "Literal", - "src": "1874:19:0", - "typeDescriptions": {}, - "value": "Minting is locked" -} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/MemberAccess.json b/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/MemberAccess.json deleted file mode 100644 index bd935f36..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/MemberAccess.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "expression": { - "id": 175, - "name": "_tokenIdCounter", - "nodeType": "Identifier", - "overloadedDeclarations": [], - "src": "3535:15:0", - "typeDescriptions": {} - }, - "id": 176, - "memberLocation": "3551:7:0", - "memberName": "current", - "nodeType": "MemberAccess", - "src": "3535:23:0", - "typeDescriptions": {} -} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/ModifierDefinition.json b/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/ModifierDefinition.json deleted file mode 100644 index 033a6ecf..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/ModifierDefinition.json +++ /dev/null @@ -1,74 +0,0 @@ -{ - "body": { - "id": 91, - "nodeType": "Block", - "src": "2015:75:0", - "statements": [ - { - "expression": { - "arguments": [ - { - "id": 86, - "name": "_isMintAllowed", - "nodeType": "Identifier", - "overloadedDeclarations": [], - "src": "2033:14:0", - "typeDescriptions": {} - }, - { - "hexValue": "4d657461646174617320617265206c6f636b6564", - "id": 87, - "kind": "string", - "nodeType": "Literal", - "src": "2049:22:0", - "typeDescriptions": {}, - "value": "Metadatas are locked" - } - ], - "expression": { - "id": 85, - "name": "require", - "nodeType": "Identifier", - "overloadedDeclarations": [], - "src": "2025:7:0", - "typeDescriptions": {} - }, - "id": 88, - "nameLocations": [], - "names": [], - "nodeType": "FunctionCall", - "src": "2025:47:0", - "tryCall": false, - "typeDescriptions": {} - }, - "id": 89, - "nodeType": "ExpressionStatement", - "src": "2025:47:0" - }, - { - "id": 90, - "nodeType": "PlaceholderStatement", - "src": "2082:1:0" - } - ] - }, - "documentation": { - "id": 83, - "nodeType": "StructuredDocumentation", - "src": "1918:63:0", - "text": "@dev Modifier that reverts when the metadatas are locked " - }, - "id": 92, - "name": "metadataNotLocked", - "nameLocation": "1995:17:0", - "nodeType": "ModifierDefinition", - "parameters": { - "id": 84, - "nodeType": "ParameterList", - "parameters": [], - "src": "2012:2:0" - }, - "src": "1986:104:0", - "virtual": false, - "visibility": "internal" -} diff --git a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/ModifierInvocation.json b/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/ModifierInvocation.json deleted file mode 100644 index 3e8f4eee..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/ModifierInvocation.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "id": 274, - "modifierName": { - "id": 273, - "name": "whenNotPaused", - "nameLocations": [ - "5445:13:0" - ], - "nodeType": "IdentifierPath", - "src": "5445:13:0" - }, - "nodeType": "ModifierInvocation", - "src": "5445:13:0" -} diff --git a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/NewExpression.json b/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/NewExpression.json deleted file mode 100644 index fb6494e7..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/NewExpression.json +++ /dev/null @@ -1,25 +0,0 @@ -{ - "id":7, - "nodeType":"NewExpression", - "src":"131:6:0", - "typeDescriptions":{ - - }, - "typeName":{ - "id":6, - "nodeType":"UserDefinedTypeName", - "pathNode":{ - "id":5, - "name":"Ok", - "nameLocations":[ - "135:2:0" - ], - "nodeType":"IdentifierPath", - "src":"135:2:0" - }, - "src":"135:2:0", - "typeDescriptions":{ - - } - } -} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/ParameterList.json b/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/ParameterList.json deleted file mode 100644 index 6b6abd8b..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/ParameterList.json +++ /dev/null @@ -1,27 +0,0 @@ -{ - "id": 197, - "nodeType": "ParameterList", - "parameters": [ - { - "constant": false, - "id": 196, - "mutability": "mutable", - "name": "newContractURI", - "nameLocation": "3919:14:0", - "nodeType": "VariableDeclaration", - "src": "3905:28:0", - "stateVariable": false, - "storageLocation": "memory", - "typeDescriptions": {}, - "typeName": { - "id": 195, - "name": "string", - "nodeType": "ElementaryTypeName", - "src": "3905:6:0", - "typeDescriptions": {} - }, - "visibility": "internal" - } - ], - "src": "3904:30:0" -} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/PlaceholderStatement.json b/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/PlaceholderStatement.json deleted file mode 100644 index b2c8fac2..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/PlaceholderStatement.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "id": 80, - "nodeType": "PlaceholderStatement", - "src": "1904:1:0" -} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/PragmaDirective.json b/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/PragmaDirective.json deleted file mode 100644 index 88b94d2e..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/PragmaDirective.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "id": 1, - "literals": [ - "solidity", - "0.8", - ".16" - ], - "nodeType": "PragmaDirective", - "src": "33:23:0" -} diff --git a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/Return.json b/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/Return.json deleted file mode 100644 index 94882bcc..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/Return.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "expression": { - "id": 295, - "name": "_contractURI", - "nodeType": "Identifier", - "overloadedDeclarations": [], - "src": "5768:12:0", - "typeDescriptions": {} - }, - "id": 296, - "nodeType": "Return", - "src": "5761:19:0" -} diff --git a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/RevertStatement.json b/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/RevertStatement.json deleted file mode 100644 index 674afc60..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/RevertStatement.json +++ /dev/null @@ -1,36 +0,0 @@ -{ - "errorCall": { - "arguments": [], - "expression": { - "argumentTypes": [], - "id": 6, - "name": "Pls", - "nodeType": "Identifier", - "overloadedDeclarations": [], - "referencedDeclaration": 3, - "src": "125:3:0", - "typeDescriptions": { - "typeIdentifier": "t_function_error_pure$__$returns$__$", - "typeString": "function () pure" - } - }, - "id": 7, - "isConstant": false, - "isLValue": false, - "isPure": false, - "kind": "functionCall", - "lValueRequested": false, - "nameLocations": [], - "names": [], - "nodeType": "FunctionCall", - "src": "125:5:0", - "tryCall": false, - "typeDescriptions": { - "typeIdentifier": "t_tuple$__$", - "typeString": "tuple()" - } - }, - "id": 8, - "nodeType": "RevertStatement", - "src": "118:12:0" -} diff --git a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/SourceUnit.json b/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/SourceUnit.json deleted file mode 100644 index cfb0f0b2..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/SourceUnit.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "absolutePath": "wow.sol", - "id": 428, - "license": "MIT", - "nodeType": "SourceUnit", - "nodes": [], - "src": "33:8524:0" -} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/StructDefinition.json b/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/StructDefinition.json deleted file mode 100644 index c28ab437..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/StructDefinition.json +++ /dev/null @@ -1,66 +0,0 @@ -{ - "canonicalName": "S", - "id": 6, - "members": [ - { - "constant": false, - "id": 3, - "mutability": "mutable", - "name": "x", - "nameLocation": "82:1:0", - "nodeType": "VariableDeclaration", - "scope": 6, - "src": "77:6:0", - "stateVariable": false, - "storageLocation": "default", - "typeDescriptions": { - "typeIdentifier": "t_uint256", - "typeString": "uint256" - }, - "typeName": { - "id": 2, - "name": "uint", - "nodeType": "ElementaryTypeName", - "src": "77:4:0", - "typeDescriptions": { - "typeIdentifier": "t_uint256", - "typeString": "uint256" - } - }, - "visibility": "internal" - }, - { - "constant": false, - "id": 5, - "mutability": "mutable", - "name": "y", - "nameLocation": "94:1:0", - "nodeType": "VariableDeclaration", - "scope": 6, - "src": "89:6:0", - "stateVariable": false, - "storageLocation": "default", - "typeDescriptions": { - "typeIdentifier": "t_uint256", - "typeString": "uint256" - }, - "typeName": { - "id": 4, - "name": "uint", - "nodeType": "ElementaryTypeName", - "src": "89:4:0", - "typeDescriptions": { - "typeIdentifier": "t_uint256", - "typeString": "uint256" - } - }, - "visibility": "internal" - } - ], - "name": "S", - "nameLocation": "69:1:0", - "nodeType": "StructDefinition", - "scope": 27, - "src": "62:36:0", - "visibility": "public" -} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/StructuredDocumentation.json b/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/StructuredDocumentation.json deleted file mode 100644 index 9263e3f1..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/StructuredDocumentation.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "id": 63, - "nodeType": "StructuredDocumentation", - "src": "1522:55:0", - "text": "@notice Event emitted when the minting is locked " -} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/TryStatement.json b/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/TryStatement.json deleted file mode 100644 index 6cda0cc1..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/TryStatement.json +++ /dev/null @@ -1,244 +0,0 @@ -{ - "clauses": [ - { - "block": { - "id": 87, - "nodeType": "Block", - "src": "1026:41:0", - "statements": [ - { - "eventCall": { - "arguments": [ - { - "id": 84, - "name": "result", - "nodeType": "Identifier", - "overloadedDeclarations": [], - "referencedDeclaration": 81, - "src": "1049:6:0", - "typeDescriptions": { - "typeIdentifier": "t_string_memory_ptr", - "typeString": "string memory" - } - } - ], - "expression": { - "argumentTypes": [ - { - "typeIdentifier": "t_string_memory_ptr", - "typeString": "string memory" - } - ], - "id": 83, - "name": "Log", - "nodeType": "Identifier", - "overloadedDeclarations": [], - "referencedDeclaration": 51, - "src": "1045:3:0", - "typeDescriptions": { - "typeIdentifier": "t_function_event_nonpayable$_t_string_memory_ptr_$returns$__$", - "typeString": "function (string memory)" - } - }, - "id": 85, - "isConstant": false, - "isLValue": false, - "isPure": false, - "kind": "functionCall", - "lValueRequested": false, - "nameLocations": [], - "names": [], - "nodeType": "FunctionCall", - "src": "1045:11:0", - "tryCall": false, - "typeDescriptions": { - "typeIdentifier": "t_tuple$__$", - "typeString": "tuple()" - } - }, - "id": 86, - "nodeType": "EmitStatement", - "src": "1040:16:0" - } - ] - }, - "errorName": "", - "id": 88, - "nodeType": "TryCatchClause", - "parameters": { - "id": 82, - "nodeType": "ParameterList", - "parameters": [ - { - "constant": false, - "id": 81, - "mutability": "mutable", - "name": "result", - "nameLocation": "1018:6:0", - "nodeType": "VariableDeclaration", - "scope": 88, - "src": "1004:20:0", - "stateVariable": false, - "storageLocation": "memory", - "typeDescriptions": { - "typeIdentifier": "t_string_memory_ptr", - "typeString": "string" - }, - "typeName": { - "id": 80, - "name": "string", - "nodeType": "ElementaryTypeName", - "src": "1004:6:0", - "typeDescriptions": { - "typeIdentifier": "t_string_storage_ptr", - "typeString": "string" - } - }, - "visibility": "internal" - } - ], - "src": "1003:22:0" - }, - "src": "995:72:0" - }, - { - "block": { - "id": 93, - "nodeType": "Block", - "src": "1074:57:0", - "statements": [ - { - "eventCall": { - "arguments": [ - { - "hexValue": "65787465726e616c2063616c6c206661696c6564", - "id": 90, - "isConstant": false, - "isLValue": false, - "isPure": true, - "kind": "string", - "lValueRequested": false, - "nodeType": "Literal", - "src": "1097:22:0", - "typeDescriptions": { - "typeIdentifier": "t_stringliteral_e0e3f7224ce5e493e1d6652b2633dc31e8929d0dd88f4f97ce03f976e5157351", - "typeString": "literal_string \"external call failed\"" - }, - "value": "external call failed" - } - ], - "expression": { - "argumentTypes": [ - { - "typeIdentifier": "t_stringliteral_e0e3f7224ce5e493e1d6652b2633dc31e8929d0dd88f4f97ce03f976e5157351", - "typeString": "literal_string \"external call failed\"" - } - ], - "id": 89, - "name": "Log", - "nodeType": "Identifier", - "overloadedDeclarations": [], - "referencedDeclaration": 51, - "src": "1093:3:0", - "typeDescriptions": { - "typeIdentifier": "t_function_event_nonpayable$_t_string_memory_ptr_$returns$__$", - "typeString": "function (string memory)" - } - }, - "id": 91, - "isConstant": false, - "isLValue": false, - "isPure": false, - "kind": "functionCall", - "lValueRequested": false, - "nameLocations": [], - "names": [], - "nodeType": "FunctionCall", - "src": "1093:27:0", - "tryCall": false, - "typeDescriptions": { - "typeIdentifier": "t_tuple$__$", - "typeString": "tuple()" - } - }, - "id": 92, - "nodeType": "EmitStatement", - "src": "1088:32:0" - } - ] - }, - "errorName": "", - "id": 94, - "nodeType": "TryCatchClause", - "src": "1068:63:0" - } - ], - "externalCall": { - "arguments": [ - { - "id": 78, - "name": "_i", - "nodeType": "Identifier", - "overloadedDeclarations": [], - "referencedDeclaration": 73, - "src": "991:2:0", - "typeDescriptions": { - "typeIdentifier": "t_uint256", - "typeString": "uint256" - } - } - ], - "expression": { - "argumentTypes": [ - { - "typeIdentifier": "t_uint256", - "typeString": "uint256" - } - ], - "expression": { - "id": 76, - "name": "foo", - "nodeType": "Identifier", - "overloadedDeclarations": [], - "referencedDeclaration": 58, - "src": "980:3:0", - "typeDescriptions": { - "typeIdentifier": "t_contract$_Foo_$47", - "typeString": "contract Foo" - } - }, - "id": 77, - "isConstant": false, - "isLValue": false, - "isPure": false, - "lValueRequested": false, - "memberLocation": "984:6:0", - "memberName": "myFunc", - "nodeType": "MemberAccess", - "referencedDeclaration": 46, - "src": "980:10:0", - "typeDescriptions": { - "typeIdentifier": "t_function_external_pure$_t_uint256_$returns$_t_string_memory_ptr_$", - "typeString": "function (uint256) pure external returns (string memory)" - } - }, - "id": 79, - "isConstant": false, - "isLValue": false, - "isPure": false, - "kind": "functionCall", - "lValueRequested": false, - "nameLocations": [], - "names": [], - "nodeType": "FunctionCall", - "src": "980:14:0", - "tryCall": true, - "typeDescriptions": { - "typeIdentifier": "t_string_memory_ptr", - "typeString": "string memory" - } - }, - "id": 95, - "nodeType": "TryStatement", - "src": "976:155:0" -} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/TupleExpression.json b/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/TupleExpression.json deleted file mode 100644 index 83397dc2..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/TupleExpression.json +++ /dev/null @@ -1,44 +0,0 @@ -{ - "components":[ - { - "hexValue":"37", - "id":13, - "kind":"number", - "nodeType":"Literal", - "src":"151:1:0", - "typeDescriptions":{ - - }, - "value":"7" - }, - { - "hexValue":"74727565", - "id":14, - "kind":"bool", - "nodeType":"Literal", - "src":"154:4:0", - "typeDescriptions":{ - - }, - "value":"true" - }, - { - "hexValue":"32", - "id":15, - "kind":"number", - "nodeType":"Literal", - "src":"160:1:0", - "typeDescriptions":{ - - }, - "value":"2" - } - ], - "id":16, - "isInlineArray":false, - "nodeType":"TupleExpression", - "src":"150:12:0", - "typeDescriptions":{ - - } -} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/TypeDescriptions.json b/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/TypeDescriptions.json deleted file mode 100644 index 622d7e8f..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/TypeDescriptions.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "typeIdentifier": "t_bool", - "typeString": "bool" -} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/UnaryOperation.json b/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/UnaryOperation.json deleted file mode 100644 index 84ecbd0f..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/UnaryOperation.json +++ /dev/null @@ -1,22 +0,0 @@ -{ - "id":7, - "nodeType":"UnaryOperation", - "operator":"++", - "prefix":false, - "src":"104:6:0", - "subExpression":{ - "id":6, - "name":"test", - "nodeType":"Identifier", - "overloadedDeclarations":[ - - ], - "src":"104:4:0", - "typeDescriptions":{ - - } - }, - "typeDescriptions":{ - - } -} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/UncheckedBlock.json b/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/UncheckedBlock.json deleted file mode 100644 index e3be941a..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/UncheckedBlock.json +++ /dev/null @@ -1,34 +0,0 @@ -{ - "id":9, - "nodeType":"UncheckedBlock", - "src":"104:21:0", - "statements":[ - { - "expression":{ - "id":7, - "nodeType":"UnaryOperation", - "operator":"++", - "prefix":false, - "src":"116:6:0", - "subExpression":{ - "id":6, - "name":"test", - "nodeType":"Identifier", - "overloadedDeclarations":[ - - ], - "src":"116:4:0", - "typeDescriptions":{ - - } - }, - "typeDescriptions":{ - - } - }, - "id":8, - "nodeType":"ExpressionStatement", - "src":"116:6:0" - } - ] -} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/UserDefinedValueTypeDefinition.json b/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/UserDefinedValueTypeDefinition.json deleted file mode 100644 index 6177c970..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/UserDefinedValueTypeDefinition.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "id":3, - "name":"UFixed256x18", - "nameLocation":"160:12:0", - "nodeType":"UserDefinedValueTypeDefinition", - "src":"155:29:0", - "underlyingType":{ - "id":2, - "name":"uint256", - "nodeType":"ElementaryTypeName", - "src":"176:7:0", - "typeDescriptions":{ - - } - } -} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/UsingForDirective.json b/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/UsingForDirective.json deleted file mode 100644 index 2e2090fb..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/UsingForDirective.json +++ /dev/null @@ -1,31 +0,0 @@ -{ - "global": false, - "id": 31, - "libraryName": { - "id": 28, - "name": "Counters", - "nameLocations": [ - "1013:8:0" - ], - "nodeType": "IdentifierPath", - "src": "1013:8:0" - }, - "nodeType": "UsingForDirective", - "src": "1007:36:0", - "typeName": { - "id": 30, - "nodeType": "UserDefinedTypeName", - "pathNode": { - "id": 29, - "name": "Counters.Counter", - "nameLocations": [ - "1026:8:0", - "1035:7:0" - ], - "nodeType": "IdentifierPath", - "src": "1026:16:0" - }, - "src": "1026:16:0", - "typeDescriptions": {} - } -} diff --git a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/VariableDeclaration.json b/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/VariableDeclaration.json deleted file mode 100644 index d283e5b1..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/VariableDeclaration.json +++ /dev/null @@ -1,20 +0,0 @@ -{ - "constant": false, - "id": 293, - "mutability": "mutable", - "name": "", - "nameLocation": "-1:-1:-1", - "nodeType": "VariableDeclaration", - "src": "5736:13:0", - "stateVariable": false, - "storageLocation": "memory", - "typeDescriptions": {}, - "typeName": { - "id": 292, - "name": "string", - "nodeType": "ElementaryTypeName", - "src": "5736:6:0", - "typeDescriptions": {} - }, - "visibility": "internal" -} diff --git a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/WhileStatement.json b/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/WhileStatement.json deleted file mode 100644 index 2aa8ae02..00000000 --- a/toolchains/solidity/linter/core/solc-wrapper/tests/files/ast/WhileStatement.json +++ /dev/null @@ -1,33 +0,0 @@ -{ - "body": { - "id": 8, - "nodeType": "Block", - "src": "158:33:0", - "statements": [ - { - "id": 7, - "nodeType": "Continue", - "src": "172:8:0" - } - ] - }, - "condition": { - "hexValue": "74727565", - "id": 6, - "isConstant": false, - "isLValue": false, - "isPure": true, - "kind": "bool", - "lValueRequested": false, - "nodeType": "Literal", - "src": "152:4:0", - "typeDescriptions": { - "typeIdentifier": "t_bool", - "typeString": "bool" - }, - "value": "true" - }, - "id": 9, - "nodeType": "WhileStatement", - "src": "145:46:0" -} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solidhunter-lib/Cargo.toml b/toolchains/solidity/linter/core/solidhunter-lib/Cargo.toml index e128998e..ea598b75 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/Cargo.toml +++ b/toolchains/solidity/linter/core/solidhunter-lib/Cargo.toml @@ -9,8 +9,8 @@ license = "GPL-3.0-or-later" clap = { version = "4.0.29", features = ["derive"] } colored = "2" serde = { version = "1.0.149", features = ["derive"] } -solc-wrapper = { path = "../solc-wrapper" } serde_json = "1.0.89" anyhow = "1.0" glob = "0.3.0" thiserror = "1.0" +ast-extractor = { path = "../../../../../libs/ast-extractor" } diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/error.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/error.rs index 49e0d7a3..b7489377 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/error.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/error.rs @@ -3,8 +3,8 @@ use thiserror::Error; #[derive(Error, Debug)] pub enum SolidHunterError { // Linter errors - #[error("SolidHunterError: Solc error occured")] - SolcError(#[from] solc_wrapper::SolcError), + #[error("SolidHunterError: Ast Extractor error occured")] + AstError(#[from] ast_extractor::errors::ExtractError), #[error("SolidHunterError: Something went wrong with the file during parsing")] ParsingError(#[from] std::io::Error), #[error("SolidHunterError: Serde error occured")] diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs index 159a6616..6331b93a 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs @@ -1,4 +1,3 @@ -use crate::error::SolidHunterError; use crate::rules::factory::RuleFactory; use crate::rules::rule_impl::{create_rules_file, parse_rules}; use crate::rules::types::*; @@ -6,10 +5,9 @@ use crate::types::*; use std::fs; use glob::glob; -use solc_wrapper::{Solc, SourceUnit}; pub struct SolidFile { - pub data: SourceUnit, + pub data: ast_extractor::File, pub path: String, pub content: String, } @@ -63,41 +61,33 @@ impl SolidLinter { false } - fn _update_file_ast(&mut self, path: &str, ast: SourceUnit) { - for file in &mut self.files { - if file.path == path { - file.data = ast.clone(); + fn _add_file(&mut self, path: &str, ast: ast_extractor::File, content: &str) { + if self._file_exists(path) { + for file in &mut self.files { + if file.path == path { + file.data = ast.clone(); + file.content = String::from(content); + } } + } else { + let file = SolidFile { + data: ast, + path: String::from(path), + content: String::from(content), + }; + self.files.push(file); } } - fn _add_file(&mut self, path: &str, ast: SourceUnit, content: &str) { - let file = SolidFile { - data: ast, - path: String::from(path), - content: String::from(content), - }; - self.files.push(file); - } - pub fn parse_file(&mut self, filepath: String) -> LintResult { - let res = Solc::default().extract_ast_file(filepath.clone()); - - if let Err(res) = res { - println!("{:?}", res); - return Err(SolidHunterError::SolcError(res)); - } - if self._file_exists(filepath.as_str()) { - self._update_file_ast(filepath.as_str(), res.expect("ast not found")); - } else { - let content = fs::read_to_string(filepath.clone()) - .map_err(|e| SolidHunterError::IoError(e))?; - self._add_file( - filepath.as_str(), - res.expect("ast not found"), - content.as_str(), - ); - } + let content = fs::read_to_string(filepath.clone())?; + let res = ast_extractor::extract::extract_ast_from_content(content)?; + + self._add_file( + filepath.as_str(), + res, + content.as_str(), + ); let mut res: Vec = Vec::new(); for rule in &self.rules { @@ -108,23 +98,13 @@ impl SolidLinter { } pub fn parse_content(&mut self, filepath: String, content: &String) -> LintResult { - let res = Solc::default().extract_ast_content(content.to_string()); - - if let Err(res) = res { - println!("{:?}", res); - return Err(SolidHunterError::SolcError(res)); - } - - if self._file_exists(filepath.as_str()) { - self._update_file_ast(filepath.as_str(), res.expect("ast not found")); - } else { - self._add_file( - filepath.as_str(), - res.expect("ast not found"), - content.as_str(), - ); - } + let res = extract::extract_ast_from_content(content.to_string())?; + self._add_file( + filepath.as_str(), + res, + content.as_str(), + ); let mut res: Vec = Vec::new(); for rule in &self.rules { From 40fd9063cc22523f309c894ca20ccec7384c6c49 Mon Sep 17 00:00:00 2001 From: 0xtekgrinder <0xtekgrinder@protonmail.com> Date: Thu, 14 Sep 2023 19:51:06 -0400 Subject: [PATCH 27/59] refactor(ast-extractor): change mod.rd into retriever.rs --- libs/ast-extractor/src/{retriever/mod.rs => retriever.rs} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename libs/ast-extractor/src/{retriever/mod.rs => retriever.rs} (100%) diff --git a/libs/ast-extractor/src/retriever/mod.rs b/libs/ast-extractor/src/retriever.rs similarity index 100% rename from libs/ast-extractor/src/retriever/mod.rs rename to libs/ast-extractor/src/retriever.rs From c97828c7063dd0778ca223f9352b8e34976eee9b Mon Sep 17 00:00:00 2001 From: 0xMemoryGrinder <35138272+0xMemoryGrinder@users.noreply.github.com> Date: Thu, 14 Sep 2023 22:43:35 -0400 Subject: [PATCH 28/59] feat(libs/ast-extractor): export LineColumn type for code location --- libs/ast-extractor/src/lib.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/libs/ast-extractor/src/lib.rs b/libs/ast-extractor/src/lib.rs index 3e452dc2..fb6764d5 100644 --- a/libs/ast-extractor/src/lib.rs +++ b/libs/ast-extractor/src/lib.rs @@ -3,4 +3,7 @@ pub mod retriever; pub mod errors; // Expose syn_solidity crate -pub use syn_solidity::*; \ No newline at end of file +pub use syn_solidity::*; + +// Publish span location type +pub use proc_macro2::LineColumn; \ No newline at end of file From 5b6130e7b12ebc204664dcae16c433705e6875f7 Mon Sep 17 00:00:00 2001 From: 0xMemoryGrinder <35138272+0xMemoryGrinder@users.noreply.github.com> Date: Thu, 14 Sep 2023 22:44:30 -0400 Subject: [PATCH 29/59] feat(libs/ast-extractor): added util method on range to compute easily its length --- .../linter/core/solidhunter-lib/src/linter.rs | 2 +- .../linter/core/solidhunter-lib/src/types.rs | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs index 6331b93a..ca45cd8d 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs @@ -98,7 +98,7 @@ impl SolidLinter { } pub fn parse_content(&mut self, filepath: String, content: &String) -> LintResult { - let res = extract::extract_ast_from_content(content.to_string())?; + let res = ast_extractor::extract::extract_ast_from_content(content.to_string())?; self._add_file( filepath.as_str(), diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs index 0662777c..09b56b23 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs @@ -114,6 +114,28 @@ pub struct Range { pub length: u64, } +impl Range { + // Compue the number of characters between the start and end of the range + pub fn compute_length(&mut self, content: &str) { + if self.start.line == self.end.line { + self.length = self.end.character - self.start.character; + } else { + let mut length = 0; + let mut line = self.start.line; + let mut character = self.start.character; + while line < self.end.line { + let line_content = content.lines().nth(line as usize - 1).unwrap(); + length += line_content.len() + 1 - character as usize; + line += 1; + character = 0; + } + let line_content = content.lines().nth(line as usize - 1).unwrap(); + length += self.end.character as usize - character as usize; + self.length = length as u64; + } + } +} + #[derive(Clone, Serialize, Deserialize, Debug)] pub enum NumberOrString { Number(i32), From 9acf77d000c17d88585b4d75d72fa5f1d5c74df8 Mon Sep 17 00:00:00 2001 From: 0xMemoryGrinder <35138272+0xMemoryGrinder@users.noreply.github.com> Date: Thu, 14 Sep 2023 23:07:09 -0400 Subject: [PATCH 30/59] refactor(solidity/linter/core): import-on-top rule now uses ast crate types --- libs/ast-extractor/src/extract.rs | 2 +- .../linter/core/solidhunter-lib/src/linter.rs | 4 +- .../src/rules/order/import_on_top.rs | 43 ++++++++++--------- .../linter/core/solidhunter-lib/src/types.rs | 1 - .../testdata/ImportOnTop/.solidhunter.json | 12 ++++++ .../testdata/ImportOnTop/file.sol | 7 +++ .../testdata/ImportOnTop/findings.csv | 1 + .../core/solidhunter-lib/tests/linter.rs | 3 +- 8 files changed, 48 insertions(+), 25 deletions(-) create mode 100644 toolchains/solidity/linter/core/solidhunter-lib/testdata/ImportOnTop/.solidhunter.json create mode 100644 toolchains/solidity/linter/core/solidhunter-lib/testdata/ImportOnTop/file.sol create mode 100644 toolchains/solidity/linter/core/solidhunter-lib/testdata/ImportOnTop/findings.csv diff --git a/libs/ast-extractor/src/extract.rs b/libs/ast-extractor/src/extract.rs index 7c76aabe..99e52f4e 100644 --- a/libs/ast-extractor/src/extract.rs +++ b/libs/ast-extractor/src/extract.rs @@ -8,7 +8,7 @@ use crate::errors::ExtractError; use proc_macro2::TokenStream; use std::str::FromStr; -pub fn extract_ast_from_content(content: String) -> Result { +pub fn extract_ast_from_content(content: &String) -> Result { let tokens = TokenStream::from_str(content.as_str())?; let ast = syn_solidity::parse2(tokens)?; Ok(ast) diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs index ca45cd8d..70d9ab48 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs @@ -81,7 +81,7 @@ impl SolidLinter { pub fn parse_file(&mut self, filepath: String) -> LintResult { let content = fs::read_to_string(filepath.clone())?; - let res = ast_extractor::extract::extract_ast_from_content(content)?; + let res = ast_extractor::extract::extract_ast_from_content(&content)?; self._add_file( filepath.as_str(), @@ -98,7 +98,7 @@ impl SolidLinter { } pub fn parse_content(&mut self, filepath: String, content: &String) -> LintResult { - let res = ast_extractor::extract::extract_ast_from_content(content.to_string())?; + let res = ast_extractor::extract::extract_ast_from_content(&content.to_string())?; self._add_file( filepath.as_str(), diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/order/import_on_top.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/order/import_on_top.rs index b9ea19ae..4bb49314 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/order/import_on_top.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/order/import_on_top.rs @@ -1,27 +1,30 @@ +use ast_extractor::Spanned; + use crate::linter::SolidFile; use crate::rules::types::*; use crate::types::*; -use solc_wrapper::{decode_location, CodeLocation, SourceUnitChildNodes}; pub struct ImportOnTop { data: RuleEntry, } impl ImportOnTop { - fn create_diag(&self, file: &SolidFile, location: (CodeLocation, CodeLocation)) -> LintDiag { + fn create_diag(&self, file: &SolidFile, location: (ast_extractor::LineColumn, ast_extractor::LineColumn)) -> LintDiag { + let mut range = Range { + start: Position { + line: location.0.line as u64, + character: location.0.column as u64, + }, + end: Position { + line: location.1.line as u64, + character: location.1.column as u64, + }, + length: 0, + }; + range.compute_length(&file.content); LintDiag { id: "import-on-top".to_string(), - range: Range { - start: Position { - line: location.0.line as u64, - character: location.0.column as u64, - }, - end: Position { - line: location.1.line as u64, - character: location.1.column as u64, - }, - length: location.0.length as u64, - }, + range, message: String::from("Import must be on top in the file"), severity: Some(self.data.severity), code: None, @@ -37,9 +40,9 @@ impl RuleType for ImportOnTop { let mut res = Vec::new(); let mut last_import_location = 0; - for i in 1..file.data.nodes.len() { - match &file.data.nodes[i] { - SourceUnitChildNodes::ImportDirective(_) => { + for i in 1..file.data.items.len() { + match &file.data.items[i] { + ast_extractor::Item::Import(_) => { last_import_location = i; } _ => { @@ -48,11 +51,11 @@ impl RuleType for ImportOnTop { } } - for i in 1..file.data.nodes.len() { - match &file.data.nodes[i] { - SourceUnitChildNodes::ImportDirective(import) => { + for i in 1..file.data.items.len() { + match &file.data.items[i] { + ast_extractor::Item::Import(import) => { if i > last_import_location { - let location = decode_location(&import.src, &file.content); + let location = (import.span().start(), import.span().end()); res.push(self.create_diag(file, location)); } } diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs index 09b56b23..207b024c 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs @@ -129,7 +129,6 @@ impl Range { line += 1; character = 0; } - let line_content = content.lines().nth(line as usize - 1).unwrap(); length += self.end.character as usize - character as usize; self.length = length as u64; } diff --git a/toolchains/solidity/linter/core/solidhunter-lib/testdata/ImportOnTop/.solidhunter.json b/toolchains/solidity/linter/core/solidhunter-lib/testdata/ImportOnTop/.solidhunter.json new file mode 100644 index 00000000..3f5b34e6 --- /dev/null +++ b/toolchains/solidity/linter/core/solidhunter-lib/testdata/ImportOnTop/.solidhunter.json @@ -0,0 +1,12 @@ +{ + "name": "solidhunter", + "includes": [], + "plugins": [], + "rules": [ + { + "id": "import-on-top", + "severity": "WARNING", + "data": [] + } + ] +} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solidhunter-lib/testdata/ImportOnTop/file.sol b/toolchains/solidity/linter/core/solidhunter-lib/testdata/ImportOnTop/file.sol new file mode 100644 index 00000000..9a64dbc0 --- /dev/null +++ b/toolchains/solidity/linter/core/solidhunter-lib/testdata/ImportOnTop/file.sol @@ -0,0 +1,7 @@ +pragma solidity 0.8.0; + +contract Test {} + +import "hardhat/console.sol"; + +contract Test2 {} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solidhunter-lib/testdata/ImportOnTop/findings.csv b/toolchains/solidity/linter/core/solidhunter-lib/testdata/ImportOnTop/findings.csv new file mode 100644 index 00000000..7c54c760 --- /dev/null +++ b/toolchains/solidity/linter/core/solidhunter-lib/testdata/ImportOnTop/findings.csv @@ -0,0 +1 @@ +import-on-top:5:0:29 \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solidhunter-lib/tests/linter.rs b/toolchains/solidity/linter/core/solidhunter-lib/tests/linter.rs index 85cbc3c5..b096cf29 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/tests/linter.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/tests/linter.rs @@ -85,5 +85,6 @@ macro_rules! test_directories { } test_directories! { - LineMaxLen + LineMaxLen, + ImportOnTop } From 7dc005f9f9736e899f112e299fbb6402e8cef30f Mon Sep 17 00:00:00 2001 From: 0xtekgrinder <0xtekgrinder@protonmail.com> Date: Thu, 14 Sep 2023 23:37:45 -0400 Subject: [PATCH 31/59] feat(ast-extractor): use refs for all retrievers --- libs/ast-extractor/src/extract.rs | 6 +++--- libs/ast-extractor/src/retriever/contract.rs | 8 ++++---- libs/ast-extractor/src/retriever/enum.rs | 12 ++++++------ libs/ast-extractor/src/retriever/error.rs | 6 +++--- libs/ast-extractor/src/retriever/event.rs | 12 ++++++------ libs/ast-extractor/src/retriever/function.rs | 8 ++++---- libs/ast-extractor/src/retriever/struct.rs | 12 ++++++------ libs/ast-extractor/src/retriever/udt.rs | 4 ++-- libs/ast-extractor/src/retriever/using.rs | 6 +++--- 9 files changed, 37 insertions(+), 37 deletions(-) diff --git a/libs/ast-extractor/src/extract.rs b/libs/ast-extractor/src/extract.rs index 99e52f4e..4e61bc5c 100644 --- a/libs/ast-extractor/src/extract.rs +++ b/libs/ast-extractor/src/extract.rs @@ -27,14 +27,14 @@ mod tests { path.push("files"); path.push("good.sol"); let source = fs::read_to_string(path).unwrap(); - let res = extract_ast_from_content(source); + let res = extract_ast_from_content(&source); assert!(res.is_ok()); } #[test] fn test_extract_ast_from_content_invalid_token() { let source = String::from("contract test { function test() public | uint a = 1 } }"); - let result = extract_ast_from_content(source); + let result = extract_ast_from_content(&source); assert!(result.is_err()); assert_eq!( result.unwrap_err().to_string(), @@ -45,7 +45,7 @@ mod tests { #[test] fn test_extract_ast_from_content_missing_semicolumn() { let source = String::from("contract test { function test() public { uint a = 1 } }"); - let result = extract_ast_from_content(source); + let result = extract_ast_from_content(&source); assert!(result.is_err()); assert_eq!(result.unwrap_err().to_string(), "Parsing error"); } diff --git a/libs/ast-extractor/src/retriever/contract.rs b/libs/ast-extractor/src/retriever/contract.rs index f3fd7189..5062ecbf 100644 --- a/libs/ast-extractor/src/retriever/contract.rs +++ b/libs/ast-extractor/src/retriever/contract.rs @@ -24,7 +24,7 @@ impl<'ast> Visit<'ast> for ContractVisitor { } } -pub fn retrieve_contract_nodes(ast: syn_solidity::File) -> Vec { +pub fn retrieve_contract_nodes(ast: &syn_solidity::File) -> Vec { let mut visitor = ContractVisitor::new(); visitor.visit_file(&ast); visitor.contracts @@ -44,7 +44,7 @@ mod tests { let source = String::from("pragma solidity ^0.8.0;"); let tokens = TokenStream::from_str(source.as_str()).unwrap(); let ast = syn_solidity::parse2(tokens).unwrap(); - let res = retrieve_contract_nodes(ast); + let res = retrieve_contract_nodes(&ast); assert_eq!(res.len(), 0); } @@ -58,7 +58,7 @@ mod tests { let source = fs::read_to_string(path).unwrap(); let tokens = TokenStream::from_str(source.as_str()).unwrap(); let ast = syn_solidity::parse2(tokens).unwrap(); - let res = retrieve_contract_nodes(ast); + let res = retrieve_contract_nodes(&ast); assert_eq!(res.len(), 1); } @@ -72,7 +72,7 @@ mod tests { let source = fs::read_to_string(path).unwrap(); let tokens = TokenStream::from_str(source.as_str()).unwrap(); let ast = syn_solidity::parse2(tokens).unwrap(); - let res = retrieve_contract_nodes(ast); + let res = retrieve_contract_nodes(&ast); assert_eq!(res.len(), 2); } } diff --git a/libs/ast-extractor/src/retriever/enum.rs b/libs/ast-extractor/src/retriever/enum.rs index 484a61b4..7f219f21 100644 --- a/libs/ast-extractor/src/retriever/enum.rs +++ b/libs/ast-extractor/src/retriever/enum.rs @@ -38,13 +38,13 @@ impl<'ast> Visit<'ast> for EnumVisitor { } } -pub fn retrieve_enums_contract_nodes(ast: syn_solidity::ItemContract) -> Vec { +pub fn retrieve_enums_contract_nodes(ast: &syn_solidity::ItemContract) -> Vec { let mut visitor = EnumVisitor::new(); visitor.visit_item_contract(&ast); visitor.contract_enums } -pub fn retrieve_enums_file_nodes(ast: syn_solidity::File) -> Vec { +pub fn retrieve_enums_file_nodes(ast: &syn_solidity::File) -> Vec { let mut visitor = EnumVisitor::new(); visitor.visit_file(&ast); visitor.file_enums @@ -73,7 +73,7 @@ mod tests { let item = ast.items.first().unwrap().clone(); if let Item::Contract(contract) = item { - let res = retrieve_enums_contract_nodes(contract); + let res = retrieve_enums_contract_nodes(&contract); assert_eq!(res.len(), 0); } else { panic!("Item shouldn't have enum"); @@ -98,7 +98,7 @@ mod tests { .clone(); if let Item::Contract(contract) = contract { - let res = retrieve_enums_contract_nodes(contract); + let res = retrieve_enums_contract_nodes(&contract); assert_eq!(res.len(), 1); } else { panic!("Item should have a contract"); @@ -116,7 +116,7 @@ mod tests { let tokens = TokenStream::from_str(source.as_str()).unwrap(); let ast = syn_solidity::parse2(tokens).unwrap(); - let res = retrieve_enums_file_nodes(ast); + let res = retrieve_enums_file_nodes(&ast); assert_eq!(res.len(), 0); } @@ -131,7 +131,7 @@ mod tests { let tokens = TokenStream::from_str(source.as_str()).unwrap(); let ast = syn_solidity::parse2(tokens).unwrap(); - let res = retrieve_enums_file_nodes(ast); + let res = retrieve_enums_file_nodes(&ast); assert_eq!(res.len(), 1); } } diff --git a/libs/ast-extractor/src/retriever/error.rs b/libs/ast-extractor/src/retriever/error.rs index 4ebc0605..d8b4e514 100644 --- a/libs/ast-extractor/src/retriever/error.rs +++ b/libs/ast-extractor/src/retriever/error.rs @@ -22,7 +22,7 @@ impl<'ast> Visit<'ast> for ErrorVisitor { } } -pub fn retrieve_errors_nodes(ast: syn_solidity::ItemContract) -> Vec { +pub fn retrieve_errors_nodes(ast: &syn_solidity::ItemContract) -> Vec { let mut visitor = ErrorVisitor::new(); visitor.visit_item_contract(&ast); visitor.errors @@ -51,7 +51,7 @@ mod tests { let item = ast.items.first().unwrap().clone(); if let Item::Contract(contract) = item { - let res = retrieve_errors_nodes(contract); + let res = retrieve_errors_nodes(&contract); assert_eq!(res.len(), 0); } else { panic!("Item should not have error"); @@ -76,7 +76,7 @@ mod tests { .clone(); if let Item::Contract(contract) = item { - let res = retrieve_errors_nodes(contract); + let res = retrieve_errors_nodes(&contract); assert_eq!(res.len(), 1); } else { panic!("Item should have a contract"); diff --git a/libs/ast-extractor/src/retriever/event.rs b/libs/ast-extractor/src/retriever/event.rs index c2642e4a..96946da6 100644 --- a/libs/ast-extractor/src/retriever/event.rs +++ b/libs/ast-extractor/src/retriever/event.rs @@ -38,13 +38,13 @@ impl<'ast> Visit<'ast> for EventVisitor { } } -pub fn retrieve_events_contract_nodes(ast: syn_solidity::ItemContract) -> Vec { +pub fn retrieve_events_contract_nodes(ast: &syn_solidity::ItemContract) -> Vec { let mut visitor = EventVisitor::new(); visitor.visit_item_contract(&ast); visitor.contract_events } -pub fn retrieve_events_file_nodes(ast: syn_solidity::File) -> Vec { +pub fn retrieve_events_file_nodes(ast: &syn_solidity::File) -> Vec { let mut visitor = EventVisitor::new(); visitor.visit_file(&ast); visitor.file_events @@ -74,7 +74,7 @@ mod tests { let item = ast.items.first().unwrap().clone(); if let Item::Contract(contract) = item { - let res = retrieve_events_contract_nodes(contract); + let res = retrieve_events_contract_nodes(&contract); assert_eq!(res.len(), 0); } else { panic!("Item should not have event"); @@ -99,7 +99,7 @@ mod tests { .clone(); if let Item::Contract(contract) = item { - let res = retrieve_events_contract_nodes(contract); + let res = retrieve_events_contract_nodes(&contract); assert_eq!(res.len(), 1); } else { panic!("Item should have a event"); @@ -117,7 +117,7 @@ mod tests { let tokens = TokenStream::from_str(source.as_str()).unwrap(); let ast = syn_solidity::parse2(tokens).unwrap(); - let res = retrieve_events_file_nodes(ast); + let res = retrieve_events_file_nodes(&ast); assert_eq!(res.len(), 0); } @@ -132,7 +132,7 @@ mod tests { let tokens = TokenStream::from_str(source.as_str()).unwrap(); let ast = syn_solidity::parse2(tokens).unwrap(); - let res = retrieve_events_file_nodes(ast); + let res = retrieve_events_file_nodes(&ast); assert_eq!(res.len(), 1); } } diff --git a/libs/ast-extractor/src/retriever/function.rs b/libs/ast-extractor/src/retriever/function.rs index 4e4937da..06079a28 100644 --- a/libs/ast-extractor/src/retriever/function.rs +++ b/libs/ast-extractor/src/retriever/function.rs @@ -24,7 +24,7 @@ impl<'ast> Visit<'ast> for FunctionVisitor { } } -pub fn retrieve_functions_nodes(ast: syn_solidity::ItemContract) -> Vec { +pub fn retrieve_functions_nodes(ast: &syn_solidity::ItemContract) -> Vec { let mut visitor = FunctionVisitor::new(); visitor.visit_item_contract(&ast); visitor.functions @@ -53,7 +53,7 @@ mod tests { let item = ast.items.first().unwrap().clone(); if let Item::Contract(contract) = item { - let res = retrieve_functions_nodes(contract); + let res = retrieve_functions_nodes(&contract); assert_eq!(res.len(), 0); } else { panic!("Item is not a contract"); @@ -73,7 +73,7 @@ mod tests { let item = ast.items.first().unwrap().clone(); if let Item::Contract(contract) = item { - let res = retrieve_functions_nodes(contract); + let res = retrieve_functions_nodes(&contract); assert_eq!(res.len(), 1); } else { panic!("Item is not a contract"); @@ -93,7 +93,7 @@ mod tests { let item = ast.items.first().unwrap().clone(); if let Item::Contract(contract) = item { - let res = retrieve_functions_nodes(contract); + let res = retrieve_functions_nodes(&contract); assert_eq!(res.len(), 2); } else { panic!("Item is not a contract"); diff --git a/libs/ast-extractor/src/retriever/struct.rs b/libs/ast-extractor/src/retriever/struct.rs index 1c59b6ae..dadbdf1c 100644 --- a/libs/ast-extractor/src/retriever/struct.rs +++ b/libs/ast-extractor/src/retriever/struct.rs @@ -37,13 +37,13 @@ impl<'ast> Visit<'ast> for StructVisitor { } } -pub fn retrieve_structs_contract_nodes(ast: syn_solidity::ItemContract) -> Vec { +pub fn retrieve_structs_contract_nodes(ast: &syn_solidity::ItemContract) -> Vec { let mut visitor = StructVisitor::new(); visitor.visit_item_contract(&ast); visitor.contract_structs } -pub fn retrieve_structs_file_nodes(ast: syn_solidity::File) -> Vec { +pub fn retrieve_structs_file_nodes(ast: &syn_solidity::File) -> Vec { let mut visitor = StructVisitor::new(); visitor.visit_file(&ast); visitor.file_structs @@ -72,7 +72,7 @@ mod tests { let item = ast.items.first().unwrap().clone(); if let Item::Contract(contract) = item { - let res = retrieve_structs_contract_nodes(contract); + let res = retrieve_structs_contract_nodes(&contract); assert_eq!(res.len(), 0); } else { panic!("Item should not have struct"); @@ -97,7 +97,7 @@ mod tests { .clone(); if let Item::Contract(contract) = item { - let res = retrieve_structs_contract_nodes(contract); + let res = retrieve_structs_contract_nodes(&contract); assert_eq!(res.len(), 1); } else { panic!("Item should have a struct"); @@ -115,7 +115,7 @@ mod tests { let tokens = TokenStream::from_str(source.as_str()).unwrap(); let ast = syn_solidity::parse2(tokens).unwrap(); - let res = retrieve_structs_file_nodes(ast); + let res = retrieve_structs_file_nodes(&ast); assert_eq!(res.len(), 0); } @@ -130,7 +130,7 @@ mod tests { let tokens = TokenStream::from_str(source.as_str()).unwrap(); let ast = syn_solidity::parse2(tokens).unwrap(); - let res = retrieve_structs_file_nodes(ast); + let res = retrieve_structs_file_nodes(&ast); assert_eq!(res.len(), 1); } } diff --git a/libs/ast-extractor/src/retriever/udt.rs b/libs/ast-extractor/src/retriever/udt.rs index 7c633064..872d02ed 100644 --- a/libs/ast-extractor/src/retriever/udt.rs +++ b/libs/ast-extractor/src/retriever/udt.rs @@ -22,7 +22,7 @@ impl<'ast> Visit<'ast> for UdtVisitor { } } -pub fn retrieve_udts_nodes(ast: syn_solidity::File) -> Vec { +pub fn retrieve_udts_nodes(ast: &syn_solidity::File) -> Vec { let mut visitor = UdtVisitor::new(); visitor.visit_file(&ast); visitor.udts @@ -48,7 +48,7 @@ mod tests { let tokens = TokenStream::from_str(source.as_str()).unwrap(); let ast = syn_solidity::parse2(tokens).unwrap(); - let res = retrieve_udts_nodes(ast); + let res = retrieve_udts_nodes(&ast); assert_eq!(res.len(), 1); } } diff --git a/libs/ast-extractor/src/retriever/using.rs b/libs/ast-extractor/src/retriever/using.rs index 7e9f8d14..07b798b9 100644 --- a/libs/ast-extractor/src/retriever/using.rs +++ b/libs/ast-extractor/src/retriever/using.rs @@ -22,7 +22,7 @@ impl<'ast> Visit<'ast> for UsingVisitor { } } -pub fn retrieve_usings_nodes(ast: syn_solidity::ItemContract) -> Vec { +pub fn retrieve_usings_nodes(ast: &syn_solidity::ItemContract) -> Vec { let mut visitor = UsingVisitor::new(); visitor.visit_item_contract(&ast); visitor.usings @@ -51,7 +51,7 @@ mod tests { let item = ast.items.first().unwrap().clone(); if let Item::Contract(contract) = item { - let res = retrieve_usings_nodes(contract); + let res = retrieve_usings_nodes(&contract); assert_eq!(res.len(), 0); } else { panic!("Item should not have any using directive"); @@ -79,7 +79,7 @@ mod tests { .clone(); if let Item::Contract(contract) = item { - let res = retrieve_usings_nodes(contract); + let res = retrieve_usings_nodes(&contract); assert_eq!(res.len(), 1); } else { panic!("Item should have a a using directive"); From d020796d50b7d1edf366b38c72c08407faaeac20 Mon Sep 17 00:00:00 2001 From: 0xtekgrinder <0xtekgrinder@protonmail.com> Date: Fri, 15 Sep 2023 00:13:24 -0400 Subject: [PATCH 32/59] refactor(solidhunter): remove length attribute from Range type --- .../linter/core/solidhunter-lib/src/linter.rs | 4 ++-- .../rules/best_practises/function_max_lines.rs | 6 ++++-- .../src/rules/best_practises/line_maxlen.rs | 11 +++++++---- .../rules/best_practises/max_states_count.rs | 5 +++-- .../src/rules/best_practises/mod.rs | 13 +++++++++---- .../src/rules/best_practises/reason_string.rs | 7 +++---- .../src/rules/miscellaneous/quotes.rs | 1 - .../src/rules/naming/func_name_camelcase.rs | 9 ++++----- .../rules/naming/func_param_name_camelcase.rs | 8 ++++---- .../solidhunter-lib/src/rules/naming/mod.rs | 17 ++++++++++++----- .../src/rules/naming/use_forbidden_name.rs | 7 ++++--- .../src/rules/order/import_on_top.rs | 11 ++++++----- .../core/solidhunter-lib/src/rules/order/mod.rs | 3 +-- .../linter/core/solidhunter-lib/src/types.rs | 9 ++++----- 14 files changed, 63 insertions(+), 48 deletions(-) diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs index 70d9ab48..aa63991f 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs @@ -86,7 +86,7 @@ impl SolidLinter { self._add_file( filepath.as_str(), res, - content.as_str(), + &content.as_str(), ); let mut res: Vec = Vec::new(); @@ -98,7 +98,7 @@ impl SolidLinter { } pub fn parse_content(&mut self, filepath: String, content: &String) -> LintResult { - let res = ast_extractor::extract::extract_ast_from_content(&content.to_string())?; + let res = ast_extractor::extract::extract_ast_from_content(content)?; self._add_file( filepath.as_str(), diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/function_max_lines.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/function_max_lines.rs index 14b57627..a6980d9d 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/function_max_lines.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/function_max_lines.rs @@ -1,7 +1,8 @@ use crate::linter::SolidFile; use crate::rules::types::*; use crate::types::*; -use solc_wrapper::ast::ast::*; + +/* // const DEFAULT_SEVERITY: &str = "warn"; const DEFAULT_MESSAGE: &str = "Function contains too much lines"; @@ -82,7 +83,6 @@ fn check_function_lines( line: last_bracket_line as u64, character: 1, }, - length: _file.content.lines().nth(_start.line - 1)?.len() as u64, }); } res @@ -131,3 +131,5 @@ impl FunctionMaxLines { } } } + +*/ \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/line_maxlen.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/line_maxlen.rs index b4d2be23..3085df0a 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/line_maxlen.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/line_maxlen.rs @@ -2,6 +2,10 @@ use crate::linter::SolidFile; use crate::rules::types::*; use crate::types::*; +pub const RULE_ID: &str = "line-max-len"; + +const DEFAULT_LENGTH: u32 = 80; + pub struct LineMaxLen { max_len: usize, data: RuleEntry, @@ -19,9 +23,8 @@ impl LineMaxLen { line: line_idx as u64, character: line.len() as u64, }, - length: (line.len() - self.max_len) as u64, }, - id: "line-max-len".to_string(), + id: RULE_ID.to_string(), message: format!("Line is too long: {}", line.len()), severity: Some(self.data.severity), code: None, @@ -58,9 +61,9 @@ impl LineMaxLen { pub(crate) fn create_default() -> RuleEntry { RuleEntry { - id: "line-max-len".to_string(), + id: RULE_ID.to_string(), severity: Severity::WARNING, - data: vec!["80".to_string()], + data: vec![DEFAULT_LENGTH.to_string()], } } } diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/max_states_count.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/max_states_count.rs index 793ef258..5b1eeb16 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/max_states_count.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/max_states_count.rs @@ -1,8 +1,8 @@ use crate::linter::SolidFile; use crate::rules::types::*; use crate::types::*; -use solc_wrapper::*; +/* pub struct MaxStatesCount { max_states: usize, data: RuleEntry, @@ -26,7 +26,6 @@ impl MaxStatesCount { line: location.1.line as u64, character: location.1.column as u64, }, - length: location.0.length as u64, }, message: format!("Too many states: {}", count), severity: Some(self.data.severity), @@ -85,3 +84,5 @@ impl MaxStatesCount { } } } + +*/ \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/mod.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/mod.rs index 7ab23cfb..eb53d044 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/mod.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/mod.rs @@ -9,31 +9,36 @@ pub mod reason_string; // List all rules -use crate::rules::best_practises::function_max_lines::FunctionMaxLines; +// use crate::rules::best_practises::function_max_lines::FunctionMaxLines; use crate::rules::best_practises::line_maxlen::LineMaxLen; -use crate::rules::best_practises::max_states_count::MaxStatesCount; -use crate::rules::best_practises::reason_string::ReasonString; +// use crate::rules::best_practises::max_states_count::MaxStatesCount; +// use crate::rules::best_practises::reason_string::ReasonString; use crate::rules::RuleBuilder; pub fn create_default_rules() -> Vec { vec![ LineMaxLen::create_default(), + /* MaxStatesCount::create_default(), FunctionMaxLines::create_default(), ReasonString::create_default(), + */ ] } pub fn create_rules() -> HashMap Box> { let mut rules: HashMap = HashMap::new(); - rules.insert("line-max-len".to_string(), LineMaxLen::create); + rules.insert(line_maxlen::RULE_ID.to_string(), LineMaxLen::create); + + /* rules.insert(MaxStatesCount::RULE_ID.to_string(), MaxStatesCount::create); rules.insert( FunctionMaxLines::RULE_ID.to_string(), FunctionMaxLines::create, ); rules.insert(reason_string::RULE_ID.to_string(), ReasonString::create); + */ rules } diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/reason_string.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/reason_string.rs index e5a67338..9921407b 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/reason_string.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/reason_string.rs @@ -1,10 +1,8 @@ -use solc_wrapper::ast::utils::{self, get_all_nodes_by_type}; -use solc_wrapper::{decode_location, CodeLocation, Expression, NodeType}; - use crate::linter::SolidFile; use crate::rules::types::{RuleEntry, RuleType}; use crate::types::{LintDiag, Position, Range, Severity}; +/* pub const RULE_ID: &str = "reason-string"; const DEFAULT_SEVERITY: Severity = Severity::WARNING; @@ -34,7 +32,6 @@ impl ReasonString { line: location.1.line as u64, character: location.1.column as u64, }, - length: location.0.length as u64, }, message, severity: Some(self.data.severity), @@ -119,3 +116,5 @@ impl ReasonString { } } } + +*/ \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/miscellaneous/quotes.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/miscellaneous/quotes.rs index e1dabdad..270cbc29 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/miscellaneous/quotes.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/miscellaneous/quotes.rs @@ -19,7 +19,6 @@ impl Quotes { line: line_idx, character: idx as u64, }, - length: 1u64, }, message: format!("Use double quotes instead of single quote"), severity: Some(self.data.severity), diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_name_camelcase.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_name_camelcase.rs index 4c99c920..f4815546 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_name_camelcase.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_name_camelcase.rs @@ -1,10 +1,8 @@ use crate::linter::SolidFile; use crate::rules::types::*; use crate::types::*; -use solc_wrapper::{ - decode_location, CodeLocation, ContractDefinitionChildNodes, FunctionDefinitionKind, - SourceUnitChildNodes, -}; + +/* pub struct FuncNameCamelCase { data: RuleEntry, @@ -23,7 +21,6 @@ impl FuncNameCamelCase { line: location.1.line as u64, character: location.1.column as u64, }, - length: location.0.length as u64, }, message: "Function name need to be in camel case".to_string(), severity: Some(self.data.severity), @@ -88,3 +85,5 @@ impl FuncNameCamelCase { } } } + +*/ \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_param_name_camelcase.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_param_name_camelcase.rs index 1b06ca49..6fea45ad 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_param_name_camelcase.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_param_name_camelcase.rs @@ -1,9 +1,8 @@ use crate::linter::SolidFile; use crate::rules::types::*; use crate::types::*; -use solc_wrapper::{ - decode_location, CodeLocation, ContractDefinitionChildNodes, SourceUnitChildNodes, -}; + +/* pub struct FuncParamNameCamelcase { data: RuleEntry, @@ -22,7 +21,6 @@ impl FuncParamNameCamelcase { line: location.1.line as u64, character: location.1.column as u64, }, - length: location.0.length as u64, }, message: "Parameter name need to be in camel case".to_string(), severity: Some(self.data.severity), @@ -88,3 +86,5 @@ impl FuncParamNameCamelcase { } } } + +*/ \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/mod.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/mod.rs index 6c29aec3..5c5de3c9 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/mod.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/mod.rs @@ -1,7 +1,10 @@ use crate::rules::naming::contract_name_pascalcase::ContractNamePascalCase; + +/* use crate::rules::naming::func_name_camelcase::FuncNameCamelCase; use crate::rules::naming::func_param_name_camelcase::FuncParamNameCamelcase; use crate::rules::naming::use_forbidden_name::UseForbiddenName; +*/ use crate::rules::types::{RuleEntry, RuleType}; use crate::rules::RuleBuilder; use std::collections::HashMap; @@ -16,10 +19,12 @@ pub(crate) mod use_forbidden_name; pub fn create_default_rules() -> Vec { vec![ - FuncParamNameCamelcase::create_default(), ContractNamePascalCase::create_default(), + /* + FuncParamNameCamelcase::create_default(), FuncNameCamelCase::create_default(), UseForbiddenName::create_default(), + */ ] } @@ -27,18 +32,20 @@ pub fn create_rules() -> HashMap Box> { let mut rules: HashMap = HashMap::new(); rules.insert( - "func-param-name-camelcase".to_string(), - FuncParamNameCamelcase::create, + contract_name_pascalcase::RULE_ID.to_string(), + ContractNamePascalCase::create, ); + /* rules.insert( - "contract-name-pascalcase".to_string(), - ContractNamePascalCase::create, + "func-param-name-camelcase".to_string(), + FuncParamNameCamelcase::create, ); rules.insert("func-name-camelcase".to_string(), FuncNameCamelCase::create); rules.insert( UseForbiddenName::RULE_ID.to_string(), UseForbiddenName::create, ); + */ rules } diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/use_forbidden_name.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/use_forbidden_name.rs index 982caf52..46d3fb5b 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/use_forbidden_name.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/use_forbidden_name.rs @@ -1,8 +1,8 @@ use crate::linter::SolidFile; use crate::rules::types::*; use crate::types::*; -use solc_wrapper::ast::utils::{get_all_nodes_by_type, Nodes}; -use solc_wrapper::*; + +/* pub struct UseForbiddenName { data: RuleEntry, @@ -26,7 +26,6 @@ impl UseForbiddenName { line: location.1.line as u64, character: location.1.column as u64, }, - length: location.0.length as u64, }, message: format!("Forbidden variable name: {}", var.name), severity: Some(self.data.severity), @@ -75,3 +74,5 @@ impl UseForbiddenName { } } } + +*/ \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/order/import_on_top.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/order/import_on_top.rs index 4bb49314..d7757d40 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/order/import_on_top.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/order/import_on_top.rs @@ -4,6 +4,9 @@ use crate::linter::SolidFile; use crate::rules::types::*; use crate::types::*; +pub const RULE_ID: &str = "import-on-top"; +const MESSAGE: &str = "Import must be on top in the file"; + pub struct ImportOnTop { data: RuleEntry, } @@ -19,13 +22,11 @@ impl ImportOnTop { line: location.1.line as u64, character: location.1.column as u64, }, - length: 0, }; - range.compute_length(&file.content); LintDiag { - id: "import-on-top".to_string(), + id: RULE_ID.to_string(), range, - message: String::from("Import must be on top in the file"), + message: MESSAGE.to_string(), severity: Some(self.data.severity), code: None, source: None, @@ -75,7 +76,7 @@ impl ImportOnTop { pub(crate) fn create_default() -> RuleEntry { RuleEntry { - id: "import-on-top".to_string(), + id: RULE_ID.to_string(), severity: Severity::WARNING, data: vec![], } diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/order/mod.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/order/mod.rs index a235f67d..e9b8017d 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/order/mod.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/order/mod.rs @@ -5,7 +5,6 @@ use std::collections::HashMap; pub(crate) mod import_on_top; // List all rules - use crate::rules::order::import_on_top::ImportOnTop; use crate::rules::RuleBuilder; @@ -16,7 +15,7 @@ pub fn create_default_rules() -> Vec { pub fn create_rules() -> HashMap Box> { let mut rules: HashMap = HashMap::new(); - rules.insert("import-on-top".to_string(), ImportOnTop::create); + rules.insert(import_on_top::RULE_ID.to_string(), ImportOnTop::create); rules } diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs index 207b024c..8b8ea3fd 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs @@ -74,7 +74,7 @@ impl fmt::Display for LintDiag { padding, line, " ".repeat(self.range.start.character as usize), - "^".repeat(self.range.length as usize) + "^".repeat(self.range.compute_length(line) as usize) ) } } @@ -111,14 +111,13 @@ pub enum Severity { pub struct Range { pub start: Position, pub end: Position, - pub length: u64, } impl Range { // Compue the number of characters between the start and end of the range - pub fn compute_length(&mut self, content: &str) { + pub fn compute_length(&self, content: &str) -> u64 { if self.start.line == self.end.line { - self.length = self.end.character - self.start.character; + self.end.character - self.start.character } else { let mut length = 0; let mut line = self.start.line; @@ -130,7 +129,7 @@ impl Range { character = 0; } length += self.end.character as usize - character as usize; - self.length = length as u64; + length as u64 } } } From 92d4ffc5094683e2a5c65a7c3c4d005da68dd380 Mon Sep 17 00:00:00 2001 From: 0xtekgrinder <0xtekgrinder@protonmail.com> Date: Fri, 15 Sep 2023 00:14:06 -0400 Subject: [PATCH 33/59] refactor(solidhunter): contract_name_pascalcase use ast_extractor --- .../rules/naming/contract_name_pascalcase.rs | 63 +++++++++---------- .../ContractNamePascalCase/.solidhunter.json | 24 +++++++ .../testdata/ContractNamePascalCase/file.sol | 4 ++ .../ContractNamePascalCase/findings.csv | 1 + 4 files changed, 58 insertions(+), 34 deletions(-) create mode 100644 toolchains/solidity/linter/core/solidhunter-lib/testdata/ContractNamePascalCase/.solidhunter.json create mode 100644 toolchains/solidity/linter/core/solidhunter-lib/testdata/ContractNamePascalCase/file.sol create mode 100644 toolchains/solidity/linter/core/solidhunter-lib/testdata/ContractNamePascalCase/findings.csv diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/contract_name_pascalcase.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/contract_name_pascalcase.rs index e85e5aa1..c8cd9b19 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/contract_name_pascalcase.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/contract_name_pascalcase.rs @@ -1,28 +1,22 @@ +use ast_extractor::Spanned; + use crate::linter::SolidFile; use crate::rules::types::*; use crate::types::*; -use solc_wrapper::{decode_location, CodeLocation, SourceUnitChildNodes}; + +pub const RULE_ID: &str = "contract-name-pascalcase"; +const MESSAGE: &str = "Contract name need to be in pascal case"; pub struct ContractNamePascalCase { data: RuleEntry, } impl ContractNamePascalCase { - fn create_diag(&self, location: (CodeLocation, CodeLocation), file: &SolidFile) -> LintDiag { + fn create_diag(&self, location: Range, file: &SolidFile) -> LintDiag { LintDiag { - id: "contract-name-pascalcase".to_string(), - range: Range { - start: Position { - line: location.0.line as u64, - character: location.0.column as u64, - }, - end: Position { - line: location.1.line as u64, - character: location.1.column as u64, - }, - length: location.0.length as u64, - }, - message: "Contract name need to be in pascal case".to_string(), + id: RULE_ID.to_string(), + range: location, + message: MESSAGE.to_string(), severity: Some(self.data.severity), code: None, source: None, @@ -35,27 +29,28 @@ impl ContractNamePascalCase { impl RuleType for ContractNamePascalCase { fn diagnose(&self, file: &SolidFile, _files: &Vec) -> Vec { let mut res = Vec::new(); + let contracts = ast_extractor::retriever::retrieve_contract_nodes(&file.data); - for node in &file.data.nodes { - match node { - SourceUnitChildNodes::ContractDefinition(contract) => { - if (contract.name.chars().nth(0).unwrap() >= 'a' - && contract.name.chars().nth(0).unwrap() <= 'z') - || contract.name.contains('_') - || contract.name.contains('-') + for contract in contracts { + if (contract.name.as_string().chars().nth(0).unwrap() >= 'a' + && contract.name.as_string().chars().nth(0).unwrap() <= 'z') + || contract.name.as_string().contains('_') + || contract.name.as_string().contains('-') { - //Untested - let location = decode_location( - contract.name_location.as_ref().unwrap(), - &file.content, - ); - res.push(self.create_diag(location, file)); + res.push(self.create_diag({ + let location = contract.name.span(); + Range { + start: Position { + line: location.start().line as u64, + character: location.start().column as u64, + }, + end: Position { + line: location.end().line as u64, + character: location.end().column as u64, + }, + } + }, file)); } - } - _ => { - continue; - } - } } res } @@ -69,7 +64,7 @@ impl ContractNamePascalCase { pub(crate) fn create_default() -> RuleEntry { RuleEntry { - id: "contract-name-pascalcase".to_string(), + id: RULE_ID.to_string(), severity: Severity::WARNING, data: vec![], } diff --git a/toolchains/solidity/linter/core/solidhunter-lib/testdata/ContractNamePascalCase/.solidhunter.json b/toolchains/solidity/linter/core/solidhunter-lib/testdata/ContractNamePascalCase/.solidhunter.json new file mode 100644 index 00000000..464becc6 --- /dev/null +++ b/toolchains/solidity/linter/core/solidhunter-lib/testdata/ContractNamePascalCase/.solidhunter.json @@ -0,0 +1,24 @@ +{ + "name": "solidhunter", + "includes": [], + "plugins": [], + "rules": [ + { + "id": "line-max-len", + "severity": "WARNING", + "data": [ + "80" + ] + }, + { + "id": "quotes", + "severity": "ERROR", + "data": [] + }, + { + "id": "contract-name-pascalcase", + "severity": "WARNING", + "data": [] + } + ] +} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solidhunter-lib/testdata/ContractNamePascalCase/file.sol b/toolchains/solidity/linter/core/solidhunter-lib/testdata/ContractNamePascalCase/file.sol new file mode 100644 index 00000000..09d51106 --- /dev/null +++ b/toolchains/solidity/linter/core/solidhunter-lib/testdata/ContractNamePascalCase/file.sol @@ -0,0 +1,4 @@ +pragma solidity 0.8.0; + +contract test { +} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solidhunter-lib/testdata/ContractNamePascalCase/findings.csv b/toolchains/solidity/linter/core/solidhunter-lib/testdata/ContractNamePascalCase/findings.csv new file mode 100644 index 00000000..7d0c6be6 --- /dev/null +++ b/toolchains/solidity/linter/core/solidhunter-lib/testdata/ContractNamePascalCase/findings.csv @@ -0,0 +1 @@ +contract-name-pascalcase:3:9:3:13 \ No newline at end of file From cfc05e015b018b7ace884383ad61f714b4ae10e9 Mon Sep 17 00:00:00 2001 From: 0xtekgrinder <0xtekgrinder@protonmail.com> Date: Fri, 15 Sep 2023 00:14:32 -0400 Subject: [PATCH 34/59] tests(solidhunter): update tests accordingly to last few changes --- .../testdata/ImportOnTop/findings.csv | 2 +- .../testdata/LineMaxLen/.solidhunter.json | 51 ------------------- .../testdata/LineMaxLen/findings.csv | 2 +- .../core/solidhunter-lib/tests/linter.rs | 12 +++-- 4 files changed, 10 insertions(+), 57 deletions(-) diff --git a/toolchains/solidity/linter/core/solidhunter-lib/testdata/ImportOnTop/findings.csv b/toolchains/solidity/linter/core/solidhunter-lib/testdata/ImportOnTop/findings.csv index 7c54c760..276c78a7 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/testdata/ImportOnTop/findings.csv +++ b/toolchains/solidity/linter/core/solidhunter-lib/testdata/ImportOnTop/findings.csv @@ -1 +1 @@ -import-on-top:5:0:29 \ No newline at end of file +import-on-top:5:0:5:29 \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solidhunter-lib/testdata/LineMaxLen/.solidhunter.json b/toolchains/solidity/linter/core/solidhunter-lib/testdata/LineMaxLen/.solidhunter.json index b4a09fbf..4fd69780 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/testdata/LineMaxLen/.solidhunter.json +++ b/toolchains/solidity/linter/core/solidhunter-lib/testdata/LineMaxLen/.solidhunter.json @@ -9,57 +9,6 @@ "data": [ "80" ] - }, - { - "id": "max-states-count", - "severity": "WARNING", - "data": [ - "15" - ] - }, - { - "id": "function-max-lines", - "severity": "WARNING", - "data": [ - "20" - ] - }, - { - "id": "reason-string", - "severity": "WARNING", - "data": [ - "32" - ] - }, - { - "id": "quotes", - "severity": "ERROR", - "data": [] - }, - { - "id": "func-param-name-camelcase", - "severity": "WARNING", - "data": [] - }, - { - "id": "contract-name-pascalcase", - "severity": "WARNING", - "data": [] - }, - { - "id": "func-name-camelcase", - "severity": "WARNING", - "data": [] - }, - { - "id": "use-forbidden-name", - "severity": "WARNING", - "data": [] - }, - { - "id": "import-on-top", - "severity": "WARNING", - "data": [] } ] } \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solidhunter-lib/testdata/LineMaxLen/findings.csv b/toolchains/solidity/linter/core/solidhunter-lib/testdata/LineMaxLen/findings.csv index 66932d7e..a92c1fc3 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/testdata/LineMaxLen/findings.csv +++ b/toolchains/solidity/linter/core/solidhunter-lib/testdata/LineMaxLen/findings.csv @@ -1 +1 @@ -line-max-len:4:80:10 \ No newline at end of file +line-max-len:4:80:4:90 \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solidhunter-lib/tests/linter.rs b/toolchains/solidity/linter/core/solidhunter-lib/tests/linter.rs index b096cf29..750ba587 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/tests/linter.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/tests/linter.rs @@ -4,7 +4,7 @@ use std::{fs, path::PathBuf}; struct Finding { start: Position, - length: u64, + end: Position, id: String, } @@ -35,7 +35,10 @@ fn test_directory(base_name: &str) { line: splitted_line[1].parse::().unwrap(), character: splitted_line[2].parse::().unwrap(), }, - length: splitted_line[3].parse::().unwrap(), + end: Position { + line: splitted_line[3].parse::().unwrap(), + character: splitted_line[4].parse::().unwrap(), + }, id: splitted_line[0].to_string(), }); } @@ -58,7 +61,7 @@ fn test_linter(config: &str, source: &str, expected_findings: &Vec) { for (_, diag) in diags.iter().enumerate() { for (_, expected_finding) in expected_findings.iter().enumerate() { if (diag.range.start == expected_finding.start) - && (diag.range.length == expected_finding.length) + && (diag.range.end == expected_finding.end) && (diag.id == expected_finding.id) { found = true; @@ -86,5 +89,6 @@ macro_rules! test_directories { test_directories! { LineMaxLen, - ImportOnTop + ImportOnTop, + ContractNamePascalCase } From c881d068b55f96f8eec4c63fe477a23449269aa9 Mon Sep 17 00:00:00 2001 From: 0xMemoryGrinder <35138272+0xMemoryGrinder@users.noreply.github.com> Date: Fri, 15 Sep 2023 00:59:59 -0400 Subject: [PATCH 35/59] refactor(solidity/linter/core): made function-max-lines use ast crate --- .../best_practises/function_max_lines.rs | 67 +++++++++---------- .../src/rules/best_practises/mod.rs | 9 ++- .../core/solidhunter-lib/src/rules/mod.rs | 1 + .../core/solidhunter-lib/src/rules/utils.rs | 21 ++++++ .../ContractNamePascalCase/.solidhunter.json | 12 ---- .../FunctionMaxLines/.solidhunter.json | 12 ++++ .../testdata/FunctionMaxLines/file.sol | 29 ++++++++ .../testdata/FunctionMaxLines/findings.csv | 1 + .../core/solidhunter-lib/tests/linter.rs | 6 +- 9 files changed, 102 insertions(+), 56 deletions(-) create mode 100644 toolchains/solidity/linter/core/solidhunter-lib/src/rules/utils.rs create mode 100644 toolchains/solidity/linter/core/solidhunter-lib/testdata/FunctionMaxLines/.solidhunter.json create mode 100644 toolchains/solidity/linter/core/solidhunter-lib/testdata/FunctionMaxLines/file.sol create mode 100644 toolchains/solidity/linter/core/solidhunter-lib/testdata/FunctionMaxLines/findings.csv diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/function_max_lines.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/function_max_lines.rs index a6980d9d..bee00885 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/function_max_lines.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/function_max_lines.rs @@ -1,9 +1,9 @@ +use ast_extractor::Spanned; + use crate::linter::SolidFile; use crate::rules::types::*; use crate::types::*; -/* - // const DEFAULT_SEVERITY: &str = "warn"; const DEFAULT_MESSAGE: &str = "Function contains too much lines"; @@ -19,9 +19,9 @@ impl RuleType for FunctionMaxLines { fn diagnose(&self, _file: &SolidFile, _files: &Vec) -> Vec { let mut res = Vec::new(); - let functions = get_all_functions_from_ast(&_file.data.nodes); + let functions = get_all_functions_from_ast(&_file.data); for function in functions { - let _report = check_function_lines(_file, Box::new(function), self.number_max_lines); + let _report = check_function_lines(_file, &function, self.number_max_lines); if let Some(report) = _report { res.push(LintDiag { id: Self::RULE_ID.to_string(), @@ -41,24 +41,19 @@ impl RuleType for FunctionMaxLines { // returns a struct containing the line number of the start and end of the function if it is too long fn check_function_lines( - _file: &SolidFile, - function: Box, + file: &SolidFile, + function: &ast_extractor::ItemFunction, nb_max_line: usize, ) -> Option { let mut res: Option = None; - let function_copy_name_location = &function.src; - let (_start, _) = decode_location(function_copy_name_location.as_str(), _file.content.as_str()); - let index = function_copy_name_location - .split(':') - .collect::>()[0] - .parse::() - .unwrap(); + let start_span = function.span().start(); + let index = crate::rules::utils::absolute_index_from_location(start_span, &file.content); let mut function_lines: usize = 0; let mut left_bracket: usize = 0; let mut right_bracket: usize = 0; let mut last_bracket_line: usize = 0; - for (_, c) in _file.content.chars().enumerate().skip(index) { + for (_, c) in file.content.chars().enumerate().skip(index) { if c == '{' { left_bracket += 1; } @@ -69,15 +64,15 @@ fn check_function_lines( function_lines += 1; } if right_bracket > 0 && left_bracket == right_bracket { - last_bracket_line = _start.line + function_lines; + last_bracket_line = start_span.line + function_lines; break; } } if function_lines > nb_max_line { res = Some(Range { start: Position { - line: _start.line as u64, - character: _start.column as u64, + line: start_span.line as u64, + character: start_span.column as u64, }, end: Position { line: last_bracket_line as u64, @@ -88,21 +83,19 @@ fn check_function_lines( res } -fn get_all_functions_from_ast(ast_nodes: &Vec) -> Vec { +fn get_all_functions_from_ast(ast_nodes: &ast_extractor::File) -> Vec { let mut res = Vec::new(); + let contract = ast_nodes + .items + .iter() + .filter_map(|item| match item { + ast_extractor::Item::Contract(contract) => Some(contract), + _ => None, + }) + .next(); - for node in ast_nodes { - let contract = match node { - SourceUnitChildNodes::ContractDefinition(contract) => contract, - _ => continue, - }; - for contract_node in &contract.nodes { - let function = match contract_node { - ContractDefinitionChildNodes::FunctionDefinition(function) => function, - _ => continue, - }; - res.push(*function.clone()); - } + if let Some(contract) = contract { + res = ast_extractor::retriever::retrieve_functions_nodes(contract); } res } @@ -111,11 +104,14 @@ impl FunctionMaxLines { pub(crate) const RULE_ID: &'static str = "function-max-lines"; pub fn create(data: RuleEntry) -> Box { - let max_number_lines = match data.data[0].parse::() { - Ok(v) => v, - Err(_) => DEFAULT_MAX_LINES, - }; - + let mut max_number_lines = DEFAULT_MAX_LINES; + + if data.data.len() > 0 { + max_number_lines = match data.data[0].parse::() { + Ok(v) => v, + Err(_) => DEFAULT_MAX_LINES, + }; + } let rule = FunctionMaxLines { number_max_lines: max_number_lines, _data: data, @@ -132,4 +128,3 @@ impl FunctionMaxLines { } } -*/ \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/mod.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/mod.rs index eb53d044..63c87040 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/mod.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/mod.rs @@ -9,7 +9,7 @@ pub mod reason_string; // List all rules -// use crate::rules::best_practises::function_max_lines::FunctionMaxLines; +use crate::rules::best_practises::function_max_lines::FunctionMaxLines; use crate::rules::best_practises::line_maxlen::LineMaxLen; // use crate::rules::best_practises::max_states_count::MaxStatesCount; // use crate::rules::best_practises::reason_string::ReasonString; @@ -18,8 +18,7 @@ use crate::rules::RuleBuilder; pub fn create_default_rules() -> Vec { vec![ LineMaxLen::create_default(), - /* - MaxStatesCount::create_default(), +// MaxStatesCount::create_default(), FunctionMaxLines::create_default(), ReasonString::create_default(), */ @@ -31,8 +30,8 @@ pub fn create_rules() -> HashMap Box> { rules.insert(line_maxlen::RULE_ID.to_string(), LineMaxLen::create); - /* - rules.insert(MaxStatesCount::RULE_ID.to_string(), MaxStatesCount::create); + +// rules.insert(MaxStatesCount::RULE_ID.to_string(), MaxStatesCount::create); rules.insert( FunctionMaxLines::RULE_ID.to_string(), FunctionMaxLines::create, diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/mod.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/mod.rs index 30c3eef0..a8f8bc3a 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/mod.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/mod.rs @@ -4,6 +4,7 @@ use std::collections::HashMap; pub mod factory; pub mod rule_impl; pub mod types; +pub mod utils; // List all rules pub mod best_practises; diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/utils.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/utils.rs new file mode 100644 index 00000000..e637bcc7 --- /dev/null +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/utils.rs @@ -0,0 +1,21 @@ +use ast_extractor::LineColumn; + +pub fn absolute_index_from_location(location: LineColumn, content: &str) -> usize { + let mut index = 0; + let mut line = 1; + let mut column = 1; + + for c in content.chars() { + if line == location.line && column == location.column { + return index; + } + if c == '\n' { + line += 1; + column = 1; + } else { + column += 1; + } + index += 1; + } + index +} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solidhunter-lib/testdata/ContractNamePascalCase/.solidhunter.json b/toolchains/solidity/linter/core/solidhunter-lib/testdata/ContractNamePascalCase/.solidhunter.json index 464becc6..9871b2e8 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/testdata/ContractNamePascalCase/.solidhunter.json +++ b/toolchains/solidity/linter/core/solidhunter-lib/testdata/ContractNamePascalCase/.solidhunter.json @@ -3,18 +3,6 @@ "includes": [], "plugins": [], "rules": [ - { - "id": "line-max-len", - "severity": "WARNING", - "data": [ - "80" - ] - }, - { - "id": "quotes", - "severity": "ERROR", - "data": [] - }, { "id": "contract-name-pascalcase", "severity": "WARNING", diff --git a/toolchains/solidity/linter/core/solidhunter-lib/testdata/FunctionMaxLines/.solidhunter.json b/toolchains/solidity/linter/core/solidhunter-lib/testdata/FunctionMaxLines/.solidhunter.json new file mode 100644 index 00000000..9097ed1c --- /dev/null +++ b/toolchains/solidity/linter/core/solidhunter-lib/testdata/FunctionMaxLines/.solidhunter.json @@ -0,0 +1,12 @@ +{ + "name": "solidhunter", + "includes": [], + "plugins": [], + "rules": [ + { + "id": "function-max-lines", + "severity": "WARNING", + "data": [] + } + ] +} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solidhunter-lib/testdata/FunctionMaxLines/file.sol b/toolchains/solidity/linter/core/solidhunter-lib/testdata/FunctionMaxLines/file.sol new file mode 100644 index 00000000..54ae6076 --- /dev/null +++ b/toolchains/solidity/linter/core/solidhunter-lib/testdata/FunctionMaxLines/file.sol @@ -0,0 +1,29 @@ +pragma solidity 0.8.0; + +contract Test { + function test() { + uint a = 1; + uint b = 2; + uint c = 3; + uint d = 4; + uint e = 5; + uint f = 6; + uint g = 7; + uint h = 8; + uint i = 9; + uint j = 10; + uint k = 11; + uint l = 12; + uint m = 13; + uint n = 14; + uint o = 15; + uint p = 16; + uint q = 17; + uint r = 18; + uint s = 19; + uint t = 20; + uint u = 21; + uint v = 22; + + } +} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solidhunter-lib/testdata/FunctionMaxLines/findings.csv b/toolchains/solidity/linter/core/solidhunter-lib/testdata/FunctionMaxLines/findings.csv new file mode 100644 index 00000000..eb4ddda0 --- /dev/null +++ b/toolchains/solidity/linter/core/solidhunter-lib/testdata/FunctionMaxLines/findings.csv @@ -0,0 +1 @@ +function-max-lines:4:13:28:1 \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solidhunter-lib/tests/linter.rs b/toolchains/solidity/linter/core/solidhunter-lib/tests/linter.rs index 750ba587..f38ba20d 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/tests/linter.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/tests/linter.rs @@ -88,7 +88,7 @@ macro_rules! test_directories { } test_directories! { - LineMaxLen, - ImportOnTop, - ContractNamePascalCase + ContractNamePascalCase, + FunctionMaxLines, + ImportOnTop } From e0c268df5a0995da5026185f162336f28679510f Mon Sep 17 00:00:00 2001 From: 0xMemoryGrinder <35138272+0xMemoryGrinder@users.noreply.github.com> Date: Fri, 15 Sep 2023 01:01:00 -0400 Subject: [PATCH 36/59] refactor(solidity/linter/core): max-states-count rule now uses ast crate --- .../rules/best_practises/max_states_count.rs | 37 +++++++++++-------- .../src/rules/best_practises/mod.rs | 14 +++---- .../testdata/MaxStatesCount/.solidhunter.json | 12 ++++++ .../testdata/MaxStatesCount/file.sol | 20 ++++++++++ .../testdata/MaxStatesCount/findings.csv | 1 + .../core/solidhunter-lib/tests/linter.rs | 3 +- 6 files changed, 63 insertions(+), 24 deletions(-) create mode 100644 toolchains/solidity/linter/core/solidhunter-lib/testdata/MaxStatesCount/.solidhunter.json create mode 100644 toolchains/solidity/linter/core/solidhunter-lib/testdata/MaxStatesCount/file.sol create mode 100644 toolchains/solidity/linter/core/solidhunter-lib/testdata/MaxStatesCount/findings.csv diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/max_states_count.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/max_states_count.rs index 5b1eeb16..71825232 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/max_states_count.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/max_states_count.rs @@ -1,8 +1,12 @@ use crate::linter::SolidFile; use crate::rules::types::*; use crate::types::*; +use ast_extractor::*; + + +pub const RULE_ID: &'static str = "max-states-count"; +const DEFAULT_MAX_STATES: usize = 15; -/* pub struct MaxStatesCount { max_states: usize, data: RuleEntry, @@ -12,11 +16,11 @@ impl MaxStatesCount { fn create_diag( &self, file: &SolidFile, - location: (CodeLocation, CodeLocation), + location: (LineColumn, LineColumn), count: usize, ) -> LintDiag { LintDiag { - id: Self::RULE_ID.to_string(), + id: RULE_ID.to_string(), range: Range { start: Position { line: location.0.line as u64, @@ -42,35 +46,39 @@ impl RuleType for MaxStatesCount { let mut res = Vec::new(); let mut count = 0; + let contracts = retriever::retrieve_contract_nodes(&file.data); // var def => contract def - for node in file.data.nodes.iter() { - let contract = match node { - SourceUnitChildNodes::ContractDefinition(contract) => contract, - _ => continue, - }; - for node_var in contract.nodes.iter() { + for contract in contracts.iter() { + for node_var in contract.body.iter() { let var = match node_var { - ContractDefinitionChildNodes::VariableDeclaration(var) => var, + Item::Variable(var) => var, _ => continue, }; count += 1; if count > self.max_states { - let location = decode_location(&var.src, &file.content); + let location = (var.span().start(), var.span().end()); res.push(self.create_diag(file, location, count)); } } } + println!("res: {:?}", res); res } } impl MaxStatesCount { - pub(crate) const RULE_ID: &'static str = "max-states-count"; pub(crate) fn create(data: RuleEntry) -> Box { + let mut max_states = DEFAULT_MAX_STATES; + if data.data.len() > 0 { + max_states = match data.data[0].parse::() { + Ok(v) => v, + Err(_) => DEFAULT_MAX_STATES, + }; + } let rule = MaxStatesCount { - max_states: data.data[0].parse::().unwrap(), + max_states, data, }; Box::new(rule) @@ -78,11 +86,10 @@ impl MaxStatesCount { pub(crate) fn create_default() -> RuleEntry { RuleEntry { - id: MaxStatesCount::RULE_ID.to_string(), + id: RULE_ID.to_string(), severity: Severity::WARNING, data: vec!["15".to_string()], } } } -*/ \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/mod.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/mod.rs index 63c87040..0dd8233a 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/mod.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/mod.rs @@ -5,23 +5,22 @@ use std::collections::HashMap; pub mod line_maxlen; pub mod function_max_lines; pub mod max_states_count; -pub mod reason_string; +//pub mod reason_string; // List all rules use crate::rules::best_practises::function_max_lines::FunctionMaxLines; use crate::rules::best_practises::line_maxlen::LineMaxLen; -// use crate::rules::best_practises::max_states_count::MaxStatesCount; +use crate::rules::best_practises::max_states_count::MaxStatesCount; // use crate::rules::best_practises::reason_string::ReasonString; use crate::rules::RuleBuilder; pub fn create_default_rules() -> Vec { vec![ LineMaxLen::create_default(), -// MaxStatesCount::create_default(), + MaxStatesCount::create_default(), FunctionMaxLines::create_default(), - ReasonString::create_default(), - */ +// ReasonString::create_default(), ] } @@ -31,13 +30,12 @@ pub fn create_rules() -> HashMap Box> { rules.insert(line_maxlen::RULE_ID.to_string(), LineMaxLen::create); -// rules.insert(MaxStatesCount::RULE_ID.to_string(), MaxStatesCount::create); + rules.insert(max_states_count::RULE_ID.to_string(), MaxStatesCount::create); rules.insert( FunctionMaxLines::RULE_ID.to_string(), FunctionMaxLines::create, ); - rules.insert(reason_string::RULE_ID.to_string(), ReasonString::create); - */ +// rules.insert(reason_string::RULE_ID.to_string(), ReasonString::create); rules } diff --git a/toolchains/solidity/linter/core/solidhunter-lib/testdata/MaxStatesCount/.solidhunter.json b/toolchains/solidity/linter/core/solidhunter-lib/testdata/MaxStatesCount/.solidhunter.json new file mode 100644 index 00000000..9a520219 --- /dev/null +++ b/toolchains/solidity/linter/core/solidhunter-lib/testdata/MaxStatesCount/.solidhunter.json @@ -0,0 +1,12 @@ +{ + "name": "solidhunter", + "includes": [], + "plugins": [], + "rules": [ + { + "id": "max-states-count", + "severity": "WARNING", + "data": [] + } + ] +} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solidhunter-lib/testdata/MaxStatesCount/file.sol b/toolchains/solidity/linter/core/solidhunter-lib/testdata/MaxStatesCount/file.sol new file mode 100644 index 00000000..d7a4124d --- /dev/null +++ b/toolchains/solidity/linter/core/solidhunter-lib/testdata/MaxStatesCount/file.sol @@ -0,0 +1,20 @@ +pragma solidity 0.8.0; + +contract Test { + uint a = 1; + uint b = 2; + uint c = 3; + uint d = 4; + uint e = 5; + uint f = 6; + uint g = 7; + uint h = 8; + uint i = 9; + uint j = 10; + uint k = 11; + uint l = 12; + uint m = 13; + uint n = 14; + uint o = 15; + uint p = 16; +} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solidhunter-lib/testdata/MaxStatesCount/findings.csv b/toolchains/solidity/linter/core/solidhunter-lib/testdata/MaxStatesCount/findings.csv new file mode 100644 index 00000000..8969b615 --- /dev/null +++ b/toolchains/solidity/linter/core/solidhunter-lib/testdata/MaxStatesCount/findings.csv @@ -0,0 +1 @@ +max-states-count:19:4:19:16 \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solidhunter-lib/tests/linter.rs b/toolchains/solidity/linter/core/solidhunter-lib/tests/linter.rs index f38ba20d..c9feb289 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/tests/linter.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/tests/linter.rs @@ -90,5 +90,6 @@ macro_rules! test_directories { test_directories! { ContractNamePascalCase, FunctionMaxLines, - ImportOnTop + ImportOnTop, + MaxStatesCount } From ee0ba07dfaa3ffeb92f2b30303b433e3a72b607e Mon Sep 17 00:00:00 2001 From: 0xtekgrinder <0xtekgrinder@protonmail.com> Date: Fri, 15 Sep 2023 01:12:34 -0400 Subject: [PATCH 37/59] refactor(solidity/linter/core): use LineColumn instead of Range --- .../rules/naming/contract_name_pascalcase.rs | 42 ++++++++++--------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/contract_name_pascalcase.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/contract_name_pascalcase.rs index c8cd9b19..4a6cc9af 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/contract_name_pascalcase.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/contract_name_pascalcase.rs @@ -12,10 +12,23 @@ pub struct ContractNamePascalCase { } impl ContractNamePascalCase { - fn create_diag(&self, location: Range, file: &SolidFile) -> LintDiag { + fn create_diag( + &self, + location: (ast_extractor::LineColumn, ast_extractor::LineColumn), + file: &SolidFile, + ) -> LintDiag { LintDiag { id: RULE_ID.to_string(), - range: location, + range: Range { + start: Position { + line: location.0.line as u64, + character: location.0.column as u64, + }, + end: Position { + line: location.1.line as u64, + character: location.1.column as u64, + }, + }, message: MESSAGE.to_string(), severity: Some(self.data.severity), code: None, @@ -33,24 +46,13 @@ impl RuleType for ContractNamePascalCase { for contract in contracts { if (contract.name.as_string().chars().nth(0).unwrap() >= 'a' - && contract.name.as_string().chars().nth(0).unwrap() <= 'z') - || contract.name.as_string().contains('_') - || contract.name.as_string().contains('-') - { - res.push(self.create_diag({ - let location = contract.name.span(); - Range { - start: Position { - line: location.start().line as u64, - character: location.start().column as u64, - }, - end: Position { - line: location.end().line as u64, - character: location.end().column as u64, - }, - } - }, file)); - } + && contract.name.as_string().chars().nth(0).unwrap() <= 'z') + || contract.name.as_string().contains('_') + || contract.name.as_string().contains('-') + { + let span = contract.name.span(); + res.push(self.create_diag((span.start(), span.end()), file)); + } } res } From d745f20548f847fc9c2d33823dfb31293fa37efa Mon Sep 17 00:00:00 2001 From: 0xtekgrinder <0xtekgrinder@protonmail.com> Date: Fri, 15 Sep 2023 01:13:04 -0400 Subject: [PATCH 38/59] feat(solidity/linter/core): FunctionNameCamelCase rule with alloy --- .../src/rules/naming/func_name_camelcase.rs | 57 ++++++++----------- .../solidhunter-lib/src/rules/naming/mod.rs | 9 ++- .../solidhunter-lib/src/rules/rule_impl.rs | 7 ++- .../FunctionNameCamelCase/.solidhunter.json | 12 ++++ .../testdata/FunctionNameCamelCase/file.sol | 7 +++ .../FunctionNameCamelCase/findings.csv | 1 + .../core/solidhunter-lib/tests/linter.rs | 10 +++- 7 files changed, 61 insertions(+), 42 deletions(-) create mode 100644 toolchains/solidity/linter/core/solidhunter-lib/testdata/FunctionNameCamelCase/.solidhunter.json create mode 100644 toolchains/solidity/linter/core/solidhunter-lib/testdata/FunctionNameCamelCase/file.sol create mode 100644 toolchains/solidity/linter/core/solidhunter-lib/testdata/FunctionNameCamelCase/findings.csv diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_name_camelcase.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_name_camelcase.rs index f4815546..2017b9f2 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_name_camelcase.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_name_camelcase.rs @@ -1,17 +1,24 @@ +use ast_extractor::Spanned; + use crate::linter::SolidFile; use crate::rules::types::*; use crate::types::*; -/* +pub const RULE_ID: &str = "func-name-camelcase"; +const MESSAGE: &str = "Function name need to be in camel case"; pub struct FuncNameCamelCase { data: RuleEntry, } impl FuncNameCamelCase { - fn create_diag(&self, location: (CodeLocation, CodeLocation), file: &SolidFile) -> LintDiag { + fn create_diag( + &self, + location: (ast_extractor::LineColumn, ast_extractor::LineColumn), + file: &SolidFile, + ) -> LintDiag { LintDiag { - id: "func-name-camelcase".to_string(), + id: RULE_ID.to_string(), range: Range { start: Position { line: location.0.line as u64, @@ -22,7 +29,7 @@ impl FuncNameCamelCase { character: location.1.column as u64, }, }, - message: "Function name need to be in camel case".to_string(), + message: MESSAGE.to_string(), severity: Some(self.data.severity), code: None, source: None, @@ -35,36 +42,20 @@ impl FuncNameCamelCase { impl RuleType for FuncNameCamelCase { fn diagnose(&self, file: &SolidFile, _files: &Vec) -> Vec { let mut res = Vec::new(); + let contracts = ast_extractor::retriever::retrieve_contract_nodes(&file.data); - for node in &file.data.nodes { - match node { - SourceUnitChildNodes::ContractDefinition(contract) => { - for node in &contract.nodes { - match node { - ContractDefinitionChildNodes::FunctionDefinition(function) => { - if function.kind != FunctionDefinitionKind::Constructor - && (!(function.name.chars().nth(0).unwrap_or(' ') >= 'a' - && function.name.chars().nth(0).unwrap_or(' ') <= 'z') - || function.name.contains('_') - || function.name.contains('-')) - { - //Untested - let location = decode_location( - function.name_location.as_ref().unwrap(), - &file.content, - ); - res.push(self.create_diag(location, file)); - } - } - _ => { - continue; - } - } + for contract in contracts { + for function in ast_extractor::retriever::retrieve_functions_nodes(&contract) { + if let Some(name) = function.name { + if !(name.as_string().chars().nth(0).unwrap_or(' ') >= 'a' + && name.as_string().chars().nth(0).unwrap_or(' ') <= 'z') + || name.as_string().contains('_') + || name.as_string().contains('-') + { + let span = name.span(); + res.push(self.create_diag((span.start(), span.end()), file)); } } - _ => { - continue; - } } } res @@ -79,11 +70,9 @@ impl FuncNameCamelCase { pub(crate) fn create_default() -> RuleEntry { RuleEntry { - id: "func-name-camelcase".to_string(), + id: RULE_ID.to_string(), severity: Severity::WARNING, data: vec![], } } } - -*/ \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/mod.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/mod.rs index 5c5de3c9..0bca8472 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/mod.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/mod.rs @@ -1,7 +1,7 @@ use crate::rules::naming::contract_name_pascalcase::ContractNamePascalCase; +use crate::rules::naming::func_name_camelcase::FuncNameCamelCase; /* -use crate::rules::naming::func_name_camelcase::FuncNameCamelCase; use crate::rules::naming::func_param_name_camelcase::FuncParamNameCamelcase; use crate::rules::naming::use_forbidden_name::UseForbiddenName; */ @@ -20,9 +20,9 @@ pub(crate) mod use_forbidden_name; pub fn create_default_rules() -> Vec { vec![ ContractNamePascalCase::create_default(), + FuncNameCamelCase::create_default(), /* FuncParamNameCamelcase::create_default(), - FuncNameCamelCase::create_default(), UseForbiddenName::create_default(), */ ] @@ -35,12 +35,15 @@ pub fn create_rules() -> HashMap Box> { contract_name_pascalcase::RULE_ID.to_string(), ContractNamePascalCase::create, ); + rules.insert( + func_name_camelcase::RULE_ID.to_string(), + FuncNameCamelCase::create, + ); /* rules.insert( "func-param-name-camelcase".to_string(), FuncParamNameCamelcase::create, ); - rules.insert("func-name-camelcase".to_string(), FuncNameCamelCase::create); rules.insert( UseForbiddenName::RULE_ID.to_string(), UseForbiddenName::create, diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs index ef52aa07..a8a36e3a 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs @@ -16,9 +16,10 @@ pub fn create_rules_file(path: &str) { pub fn parse_rules(path: &str) -> Result { if !std::path::Path::new(&path).is_file() { - return Err(SolidHunterError::IoError( - std::io::Error::new(std::io::ErrorKind::NotFound, "Rules file not found") - )); + return Err(SolidHunterError::IoError(std::io::Error::new( + std::io::ErrorKind::NotFound, + "Rules file not found", + ))); } let file = std::fs::read_to_string(path)?; let parsed: Rules = serde_json::from_str(&file)?; diff --git a/toolchains/solidity/linter/core/solidhunter-lib/testdata/FunctionNameCamelCase/.solidhunter.json b/toolchains/solidity/linter/core/solidhunter-lib/testdata/FunctionNameCamelCase/.solidhunter.json new file mode 100644 index 00000000..f623e561 --- /dev/null +++ b/toolchains/solidity/linter/core/solidhunter-lib/testdata/FunctionNameCamelCase/.solidhunter.json @@ -0,0 +1,12 @@ +{ + "name": "solidhunter", + "includes": [], + "plugins": [], + "rules": [ + { + "id": "func-name-camelcase", + "severity": "WARNING", + "data": [] + } + ] +} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solidhunter-lib/testdata/FunctionNameCamelCase/file.sol b/toolchains/solidity/linter/core/solidhunter-lib/testdata/FunctionNameCamelCase/file.sol new file mode 100644 index 00000000..de8ffdb7 --- /dev/null +++ b/toolchains/solidity/linter/core/solidhunter-lib/testdata/FunctionNameCamelCase/file.sol @@ -0,0 +1,7 @@ +pragma solidity 0.8.0; + +contract Test { + function test_shbs() public pure returns (uint) { + return 1; + } +} diff --git a/toolchains/solidity/linter/core/solidhunter-lib/testdata/FunctionNameCamelCase/findings.csv b/toolchains/solidity/linter/core/solidhunter-lib/testdata/FunctionNameCamelCase/findings.csv new file mode 100644 index 00000000..58982bcd --- /dev/null +++ b/toolchains/solidity/linter/core/solidhunter-lib/testdata/FunctionNameCamelCase/findings.csv @@ -0,0 +1 @@ +func-name-camelcase:4:13:4:22 \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solidhunter-lib/tests/linter.rs b/toolchains/solidity/linter/core/solidhunter-lib/tests/linter.rs index c9feb289..7c989d52 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/tests/linter.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/tests/linter.rs @@ -55,7 +55,12 @@ fn test_linter(config: &str, source: &str, expected_findings: &Vec) { let result = linter.parse_file(String::from(source)); match result { Ok(diags) => { - assert_eq!(diags.len(), expected_findings.len(), "Wrong number of findings for {}", source); + assert_eq!( + diags.len(), + expected_findings.len(), + "Wrong number of findings for {}", + source + ); let mut found = false; for (_, diag) in diags.iter().enumerate() { @@ -91,5 +96,6 @@ test_directories! { ContractNamePascalCase, FunctionMaxLines, ImportOnTop, - MaxStatesCount + MaxStatesCount, + FunctionNameCamelCase } From 9fd70220de0cce6744f1b792abfbac393162d309 Mon Sep 17 00:00:00 2001 From: 0xtekgrinder <0xtekgrinder@protonmail.com> Date: Fri, 15 Sep 2023 15:20:37 -0400 Subject: [PATCH 39/59] feat(solidity/linter/core): func-param-camel-case rule --- .../rules/naming/func_param_name_camelcase.rs | 57 ++++++++----------- .../solidhunter-lib/src/rules/naming/mod.rs | 8 +-- .../.solidhunter.json | 12 ++++ .../FunctionParamNameCamelCase/file.sol | 7 +++ .../FunctionParamNameCamelCase/findings.csv | 1 + .../core/solidhunter-lib/tests/linter.rs | 3 +- 6 files changed, 49 insertions(+), 39 deletions(-) create mode 100644 toolchains/solidity/linter/core/solidhunter-lib/testdata/FunctionParamNameCamelCase/.solidhunter.json create mode 100644 toolchains/solidity/linter/core/solidhunter-lib/testdata/FunctionParamNameCamelCase/file.sol create mode 100644 toolchains/solidity/linter/core/solidhunter-lib/testdata/FunctionParamNameCamelCase/findings.csv diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_param_name_camelcase.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_param_name_camelcase.rs index 6fea45ad..1f617ea5 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_param_name_camelcase.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_param_name_camelcase.rs @@ -1,17 +1,23 @@ use crate::linter::SolidFile; use crate::rules::types::*; use crate::types::*; +use ast_extractor::Spanned; -/* +pub const RULE_ID: &str = "func-param-name-camelcase"; +const MESSAGE: &str = "Parameter name need to be in camel case"; pub struct FuncParamNameCamelcase { data: RuleEntry, } impl FuncParamNameCamelcase { - fn create_diag(&self, location: (CodeLocation, CodeLocation), file: &SolidFile) -> LintDiag { + fn create_diag( + &self, + location: (ast_extractor::LineColumn, ast_extractor::LineColumn), + file: &SolidFile, + ) -> LintDiag { LintDiag { - id: "func-param-name-camelcase".to_string(), + id: RULE_ID.to_string(), range: Range { start: Position { line: location.0.line as u64, @@ -22,7 +28,7 @@ impl FuncParamNameCamelcase { character: location.1.column as u64, }, }, - message: "Parameter name need to be in camel case".to_string(), + message: MESSAGE.to_string(), severity: Some(self.data.severity), code: None, source: None, @@ -35,37 +41,22 @@ impl FuncParamNameCamelcase { impl RuleType for FuncParamNameCamelcase { fn diagnose(&self, file: &SolidFile, _files: &Vec) -> Vec { let mut res = Vec::new(); + let contracts = ast_extractor::retriever::retrieve_contract_nodes(&file.data); - for node in &file.data.nodes { - match node { - SourceUnitChildNodes::ContractDefinition(contract) => { - for node in &contract.nodes { - match node { - ContractDefinitionChildNodes::FunctionDefinition(function) => { - for parameter in &function.parameters.parameters { - if !(parameter.name.chars().nth(0).unwrap() >= 'a' - && parameter.name.chars().nth(0).unwrap() <= 'z') - || parameter.name.contains('_') - || parameter.name.contains('-') - { - //Untested - let location = decode_location( - parameter.name_location.as_ref().unwrap(), - &file.content, - ); - res.push(self.create_diag(location, file)); - } - } - } - _ => { - continue; - } + for contract in contracts { + for function in ast_extractor::retriever::retrieve_functions_nodes(&contract) { + for arg in function.arguments.iter() { + if let Some(name) = &arg.name { + if !(name.as_string().chars().nth(0).unwrap() >= 'a' + && name.as_string().chars().nth(0).unwrap() <= 'z') + || name.as_string().contains('_') + || name.as_string().contains('-') + { + let span = name.span(); + res.push(self.create_diag((span.start(), span.end()), file)); } } } - _ => { - continue; - } } } res @@ -80,11 +71,9 @@ impl FuncParamNameCamelcase { pub(crate) fn create_default() -> RuleEntry { RuleEntry { - id: "func-param-name-camelcase".to_string(), + id: RULE_ID.to_string(), severity: Severity::WARNING, data: vec![], } } } - -*/ \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/mod.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/mod.rs index 0bca8472..ee04c057 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/mod.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/mod.rs @@ -2,9 +2,9 @@ use crate::rules::naming::contract_name_pascalcase::ContractNamePascalCase; use crate::rules::naming::func_name_camelcase::FuncNameCamelCase; /* -use crate::rules::naming::func_param_name_camelcase::FuncParamNameCamelcase; use crate::rules::naming::use_forbidden_name::UseForbiddenName; */ +use crate::rules::naming::func_param_name_camelcase::FuncParamNameCamelcase; use crate::rules::types::{RuleEntry, RuleType}; use crate::rules::RuleBuilder; use std::collections::HashMap; @@ -21,8 +21,8 @@ pub fn create_default_rules() -> Vec { vec![ ContractNamePascalCase::create_default(), FuncNameCamelCase::create_default(), - /* FuncParamNameCamelcase::create_default(), + /* UseForbiddenName::create_default(), */ ] @@ -39,11 +39,11 @@ pub fn create_rules() -> HashMap Box> { func_name_camelcase::RULE_ID.to_string(), FuncNameCamelCase::create, ); - /* rules.insert( - "func-param-name-camelcase".to_string(), + func_param_name_camelcase::RULE_ID.to_string(), FuncParamNameCamelcase::create, ); + /* rules.insert( UseForbiddenName::RULE_ID.to_string(), UseForbiddenName::create, diff --git a/toolchains/solidity/linter/core/solidhunter-lib/testdata/FunctionParamNameCamelCase/.solidhunter.json b/toolchains/solidity/linter/core/solidhunter-lib/testdata/FunctionParamNameCamelCase/.solidhunter.json new file mode 100644 index 00000000..cc4a2a76 --- /dev/null +++ b/toolchains/solidity/linter/core/solidhunter-lib/testdata/FunctionParamNameCamelCase/.solidhunter.json @@ -0,0 +1,12 @@ +{ + "name": "solidhunter", + "includes": [], + "plugins": [], + "rules": [ + { + "id": "func-param-name-camelcase", + "severity": "WARNING", + "data": [] + } + ] +} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solidhunter-lib/testdata/FunctionParamNameCamelCase/file.sol b/toolchains/solidity/linter/core/solidhunter-lib/testdata/FunctionParamNameCamelCase/file.sol new file mode 100644 index 00000000..7399dec8 --- /dev/null +++ b/toolchains/solidity/linter/core/solidhunter-lib/testdata/FunctionParamNameCamelCase/file.sol @@ -0,0 +1,7 @@ +pragma solidity 0.8.0; + +contract Test { + function test(uint256 snake_case) public pure returns (uint256) { + return snake_case; + } +} diff --git a/toolchains/solidity/linter/core/solidhunter-lib/testdata/FunctionParamNameCamelCase/findings.csv b/toolchains/solidity/linter/core/solidhunter-lib/testdata/FunctionParamNameCamelCase/findings.csv new file mode 100644 index 00000000..2011650b --- /dev/null +++ b/toolchains/solidity/linter/core/solidhunter-lib/testdata/FunctionParamNameCamelCase/findings.csv @@ -0,0 +1 @@ +func-param-name-camelcase:4:26:4:36 \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solidhunter-lib/tests/linter.rs b/toolchains/solidity/linter/core/solidhunter-lib/tests/linter.rs index 7c989d52..723e5588 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/tests/linter.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/tests/linter.rs @@ -97,5 +97,6 @@ test_directories! { FunctionMaxLines, ImportOnTop, MaxStatesCount, - FunctionNameCamelCase + FunctionNameCamelCase, + FunctionParamNameCamelCase } From ca53f87d08f48583fa29f4e1a496d8ba1f0f2988 Mon Sep 17 00:00:00 2001 From: 0xtekgrinder <0xtekgrinder@protonmail.com> Date: Fri, 15 Sep 2023 16:06:31 -0400 Subject: [PATCH 40/59] feat(solidity/linter/core): add UseForbiddenName rule with alloy --- .../solidhunter-lib/src/rules/naming/mod.rs | 10 +---- .../src/rules/naming/use_forbidden_name.rs | 41 ++++++++++--------- .../UseForbiddenName/.solidhunter.json | 12 ++++++ .../testdata/UseForbiddenName/file.sol | 7 ++++ .../testdata/UseForbiddenName/findings.csv | 3 ++ .../core/solidhunter-lib/tests/linter.rs | 3 +- 6 files changed, 48 insertions(+), 28 deletions(-) create mode 100644 toolchains/solidity/linter/core/solidhunter-lib/testdata/UseForbiddenName/.solidhunter.json create mode 100644 toolchains/solidity/linter/core/solidhunter-lib/testdata/UseForbiddenName/file.sol create mode 100644 toolchains/solidity/linter/core/solidhunter-lib/testdata/UseForbiddenName/findings.csv diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/mod.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/mod.rs index ee04c057..4248b2d7 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/mod.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/mod.rs @@ -1,10 +1,8 @@ use crate::rules::naming::contract_name_pascalcase::ContractNamePascalCase; use crate::rules::naming::func_name_camelcase::FuncNameCamelCase; -/* -use crate::rules::naming::use_forbidden_name::UseForbiddenName; -*/ use crate::rules::naming::func_param_name_camelcase::FuncParamNameCamelcase; +use crate::rules::naming::use_forbidden_name::UseForbiddenName; use crate::rules::types::{RuleEntry, RuleType}; use crate::rules::RuleBuilder; use std::collections::HashMap; @@ -22,9 +20,7 @@ pub fn create_default_rules() -> Vec { ContractNamePascalCase::create_default(), FuncNameCamelCase::create_default(), FuncParamNameCamelcase::create_default(), - /* UseForbiddenName::create_default(), - */ ] } @@ -43,12 +39,10 @@ pub fn create_rules() -> HashMap Box> { func_param_name_camelcase::RULE_ID.to_string(), FuncParamNameCamelcase::create, ); - /* rules.insert( - UseForbiddenName::RULE_ID.to_string(), + use_forbidden_name::RULE_ID.to_string(), UseForbiddenName::create, ); - */ rules } diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/use_forbidden_name.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/use_forbidden_name.rs index 46d3fb5b..2924f955 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/use_forbidden_name.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/use_forbidden_name.rs @@ -1,8 +1,10 @@ use crate::linter::SolidFile; use crate::rules::types::*; use crate::types::*; +use ast_extractor::Spanned; +use ast_extractor::*; -/* +pub const RULE_ID: &str = "use-forbidden-name"; pub struct UseForbiddenName { data: RuleEntry, @@ -11,12 +13,12 @@ pub struct UseForbiddenName { impl UseForbiddenName { fn create_diag( &self, - location: (CodeLocation, CodeLocation), - var: Box, file: &SolidFile, + location: (LineColumn, LineColumn), + name: &String, ) -> LintDiag { LintDiag { - id: "use-forbidden-name".to_string(), + id: RULE_ID.to_string(), range: Range { start: Position { line: location.0.line as u64, @@ -27,7 +29,7 @@ impl UseForbiddenName { character: location.1.column as u64, }, }, - message: format!("Forbidden variable name: {}", var.name), + message: format!("Forbidden variable name: {}", name), severity: Some(self.data.severity), code: None, source: None, @@ -42,16 +44,21 @@ impl RuleType for UseForbiddenName { let mut res = Vec::new(); let blacklist = ['I', 'l', 'O']; - let nodes = get_all_nodes_by_type(file.data.clone(), NodeType::VariableDeclaration); + let contracts = retriever::retrieve_contract_nodes(&file.data); - for node in nodes { - let var = match node { - Nodes::VariableDeclaration(var) => var, - _ => continue, - }; - if var.name.len() == 1 && blacklist.contains(&var.name.chars().next().unwrap()) { - let location = decode_location(&var.src, &file.content); - res.push(self.create_diag(location, var, file)); + // var def => contract def + for contract in contracts.iter() { + for node_var in contract.body.iter() { + let var = match node_var { + Item::Variable(var) => var, + _ => continue, + }; + if var.name.as_string().len() == 1 + && blacklist.contains(&var.name.as_string().chars().next().unwrap()) + { + let location = (var.name.span().start(), var.name.span().end()); + res.push(self.create_diag(file, location, &var.name.as_string())); + } } } res @@ -59,8 +66,6 @@ impl RuleType for UseForbiddenName { } impl UseForbiddenName { - pub const RULE_ID: &'static str = "use-forbidden-name"; - pub(crate) fn create(data: RuleEntry) -> Box { let rule = UseForbiddenName { data }; Box::new(rule) @@ -68,11 +73,9 @@ impl UseForbiddenName { pub(crate) fn create_default() -> RuleEntry { RuleEntry { - id: UseForbiddenName::RULE_ID.to_string(), + id: RULE_ID.to_string(), severity: Severity::WARNING, data: vec![], } } } - -*/ \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solidhunter-lib/testdata/UseForbiddenName/.solidhunter.json b/toolchains/solidity/linter/core/solidhunter-lib/testdata/UseForbiddenName/.solidhunter.json new file mode 100644 index 00000000..0cbc855f --- /dev/null +++ b/toolchains/solidity/linter/core/solidhunter-lib/testdata/UseForbiddenName/.solidhunter.json @@ -0,0 +1,12 @@ +{ + "name": "solidhunter", + "includes": [], + "plugins": [], + "rules": [ + { + "id": "use-forbidden-name", + "severity": "WARNING", + "data": [] + } + ] +} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solidhunter-lib/testdata/UseForbiddenName/file.sol b/toolchains/solidity/linter/core/solidhunter-lib/testdata/UseForbiddenName/file.sol new file mode 100644 index 00000000..f99f3eab --- /dev/null +++ b/toolchains/solidity/linter/core/solidhunter-lib/testdata/UseForbiddenName/file.sol @@ -0,0 +1,7 @@ +pragma solidity 0.8.0; + +contract Test { + uint256 public I; + uint256 public l; + uint256 public O; +} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solidhunter-lib/testdata/UseForbiddenName/findings.csv b/toolchains/solidity/linter/core/solidhunter-lib/testdata/UseForbiddenName/findings.csv new file mode 100644 index 00000000..5f69d1f9 --- /dev/null +++ b/toolchains/solidity/linter/core/solidhunter-lib/testdata/UseForbiddenName/findings.csv @@ -0,0 +1,3 @@ +use-forbidden-name:4:19:4:20 +use-forbidden-name:5:19:5:20 +use-forbidden-name:5:19:5:20 \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solidhunter-lib/tests/linter.rs b/toolchains/solidity/linter/core/solidhunter-lib/tests/linter.rs index 723e5588..f44e4517 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/tests/linter.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/tests/linter.rs @@ -98,5 +98,6 @@ test_directories! { ImportOnTop, MaxStatesCount, FunctionNameCamelCase, - FunctionParamNameCamelCase + FunctionParamNameCamelCase, + UseForbiddenName } From 9299e15dd1da0e9e9aefe44b486dc920a097e9cb Mon Sep 17 00:00:00 2001 From: EnergyCube Date: Sat, 16 Sep 2023 19:28:55 +0200 Subject: [PATCH 41/59] feat(solidity/linter/core): added WIP ReasonString rule with alloy --- .../src/rules/best_practises/mod.rs | 14 ++- .../src/rules/best_practises/reason_string.rs | 104 +++++++++--------- .../testdata/ReasonString/.solidhunter.json | 14 +++ .../testdata/ReasonString/file.sol | 13 +++ .../testdata/ReasonString/findings.csv | 2 + .../core/solidhunter-lib/tests/linter.rs | 1 + 6 files changed, 90 insertions(+), 58 deletions(-) create mode 100644 toolchains/solidity/linter/core/solidhunter-lib/testdata/ReasonString/.solidhunter.json create mode 100644 toolchains/solidity/linter/core/solidhunter-lib/testdata/ReasonString/file.sol create mode 100644 toolchains/solidity/linter/core/solidhunter-lib/testdata/ReasonString/findings.csv diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/mod.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/mod.rs index 0dd8233a..dc12d3de 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/mod.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/mod.rs @@ -5,14 +5,14 @@ use std::collections::HashMap; pub mod line_maxlen; pub mod function_max_lines; pub mod max_states_count; -//pub mod reason_string; +pub mod reason_string; // List all rules use crate::rules::best_practises::function_max_lines::FunctionMaxLines; use crate::rules::best_practises::line_maxlen::LineMaxLen; use crate::rules::best_practises::max_states_count::MaxStatesCount; -// use crate::rules::best_practises::reason_string::ReasonString; +use crate::rules::best_practises::reason_string::ReasonString; use crate::rules::RuleBuilder; pub fn create_default_rules() -> Vec { @@ -20,7 +20,7 @@ pub fn create_default_rules() -> Vec { LineMaxLen::create_default(), MaxStatesCount::create_default(), FunctionMaxLines::create_default(), -// ReasonString::create_default(), + ReasonString::create_default(), ] } @@ -29,13 +29,15 @@ pub fn create_rules() -> HashMap Box> { rules.insert(line_maxlen::RULE_ID.to_string(), LineMaxLen::create); - - rules.insert(max_states_count::RULE_ID.to_string(), MaxStatesCount::create); + rules.insert( + max_states_count::RULE_ID.to_string(), + MaxStatesCount::create, + ); rules.insert( FunctionMaxLines::RULE_ID.to_string(), FunctionMaxLines::create, ); -// rules.insert(reason_string::RULE_ID.to_string(), ReasonString::create); + rules.insert(reason_string::RULE_ID.to_string(), ReasonString::create); rules } diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/reason_string.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/reason_string.rs index 9921407b..b21b3b4e 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/reason_string.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/reason_string.rs @@ -1,8 +1,9 @@ +use ast_extractor::LineColumn; + use crate::linter::SolidFile; use crate::rules::types::{RuleEntry, RuleType}; use crate::types::{LintDiag, Position, Range, Severity}; -/* pub const RULE_ID: &str = "reason-string"; const DEFAULT_SEVERITY: Severity = Severity::WARNING; @@ -18,7 +19,7 @@ impl ReasonString { fn create_diag( &self, file: &SolidFile, - location: (CodeLocation, CodeLocation), + location: (LineColumn, LineColumn), message: String, ) -> LintDiag { LintDiag { @@ -47,54 +48,55 @@ impl RuleType for ReasonString { fn diagnose(&self, file: &SolidFile, _files: &Vec) -> Vec { let mut res = Vec::new(); - let nodes = get_all_nodes_by_type(file.data.clone(), NodeType::FunctionCall); - for i in &nodes { - match i { - utils::Nodes::FunctionCall(j) => match &j.expression { - Expression::Identifier(v) => { - if v.name == "require" { - if j.arguments.len() != 2 { - let location = decode_location(&j.src, &file.content); - res.push(self.create_diag(file, location, "reason-string: A require statement must have a reason string".to_string())); - } else { - for nj in &j.arguments { - match nj { - Expression::Literal(z) => { - if z.value.clone().unwrap().len() - > self.max_length as usize - { - let location = - decode_location(&z.src, &file.content); - res.push(self.create_diag(file, location, format!("reason-string: A revert statement must have a reason string of length less than {}", self.max_length))); - } - } - _ => {} - } - } - } - } else if v.name == "revert" { - if j.arguments.is_empty() { - let location = decode_location(&j.src, &file.content); - res.push(self.create_diag(file, location, "reason-string: A revert statement must have a reason string".to_string())); - } else { - match &j.arguments[0] { - Expression::Literal(z) => { - if z.value.clone().unwrap().len() > self.max_length as usize - { - let location = decode_location(&z.src, &file.content); - res.push(self.create_diag(file, location, format!("reason-string: A revert statement must have a reason string of length less than {}", self.max_length))); - } - } - _ => {} - } - } - } - } - _ => {} - }, - _ => {} - } - } + println!("debug"); + // let nodes = get_all_nodes_by_type(file.data.clone(), NodeType::FunctionCall); + // for i in &nodes { + // match i { + // utils::Nodes::FunctionCall(j) => match &j.expression { + // Expression::Identifier(v) => { + // if v.name == "require" { + // if j.arguments.len() != 2 { + // let location = decode_location(&j.src, &file.content); + // res.push(self.create_diag(file, location, "reason-string: A require statement must have a reason string".to_string())); + // } else { + // for nj in &j.arguments { + // match nj { + // Expression::Literal(z) => { + // if z.value.clone().unwrap().len() + // > self.max_length as usize + // { + // let location = + // decode_location(&z.src, &file.content); + // res.push(self.create_diag(file, location, format!("reason-string: A revert statement must have a reason string of length less than {}", self.max_length))); + // } + // } + // _ => {} + // } + // } + // } + // } else if v.name == "revert" { + // if j.arguments.is_empty() { + // let location = decode_location(&j.src, &file.content); + // res.push(self.create_diag(file, location, "reason-string: A revert statement must have a reason string".to_string())); + // } else { + // match &j.arguments[0] { + // Expression::Literal(z) => { + // if z.value.clone().unwrap().len() > self.max_length as usize + // { + // let location = decode_location(&z.src, &file.content); + // res.push(self.create_diag(file, location, format!("reason-string: A revert statement must have a reason string of length less than {}", self.max_length))); + // } + // } + // _ => {} + // } + // } + // } + // } + // _ => {} + // }, + // _ => {} + // } + // } res } } @@ -116,5 +118,3 @@ impl ReasonString { } } } - -*/ \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solidhunter-lib/testdata/ReasonString/.solidhunter.json b/toolchains/solidity/linter/core/solidhunter-lib/testdata/ReasonString/.solidhunter.json new file mode 100644 index 00000000..20c03fed --- /dev/null +++ b/toolchains/solidity/linter/core/solidhunter-lib/testdata/ReasonString/.solidhunter.json @@ -0,0 +1,14 @@ +{ + "name": "solidhunter", + "includes": [], + "plugins": [], + "rules": [ + { + "id": "reason-string", + "severity": "WARNING", + "data": [ + "32" + ] + } + ] +} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solidhunter-lib/testdata/ReasonString/file.sol b/toolchains/solidity/linter/core/solidhunter-lib/testdata/ReasonString/file.sol new file mode 100644 index 00000000..44b09bf6 --- /dev/null +++ b/toolchains/solidity/linter/core/solidhunter-lib/testdata/ReasonString/file.sol @@ -0,0 +1,13 @@ +pragma solidity 0.8.0; + +contract Test { + function awesome() public { + require(!has(role, account), "This is perfect"); + } + function not_awesome() public { + require(!has(role, account), "This is not perfect at all because it's really too long but the code is 0xSwapFeeder compliant"); + } + function not_awesome_either() public { + require(!has(role, account)); + } +} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solidhunter-lib/testdata/ReasonString/findings.csv b/toolchains/solidity/linter/core/solidhunter-lib/testdata/ReasonString/findings.csv new file mode 100644 index 00000000..470a966f --- /dev/null +++ b/toolchains/solidity/linter/core/solidhunter-lib/testdata/ReasonString/findings.csv @@ -0,0 +1,2 @@ +reason-string:8:9:8:136 +reason-string:11:9:11:38 \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solidhunter-lib/tests/linter.rs b/toolchains/solidity/linter/core/solidhunter-lib/tests/linter.rs index f44e4517..1c83886f 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/tests/linter.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/tests/linter.rs @@ -100,4 +100,5 @@ test_directories! { FunctionNameCamelCase, FunctionParamNameCamelCase, UseForbiddenName + ReasonString, } From e73f9ad456a2d70287e0663d132549fa12f8a165 Mon Sep 17 00:00:00 2001 From: EnergyCube Date: Sat, 16 Sep 2023 19:42:37 +0200 Subject: [PATCH 42/59] feat(solidhunter): fixed tests folder listing --- toolchains/solidity/linter/core/solidhunter-lib/tests/linter.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/toolchains/solidity/linter/core/solidhunter-lib/tests/linter.rs b/toolchains/solidity/linter/core/solidhunter-lib/tests/linter.rs index 1c83886f..8d6883f0 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/tests/linter.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/tests/linter.rs @@ -99,6 +99,6 @@ test_directories! { MaxStatesCount, FunctionNameCamelCase, FunctionParamNameCamelCase, - UseForbiddenName + UseForbiddenName, ReasonString, } From 9e4b69cdbfa25b28f4061330bb86690ad290fd0b Mon Sep 17 00:00:00 2001 From: EnergyCube Date: Sun, 17 Sep 2023 00:20:25 +0200 Subject: [PATCH 43/59] feat(solidity/linter/core): completed ReasonString rule with alloy --- .../src/rules/best_practises/reason_string.rs | 136 +++++++++++------- .../testdata/ReasonString/findings.csv | 4 +- 2 files changed, 88 insertions(+), 52 deletions(-) diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/reason_string.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/reason_string.rs index b21b3b4e..55e7858c 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/reason_string.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/reason_string.rs @@ -1,4 +1,4 @@ -use ast_extractor::LineColumn; +use ast_extractor::{ArgListImpl, Expr, FunctionBody, LineColumn, Stmt}; use crate::linter::SolidFile; use crate::rules::types::{RuleEntry, RuleType}; @@ -44,59 +44,95 @@ impl ReasonString { } } +fn get_all_functions_from_ast(ast_nodes: &ast_extractor::File) -> Vec { + let mut res = Vec::new(); + let contract = ast_nodes + .items + .iter() + .filter_map(|item| match item { + ast_extractor::Item::Contract(contract) => Some(contract), + _ => None, + }) + .next(); + + if let Some(contract) = contract { + res = ast_extractor::retriever::retrieve_functions_nodes(contract); + } + res +} + impl RuleType for ReasonString { fn diagnose(&self, file: &SolidFile, _files: &Vec) -> Vec { let mut res = Vec::new(); + let allfn = get_all_functions_from_ast(&file.data); + + for func in allfn { + if let FunctionBody::Block(fn_body) = func.body { + for stmt in fn_body.stmts { + if let Stmt::Expr(stmt_expr) = stmt { + if let Expr::Call(call_expr) = stmt_expr.expr { + let expr_require = match *call_expr.expr { + Expr::Ident(require_ident) => require_ident, + _ => continue, + }; + + if expr_require.as_string() != "require" + && expr_require.as_string() != "revert" + { + continue; + } + + let expr_args = match call_expr.args.list { + ArgListImpl::Named(_) => continue, + ArgListImpl::Unnamed(args) => args, + }; + + if let Some(expr_string) = expr_args.iter().find(|&x| { + if let Expr::Lit(lit) = x { + matches!(lit, ast_extractor::Lit::Str(_)) + } else { + false + } + }) { + if let Expr::Lit(lit_string) = expr_string { + if let ast_extractor::Lit::Str(lit_str) = lit_string { + let actual_string = lit_str.values[0].token().to_string(); + + if actual_string.len() > self.max_length as usize { + let location = ( + lit_str.values[0].span().start(), + lit_str.values[0].span().end(), + ); + res.push( + self.create_diag( + file, + location, + format!( + "reason-string: A revert statement must have a reason string of length less than {}", + self.max_length + ), + ), + ); + } + } + } + } else { + let location = + (expr_require.0.span().start(), expr_require.0.span().end()); + res.push( + self.create_diag( + file, + location, + "reason-string: A require statement must have a reason string".to_string(), + ), + ); + } + } + } + } + } + } - println!("debug"); - // let nodes = get_all_nodes_by_type(file.data.clone(), NodeType::FunctionCall); - // for i in &nodes { - // match i { - // utils::Nodes::FunctionCall(j) => match &j.expression { - // Expression::Identifier(v) => { - // if v.name == "require" { - // if j.arguments.len() != 2 { - // let location = decode_location(&j.src, &file.content); - // res.push(self.create_diag(file, location, "reason-string: A require statement must have a reason string".to_string())); - // } else { - // for nj in &j.arguments { - // match nj { - // Expression::Literal(z) => { - // if z.value.clone().unwrap().len() - // > self.max_length as usize - // { - // let location = - // decode_location(&z.src, &file.content); - // res.push(self.create_diag(file, location, format!("reason-string: A revert statement must have a reason string of length less than {}", self.max_length))); - // } - // } - // _ => {} - // } - // } - // } - // } else if v.name == "revert" { - // if j.arguments.is_empty() { - // let location = decode_location(&j.src, &file.content); - // res.push(self.create_diag(file, location, "reason-string: A revert statement must have a reason string".to_string())); - // } else { - // match &j.arguments[0] { - // Expression::Literal(z) => { - // if z.value.clone().unwrap().len() > self.max_length as usize - // { - // let location = decode_location(&z.src, &file.content); - // res.push(self.create_diag(file, location, format!("reason-string: A revert statement must have a reason string of length less than {}", self.max_length))); - // } - // } - // _ => {} - // } - // } - // } - // } - // _ => {} - // }, - // _ => {} - // } - // } res } } diff --git a/toolchains/solidity/linter/core/solidhunter-lib/testdata/ReasonString/findings.csv b/toolchains/solidity/linter/core/solidhunter-lib/testdata/ReasonString/findings.csv index 470a966f..abf94805 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/testdata/ReasonString/findings.csv +++ b/toolchains/solidity/linter/core/solidhunter-lib/testdata/ReasonString/findings.csv @@ -1,2 +1,2 @@ -reason-string:8:9:8:136 -reason-string:11:9:11:38 \ No newline at end of file +reason-string:8:37:8:133 +reason-string:11:8:11:15 \ No newline at end of file From f199786f22359eef3efad462c0ac6a19949c21a8 Mon Sep 17 00:00:00 2001 From: 0xSwapFeeder Date: Wed, 20 Sep 2023 14:34:08 -0400 Subject: [PATCH 44/59] chore(solidity/linter): created a get_call_expressions custom visitor --- .../src/rules/best_practises/reason_string.rs | 135 +++++++++--------- 1 file changed, 70 insertions(+), 65 deletions(-) diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/reason_string.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/reason_string.rs index 55e7858c..3eaeae96 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/reason_string.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/reason_string.rs @@ -1,4 +1,4 @@ -use ast_extractor::{ArgListImpl, Expr, FunctionBody, LineColumn, Stmt}; +use ast_extractor::*; use crate::linter::SolidFile; use crate::rules::types::{RuleEntry, RuleType}; @@ -44,8 +44,9 @@ impl ReasonString { } } -fn get_all_functions_from_ast(ast_nodes: &ast_extractor::File) -> Vec { +fn get_call_expressions(ast_nodes: &ast_extractor::File) -> Vec { let mut res = Vec::new(); + let mut calls: Vec = Vec::new(); let contract = ast_nodes .items .iter() @@ -58,78 +59,82 @@ fn get_all_functions_from_ast(ast_nodes: &ast_extractor::File) -> Vec) -> Vec { let mut res = Vec::new(); - let allfn = get_all_functions_from_ast(&file.data); - - for func in allfn { - if let FunctionBody::Block(fn_body) = func.body { - for stmt in fn_body.stmts { - if let Stmt::Expr(stmt_expr) = stmt { - if let Expr::Call(call_expr) = stmt_expr.expr { - let expr_require = match *call_expr.expr { - Expr::Ident(require_ident) => require_ident, - _ => continue, - }; - - if expr_require.as_string() != "require" - && expr_require.as_string() != "revert" - { - continue; - } - - let expr_args = match call_expr.args.list { - ArgListImpl::Named(_) => continue, - ArgListImpl::Unnamed(args) => args, - }; - - if let Some(expr_string) = expr_args.iter().find(|&x| { - if let Expr::Lit(lit) = x { - matches!(lit, ast_extractor::Lit::Str(_)) - } else { - false - } - }) { - if let Expr::Lit(lit_string) = expr_string { - if let ast_extractor::Lit::Str(lit_str) = lit_string { - let actual_string = lit_str.values[0].token().to_string(); - - if actual_string.len() > self.max_length as usize { - let location = ( - lit_str.values[0].span().start(), - lit_str.values[0].span().end(), - ); - res.push( - self.create_diag( - file, - location, - format!( - "reason-string: A revert statement must have a reason string of length less than {}", - self.max_length - ), - ), - ); - } - } - } - } else { - let location = - (expr_require.0.span().start(), expr_require.0.span().end()); - res.push( - self.create_diag( - file, - location, - "reason-string: A require statement must have a reason string".to_string(), - ), + let calls = get_call_expressions(&file.data); + + for call_expr in calls { + let expr_require = match *call_expr.expr { + Expr::Ident(require_ident) => require_ident, + _ => continue, + }; + + if expr_require.as_string() != "require" + && expr_require.as_string() != "revert" + { + continue; + } + + let expr_args = match call_expr.args.list { + ArgListImpl::Named(_) => continue, + ArgListImpl::Unnamed(args) => args, + }; + + if let Some(expr_string) = expr_args.iter().find(|&x| { + if let Expr::Lit(lit) = x { + matches!(lit, ast_extractor::Lit::Str(_)) + } else { + false + } + }) { + if let Expr::Lit(lit_string) = expr_string { + if let ast_extractor::Lit::Str(lit_str) = lit_string { + let actual_string = lit_str.values[0].token().to_string(); + + if actual_string.len() > self.max_length as usize { + let location = ( + lit_str.values[0].span().start(), + lit_str.values[0].span().end(), ); - } + res.push( + self.create_diag( + file, + location, + format!( + "reason-string: A revert statement must have a reason string of length less than {}", + self.max_length + ), + ), + ); } } } + } else { + let location = + (expr_require.0.span().start(), expr_require.0.span().end()); + res.push( + self.create_diag( + file, + location, + "reason-string: A require statement must have a reason string".to_string(), + ), + ); } } From 8a941da9d5aac9d574b67a85289aef2b483634d4 Mon Sep 17 00:00:00 2001 From: 0xtekgrinder <0xtekgrinder@protonmail.com> Date: Wed, 20 Sep 2023 14:40:37 -0400 Subject: [PATCH 45/59] refactor(solidity/linter/core): fix lcippy warnings and change u64 to usize --- .../linter/core/solidhunter-lib/src/lib.rs | 2 +- .../linter/core/solidhunter-lib/src/linter.rs | 12 +---- .../best_practises/function_max_lines.rs | 20 ++++---- .../src/rules/best_practises/line_maxlen.rs | 10 ++-- .../rules/best_practises/max_states_count.rs | 22 +++----- .../src/rules/best_practises/mod.rs | 6 +-- .../src/rules/best_practises/reason_string.rs | 50 ++++++++----------- .../core/solidhunter-lib/src/rules/factory.rs | 2 +- .../src/rules/miscellaneous/mod.rs | 4 +- .../src/rules/miscellaneous/quotes.rs | 16 +++--- .../core/solidhunter-lib/src/rules/mod.rs | 4 +- .../rules/naming/contract_name_pascalcase.rs | 10 ++-- .../src/rules/naming/func_name_camelcase.rs | 10 ++-- .../rules/naming/func_param_name_camelcase.rs | 10 ++-- .../solidhunter-lib/src/rules/naming/mod.rs | 4 +- .../src/rules/naming/use_forbidden_name.rs | 10 ++-- .../src/rules/order/import_on_top.rs | 29 +++++------ .../solidhunter-lib/src/rules/order/mod.rs | 4 +- .../core/solidhunter-lib/src/rules/types.rs | 5 +- .../core/solidhunter-lib/src/rules/utils.rs | 2 +- .../linter/core/solidhunter-lib/src/types.rs | 20 ++++---- .../core/solidhunter-lib/tests/linter.rs | 8 +-- 22 files changed, 121 insertions(+), 139 deletions(-) diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/lib.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/lib.rs index 460aae37..7a833d6f 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/lib.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/lib.rs @@ -1,4 +1,4 @@ pub mod error; pub mod linter; pub mod rules; -pub mod types; \ No newline at end of file +pub mod types; diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs index aa63991f..323f5f84 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs @@ -83,11 +83,7 @@ impl SolidLinter { let content = fs::read_to_string(filepath.clone())?; let res = ast_extractor::extract::extract_ast_from_content(&content)?; - self._add_file( - filepath.as_str(), - res, - &content.as_str(), - ); + self._add_file(filepath.as_str(), res, content.as_str()); let mut res: Vec = Vec::new(); for rule in &self.rules { @@ -100,11 +96,7 @@ impl SolidLinter { pub fn parse_content(&mut self, filepath: String, content: &String) -> LintResult { let res = ast_extractor::extract::extract_ast_from_content(content)?; - self._add_file( - filepath.as_str(), - res, - content.as_str(), - ); + self._add_file(filepath.as_str(), res, content.as_str()); let mut res: Vec = Vec::new(); for rule in &self.rules { diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/function_max_lines.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/function_max_lines.rs index bee00885..c40f1e17 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/function_max_lines.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/function_max_lines.rs @@ -6,6 +6,7 @@ use crate::types::*; // const DEFAULT_SEVERITY: &str = "warn"; const DEFAULT_MESSAGE: &str = "Function contains too much lines"; +pub const RULE_ID: &str = "function-max-lines"; // specific pub const DEFAULT_MAX_LINES: usize = 20; @@ -16,7 +17,7 @@ pub struct FunctionMaxLines { } impl RuleType for FunctionMaxLines { - fn diagnose(&self, _file: &SolidFile, _files: &Vec) -> Vec { + fn diagnose(&self, _file: &SolidFile, _files: &[SolidFile]) -> Vec { let mut res = Vec::new(); let functions = get_all_functions_from_ast(&_file.data); @@ -24,7 +25,7 @@ impl RuleType for FunctionMaxLines { let _report = check_function_lines(_file, &function, self.number_max_lines); if let Some(report) = _report { res.push(LintDiag { - id: Self::RULE_ID.to_string(), + id: RULE_ID.to_string(), range: report, severity: Some(Severity::WARNING), code: None, @@ -71,11 +72,11 @@ fn check_function_lines( if function_lines > nb_max_line { res = Some(Range { start: Position { - line: start_span.line as u64, - character: start_span.column as u64, + line: start_span.line, + character: start_span.column, }, end: Position { - line: last_bracket_line as u64, + line: last_bracket_line, character: 1, }, }); @@ -101,12 +102,10 @@ fn get_all_functions_from_ast(ast_nodes: &ast_extractor::File) -> Vec Box { let mut max_number_lines = DEFAULT_MAX_LINES; - - if data.data.len() > 0 { + + if !data.data.is_empty() { max_number_lines = match data.data[0].parse::() { Ok(v) => v, Err(_) => DEFAULT_MAX_LINES, @@ -121,10 +120,9 @@ impl FunctionMaxLines { pub fn create_default() -> RuleEntry { RuleEntry { - id: FunctionMaxLines::RULE_ID.to_string(), + id: RULE_ID.to_string(), severity: Severity::WARNING, data: vec![DEFAULT_MAX_LINES.to_string()], } } } - diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/line_maxlen.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/line_maxlen.rs index 3085df0a..4af03e1b 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/line_maxlen.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/line_maxlen.rs @@ -16,12 +16,12 @@ impl LineMaxLen { LintDiag { range: Range { start: Position { - line: line_idx as u64, - character: self.max_len as u64, + line: line_idx, + character: self.max_len, }, end: Position { - line: line_idx as u64, - character: line.len() as u64, + line: line_idx, + character: line.len(), }, }, id: RULE_ID.to_string(), @@ -36,7 +36,7 @@ impl LineMaxLen { } impl RuleType for LineMaxLen { - fn diagnose(&self, file: &SolidFile, _files: &Vec) -> Vec { + fn diagnose(&self, file: &SolidFile, _files: &[SolidFile]) -> Vec { let mut res = Vec::new(); let mut line_idx = 1; diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/max_states_count.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/max_states_count.rs index 71825232..17e5385a 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/max_states_count.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/max_states_count.rs @@ -3,8 +3,7 @@ use crate::rules::types::*; use crate::types::*; use ast_extractor::*; - -pub const RULE_ID: &'static str = "max-states-count"; +pub const RULE_ID: &str = "max-states-count"; const DEFAULT_MAX_STATES: usize = 15; pub struct MaxStatesCount { @@ -23,12 +22,12 @@ impl MaxStatesCount { id: RULE_ID.to_string(), range: Range { start: Position { - line: location.0.line as u64, - character: location.0.column as u64, + line: location.0.line, + character: location.0.column, }, end: Position { - line: location.1.line as u64, - character: location.1.column as u64, + line: location.1.line, + character: location.1.column, }, }, message: format!("Too many states: {}", count), @@ -42,7 +41,7 @@ impl MaxStatesCount { } impl RuleType for MaxStatesCount { - fn diagnose(&self, file: &SolidFile, _files: &Vec) -> Vec { + fn diagnose(&self, file: &SolidFile, _files: &[SolidFile]) -> Vec { let mut res = Vec::new(); let mut count = 0; @@ -68,19 +67,15 @@ impl RuleType for MaxStatesCount { } impl MaxStatesCount { - pub(crate) fn create(data: RuleEntry) -> Box { let mut max_states = DEFAULT_MAX_STATES; - if data.data.len() > 0 { + if !data.data.is_empty() { max_states = match data.data[0].parse::() { Ok(v) => v, Err(_) => DEFAULT_MAX_STATES, }; } - let rule = MaxStatesCount { - max_states, - data, - }; + let rule = MaxStatesCount { max_states, data }; Box::new(rule) } @@ -92,4 +87,3 @@ impl MaxStatesCount { } } } - diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/mod.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/mod.rs index dc12d3de..3991bee1 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/mod.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/mod.rs @@ -1,4 +1,4 @@ -use crate::rules::types::{RuleEntry, RuleType}; +use crate::rules::types::{RuleEntry, RulesMap}; use std::collections::HashMap; #[macro_use] @@ -24,7 +24,7 @@ pub fn create_default_rules() -> Vec { ] } -pub fn create_rules() -> HashMap Box> { +pub fn create_rules() -> RulesMap { let mut rules: HashMap = HashMap::new(); rules.insert(line_maxlen::RULE_ID.to_string(), LineMaxLen::create); @@ -34,7 +34,7 @@ pub fn create_rules() -> HashMap Box> { MaxStatesCount::create, ); rules.insert( - FunctionMaxLines::RULE_ID.to_string(), + function_max_lines::RULE_ID.to_string(), FunctionMaxLines::create, ); rules.insert(reason_string::RULE_ID.to_string(), ReasonString::create); diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/reason_string.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/reason_string.rs index 3eaeae96..0a0b832a 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/reason_string.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/reason_string.rs @@ -26,12 +26,12 @@ impl ReasonString { id: RULE_ID.to_string(), range: Range { start: Position { - line: location.0.line as u64, - character: location.0.column as u64, + line: location.0.line, + character: location.0.column, }, end: Position { - line: location.1.line as u64, - character: location.1.column as u64, + line: location.1.line, + character: location.1.column, }, }, message, @@ -67,7 +67,6 @@ fn get_call_expressions(ast_nodes: &ast_extractor::File) -> Vec { calls.push(call_expr); } } - } } } @@ -75,7 +74,7 @@ fn get_call_expressions(ast_nodes: &ast_extractor::File) -> Vec { } impl RuleType for ReasonString { - fn diagnose(&self, file: &SolidFile, _files: &Vec) -> Vec { + fn diagnose(&self, file: &SolidFile, _files: &[SolidFile]) -> Vec { let mut res = Vec::new(); let calls = get_call_expressions(&file.data); @@ -85,9 +84,7 @@ impl RuleType for ReasonString { _ => continue, }; - if expr_require.as_string() != "require" - && expr_require.as_string() != "revert" - { + if expr_require.as_string() != "require" && expr_require.as_string() != "revert" { continue; } @@ -103,16 +100,15 @@ impl RuleType for ReasonString { false } }) { - if let Expr::Lit(lit_string) = expr_string { - if let ast_extractor::Lit::Str(lit_str) = lit_string { - let actual_string = lit_str.values[0].token().to_string(); - - if actual_string.len() > self.max_length as usize { - let location = ( - lit_str.values[0].span().start(), - lit_str.values[0].span().end(), - ); - res.push( + if let Expr::Lit(ast_extractor::Lit::Str(lit_str)) = expr_string { + let actual_string = lit_str.values[0].token().to_string(); + + if actual_string.len() > self.max_length as usize { + let location = ( + lit_str.values[0].span().start(), + lit_str.values[0].span().end(), + ); + res.push( self.create_diag( file, location, @@ -122,19 +118,15 @@ impl RuleType for ReasonString { ), ), ); - } } } } else { - let location = - (expr_require.0.span().start(), expr_require.0.span().end()); - res.push( - self.create_diag( - file, - location, - "reason-string: A require statement must have a reason string".to_string(), - ), - ); + let location = (expr_require.0.span().start(), expr_require.0.span().end()); + res.push(self.create_diag( + file, + location, + "reason-string: A require statement must have a reason string".to_string(), + )); } } diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/factory.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/factory.rs index 0e94596a..d0ee4f32 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/factory.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/factory.rs @@ -3,7 +3,7 @@ use crate::rules::types::*; use std::collections::HashMap; pub struct RuleFactory { - _buildables: HashMap Box>, + _buildables: RulesMap, _rules: Vec>, } diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/miscellaneous/mod.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/miscellaneous/mod.rs index a2ff21c0..df11a6e4 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/miscellaneous/mod.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/miscellaneous/mod.rs @@ -1,4 +1,4 @@ -use crate::rules::types::{RuleEntry, RuleType}; +use crate::rules::types::{RuleEntry, RulesMap}; use std::collections::HashMap; #[macro_use] @@ -13,7 +13,7 @@ pub fn create_default_rules() -> Vec { vec![Quotes::create_default()] } -pub fn create_rules() -> HashMap Box> { +pub fn create_rules() -> RulesMap { let mut rules: HashMap = HashMap::new(); rules.insert("quotes".to_string(), Quotes::create); diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/miscellaneous/quotes.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/miscellaneous/quotes.rs index 270cbc29..ab04500f 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/miscellaneous/quotes.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/miscellaneous/quotes.rs @@ -2,25 +2,27 @@ use crate::linter::SolidFile; use crate::rules::types::*; use crate::types::*; +pub const RULE_ID: &str = "quotes"; + pub struct Quotes { data: RuleEntry, } impl Quotes { - fn create_diag(&self, file: &SolidFile, idx: usize, line_idx: u64) -> LintDiag { + fn create_diag(&self, file: &SolidFile, idx: usize, line_idx: usize) -> LintDiag { LintDiag { - id: "quotes".to_string(), + id: RULE_ID.to_string(), range: Range { start: Position { line: line_idx, - character: idx as u64, + character: idx, }, end: Position { line: line_idx, - character: idx as u64, + character: idx, }, }, - message: format!("Use double quotes instead of single quote"), + message: "Use double quotes instead of single quote".to_string(), severity: Some(self.data.severity), code: None, source: None, @@ -31,7 +33,7 @@ impl Quotes { } impl RuleType for Quotes { - fn diagnose(&self, file: &SolidFile, _files: &Vec) -> Vec { + fn diagnose(&self, file: &SolidFile, _files: &[SolidFile]) -> Vec { let mut res = Vec::new(); let mut line_idx = 1; @@ -55,7 +57,7 @@ impl Quotes { pub(crate) fn create_default() -> RuleEntry { RuleEntry { - id: "quotes".to_string(), + id: RULE_ID.to_string(), severity: Severity::ERROR, data: vec![], } diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/mod.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/mod.rs index a8f8bc3a..22f2c202 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/mod.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/mod.rs @@ -1,4 +1,4 @@ -use crate::rules::types::{RuleEntry, RuleType}; +use crate::rules::types::{RuleEntry, RuleType, RulesMap}; use std::collections::HashMap; pub mod factory; @@ -31,7 +31,7 @@ pub fn add_rules(rules: &mut HashMap, to_add: HashMap HashMap Box> { +pub fn create_rules() -> RulesMap { let mut rules = HashMap::new(); add_rules(&mut rules, best_practises::create_rules()); diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/contract_name_pascalcase.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/contract_name_pascalcase.rs index 4a6cc9af..1fdabe06 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/contract_name_pascalcase.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/contract_name_pascalcase.rs @@ -21,12 +21,12 @@ impl ContractNamePascalCase { id: RULE_ID.to_string(), range: Range { start: Position { - line: location.0.line as u64, - character: location.0.column as u64, + line: location.0.line, + character: location.0.column, }, end: Position { - line: location.1.line as u64, - character: location.1.column as u64, + line: location.1.line, + character: location.1.column, }, }, message: MESSAGE.to_string(), @@ -40,7 +40,7 @@ impl ContractNamePascalCase { } impl RuleType for ContractNamePascalCase { - fn diagnose(&self, file: &SolidFile, _files: &Vec) -> Vec { + fn diagnose(&self, file: &SolidFile, _files: &[SolidFile]) -> Vec { let mut res = Vec::new(); let contracts = ast_extractor::retriever::retrieve_contract_nodes(&file.data); diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_name_camelcase.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_name_camelcase.rs index 2017b9f2..8ae09d81 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_name_camelcase.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_name_camelcase.rs @@ -21,12 +21,12 @@ impl FuncNameCamelCase { id: RULE_ID.to_string(), range: Range { start: Position { - line: location.0.line as u64, - character: location.0.column as u64, + line: location.0.line, + character: location.0.column, }, end: Position { - line: location.1.line as u64, - character: location.1.column as u64, + line: location.1.line, + character: location.1.column, }, }, message: MESSAGE.to_string(), @@ -40,7 +40,7 @@ impl FuncNameCamelCase { } impl RuleType for FuncNameCamelCase { - fn diagnose(&self, file: &SolidFile, _files: &Vec) -> Vec { + fn diagnose(&self, file: &SolidFile, _files: &[SolidFile]) -> Vec { let mut res = Vec::new(); let contracts = ast_extractor::retriever::retrieve_contract_nodes(&file.data); diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_param_name_camelcase.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_param_name_camelcase.rs index 1f617ea5..abf5e77a 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_param_name_camelcase.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/func_param_name_camelcase.rs @@ -20,12 +20,12 @@ impl FuncParamNameCamelcase { id: RULE_ID.to_string(), range: Range { start: Position { - line: location.0.line as u64, - character: location.0.column as u64, + line: location.0.line, + character: location.0.column, }, end: Position { - line: location.1.line as u64, - character: location.1.column as u64, + line: location.1.line, + character: location.1.column, }, }, message: MESSAGE.to_string(), @@ -39,7 +39,7 @@ impl FuncParamNameCamelcase { } impl RuleType for FuncParamNameCamelcase { - fn diagnose(&self, file: &SolidFile, _files: &Vec) -> Vec { + fn diagnose(&self, file: &SolidFile, _files: &[SolidFile]) -> Vec { let mut res = Vec::new(); let contracts = ast_extractor::retriever::retrieve_contract_nodes(&file.data); diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/mod.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/mod.rs index 4248b2d7..94654601 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/mod.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/mod.rs @@ -3,7 +3,7 @@ use crate::rules::naming::func_name_camelcase::FuncNameCamelCase; use crate::rules::naming::func_param_name_camelcase::FuncParamNameCamelcase; use crate::rules::naming::use_forbidden_name::UseForbiddenName; -use crate::rules::types::{RuleEntry, RuleType}; +use crate::rules::types::{RuleEntry, RulesMap}; use crate::rules::RuleBuilder; use std::collections::HashMap; @@ -24,7 +24,7 @@ pub fn create_default_rules() -> Vec { ] } -pub fn create_rules() -> HashMap Box> { +pub fn create_rules() -> RulesMap { let mut rules: HashMap = HashMap::new(); rules.insert( diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/use_forbidden_name.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/use_forbidden_name.rs index 2924f955..80436ed5 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/use_forbidden_name.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/use_forbidden_name.rs @@ -21,12 +21,12 @@ impl UseForbiddenName { id: RULE_ID.to_string(), range: Range { start: Position { - line: location.0.line as u64, - character: location.0.column as u64, + line: location.0.line, + character: location.0.column, }, end: Position { - line: location.1.line as u64, - character: location.1.column as u64, + line: location.1.line, + character: location.1.column, }, }, message: format!("Forbidden variable name: {}", name), @@ -40,7 +40,7 @@ impl UseForbiddenName { } impl RuleType for UseForbiddenName { - fn diagnose(&self, file: &SolidFile, _files: &Vec) -> Vec { + fn diagnose(&self, file: &SolidFile, _files: &[SolidFile]) -> Vec { let mut res = Vec::new(); let blacklist = ['I', 'l', 'O']; diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/order/import_on_top.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/order/import_on_top.rs index d7757d40..0931330f 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/order/import_on_top.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/order/import_on_top.rs @@ -12,15 +12,19 @@ pub struct ImportOnTop { } impl ImportOnTop { - fn create_diag(&self, file: &SolidFile, location: (ast_extractor::LineColumn, ast_extractor::LineColumn)) -> LintDiag { - let mut range = Range { + fn create_diag( + &self, + file: &SolidFile, + location: (ast_extractor::LineColumn, ast_extractor::LineColumn), + ) -> LintDiag { + let range = Range { start: Position { - line: location.0.line as u64, - character: location.0.column as u64, + line: location.0.line, + character: location.0.column, }, end: Position { - line: location.1.line as u64, - character: location.1.column as u64, + line: location.1.line, + character: location.1.column, }, }; LintDiag { @@ -37,7 +41,7 @@ impl ImportOnTop { } impl RuleType for ImportOnTop { - fn diagnose(&self, file: &SolidFile, _files: &Vec) -> Vec { + fn diagnose(&self, file: &SolidFile, _files: &[SolidFile]) -> Vec { let mut res = Vec::new(); let mut last_import_location = 0; @@ -53,14 +57,11 @@ impl RuleType for ImportOnTop { } for i in 1..file.data.items.len() { - match &file.data.items[i] { - ast_extractor::Item::Import(import) => { - if i > last_import_location { - let location = (import.span().start(), import.span().end()); - res.push(self.create_diag(file, location)); - } + if let ast_extractor::Item::Import(import) = &file.data.items[i] { + if i > last_import_location { + let location = (import.span().start(), import.span().end()); + res.push(self.create_diag(file, location)); } - _ => {} } } diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/order/mod.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/order/mod.rs index e9b8017d..a22ad358 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/order/mod.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/order/mod.rs @@ -1,4 +1,4 @@ -use crate::rules::types::{RuleEntry, RuleType}; +use crate::rules::types::{RuleEntry, RulesMap}; use std::collections::HashMap; #[macro_use] @@ -12,7 +12,7 @@ pub fn create_default_rules() -> Vec { vec![ImportOnTop::create_default()] } -pub fn create_rules() -> HashMap Box> { +pub fn create_rules() -> RulesMap { let mut rules: HashMap = HashMap::new(); rules.insert(import_on_top::RULE_ID.to_string(), ImportOnTop::create); diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/types.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/types.rs index 722e547f..f18dedcf 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/types.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/types.rs @@ -1,6 +1,7 @@ use crate::linter::SolidFile; use crate::types::*; use serde::{Deserialize, Serialize}; +use std::collections::HashMap; #[derive(Serialize, Deserialize, Debug)] pub struct RuleEntry { @@ -18,5 +19,7 @@ pub struct Rules { } pub trait RuleType: Send + Sync + 'static { - fn diagnose(&self, file: &SolidFile, files: &Vec) -> Vec; + fn diagnose(&self, file: &SolidFile, files: &[SolidFile]) -> Vec; } + +pub type RulesMap = HashMap Box>; diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/utils.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/utils.rs index e637bcc7..5377471d 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/utils.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/utils.rs @@ -18,4 +18,4 @@ pub fn absolute_index_from_location(location: LineColumn, content: &str) -> usiz index += 1; } index -} \ No newline at end of file +} diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs index 8b8ea3fd..d69d1cd8 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs @@ -59,7 +59,7 @@ impl fmt::Display for LintDiag { let line = self .source_file_content .lines() - .nth((self.range.start.line - 1) as usize) + .nth(self.range.start.line - 1) .unwrap(); write!( @@ -73,8 +73,8 @@ impl fmt::Display for LintDiag { self.range.start.line, padding, line, - " ".repeat(self.range.start.character as usize), - "^".repeat(self.range.compute_length(line) as usize) + " ".repeat(self.range.start.character), + "^".repeat(self.range.compute_length(line)) ) } } @@ -91,8 +91,8 @@ impl PartialEq for Position { #[derive(Clone, Serialize, Deserialize, Debug)] pub struct Position { - pub line: u64, - pub character: u64, + pub line: usize, + pub character: usize, } #[derive(PartialEq, Eq, Clone, Copy, Serialize, Deserialize, Debug)] @@ -115,7 +115,7 @@ pub struct Range { impl Range { // Compue the number of characters between the start and end of the range - pub fn compute_length(&self, content: &str) -> u64 { + pub fn compute_length(&self, content: &str) -> usize { if self.start.line == self.end.line { self.end.character - self.start.character } else { @@ -123,13 +123,13 @@ impl Range { let mut line = self.start.line; let mut character = self.start.character; while line < self.end.line { - let line_content = content.lines().nth(line as usize - 1).unwrap(); - length += line_content.len() + 1 - character as usize; + let line_content = content.lines().nth(line - 1).unwrap(); + length += line_content.len() + 1 - character; line += 1; character = 0; } - length += self.end.character as usize - character as usize; - length as u64 + length += self.end.character - character; + length } } } diff --git a/toolchains/solidity/linter/core/solidhunter-lib/tests/linter.rs b/toolchains/solidity/linter/core/solidhunter-lib/tests/linter.rs index 8d6883f0..4bd2aaa3 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/tests/linter.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/tests/linter.rs @@ -32,12 +32,12 @@ fn test_directory(base_name: &str) { let splitted_line: Vec<&str> = line.split(':').collect(); expected_findings.push(Finding { start: Position { - line: splitted_line[1].parse::().unwrap(), - character: splitted_line[2].parse::().unwrap(), + line: splitted_line[1].parse::().unwrap(), + character: splitted_line[2].parse::().unwrap(), }, end: Position { - line: splitted_line[3].parse::().unwrap(), - character: splitted_line[4].parse::().unwrap(), + line: splitted_line[3].parse::().unwrap(), + character: splitted_line[4].parse::().unwrap(), }, id: splitted_line[0].to_string(), }); From 593a1755845a67dcb612dcfda6ca6bc8e0b87f07 Mon Sep 17 00:00:00 2001 From: 0xtekgrinder <0xtekgrinder@protonmail.com> Date: Wed, 20 Sep 2023 15:00:50 -0400 Subject: [PATCH 46/59] refactor(solidity/linter/core): rename error into errors --- .../linter/core/solidhunter-lib/src/{error.rs => errors.rs} | 0 toolchains/solidity/linter/core/solidhunter-lib/src/lib.rs | 2 +- .../solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs | 2 +- toolchains/solidity/linter/core/solidhunter-lib/src/types.rs | 2 +- 4 files changed, 3 insertions(+), 3 deletions(-) rename toolchains/solidity/linter/core/solidhunter-lib/src/{error.rs => errors.rs} (100%) diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/error.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/errors.rs similarity index 100% rename from toolchains/solidity/linter/core/solidhunter-lib/src/error.rs rename to toolchains/solidity/linter/core/solidhunter-lib/src/errors.rs diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/lib.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/lib.rs index 7a833d6f..5e557edc 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/lib.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/lib.rs @@ -1,4 +1,4 @@ -pub mod error; +pub mod errors; pub mod linter; pub mod rules; pub mod types; diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs index a8a36e3a..e049745b 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs @@ -1,4 +1,4 @@ -use crate::error::SolidHunterError; +use crate::errors::SolidHunterError; use crate::rules::create_default_rules; use crate::rules::types::*; diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs index d69d1cd8..df542d36 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs @@ -1,4 +1,4 @@ -use crate::error::SolidHunterError; +use crate::errors::SolidHunterError; use colored::Colorize; use serde::{Deserialize, Serialize}; use std::fmt; From 53d29fb680bf2c29118cd32d7c03313ed7add5c4 Mon Sep 17 00:00:00 2001 From: 0xtekgrinder <0xtekgrinder@protonmail.com> Date: Wed, 20 Sep 2023 15:03:53 -0400 Subject: [PATCH 47/59] feat(solidity/linter/core): add missing directory in tests --- toolchains/solidity/linter/core/solidhunter-lib/tests/linter.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/toolchains/solidity/linter/core/solidhunter-lib/tests/linter.rs b/toolchains/solidity/linter/core/solidhunter-lib/tests/linter.rs index 4bd2aaa3..618fc090 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/tests/linter.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/tests/linter.rs @@ -96,6 +96,7 @@ test_directories! { ContractNamePascalCase, FunctionMaxLines, ImportOnTop, + LineMaxLen, MaxStatesCount, FunctionNameCamelCase, FunctionParamNameCamelCase, From e7b691a36639b5128c4c41dc18bc7435e902d112 Mon Sep 17 00:00:00 2001 From: 0xtekgrinder <0xtekgrinder@protonmail.com> Date: Wed, 20 Sep 2023 17:50:15 -0400 Subject: [PATCH 48/59] fix(solidity.linter/core): can parse a folder --- .../linter/core/solidhunter-lib/src/linter.rs | 4 +- .../rules/best_practises/max_states_count.rs | 2 - .../src/rules/miscellaneous/mod.rs | 16 +---- .../src/rules/miscellaneous/quotes.rs | 65 ------------------- .../src/rules/naming/use_forbidden_name.rs | 1 - .../linter/core/solidhunter-lib/src/types.rs | 4 +- 6 files changed, 6 insertions(+), 86 deletions(-) delete mode 100644 toolchains/solidity/linter/core/solidhunter-lib/src/rules/miscellaneous/quotes.rs diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs index 323f5f84..3bfb0a07 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs @@ -87,7 +87,7 @@ impl SolidLinter { let mut res: Vec = Vec::new(); for rule in &self.rules { - let mut diags = rule.diagnose(&self.files[0], &self.files); + let mut diags = rule.diagnose(&self.files[self.files.len() - 1], &self.files); res.append(&mut diags); } Ok(res) @@ -100,7 +100,7 @@ impl SolidLinter { let mut res: Vec = Vec::new(); for rule in &self.rules { - let mut diags = rule.diagnose(&self.files[0], &self.files); + let mut diags = rule.diagnose(&self.files[self.files.len() - 1], &self.files); res.append(&mut diags); } Ok(res) diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/max_states_count.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/max_states_count.rs index 17e5385a..c8f615fe 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/max_states_count.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/max_states_count.rs @@ -47,7 +47,6 @@ impl RuleType for MaxStatesCount { let mut count = 0; let contracts = retriever::retrieve_contract_nodes(&file.data); - // var def => contract def for contract in contracts.iter() { for node_var in contract.body.iter() { let var = match node_var { @@ -61,7 +60,6 @@ impl RuleType for MaxStatesCount { } } } - println!("res: {:?}", res); res } } diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/miscellaneous/mod.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/miscellaneous/mod.rs index df11a6e4..b6a377f2 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/miscellaneous/mod.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/miscellaneous/mod.rs @@ -1,22 +1,10 @@ use crate::rules::types::{RuleEntry, RulesMap}; use std::collections::HashMap; -#[macro_use] -pub mod quotes; - -// List all rules - -use crate::rules::miscellaneous::quotes::Quotes; -use crate::rules::RuleBuilder; - pub fn create_default_rules() -> Vec { - vec![Quotes::create_default()] + vec![] } pub fn create_rules() -> RulesMap { - let mut rules: HashMap = HashMap::new(); - - rules.insert("quotes".to_string(), Quotes::create); - - rules + HashMap::new() } diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/miscellaneous/quotes.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/miscellaneous/quotes.rs deleted file mode 100644 index ab04500f..00000000 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/miscellaneous/quotes.rs +++ /dev/null @@ -1,65 +0,0 @@ -use crate::linter::SolidFile; -use crate::rules::types::*; -use crate::types::*; - -pub const RULE_ID: &str = "quotes"; - -pub struct Quotes { - data: RuleEntry, -} - -impl Quotes { - fn create_diag(&self, file: &SolidFile, idx: usize, line_idx: usize) -> LintDiag { - LintDiag { - id: RULE_ID.to_string(), - range: Range { - start: Position { - line: line_idx, - character: idx, - }, - end: Position { - line: line_idx, - character: idx, - }, - }, - message: "Use double quotes instead of single quote".to_string(), - severity: Some(self.data.severity), - code: None, - source: None, - uri: file.path.clone(), - source_file_content: file.content.clone(), - } - } -} - -impl RuleType for Quotes { - fn diagnose(&self, file: &SolidFile, _files: &[SolidFile]) -> Vec { - let mut res = Vec::new(); - let mut line_idx = 1; - - for line in file.content.lines() { - line.chars().enumerate().for_each(|(idx, c)| { - if c == '\'' && line.chars().nth(idx - 1).unwrap_or(' ') != '\\' { - res.push(self.create_diag(file, idx, line_idx)); - } - }); - line_idx += 1; - } - res - } -} - -impl Quotes { - pub(crate) fn create(data: RuleEntry) -> Box { - let rule = Quotes { data }; - Box::new(rule) - } - - pub(crate) fn create_default() -> RuleEntry { - RuleEntry { - id: RULE_ID.to_string(), - severity: Severity::ERROR, - data: vec![], - } - } -} diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/use_forbidden_name.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/use_forbidden_name.rs index 80436ed5..260aa857 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/use_forbidden_name.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/naming/use_forbidden_name.rs @@ -1,7 +1,6 @@ use crate::linter::SolidFile; use crate::rules::types::*; use crate::types::*; -use ast_extractor::Spanned; use ast_extractor::*; pub const RULE_ID: &str = "use-forbidden-name"; diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs index df542d36..f5195b0e 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs @@ -74,7 +74,7 @@ impl fmt::Display for LintDiag { padding, line, " ".repeat(self.range.start.character), - "^".repeat(self.range.compute_length(line)) + "^".repeat(self.range.compute_length(self.source_file_content.as_str())) ) } } @@ -114,7 +114,7 @@ pub struct Range { } impl Range { - // Compue the number of characters between the start and end of the range + // Compute the number of characters between the start and end of the range pub fn compute_length(&self, content: &str) -> usize { if self.start.line == self.end.line { self.end.character - self.start.character From 4221e90c34ec4f64c2eee19ed873890e944cc73b Mon Sep 17 00:00:00 2001 From: 0xtekgrinder <0xtekgrinder@protonmail.com> Date: Wed, 20 Sep 2023 17:52:25 -0400 Subject: [PATCH 49/59] tests(solidity/linter/core): add all rules to be sure everything works good --- .../ContractNamePascalCase/.solidhunter.json | 48 +++++++++++++++++++ .../FunctionMaxLines/.solidhunter.json | 48 +++++++++++++++++++ .../FunctionNameCamelCase/.solidhunter.json | 48 +++++++++++++++++++ .../.solidhunter.json | 48 +++++++++++++++++++ .../testdata/ImportOnTop/.solidhunter.json | 48 +++++++++++++++++++ .../testdata/LineMaxLen/.solidhunter.json | 46 ++++++++++++++++++ .../testdata/MaxStatesCount/.solidhunter.json | 48 +++++++++++++++++++ .../testdata/MaxStatesCount/file.sol | 2 +- .../testdata/ReasonString/.solidhunter.json | 46 ++++++++++++++++++ .../testdata/ReasonString/file.sol | 6 +-- .../UseForbiddenName/.solidhunter.json | 48 +++++++++++++++++++ 11 files changed, 432 insertions(+), 4 deletions(-) diff --git a/toolchains/solidity/linter/core/solidhunter-lib/testdata/ContractNamePascalCase/.solidhunter.json b/toolchains/solidity/linter/core/solidhunter-lib/testdata/ContractNamePascalCase/.solidhunter.json index 9871b2e8..6cc3dcc2 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/testdata/ContractNamePascalCase/.solidhunter.json +++ b/toolchains/solidity/linter/core/solidhunter-lib/testdata/ContractNamePascalCase/.solidhunter.json @@ -3,10 +3,58 @@ "includes": [], "plugins": [], "rules": [ + { + "id": "line-max-len", + "severity": "WARNING", + "data": [ + "80" + ] + }, + { + "id": "max-states-count", + "severity": "WARNING", + "data": [ + "15" + ] + }, + { + "id": "function-max-lines", + "severity": "WARNING", + "data": [ + "20" + ] + }, + { + "id": "reason-string", + "severity": "WARNING", + "data": [ + "32" + ] + }, { "id": "contract-name-pascalcase", "severity": "WARNING", "data": [] + }, + { + "id": "func-name-camelcase", + "severity": "WARNING", + "data": [] + }, + { + "id": "func-param-name-camelcase", + "severity": "WARNING", + "data": [] + }, + { + "id": "use-forbidden-name", + "severity": "WARNING", + "data": [] + }, + { + "id": "import-on-top", + "severity": "WARNING", + "data": [] } ] } \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solidhunter-lib/testdata/FunctionMaxLines/.solidhunter.json b/toolchains/solidity/linter/core/solidhunter-lib/testdata/FunctionMaxLines/.solidhunter.json index 9097ed1c..6cc3dcc2 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/testdata/FunctionMaxLines/.solidhunter.json +++ b/toolchains/solidity/linter/core/solidhunter-lib/testdata/FunctionMaxLines/.solidhunter.json @@ -3,9 +3,57 @@ "includes": [], "plugins": [], "rules": [ + { + "id": "line-max-len", + "severity": "WARNING", + "data": [ + "80" + ] + }, + { + "id": "max-states-count", + "severity": "WARNING", + "data": [ + "15" + ] + }, { "id": "function-max-lines", "severity": "WARNING", + "data": [ + "20" + ] + }, + { + "id": "reason-string", + "severity": "WARNING", + "data": [ + "32" + ] + }, + { + "id": "contract-name-pascalcase", + "severity": "WARNING", + "data": [] + }, + { + "id": "func-name-camelcase", + "severity": "WARNING", + "data": [] + }, + { + "id": "func-param-name-camelcase", + "severity": "WARNING", + "data": [] + }, + { + "id": "use-forbidden-name", + "severity": "WARNING", + "data": [] + }, + { + "id": "import-on-top", + "severity": "WARNING", "data": [] } ] diff --git a/toolchains/solidity/linter/core/solidhunter-lib/testdata/FunctionNameCamelCase/.solidhunter.json b/toolchains/solidity/linter/core/solidhunter-lib/testdata/FunctionNameCamelCase/.solidhunter.json index f623e561..6cc3dcc2 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/testdata/FunctionNameCamelCase/.solidhunter.json +++ b/toolchains/solidity/linter/core/solidhunter-lib/testdata/FunctionNameCamelCase/.solidhunter.json @@ -3,10 +3,58 @@ "includes": [], "plugins": [], "rules": [ + { + "id": "line-max-len", + "severity": "WARNING", + "data": [ + "80" + ] + }, + { + "id": "max-states-count", + "severity": "WARNING", + "data": [ + "15" + ] + }, + { + "id": "function-max-lines", + "severity": "WARNING", + "data": [ + "20" + ] + }, + { + "id": "reason-string", + "severity": "WARNING", + "data": [ + "32" + ] + }, + { + "id": "contract-name-pascalcase", + "severity": "WARNING", + "data": [] + }, { "id": "func-name-camelcase", "severity": "WARNING", "data": [] + }, + { + "id": "func-param-name-camelcase", + "severity": "WARNING", + "data": [] + }, + { + "id": "use-forbidden-name", + "severity": "WARNING", + "data": [] + }, + { + "id": "import-on-top", + "severity": "WARNING", + "data": [] } ] } \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solidhunter-lib/testdata/FunctionParamNameCamelCase/.solidhunter.json b/toolchains/solidity/linter/core/solidhunter-lib/testdata/FunctionParamNameCamelCase/.solidhunter.json index cc4a2a76..6cc3dcc2 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/testdata/FunctionParamNameCamelCase/.solidhunter.json +++ b/toolchains/solidity/linter/core/solidhunter-lib/testdata/FunctionParamNameCamelCase/.solidhunter.json @@ -3,10 +3,58 @@ "includes": [], "plugins": [], "rules": [ + { + "id": "line-max-len", + "severity": "WARNING", + "data": [ + "80" + ] + }, + { + "id": "max-states-count", + "severity": "WARNING", + "data": [ + "15" + ] + }, + { + "id": "function-max-lines", + "severity": "WARNING", + "data": [ + "20" + ] + }, + { + "id": "reason-string", + "severity": "WARNING", + "data": [ + "32" + ] + }, + { + "id": "contract-name-pascalcase", + "severity": "WARNING", + "data": [] + }, + { + "id": "func-name-camelcase", + "severity": "WARNING", + "data": [] + }, { "id": "func-param-name-camelcase", "severity": "WARNING", "data": [] + }, + { + "id": "use-forbidden-name", + "severity": "WARNING", + "data": [] + }, + { + "id": "import-on-top", + "severity": "WARNING", + "data": [] } ] } \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solidhunter-lib/testdata/ImportOnTop/.solidhunter.json b/toolchains/solidity/linter/core/solidhunter-lib/testdata/ImportOnTop/.solidhunter.json index 3f5b34e6..6cc3dcc2 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/testdata/ImportOnTop/.solidhunter.json +++ b/toolchains/solidity/linter/core/solidhunter-lib/testdata/ImportOnTop/.solidhunter.json @@ -3,6 +3,54 @@ "includes": [], "plugins": [], "rules": [ + { + "id": "line-max-len", + "severity": "WARNING", + "data": [ + "80" + ] + }, + { + "id": "max-states-count", + "severity": "WARNING", + "data": [ + "15" + ] + }, + { + "id": "function-max-lines", + "severity": "WARNING", + "data": [ + "20" + ] + }, + { + "id": "reason-string", + "severity": "WARNING", + "data": [ + "32" + ] + }, + { + "id": "contract-name-pascalcase", + "severity": "WARNING", + "data": [] + }, + { + "id": "func-name-camelcase", + "severity": "WARNING", + "data": [] + }, + { + "id": "func-param-name-camelcase", + "severity": "WARNING", + "data": [] + }, + { + "id": "use-forbidden-name", + "severity": "WARNING", + "data": [] + }, { "id": "import-on-top", "severity": "WARNING", diff --git a/toolchains/solidity/linter/core/solidhunter-lib/testdata/LineMaxLen/.solidhunter.json b/toolchains/solidity/linter/core/solidhunter-lib/testdata/LineMaxLen/.solidhunter.json index 4fd69780..6cc3dcc2 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/testdata/LineMaxLen/.solidhunter.json +++ b/toolchains/solidity/linter/core/solidhunter-lib/testdata/LineMaxLen/.solidhunter.json @@ -9,6 +9,52 @@ "data": [ "80" ] + }, + { + "id": "max-states-count", + "severity": "WARNING", + "data": [ + "15" + ] + }, + { + "id": "function-max-lines", + "severity": "WARNING", + "data": [ + "20" + ] + }, + { + "id": "reason-string", + "severity": "WARNING", + "data": [ + "32" + ] + }, + { + "id": "contract-name-pascalcase", + "severity": "WARNING", + "data": [] + }, + { + "id": "func-name-camelcase", + "severity": "WARNING", + "data": [] + }, + { + "id": "func-param-name-camelcase", + "severity": "WARNING", + "data": [] + }, + { + "id": "use-forbidden-name", + "severity": "WARNING", + "data": [] + }, + { + "id": "import-on-top", + "severity": "WARNING", + "data": [] } ] } \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solidhunter-lib/testdata/MaxStatesCount/.solidhunter.json b/toolchains/solidity/linter/core/solidhunter-lib/testdata/MaxStatesCount/.solidhunter.json index 9a520219..6cc3dcc2 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/testdata/MaxStatesCount/.solidhunter.json +++ b/toolchains/solidity/linter/core/solidhunter-lib/testdata/MaxStatesCount/.solidhunter.json @@ -3,9 +3,57 @@ "includes": [], "plugins": [], "rules": [ + { + "id": "line-max-len", + "severity": "WARNING", + "data": [ + "80" + ] + }, { "id": "max-states-count", "severity": "WARNING", + "data": [ + "15" + ] + }, + { + "id": "function-max-lines", + "severity": "WARNING", + "data": [ + "20" + ] + }, + { + "id": "reason-string", + "severity": "WARNING", + "data": [ + "32" + ] + }, + { + "id": "contract-name-pascalcase", + "severity": "WARNING", + "data": [] + }, + { + "id": "func-name-camelcase", + "severity": "WARNING", + "data": [] + }, + { + "id": "func-param-name-camelcase", + "severity": "WARNING", + "data": [] + }, + { + "id": "use-forbidden-name", + "severity": "WARNING", + "data": [] + }, + { + "id": "import-on-top", + "severity": "WARNING", "data": [] } ] diff --git a/toolchains/solidity/linter/core/solidhunter-lib/testdata/MaxStatesCount/file.sol b/toolchains/solidity/linter/core/solidhunter-lib/testdata/MaxStatesCount/file.sol index d7a4124d..a82ad086 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/testdata/MaxStatesCount/file.sol +++ b/toolchains/solidity/linter/core/solidhunter-lib/testdata/MaxStatesCount/file.sol @@ -12,7 +12,7 @@ contract Test { uint i = 9; uint j = 10; uint k = 11; - uint l = 12; + uint z = 12; uint m = 13; uint n = 14; uint o = 15; diff --git a/toolchains/solidity/linter/core/solidhunter-lib/testdata/ReasonString/.solidhunter.json b/toolchains/solidity/linter/core/solidhunter-lib/testdata/ReasonString/.solidhunter.json index 20c03fed..6cc3dcc2 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/testdata/ReasonString/.solidhunter.json +++ b/toolchains/solidity/linter/core/solidhunter-lib/testdata/ReasonString/.solidhunter.json @@ -3,12 +3,58 @@ "includes": [], "plugins": [], "rules": [ + { + "id": "line-max-len", + "severity": "WARNING", + "data": [ + "80" + ] + }, + { + "id": "max-states-count", + "severity": "WARNING", + "data": [ + "15" + ] + }, + { + "id": "function-max-lines", + "severity": "WARNING", + "data": [ + "20" + ] + }, { "id": "reason-string", "severity": "WARNING", "data": [ "32" ] + }, + { + "id": "contract-name-pascalcase", + "severity": "WARNING", + "data": [] + }, + { + "id": "func-name-camelcase", + "severity": "WARNING", + "data": [] + }, + { + "id": "func-param-name-camelcase", + "severity": "WARNING", + "data": [] + }, + { + "id": "use-forbidden-name", + "severity": "WARNING", + "data": [] + }, + { + "id": "import-on-top", + "severity": "WARNING", + "data": [] } ] } \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solidhunter-lib/testdata/ReasonString/file.sol b/toolchains/solidity/linter/core/solidhunter-lib/testdata/ReasonString/file.sol index 44b09bf6..bc29cc8e 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/testdata/ReasonString/file.sol +++ b/toolchains/solidity/linter/core/solidhunter-lib/testdata/ReasonString/file.sol @@ -4,10 +4,10 @@ contract Test { function awesome() public { require(!has(role, account), "This is perfect"); } - function not_awesome() public { - require(!has(role, account), "This is not perfect at all because it's really too long but the code is 0xSwapFeeder compliant"); + function notAwesome() public { + require(!has(role, account), "This is not perfect at all because i"); } - function not_awesome_either() public { + function notAwesomeEither() public { require(!has(role, account)); } } \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solidhunter-lib/testdata/UseForbiddenName/.solidhunter.json b/toolchains/solidity/linter/core/solidhunter-lib/testdata/UseForbiddenName/.solidhunter.json index 0cbc855f..6cc3dcc2 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/testdata/UseForbiddenName/.solidhunter.json +++ b/toolchains/solidity/linter/core/solidhunter-lib/testdata/UseForbiddenName/.solidhunter.json @@ -3,10 +3,58 @@ "includes": [], "plugins": [], "rules": [ + { + "id": "line-max-len", + "severity": "WARNING", + "data": [ + "80" + ] + }, + { + "id": "max-states-count", + "severity": "WARNING", + "data": [ + "15" + ] + }, + { + "id": "function-max-lines", + "severity": "WARNING", + "data": [ + "20" + ] + }, + { + "id": "reason-string", + "severity": "WARNING", + "data": [ + "32" + ] + }, + { + "id": "contract-name-pascalcase", + "severity": "WARNING", + "data": [] + }, + { + "id": "func-name-camelcase", + "severity": "WARNING", + "data": [] + }, + { + "id": "func-param-name-camelcase", + "severity": "WARNING", + "data": [] + }, { "id": "use-forbidden-name", "severity": "WARNING", "data": [] + }, + { + "id": "import-on-top", + "severity": "WARNING", + "data": [] } ] } \ No newline at end of file From 912caac669825eb8c8e10955a4ed59cbcb6894d7 Mon Sep 17 00:00:00 2001 From: 0xMemoryGrinder <35138272+0xMemoryGrinder@users.noreply.github.com> Date: Wed, 20 Sep 2023 19:06:59 -0400 Subject: [PATCH 50/59] fix(solidity/linter/core): fixed LintDiag formatting --- .../linter/core/solidhunter-lib/src/types.rs | 101 +++++++++++++----- 1 file changed, 77 insertions(+), 24 deletions(-) diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs index f5195b0e..f2152550 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs @@ -1,3 +1,4 @@ +use std::cmp::min; use crate::errors::SolidHunterError; use colored::Colorize; use serde::{Deserialize, Serialize}; @@ -35,46 +36,87 @@ pub struct LintDiag { pub source_file_content: String, } -fn severity_to_string(severity: Option) -> String { - match severity { - Some(Severity::ERROR) => "error".to_string().red(), - Some(Severity::WARNING) => "warning".to_string().yellow(), - Some(Severity::INFO) => "info".to_string().blue(), - Some(Severity::HINT) => "hint".to_string().green(), - _ => "error".to_string().red(), + +fn compute_format_line_padding(line: usize) -> String { + let padding: String; + if line > 99 { + padding = " ".repeat(0); + } else if line > 9 { + padding = " ".to_string(); + } else { + padding = " ".repeat(2); } - .to_string() + padding } -impl fmt::Display for LintDiag { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let padding: String; - if self.range.start.line > 99 { - padding = " ".repeat(0); - } else if self.range.start.line > 9 { - padding = " ".to_string(); - } else { - padding = " ".repeat(2); +fn try_trim_max_offset(line: &str, max_offset: usize) -> (&str, usize) { + let mut offset: usize = 0; + + for (i, c) in line.chars().enumerate() { + if i >= max_offset { + break; + } + if c.is_whitespace() { + offset += 1; } - let line = self + } + (&line[offset..], offset) +} + +impl LintDiag { + + fn format_highlighted_lines(&self) -> String { + let mut formatted = format!(" |\n"); + let first_line = self .source_file_content .lines() .nth(self.range.start.line - 1) .unwrap(); + let trimmed_first_line = first_line.trim_start(); + let max_offset = first_line.len() - trimmed_first_line.len(); + + for line_nb in self.range.start.line..self.range.end.line + 1 { + let line = self + .source_file_content + .lines() + .nth(line_nb - 1) + .unwrap(); + let (trimmed_line, offset) = try_trim_max_offset(line, max_offset); + let mut higlight_length = trimmed_line.len(); + + if self.range.start.line == self.range.end.line { + higlight_length = self.range.end.character - self.range.start.character; + } else if line_nb == self.range.start.line { + higlight_length = trimmed_line.len() - (self.range.start.character - offset); + } else if line_nb == self.range.end.line { + higlight_length = trimmed_line.len() - (self.range.end.character - min(offset, trimmed_line.len())); + } + + formatted = format!( + "{}{}{}| {}\n | {}{}\n", + formatted, + self.range.start.line, + compute_format_line_padding(self.range.start.line), + trimmed_line, + " ".repeat(if line_nb == self.range.start.line { self.range.start.character - offset } else { 0 }), + "^".repeat(higlight_length) + ); + } + formatted + } +} +impl fmt::Display for LintDiag { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, - "\n{}: {}\n --> {}:{}:{}\n |\n{}{}|{}\n |{}{}", + "\n{}: {}\n --> {}:{}:{}\n |\n{}", severity_to_string(self.severity), self.message, self.uri, self.range.start.line, self.range.start.character, - self.range.start.line, - padding, - line, - " ".repeat(self.range.start.character), - "^".repeat(self.range.compute_length(self.source_file_content.as_str())) + self.format_highlighted_lines() ) } } @@ -83,6 +125,17 @@ impl fmt::Display for LintDiag { /////////////////// RELATED TYPES: ///////////////////////// //////////////////////////////////////////////////////////// +fn severity_to_string(severity: Option) -> String { + match severity { + Some(Severity::ERROR) => "error".to_string().red(), + Some(Severity::WARNING) => "warning".to_string().yellow(), + Some(Severity::INFO) => "info".to_string().blue(), + Some(Severity::HINT) => "hint".to_string().green(), + _ => "error".to_string().red(), + } + .to_string() +} + impl PartialEq for Position { fn eq(&self, other: &Self) -> bool { self.line == other.line && self.character == other.character From 40445fec614ac360d516ec0a294e5f9b7a48d511 Mon Sep 17 00:00:00 2001 From: 0xtekgrinder <0xtekgrinder@protonmail.com> Date: Wed, 20 Sep 2023 22:22:59 -0400 Subject: [PATCH 51/59] fix(solidity/linter/core): line of linter formatting is now good --- .../solidity/linter/core/solidhunter-lib/src/types.rs | 6 +++--- .../core/solidhunter-lib/testdata/FunctionMaxLines/file.sol | 1 - 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs index f2152550..e198254a 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs @@ -74,7 +74,7 @@ impl LintDiag { .unwrap(); let trimmed_first_line = first_line.trim_start(); let max_offset = first_line.len() - trimmed_first_line.len(); - + for line_nb in self.range.start.line..self.range.end.line + 1 { let line = self .source_file_content @@ -95,8 +95,8 @@ impl LintDiag { formatted = format!( "{}{}{}| {}\n | {}{}\n", formatted, - self.range.start.line, - compute_format_line_padding(self.range.start.line), + line_nb, + compute_format_line_padding(line_nb), trimmed_line, " ".repeat(if line_nb == self.range.start.line { self.range.start.character - offset } else { 0 }), "^".repeat(higlight_length) diff --git a/toolchains/solidity/linter/core/solidhunter-lib/testdata/FunctionMaxLines/file.sol b/toolchains/solidity/linter/core/solidhunter-lib/testdata/FunctionMaxLines/file.sol index 54ae6076..55db8a98 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/testdata/FunctionMaxLines/file.sol +++ b/toolchains/solidity/linter/core/solidhunter-lib/testdata/FunctionMaxLines/file.sol @@ -24,6 +24,5 @@ contract Test { uint t = 20; uint u = 21; uint v = 22; - } } \ No newline at end of file From 59d3af3298a40fea7afb6907d84c30a35e8c19ff Mon Sep 17 00:00:00 2001 From: 0xtekgrinder <0xtekgrinder@protonmail.com> Date: Wed, 20 Sep 2023 22:40:36 -0400 Subject: [PATCH 52/59] chore: remove remove-me --- remove-me-1b9390d0431343b6a1aa.txt | 1 - 1 file changed, 1 deletion(-) delete mode 100644 remove-me-1b9390d0431343b6a1aa.txt diff --git a/remove-me-1b9390d0431343b6a1aa.txt b/remove-me-1b9390d0431343b6a1aa.txt deleted file mode 100644 index 3313bfdf..00000000 --- a/remove-me-1b9390d0431343b6a1aa.txt +++ /dev/null @@ -1 +0,0 @@ -1b9390d0431343b6a1aa From bf698e5f1a2fc6802a04c17d9dd590645b8ca59b Mon Sep 17 00:00:00 2001 From: Astrodevs CI Date: Tue, 26 Sep 2023 22:38:32 +0000 Subject: [PATCH 53/59] chore: create branch feature/49-solidity-linter/131-default-rules-are-loaded-and-not-created-when-linting-the-files-staging --- remove-me-8bf2020aa87d4372b9f9.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 remove-me-8bf2020aa87d4372b9f9.txt diff --git a/remove-me-8bf2020aa87d4372b9f9.txt b/remove-me-8bf2020aa87d4372b9f9.txt new file mode 100644 index 00000000..4d1009ef --- /dev/null +++ b/remove-me-8bf2020aa87d4372b9f9.txt @@ -0,0 +1 @@ +8bf2020aa87d4372b9f9 From 684f24bf8607cae47c24724437c7377e1595222e Mon Sep 17 00:00:00 2001 From: 0xMemoryGrinder <35138272+0xMemoryGrinder@users.noreply.github.com> Date: Thu, 28 Sep 2023 22:33:50 -0400 Subject: [PATCH 54/59] fix(solidity/linter/core): doesn't create a new file when running the linter --- .../linter/core/solidhunter-lib/src/linter.rs | 26 +++++++------------ .../solidhunter-lib/src/rules/rule_impl.rs | 4 +-- 2 files changed, 11 insertions(+), 19 deletions(-) diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs index 3bfb0a07..e940e233 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs @@ -1,5 +1,6 @@ +use crate::errors::SolidHunterError; use crate::rules::factory::RuleFactory; -use crate::rules::rule_impl::{create_rules_file, parse_rules}; +use crate::rules::rule_impl::parse_rules; use crate::rules::types::*; use crate::types::*; use std::fs; @@ -20,7 +21,7 @@ pub struct SolidLinter { impl Default for SolidLinter { fn default() -> Self { - SolidLinter::new(&String::new()) + SolidLinter::new(&".solidhunter.json".to_string()) } } @@ -31,26 +32,17 @@ impl SolidLinter { rule_factory: RuleFactory::default(), rules: Vec::new(), }; - linter._create_rules(rules_config, true); + linter._create_rules(rules_config).unwrap(); linter } - fn _create_rules(&mut self, rules_config: &String, first: bool) { - let res = parse_rules(rules_config.as_str()); - match res { - Ok(rules) => { - for rule in rules.rules { - self.rules.push(self.rule_factory.create_rule(rule)); - } - } - Err(_) => { - create_rules_file(rules_config.as_str()); - if first { - self._create_rules(rules_config, false); - } + fn _create_rules(&mut self, rules_config: &String) -> Result<(), SolidHunterError> { + let res = parse_rules(rules_config.as_str())?; + for rule in res.rules { + self.rules.push(self.rule_factory.create_rule(rule)); } + Ok(()) } - } fn _file_exists(&self, path: &str) -> bool { for file in &self.files { diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs index e049745b..5975da80 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs @@ -16,9 +16,9 @@ pub fn create_rules_file(path: &str) { pub fn parse_rules(path: &str) -> Result { if !std::path::Path::new(&path).is_file() { - return Err(SolidHunterError::IoError(std::io::Error::new( + return Err(SolidHunterError::IoError(std::io::Error::new( std::io::ErrorKind::NotFound, - "Rules file not found", + "Failed to load a solidhunter's config file", ))); } let file = std::fs::read_to_string(path)?; From ec6a83ae03faf9a16cf41a37a0c8760dd043f250 Mon Sep 17 00:00:00 2001 From: 0xtekgrinder <0xtekgrinder@protonmail.com> Date: Thu, 28 Sep 2023 14:10:30 -0400 Subject: [PATCH 55/59] fix(solidity/linter/core): doesn't create a file when none are specificied --- libs/ast-extractor/src/errors.rs | 4 ++-- libs/ast-extractor/src/extract.rs | 1 - libs/ast-extractor/src/lib.rs | 4 ++-- .../linter/core/solidhunter-lib/src/linter.rs | 14 +++++------ .../core/solidhunter-lib/src/rules/factory.rs | 3 --- .../solidhunter-lib/src/rules/rule_impl.rs | 2 +- .../linter/core/solidhunter-lib/src/types.rs | 23 +++++++++---------- 7 files changed, 23 insertions(+), 28 deletions(-) diff --git a/libs/ast-extractor/src/errors.rs b/libs/ast-extractor/src/errors.rs index c8192a38..ab5ce042 100644 --- a/libs/ast-extractor/src/errors.rs +++ b/libs/ast-extractor/src/errors.rs @@ -1,6 +1,6 @@ +use proc_macro2::LexError; use syn::Error; use thiserror::Error; -use proc_macro2::LexError; #[derive(Error, Debug)] pub enum ExtractError { @@ -8,4 +8,4 @@ pub enum ExtractError { Tokenize(#[from] LexError), #[error("Parsing error")] Parse(#[from] Error), -} \ No newline at end of file +} diff --git a/libs/ast-extractor/src/extract.rs b/libs/ast-extractor/src/extract.rs index 4e61bc5c..4130cb3b 100644 --- a/libs/ast-extractor/src/extract.rs +++ b/libs/ast-extractor/src/extract.rs @@ -3,7 +3,6 @@ * Extract AST from solidity source code * author: 0xMemoryGrinder */ - use crate::errors::ExtractError; use proc_macro2::TokenStream; use std::str::FromStr; diff --git a/libs/ast-extractor/src/lib.rs b/libs/ast-extractor/src/lib.rs index fb6764d5..00c15349 100644 --- a/libs/ast-extractor/src/lib.rs +++ b/libs/ast-extractor/src/lib.rs @@ -1,9 +1,9 @@ +pub mod errors; pub mod extract; pub mod retriever; -pub mod errors; // Expose syn_solidity crate pub use syn_solidity::*; // Publish span location type -pub use proc_macro2::LineColumn; \ No newline at end of file +pub use proc_macro2::LineColumn; diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs index e940e233..e5067a46 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/linter.rs @@ -30,19 +30,19 @@ impl SolidLinter { let mut linter = SolidLinter { files: Vec::new(), rule_factory: RuleFactory::default(), - rules: Vec::new(), + rules: vec![], }; linter._create_rules(rules_config).unwrap(); linter } - fn _create_rules(&mut self, rules_config: &String) -> Result<(), SolidHunterError> { - let res = parse_rules(rules_config.as_str())?; - for rule in res.rules { - self.rules.push(self.rule_factory.create_rule(rule)); - } - Ok(()) + fn _create_rules(&mut self, rules_config: &str) -> Result<(), SolidHunterError> { + let res = parse_rules(rules_config)?; + for rule in res.rules { + self.rules.push(self.rule_factory.create_rule(rule)); } + Ok(()) + } fn _file_exists(&self, path: &str) -> bool { for file in &self.files { diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/factory.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/factory.rs index d0ee4f32..0d7585d5 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/factory.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/factory.rs @@ -4,14 +4,12 @@ use std::collections::HashMap; pub struct RuleFactory { _buildables: RulesMap, - _rules: Vec>, } impl Default for RuleFactory { fn default() -> Self { RuleFactory { _buildables: create_rules(), - _rules: Vec::new(), } } } @@ -20,7 +18,6 @@ impl RuleFactory { pub fn new() -> RuleFactory { RuleFactory { _buildables: HashMap::new(), - _rules: Vec::new(), } } diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs index 5975da80..bf489450 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/rule_impl.rs @@ -16,7 +16,7 @@ pub fn create_rules_file(path: &str) { pub fn parse_rules(path: &str) -> Result { if !std::path::Path::new(&path).is_file() { - return Err(SolidHunterError::IoError(std::io::Error::new( + return Err(SolidHunterError::IoError(std::io::Error::new( std::io::ErrorKind::NotFound, "Failed to load a solidhunter's config file", ))); diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs index e198254a..1652ba45 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/types.rs @@ -1,7 +1,7 @@ -use std::cmp::min; use crate::errors::SolidHunterError; use colored::Colorize; use serde::{Deserialize, Serialize}; +use std::cmp::min; use std::fmt; pub type LintResult = Result, SolidHunterError>; @@ -36,7 +36,6 @@ pub struct LintDiag { pub source_file_content: String, } - fn compute_format_line_padding(line: usize) -> String { let padding: String; if line > 99 { @@ -64,9 +63,8 @@ fn try_trim_max_offset(line: &str, max_offset: usize) -> (&str, usize) { } impl LintDiag { - fn format_highlighted_lines(&self) -> String { - let mut formatted = format!(" |\n"); + let mut formatted = " |\n".to_string(); let first_line = self .source_file_content .lines() @@ -76,11 +74,7 @@ impl LintDiag { let max_offset = first_line.len() - trimmed_first_line.len(); for line_nb in self.range.start.line..self.range.end.line + 1 { - let line = self - .source_file_content - .lines() - .nth(line_nb - 1) - .unwrap(); + let line = self.source_file_content.lines().nth(line_nb - 1).unwrap(); let (trimmed_line, offset) = try_trim_max_offset(line, max_offset); let mut higlight_length = trimmed_line.len(); @@ -89,7 +83,8 @@ impl LintDiag { } else if line_nb == self.range.start.line { higlight_length = trimmed_line.len() - (self.range.start.character - offset); } else if line_nb == self.range.end.line { - higlight_length = trimmed_line.len() - (self.range.end.character - min(offset, trimmed_line.len())); + higlight_length = trimmed_line.len() + - (self.range.end.character - min(offset, trimmed_line.len())); } formatted = format!( @@ -98,7 +93,11 @@ impl LintDiag { line_nb, compute_format_line_padding(line_nb), trimmed_line, - " ".repeat(if line_nb == self.range.start.line { self.range.start.character - offset } else { 0 }), + " ".repeat(if line_nb == self.range.start.line { + self.range.start.character - offset + } else { + 0 + }), "^".repeat(higlight_length) ); } @@ -133,7 +132,7 @@ fn severity_to_string(severity: Option) -> String { Some(Severity::HINT) => "hint".to_string().green(), _ => "error".to_string().red(), } - .to_string() + .to_string() } impl PartialEq for Position { From a4457f0c8eebb2aef2c2e413bd049fea04f84d45 Mon Sep 17 00:00:00 2001 From: 0xMemoryGrinder <35138272+0xMemoryGrinder@users.noreply.github.com> Date: Thu, 28 Sep 2023 22:35:51 -0400 Subject: [PATCH 56/59] chore: deleted remove-me.txt --- remove-me-8bf2020aa87d4372b9f9.txt | 1 - 1 file changed, 1 deletion(-) delete mode 100644 remove-me-8bf2020aa87d4372b9f9.txt diff --git a/remove-me-8bf2020aa87d4372b9f9.txt b/remove-me-8bf2020aa87d4372b9f9.txt deleted file mode 100644 index 4d1009ef..00000000 --- a/remove-me-8bf2020aa87d4372b9f9.txt +++ /dev/null @@ -1 +0,0 @@ -8bf2020aa87d4372b9f9 From 32889a73489c9960bd2c8e38744c2646f7c244ab Mon Sep 17 00:00:00 2001 From: 0xMemoryGrinder <35138272+0xMemoryGrinder@users.noreply.github.com> Date: Fri, 29 Sep 2023 12:23:57 -0400 Subject: [PATCH 57/59] fix(ci): now track extension subfolders as part of extension workspace --- .../workflows/detect-workspace-changes.yml | 46 +++++++++++++++++-- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/.github/workflows/detect-workspace-changes.yml b/.github/workflows/detect-workspace-changes.yml index b055cb2b..4149f26a 100644 --- a/.github/workflows/detect-workspace-changes.yml +++ b/.github/workflows/detect-workspace-changes.yml @@ -75,24 +75,62 @@ jobs: console.log(workspaces) const affectedWorkspaces = workspaces.filter(workspace => { return workspace.location !== '.' + }).map(workspace => { + return { + name: workspace.name + } }) console.log(affectedWorkspaces) const toolchainsWorkspaces = affectedWorkspaces.filter(workspace => { return workspace.name.startsWith('@osmium-toolchains/') - }) + }).map(workspace => ({ + name: workspace.name + })) const librariesWorkspaces = affectedWorkspaces.filter(workspace => { return workspace.name.startsWith('@osmium-libs/') - }) + }).map(workspace => ({ + name: workspace.name + })) const packagesWorkspaces = affectedWorkspaces.filter(workspace => { return workspace.name.startsWith('@osmium-packages/') - }) + }).map(workspace => ({ + name: workspace.name + })) const extensionsWorkspaces = affectedWorkspaces.filter(workspace => { return workspace.name.match(/@osmium\/(?:manager|(?!\w+-\w+-\w+)(?:\w+-\w+))/) + }).map(workspace => ({ + name: workspace.name + })) + const extensionsFoldersWorkspaces = affectedWorkspaces.filter(workspace => { + return workspace.name.match(/@osmium\/\w+-\w+-\w+/) + }).map(workspace => ({ + name: workspace.name + })) + + const missingExtensionsWorkspaces = extensionsFoldersWorkspaces.map(workspaceFolder => { + const extensionName = workspaceFolder.name.split('-').slice(0, 2).join('-') + const extensionWorkspace = extensionsWorkspaces.find(workspace => { + return workspace.name === extensionName + }) + if (!extensionWorkspace) { + return { + name: extensionName + } + } + return extensionWorkspace }) + + const completeList = [...extensionsWorkspaces, ...missingExtensionsWorkspaces] + const completeExtensionsWorkspaces = completeList.filter((workspace, index, self) => { + return index === self.findIndex((t) => ( + t.name === workspace.name + )) + }) + return { affectedWorkspaces, toolchainsWorkspaces, - extensionsWorkspaces, + extensionsWorkspaces: completeExtensionsWorkspaces, librariesWorkspaces, packagesWorkspaces } From 47df9ef1e4254a10821723f9e7269c21b1fbf484 Mon Sep 17 00:00:00 2001 From: 0xMemoryGrinder <35138272+0xMemoryGrinder@users.noreply.github.com> Date: Fri, 29 Sep 2023 12:43:24 -0400 Subject: [PATCH 58/59] refactor(solidity/linter/core): fixed linting errors --- libs/ast-extractor/src/extract.rs | 4 ++-- libs/ast-extractor/src/retriever/contract.rs | 2 +- libs/ast-extractor/src/retriever/enum.rs | 4 ++-- libs/ast-extractor/src/retriever/error.rs | 2 +- libs/ast-extractor/src/retriever/event.rs | 4 ++-- libs/ast-extractor/src/retriever/function.rs | 2 +- libs/ast-extractor/src/retriever/struct.rs | 4 ++-- libs/ast-extractor/src/retriever/udt.rs | 2 +- libs/ast-extractor/src/retriever/using.rs | 2 +- 9 files changed, 13 insertions(+), 13 deletions(-) diff --git a/libs/ast-extractor/src/extract.rs b/libs/ast-extractor/src/extract.rs index 4130cb3b..0b88575d 100644 --- a/libs/ast-extractor/src/extract.rs +++ b/libs/ast-extractor/src/extract.rs @@ -7,8 +7,8 @@ use crate::errors::ExtractError; use proc_macro2::TokenStream; use std::str::FromStr; -pub fn extract_ast_from_content(content: &String) -> Result { - let tokens = TokenStream::from_str(content.as_str())?; +pub fn extract_ast_from_content(content: &str) -> Result { + let tokens = TokenStream::from_str(content)?; let ast = syn_solidity::parse2(tokens)?; Ok(ast) } diff --git a/libs/ast-extractor/src/retriever/contract.rs b/libs/ast-extractor/src/retriever/contract.rs index 5062ecbf..c24575fa 100644 --- a/libs/ast-extractor/src/retriever/contract.rs +++ b/libs/ast-extractor/src/retriever/contract.rs @@ -26,7 +26,7 @@ impl<'ast> Visit<'ast> for ContractVisitor { pub fn retrieve_contract_nodes(ast: &syn_solidity::File) -> Vec { let mut visitor = ContractVisitor::new(); - visitor.visit_file(&ast); + visitor.visit_file(ast); visitor.contracts } diff --git a/libs/ast-extractor/src/retriever/enum.rs b/libs/ast-extractor/src/retriever/enum.rs index 7f219f21..95b50fe9 100644 --- a/libs/ast-extractor/src/retriever/enum.rs +++ b/libs/ast-extractor/src/retriever/enum.rs @@ -40,13 +40,13 @@ impl<'ast> Visit<'ast> for EnumVisitor { pub fn retrieve_enums_contract_nodes(ast: &syn_solidity::ItemContract) -> Vec { let mut visitor = EnumVisitor::new(); - visitor.visit_item_contract(&ast); + visitor.visit_item_contract(ast); visitor.contract_enums } pub fn retrieve_enums_file_nodes(ast: &syn_solidity::File) -> Vec { let mut visitor = EnumVisitor::new(); - visitor.visit_file(&ast); + visitor.visit_file(ast); visitor.file_enums } diff --git a/libs/ast-extractor/src/retriever/error.rs b/libs/ast-extractor/src/retriever/error.rs index d8b4e514..c0099bc0 100644 --- a/libs/ast-extractor/src/retriever/error.rs +++ b/libs/ast-extractor/src/retriever/error.rs @@ -24,7 +24,7 @@ impl<'ast> Visit<'ast> for ErrorVisitor { pub fn retrieve_errors_nodes(ast: &syn_solidity::ItemContract) -> Vec { let mut visitor = ErrorVisitor::new(); - visitor.visit_item_contract(&ast); + visitor.visit_item_contract(ast); visitor.errors } diff --git a/libs/ast-extractor/src/retriever/event.rs b/libs/ast-extractor/src/retriever/event.rs index 96946da6..3bdea41a 100644 --- a/libs/ast-extractor/src/retriever/event.rs +++ b/libs/ast-extractor/src/retriever/event.rs @@ -40,13 +40,13 @@ impl<'ast> Visit<'ast> for EventVisitor { pub fn retrieve_events_contract_nodes(ast: &syn_solidity::ItemContract) -> Vec { let mut visitor = EventVisitor::new(); - visitor.visit_item_contract(&ast); + visitor.visit_item_contract(ast); visitor.contract_events } pub fn retrieve_events_file_nodes(ast: &syn_solidity::File) -> Vec { let mut visitor = EventVisitor::new(); - visitor.visit_file(&ast); + visitor.visit_file(ast); visitor.file_events } diff --git a/libs/ast-extractor/src/retriever/function.rs b/libs/ast-extractor/src/retriever/function.rs index 06079a28..29c4aeb6 100644 --- a/libs/ast-extractor/src/retriever/function.rs +++ b/libs/ast-extractor/src/retriever/function.rs @@ -26,7 +26,7 @@ impl<'ast> Visit<'ast> for FunctionVisitor { pub fn retrieve_functions_nodes(ast: &syn_solidity::ItemContract) -> Vec { let mut visitor = FunctionVisitor::new(); - visitor.visit_item_contract(&ast); + visitor.visit_item_contract(ast); visitor.functions } diff --git a/libs/ast-extractor/src/retriever/struct.rs b/libs/ast-extractor/src/retriever/struct.rs index dadbdf1c..2be3b0ca 100644 --- a/libs/ast-extractor/src/retriever/struct.rs +++ b/libs/ast-extractor/src/retriever/struct.rs @@ -39,13 +39,13 @@ impl<'ast> Visit<'ast> for StructVisitor { pub fn retrieve_structs_contract_nodes(ast: &syn_solidity::ItemContract) -> Vec { let mut visitor = StructVisitor::new(); - visitor.visit_item_contract(&ast); + visitor.visit_item_contract(ast); visitor.contract_structs } pub fn retrieve_structs_file_nodes(ast: &syn_solidity::File) -> Vec { let mut visitor = StructVisitor::new(); - visitor.visit_file(&ast); + visitor.visit_file(ast); visitor.file_structs } diff --git a/libs/ast-extractor/src/retriever/udt.rs b/libs/ast-extractor/src/retriever/udt.rs index 872d02ed..4e681ed8 100644 --- a/libs/ast-extractor/src/retriever/udt.rs +++ b/libs/ast-extractor/src/retriever/udt.rs @@ -24,7 +24,7 @@ impl<'ast> Visit<'ast> for UdtVisitor { pub fn retrieve_udts_nodes(ast: &syn_solidity::File) -> Vec { let mut visitor = UdtVisitor::new(); - visitor.visit_file(&ast); + visitor.visit_file(ast); visitor.udts } diff --git a/libs/ast-extractor/src/retriever/using.rs b/libs/ast-extractor/src/retriever/using.rs index 07b798b9..c9ac115b 100644 --- a/libs/ast-extractor/src/retriever/using.rs +++ b/libs/ast-extractor/src/retriever/using.rs @@ -24,7 +24,7 @@ impl<'ast> Visit<'ast> for UsingVisitor { pub fn retrieve_usings_nodes(ast: &syn_solidity::ItemContract) -> Vec { let mut visitor = UsingVisitor::new(); - visitor.visit_item_contract(&ast); + visitor.visit_item_contract(ast); visitor.usings } From fb9e89ca707dcb4115f2bd6b28d8f5260c324057 Mon Sep 17 00:00:00 2001 From: 0xMemoryGrinder <35138272+0xMemoryGrinder@users.noreply.github.com> Date: Fri, 29 Sep 2023 13:38:07 -0400 Subject: [PATCH 59/59] chore: deleted remove-me --- remove-me-34219c48b31d404ab611.txt | 1 - 1 file changed, 1 deletion(-) delete mode 100644 remove-me-34219c48b31d404ab611.txt diff --git a/remove-me-34219c48b31d404ab611.txt b/remove-me-34219c48b31d404ab611.txt deleted file mode 100644 index 78564588..00000000 --- a/remove-me-34219c48b31d404ab611.txt +++ /dev/null @@ -1 +0,0 @@ -34219c48b31d404ab611