| 1 | import "package:analyzer/analysis_rule/analysis_rule.dart"; |
| 2 | import "package:analyzer/analysis_rule/rule_context.dart"; |
| 3 | import "package:analyzer/analysis_rule/rule_visitor_registry.dart"; |
| 4 | import "package:analyzer/dart/ast/ast.dart"; |
| 5 | import "package:analyzer/dart/ast/visitor.dart"; |
| 6 | import "package:analyzer/error/error.dart"; |
| 7 | |
| 8 | class PrintVerboseRule extends AnalysisRule { |
| 9 | PrintVerboseRule() |
| 10 | : super( |
| 11 | name: "use_print_v", |
| 12 | description: "Use printV() from cw_core instead of print().", |
| 13 | ); |
| 14 | |
| 15 | static const LintCode code = LintCode( |
| 16 | "use_print_v", |
| 17 | "Use printV() from cw_core instead", |
| 18 | correctionMessage: "Replace print with printV", |
| 19 | severity: DiagnosticSeverity.WARNING, |
| 20 | ); |
| 21 | |
| 22 | @override |
| 23 | LintCode get diagnosticCode => code; |
| 24 | |
| 25 | @override |
| 26 | void registerNodeProcessors( |
| 27 | RuleVisitorRegistry registry, |
| 28 | RuleContext context, |
| 29 | ) { |
| 30 | final filePath = context.definingUnit.file.path; |
| 31 | |
| 32 | if (filePath.contains("/tool/") || filePath.contains("print_verbose.dart")) { |
| 33 | // tool/ is allowed to use print as it never makes its way into the app |
| 34 | return; |
| 35 | } |
| 36 | |
| 37 | registry.addMethodInvocation(this, _Visitor(this)); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | class _Visitor extends SimpleAstVisitor<void> { |
| 42 | _Visitor(this.rule); |
| 43 | |
| 44 | final AnalysisRule rule; |
| 45 | |
| 46 | @override |
| 47 | void visitMethodInvocation(MethodInvocation node) { |
| 48 | if (node.methodName.name == "print" && node.target == null) { |
| 49 | rule.reportAtNode(node); |
| 50 | } |
| 51 | } |
| 52 | } |