-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor avoid_final_with_getter_rule.dart and avoid_final_with_gette…
…r_visitor.dart
- Loading branch information
Showing
5 changed files
with
90 additions
and
79 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
lib/src/lints/avoid_final_with_getter/avoid_final_with_getter_rule.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 0 additions & 77 deletions
77
lib/src/lints/avoid_final_with_getter/avoid_final_with_getter_visitor.dart
This file was deleted.
Oops, something went wrong.
28 changes: 28 additions & 0 deletions
28
lib/src/lints/avoid_final_with_getter/visitors/avoid_final_with_getter_visitor.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import 'package:analyzer/dart/ast/ast.dart'; | ||
import 'package:analyzer/dart/ast/visitor.dart'; | ||
import 'package:solid_lints/src/lints/avoid_final_with_getter/visitors/variable_getter_visitor.dart'; | ||
|
||
/// A visitor that checks for final private fields with getters. | ||
/// If a final private field has a getter, it is considered as a public field. | ||
class AvoidFinalWithGetterVisitor extends RecursiveAstVisitor<void> { | ||
final _variables = <VariableDeclaration>[]; | ||
|
||
/// List of final private fields with getters | ||
Iterable<VariableDeclaration> get variables => _variables; | ||
|
||
@override | ||
void visitVariableDeclaration(VariableDeclaration node) { | ||
final isPrivate = node.declaredElement?.isPrivate ?? false; | ||
final isFinalPrivate = node.isFinal && isPrivate; | ||
|
||
if (!isFinalPrivate) return; | ||
|
||
final visitor = VariableGetterVisitor(node); | ||
node.parent?.parent?.parent?.accept(visitor); | ||
|
||
if (visitor.getter != null) { | ||
_variables.add(node); | ||
} | ||
super.visitVariableDeclaration(node); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
lib/src/lints/avoid_final_with_getter/visitors/variable_getter_visitor.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import 'package:analyzer/dart/ast/ast.dart'; | ||
import 'package:analyzer/dart/ast/visitor.dart'; | ||
import 'package:analyzer/dart/element/element.dart'; | ||
|
||
/// A visitor that checks for final private fields with getters. | ||
class VariableGetterVisitor extends RecursiveAstVisitor<void> { | ||
final VariableDeclaration _variable; | ||
final String _fieldName; | ||
MethodDeclaration? _getter; | ||
|
||
/// Creates a new instance of [VariableGetterVisitor] | ||
VariableGetterVisitor(this._variable) | ||
: _fieldName = _variable.name.toString().replaceFirst('_', ''); | ||
|
||
/// The getter of the variable | ||
MethodDeclaration? get getter => _getter; | ||
|
||
@override | ||
void visitMethodDeclaration(MethodDeclaration node) { | ||
final name = node.name.toString(); | ||
|
||
if (name == _fieldName && node.isGetter) { | ||
final nodeId = node.getterReferenceId; | ||
|
||
final variableId = _variable.variableId; | ||
|
||
if (nodeId == variableId) { | ||
_getter = node; | ||
} | ||
} | ||
super.visitMethodDeclaration(node); | ||
} | ||
} | ||
|
||
extension on MethodDeclaration { | ||
int? get getterReferenceId => switch (body) { | ||
ExpressionFunctionBody( | ||
expression: SimpleIdentifier( | ||
staticElement: Element( | ||
declaration: PropertyAccessorElement( | ||
variable: PropertyInducingElement(id: final int id) | ||
) | ||
) | ||
) | ||
) => | ||
id, | ||
_ => null, | ||
}; | ||
} | ||
|
||
extension on VariableDeclaration { | ||
int? get variableId => switch (declaredElement) { | ||
VariableElement(id: final int id) => id, | ||
_ => null, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters