diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/global_import.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/global_import.rs index 830240c4..a241f667 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/global_import.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/global_import.rs @@ -1,3 +1,5 @@ +use ast_extractor::{ImportPath, Item, Spanned}; + use crate::linter::SolidFile; use crate::rules::types::*; use crate::types::*; @@ -13,40 +15,63 @@ pub struct GlobalImport { impl RuleType for GlobalImport { fn diagnose(&self, _file: &SolidFile, _files: &[SolidFile]) -> Vec { let mut res = Vec::new(); - let mut index_line = 1; - _file.content.lines().for_each(|line| { - if line.find("import").is_some() { - let line_splitted = line.split(' ').collect::>(); - if line_splitted.len() <= 2 || (line_splitted[1] == "*" && line_splitted[2] != "as") - { - let range = Range { + let reports = check_global_import(_file); + for report in reports { + if let Some(rep) = report { + res.push(LintDiag { + id: RULE_ID.to_string(), + range: rep, + severity: Some(Severity::WARNING), + code: None, + source: None, + message: DEFAULT_MESSAGE.to_string(), + uri: _file.path.clone(), + source_file_content: _file.content.clone(), + }); + } + } + println!("res: {:?}", res); + res + } +} + +fn check_global_import(file: &SolidFile) -> Vec> { + let mut reports: Vec> = Vec::new(); + + file.data.items.iter().for_each(|item| { + if let Item::Import(import) = item { + if let ImportPath::Plain(plain) = &import.path { + if plain.alias.is_none() { + reports.push(Some(Range { start: Position { - line: index_line, - character: 0, + line: plain.span().start().line, + character: plain.span().start().column, }, end: Position { - line: index_line, - character: line.len(), + line: plain.span().end().line, + character: plain.span().end().column, }, - }; - res.push(LintDiag { - id: RULE_ID.to_string(), - range: range, - severity: Some(Severity::WARNING), - code: None, - source: None, - message: DEFAULT_MESSAGE.to_string(), - uri: _file.path.clone(), - source_file_content: _file.content.clone(), - }); + })); } } - index_line += 1; - }); - println!("res: {:?}", res); - res - } + if let ImportPath::Glob(glob) = &import.path { + if glob.alias.is_none() { + reports.push(Some(Range { + start: Position { + line: glob.span().start().line, + character: glob.span().start().column, + }, + end: Position { + line: glob.span().end().line, + character: glob.span().end().column, + }, + })); + } + } + } + }); + reports } impl GlobalImport { diff --git a/toolchains/solidity/linter/core/solidhunter-lib/testdata/GlobalImport/file.sol b/toolchains/solidity/linter/core/solidhunter-lib/testdata/GlobalImport/file.sol index fd554bec..93526a0e 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/testdata/GlobalImport/file.sol +++ b/toolchains/solidity/linter/core/solidhunter-lib/testdata/GlobalImport/file.sol @@ -1,6 +1,14 @@ pragma solidity ^0.8.0; -import "./add.sol"; +import {add} from "./add.sol"; //pas flag + +import "./A.sol" as A; //pas flag + +import * as B from "./B.sol"; //pas flag + +import * from "C.sol"; //flag + +import "./D.sol"; //flag contract Test { function test() public pure returns (uint256) { diff --git a/toolchains/solidity/linter/core/solidhunter-lib/testdata/GlobalImport/findings.csv b/toolchains/solidity/linter/core/solidhunter-lib/testdata/GlobalImport/findings.csv index 6b93d638..d553f1be 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/testdata/GlobalImport/findings.csv +++ b/toolchains/solidity/linter/core/solidhunter-lib/testdata/GlobalImport/findings.csv @@ -1 +1,2 @@ -global-import:3:0:3:19 \ No newline at end of file +global-import:9:7:9:21 +global-import:11:7:11: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 28b4d29d..5db50ff1 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/tests/linter.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/tests/linter.rs @@ -106,7 +106,7 @@ test_directories! { FunctionVisibility, OneContractPerFile, CustomErrors, - EventNameCamelCase + EventNameCamelCase, ConstNameSnakeCase, StateVisibility, GlobalImport