|
| 1 | +import 'package:analyzer/dart/ast/ast.dart'; |
| 2 | +import 'package:analyzer/dart/ast/syntactic_entity.dart'; |
| 3 | +import 'package:analyzer/error/listener.dart'; |
| 4 | +import 'package:custom_lint_builder/custom_lint_builder.dart'; |
| 5 | +import 'package:solid_lints/src/lints/avoid_debug_print/models/avoid_debug_print_func_model.dart'; |
| 6 | +import 'package:solid_lints/src/models/rule_config.dart'; |
| 7 | +import 'package:solid_lints/src/models/solid_lint_rule.dart'; |
| 8 | + |
| 9 | +/// A `avoid_debug_print` rule which forbids calling or referencing |
| 10 | +/// debugPrint function from flutter/foundation. |
| 11 | +/// |
| 12 | +/// ### Example |
| 13 | +/// |
| 14 | +/// #### BAD: |
| 15 | +/// |
| 16 | +/// ```dart |
| 17 | +/// debugPrint(''); // LINT |
| 18 | +/// var ref = debugPrint; // LINT |
| 19 | +/// var ref2; |
| 20 | +/// ref2 = debugPrint; // LINT |
| 21 | +/// ``` |
| 22 | +/// |
| 23 | +/// #### GOOD: |
| 24 | +/// |
| 25 | +/// ```dart |
| 26 | +/// log(''); |
| 27 | +/// ``` |
| 28 | +class AvoidDebugPrint extends SolidLintRule { |
| 29 | + /// The [LintCode] of this lint rule that represents |
| 30 | + /// the error when debugPrint is called |
| 31 | + static const lintName = 'avoid_debug_print'; |
| 32 | + |
| 33 | + AvoidDebugPrint._(super.config); |
| 34 | + |
| 35 | + /// Creates a new instance of [AvoidDebugPrint] |
| 36 | + /// based on the lint configuration. |
| 37 | + factory AvoidDebugPrint.createRule(CustomLintConfigs configs) { |
| 38 | + final rule = RuleConfig( |
| 39 | + configs: configs, |
| 40 | + name: lintName, |
| 41 | + problemMessage: (_) => "Avoid using 'debugPrint'", |
| 42 | + ); |
| 43 | + |
| 44 | + return AvoidDebugPrint._(rule); |
| 45 | + } |
| 46 | + |
| 47 | + @override |
| 48 | + void run( |
| 49 | + CustomLintResolver resolver, |
| 50 | + ErrorReporter reporter, |
| 51 | + CustomLintContext context, |
| 52 | + ) { |
| 53 | + context.registry.addFunctionExpressionInvocation( |
| 54 | + (node) { |
| 55 | + final func = node.function; |
| 56 | + if (func is! Identifier) { |
| 57 | + return; |
| 58 | + } |
| 59 | + _checkIdentifier( |
| 60 | + identifier: func, |
| 61 | + node: node, |
| 62 | + reporter: reporter, |
| 63 | + ); |
| 64 | + }, |
| 65 | + ); |
| 66 | + |
| 67 | + // addFunctionReference does not get triggered. |
| 68 | + // addVariableDeclaration and addAssignmentExpression |
| 69 | + // are used as a workaround for simple cases |
| 70 | + |
| 71 | + context.registry.addVariableDeclaration((node) { |
| 72 | + _handleVariableAssignmentDeclaration( |
| 73 | + node: node, |
| 74 | + reporter: reporter, |
| 75 | + ); |
| 76 | + }); |
| 77 | + |
| 78 | + context.registry.addAssignmentExpression((node) { |
| 79 | + _handleVariableAssignmentDeclaration( |
| 80 | + node: node, |
| 81 | + reporter: reporter, |
| 82 | + ); |
| 83 | + }); |
| 84 | + } |
| 85 | + |
| 86 | + /// Checks whether the function identifier satisfies conditions |
| 87 | + void _checkIdentifier({ |
| 88 | + required Identifier identifier, |
| 89 | + required AstNode node, |
| 90 | + required ErrorReporter reporter, |
| 91 | + }) { |
| 92 | + final funcModel = AvoidDebugPrintFuncModel.parseExpression(identifier); |
| 93 | + |
| 94 | + if (funcModel.hasSameName && funcModel.hasTheSameSource) { |
| 95 | + reporter.reportErrorForNode(code, node); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + /// Returns null if doesnt have right operand |
| 100 | + SyntacticEntity? _getRightOperand(List<SyntacticEntity> entities) { |
| 101 | + /// Example var t = 15; 15 is in 3d position |
| 102 | + if (entities.length < 3) { |
| 103 | + return null; |
| 104 | + } |
| 105 | + return entities[2]; |
| 106 | + } |
| 107 | + |
| 108 | + /// Handles variable assignment and declaration |
| 109 | + void _handleVariableAssignmentDeclaration({ |
| 110 | + required AstNode node, |
| 111 | + required ErrorReporter reporter, |
| 112 | + }) { |
| 113 | + final rightOperand = _getRightOperand(node.childEntities.toList()); |
| 114 | + |
| 115 | + if (rightOperand == null || rightOperand is! Identifier) { |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + _checkIdentifier( |
| 120 | + identifier: rightOperand, |
| 121 | + node: node, |
| 122 | + reporter: reporter, |
| 123 | + ); |
| 124 | + } |
| 125 | +} |
0 commit comments