1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
use osmium_libs_solidity_ast_extractor::*;

use crate::linter::SolidFile;
use crate::rules::types::{RuleEntry, RuleType};
use crate::types::{LintDiag, Position, Range, Severity};

// global
pub const RULE_ID: &str = "no-console";

// specific
const DEFAULT_SEVERITY: Severity = Severity::WARNING;

pub struct NoConsole {
    data: RuleEntry,
}

impl NoConsole {
    fn create_diag(
        &self,
        file: &SolidFile,
        location: (LineColumn, LineColumn),
        message: String,
    ) -> LintDiag {
        LintDiag {
            id: RULE_ID.to_string(),
            range: Range {
                start: Position {
                    line: location.0.line,
                    character: location.0.column,
                },
                end: Position {
                    line: location.1.line,
                    character: location.1.column,
                },
            },
            message,
            severity: self.data.severity,
            code: None,
            source: None,
            uri: file.path.clone(),
        }
    }
}

impl RuleType for NoConsole {
    fn diagnose(&self, file: &SolidFile, _files: &[SolidFile]) -> Vec<LintDiag> {
        let mut res: Vec<LintDiag> = Vec::new();

        // Check functions calls
        for expr_member in retriever::retrieve_expr_member_nodes(&file.data) {
            if let Expr::Ident(expr_ident) = *expr_member.expr {
                if expr_ident == "console" {
                    if let Expr::Call(expr_call) = *expr_member.member {
                        if let Expr::Ident(expr_ident) = *expr_call.expr {
                            if expr_ident.as_string().starts_with("log") {
                                let diag = self.create_diag(
                                    file,
                                    (expr_ident.span().start(), expr_ident.span().end()),
                                    "Unexpected console statement".to_string(),
                                );
                                res.push(diag);
                            }
                        }
                    }
                }
            }
        }

        // Check imports
        let blacklist: Vec<String> = vec![
            "hardhat/console.sol".to_string(),
            "forge-std/console".to_string(), // console?.sol (easier to blacklist this way)
        ];

        for header in retriever::retrieve_import_directive_nodes(&file.data) {
            if let ImportPath::Plain(pathplain) = &header.path {
                for test in &pathplain.path.values {
                    for refused in blacklist.clone() {
                        if test.value().contains(&refused) {
                            let diag = self.create_diag(
                                file,
                                (header.span().start(), header.span().end()),
                                "Unexpected import of console file".to_string(),
                            );
                            res.push(diag);
                        }
                    }
                }
            }
        }

        res
    }
}

impl NoConsole {
    pub fn create(data: RuleEntry) -> Box<dyn RuleType> {
        let rule = NoConsole { data };
        Box::new(rule)
    }

    pub fn create_default() -> RuleEntry {
        RuleEntry {
            id: RULE_ID.to_string(),
            severity: DEFAULT_SEVERITY,
            data: None,
        }
    }
}