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

Commit

Permalink
refactor: use ast
Browse files Browse the repository at this point in the history
  • Loading branch information
Leon-azerty authored and 0xmemorygrinder committed Oct 7, 2023
1 parent def7032 commit 27cec17
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use ast_extractor::{ImportPath, Item, Spanned};

use crate::linter::SolidFile;
use crate::rules::types::*;
use crate::types::*;
Expand All @@ -13,40 +15,63 @@ pub struct GlobalImport {
impl RuleType for GlobalImport {
fn diagnose(&self, _file: &SolidFile, _files: &[SolidFile]) -> Vec<LintDiag> {
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::<Vec<&str>>();
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<Option<Range>> {
let mut reports: Vec<Option<Range>> = 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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
global-import:3:0:3:19
global-import:9:7:9:21
global-import:11:7:11:16
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ test_directories! {
FunctionVisibility,
OneContractPerFile,
CustomErrors,
EventNameCamelCase
EventNameCamelCase,
ConstNameSnakeCase,
StateVisibility,
GlobalImport
Expand Down

0 comments on commit 27cec17

Please sign in to comment.