|
| 1 | +import 'package:analyzer/dart/ast/ast.dart'; |
| 2 | +import 'package:analyzer/dart/ast/visitor.dart'; |
| 3 | +import 'package:analyzer/error/listener.dart'; |
| 4 | +import 'package:collection/collection.dart'; |
| 5 | +import 'package:custom_lint_builder/custom_lint_builder.dart'; |
| 6 | +import 'package:solid_lints/src/lints/avoid_initstate_field_initialization/visitors/avoid_initstate_field_initialization_visitor.dart'; |
| 7 | +import 'package:solid_lints/src/models/rule_config.dart'; |
| 8 | +import 'package:solid_lints/src/models/solid_lint_rule.dart'; |
| 9 | + |
| 10 | +/// An `avoid_initstate_field_initialization` rule which suggests using |
| 11 | +/// declaration initializer over initializing field from `initState()` |
| 12 | +/// if possible. |
| 13 | +/// |
| 14 | +/// See more here: https://github.com/solid-software/solid_lints/issues/148 |
| 15 | +/// |
| 16 | +/// ### Example |
| 17 | +/// |
| 18 | +/// #### BAD: |
| 19 | +/// |
| 20 | +/// ```dart |
| 21 | +/// class SomeState extends State<Some> { |
| 22 | +/// late final X? x; |
| 23 | +/// void initState() { |
| 24 | +/// x = widget.x; |
| 25 | +/// } |
| 26 | +/// } |
| 27 | +/// ``` |
| 28 | +/// |
| 29 | +/// #### GOOD: |
| 30 | +/// |
| 31 | +/// ```dart |
| 32 | +/// class SomeState extends State<Some> { |
| 33 | +/// late final x = widget.x; |
| 34 | +/// } |
| 35 | +/// ``` |
| 36 | +/// |
| 37 | +class AvoidInitstateFieldInitializationRule extends SolidLintRule { |
| 38 | + /// This lint rule represents the error when field |
| 39 | + /// is initialized from `initState()` |
| 40 | + /// when declaration initializer is possible. |
| 41 | + static const lintName = 'avoid_initstate_field_initialization'; |
| 42 | + |
| 43 | + /// Name of `initState()` method. |
| 44 | + static const initState = 'initState'; |
| 45 | + |
| 46 | + AvoidInitstateFieldInitializationRule._(super.config); |
| 47 | + |
| 48 | + /// Creates a new instance of [AvoidInitstateFieldInitializationRule] |
| 49 | + /// based on the lint configuration. |
| 50 | + factory AvoidInitstateFieldInitializationRule.createRule( |
| 51 | + CustomLintConfigs configs, |
| 52 | + ) { |
| 53 | + final rule = RuleConfig( |
| 54 | + configs: configs, |
| 55 | + name: lintName, |
| 56 | + problemMessage: (_) => """ |
| 57 | +Avoid initialization final fields in initState(). |
| 58 | +Use declaration initializer instead.""", |
| 59 | + ); |
| 60 | + |
| 61 | + return AvoidInitstateFieldInitializationRule._(rule); |
| 62 | + } |
| 63 | + |
| 64 | + @override |
| 65 | + void run( |
| 66 | + CustomLintResolver resolver, |
| 67 | + ErrorReporter reporter, |
| 68 | + CustomLintContext context, |
| 69 | + ) { |
| 70 | + context.registry.addClassDeclaration((classDec) { |
| 71 | + _checkClass( |
| 72 | + classDec: classDec, |
| 73 | + reporter: reporter, |
| 74 | + context: context, |
| 75 | + ); |
| 76 | + }); |
| 77 | + // context.registry.addMethodDeclaration( |
| 78 | + // (method) { |
| 79 | + // if (method.name.toString() != initState) return; |
| 80 | + |
| 81 | + // _checkInitStateMethod( |
| 82 | + // method: method, |
| 83 | + // reporter: reporter, |
| 84 | + // context: context, |
| 85 | + // ); |
| 86 | + // }, |
| 87 | + // ); |
| 88 | + } |
| 89 | + |
| 90 | + void _checkClass({ |
| 91 | + required ClassDeclaration classDec, |
| 92 | + required ErrorReporter reporter, |
| 93 | + required CustomLintContext context, |
| 94 | + }) { |
| 95 | + final classElement = classDec.declaredFragment?.element; |
| 96 | + if (classElement == null) return; |
| 97 | + |
| 98 | + //shortcut by checking class info without any visitor |
| 99 | + final initStateMethod = classElement.methods2.firstWhereOrNull( |
| 100 | + (method) => method.name3 == initState, |
| 101 | + ); |
| 102 | + if (initStateMethod == null) return; |
| 103 | + |
| 104 | + //TODO REMOVE THIS |
| 105 | + //For test purposes ignoring all classes except in test file. |
| 106 | + if (!classElement.name3.toString().startsWith("_TestWidgetState")) return; |
| 107 | + print("\n${classElement.name3}"); |
| 108 | + //TODO END REMOVE |
| 109 | + |
| 110 | + final visitor = AvoidInitstateFieldInitializationVisitor(); |
| 111 | + |
| 112 | + classDec.accept(visitor); |
| 113 | + } |
| 114 | + |
| 115 | + void _checkInitStateMethod({ |
| 116 | + required MethodDeclaration method, |
| 117 | + required ErrorReporter reporter, |
| 118 | + required CustomLintContext context, |
| 119 | + }) { |
| 120 | + final classDeclaration = method.thisOrAncestorOfType<ClassDeclaration>(); |
| 121 | + final classElement = classDeclaration?.declaredFragment?.element; |
| 122 | + if (classElement == null) return; |
| 123 | + |
| 124 | + //TODO REMOVE THIS |
| 125 | + //For test purposes ignoring all classes except in test file. |
| 126 | + if (!classElement.name3.toString().startsWith("_TestWidgetState")) return; |
| 127 | + print("\n${classElement.name3}"); |
| 128 | + //TODO END REMOVE |
| 129 | + |
| 130 | + //fields are clearly recognizeable here |
| 131 | + print(classElement.fields2); |
| 132 | + |
| 133 | + AstNode body = method.body; |
| 134 | + while (body.childEntities.firstOrNull != null) { |
| 135 | + if (body.childEntities.first is! AstNode) break; |
| 136 | + body = body.childEntities.first as AstNode; |
| 137 | + } |
| 138 | + |
| 139 | + for (final child in body.childEntities) { |
| 140 | + // print(child); |
| 141 | + // print(child.runtimeType); |
| 142 | + if (child is! ExpressionStatement) continue; |
| 143 | + |
| 144 | + final expr = child.expression; |
| 145 | + if (expr is! AssignmentExpression) continue; |
| 146 | + |
| 147 | + final leftHand = expr.leftHandSide; |
| 148 | + if (leftHand is! SimpleIdentifier) continue; |
| 149 | + print(leftHand.element); |
| 150 | + |
| 151 | + if (child.expression case final SimpleIdentifier id) { |
| 152 | + print(id.element); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + //TODO: if restoring this method, visitor should become Recursive again |
| 157 | + final visitor = AvoidInitstateFieldInitializationVisitor(); |
| 158 | + method.visitChildren(visitor); |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +//HACK: Failed attempt to use separate visitor, didn't help |
| 163 | +class _VisitorWrapper extends SimpleAstVisitor<void> { |
| 164 | + @override |
| 165 | + void visitMethodDeclaration(MethodDeclaration node) { |
| 166 | + if (node.name.toString() != |
| 167 | + AvoidInitstateFieldInitializationRule.initState) { |
| 168 | + return; |
| 169 | + } |
| 170 | + final visitor = AvoidInitstateFieldInitializationVisitor(); |
| 171 | + node.visitChildren(visitor); |
| 172 | + } |
| 173 | +} |
0 commit comments