|
1 | 1 | import 'package:analyzer/dart/ast/ast.dart'; |
2 | | -import 'package:analyzer/dart/ast/syntactic_entity.dart'; |
| 2 | +import 'package:analyzer/dart/element/element2.dart'; |
3 | 3 | import 'package:analyzer/error/listener.dart'; |
4 | | -import 'package:collection/collection.dart'; |
5 | 4 | import 'package:custom_lint_builder/custom_lint_builder.dart'; |
| 5 | +import 'package:solid_lints/src/lints/dont_create_a_return_var/visitors/dont_create_a_return_var_visitor.dart'; |
6 | 6 | import 'package:solid_lints/src/models/rule_config.dart'; |
7 | 7 | import 'package:solid_lints/src/models/solid_lint_rule.dart'; |
8 | 8 |
|
@@ -77,34 +77,82 @@ Rewrite the variable evaluation into return statement instead.""", |
77 | 77 | }) { |
78 | 78 | final expr = statement.expression; |
79 | 79 | if (expr is! SimpleIdentifier) return; |
80 | | - final returnVariableToken = expr.token; |
81 | 80 |
|
82 | | - //Looking for statement previous to return |
83 | | - final parent = statement.parent; |
84 | | - if (parent == null) return; |
| 81 | + final element = expr.element; |
| 82 | + if(element is! LocalVariableElement2) return; |
| 83 | + final returnVariableElement = element; |
85 | 84 |
|
86 | | - SyntacticEntity? previous; |
87 | | - for (final child in parent.childEntities) { |
88 | | - if (child == statement) break; |
89 | | - previous = child; |
| 85 | + if(!returnVariableElement.isFinal && !returnVariableElement.isConst) return; |
| 86 | + |
| 87 | + final blockBody = statement.parent; |
| 88 | + if (blockBody == null) return; |
| 89 | + |
| 90 | + final visitor = DontCreateAReturnVarVisitor( |
| 91 | + returnVariableElement, |
| 92 | + statement, |
| 93 | + ); |
| 94 | + blockBody.visitChildren(visitor); |
| 95 | + |
| 96 | + if (!visitor.hasBadStatementCount()) return; |
| 97 | + |
| 98 | + //it is 100% bad if return statement follows declaration |
| 99 | + if (!visitor.foundTokensBetweenDeclarationAndReturn) { |
| 100 | + reporter.atNode(statement, code); |
| 101 | + return; |
90 | 102 | } |
91 | | - if (previous == null) return; |
92 | 103 |
|
93 | | - if (previous is! VariableDeclarationStatement) return; |
94 | | - final declarationStatement = previous; |
| 104 | + final declaration = visitor.variableDeclaration; |
95 | 105 |
|
96 | | - //Checking if return variable was declared in previous statement |
97 | | - final VariableDeclaration? variableDeclaration = |
98 | | - declarationStatement.variables.variables |
99 | | - .firstWhereOrNull( |
100 | | - (v) => v.name.toString() == returnVariableToken.toString(), |
101 | | - ); |
102 | | - if (variableDeclaration == null) return; |
103 | | - |
104 | | - //Skipping mutable variables |
105 | | - if (!variableDeclaration.isFinal && !variableDeclaration.isConst) return; |
106 | | - |
107 | | - reporter.atNode(expr, code); |
| 106 | + //check if immutable |
| 107 | + final initializer = declaration?.initializer; |
| 108 | + if (initializer == null) return; |
| 109 | + |
| 110 | + if (!_isExpressionImmutable(initializer)) return; |
| 111 | + |
| 112 | + reporter.atNode(statement, code); |
108 | 113 | } |
109 | 114 |
|
| 115 | + bool _isExpressionImmutable(Expression expr) { |
| 116 | + if (expr is Literal) return true; |
| 117 | + |
| 118 | + if (expr case final PrefixedIdentifier prefixed) { |
| 119 | + return |
| 120 | + _isExpressionImmutable(prefixed.prefix) |
| 121 | + && _isExpressionImmutable(prefixed.identifier); |
| 122 | + } |
| 123 | + |
| 124 | + if (expr case final BinaryExpression binExpr) { |
| 125 | + return |
| 126 | + _isExpressionImmutable(binExpr.leftOperand) |
| 127 | + && _isExpressionImmutable(binExpr.rightOperand); |
| 128 | + } |
| 129 | + |
| 130 | + if (expr case final SimpleIdentifier identifier) { |
| 131 | + return _isSimpleIdentifierImmutable(identifier); |
| 132 | + } |
| 133 | + |
| 134 | + return false; |
| 135 | + } |
| 136 | + |
| 137 | + bool _isSimpleIdentifierImmutable(SimpleIdentifier identifier) { |
| 138 | + if (identifier.element |
| 139 | + case final VariableElement2 variable |
| 140 | + when variable.isFinal || variable.isConst |
| 141 | + ) { |
| 142 | + return true; |
| 143 | + } |
| 144 | + |
| 145 | + if (identifier.element is ClassElement2) return true; |
| 146 | + |
| 147 | + if (identifier.element case final GetterElement getter) { |
| 148 | + if (getter.variable3 |
| 149 | + case final PropertyInducingElement2 property |
| 150 | + when property.isFinal || property.isConst |
| 151 | + ) { |
| 152 | + return true; |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + return false; |
| 157 | + } |
110 | 158 | } |
0 commit comments