-
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.
change avoid_final_with_getter, now it lints getter, no getter name e…
…quality
- Loading branch information
Showing
5 changed files
with
75 additions
and
78 deletions.
There are no files selected for viewing
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
36 changes: 21 additions & 15 deletions
36
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 |
---|---|---|
@@ -1,28 +1,34 @@ | ||
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'; | ||
import 'package:analyzer/dart/element/element.dart'; | ||
import 'package:solid_lints/src/lints/avoid_final_with_getter/visitors/getter_variable_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>[]; | ||
final _getters = <MethodDeclaration>[]; | ||
|
||
/// List of final private fields with getters | ||
Iterable<VariableDeclaration> get variables => _variables; | ||
/// List of getters | ||
Iterable<MethodDeclaration> get getters => _getters; | ||
|
||
@override | ||
void visitVariableDeclaration(VariableDeclaration node) { | ||
final isPrivate = node.declaredElement?.isPrivate ?? false; | ||
final isFinalPrivate = node.isFinal && isPrivate; | ||
void visitMethodDeclaration(MethodDeclaration node) { | ||
if (node | ||
case MethodDeclaration( | ||
isGetter: true, | ||
declaredElement: ExecutableElement( | ||
isAbstract: false, | ||
isStatic: false, | ||
isPublic: true, | ||
) | ||
)) { | ||
final visitor = GetterVariableVisitor(node); | ||
node.parent?.accept(visitor); | ||
|
||
if (!isFinalPrivate) return; | ||
|
||
final visitor = VariableGetterVisitor(node); | ||
node.parent?.parent?.parent?.accept(visitor); | ||
|
||
if (visitor.getter != null) { | ||
_variables.add(node); | ||
if (visitor.hasVariable) { | ||
_getters.add(node); | ||
} | ||
} | ||
super.visitVariableDeclaration(node); | ||
super.visitMethodDeclaration(node); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
lib/src/lints/avoid_final_with_getter/visitors/getter_variable_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,46 @@ | ||
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 the association of the getter with | ||
/// the final private variable | ||
class GetterVariableVisitor extends RecursiveAstVisitor<void> { | ||
final int? _getterId; | ||
VariableDeclaration? _variable; | ||
|
||
/// Creates a new instance of [GetterVariableVisitor] | ||
GetterVariableVisitor(MethodDeclaration getter) | ||
: _getterId = getter.getterReferenceId; | ||
|
||
/// Is there a variable associated with the getter | ||
bool get hasVariable => _variable != null; | ||
|
||
@override | ||
void visitVariableDeclaration(VariableDeclaration node) { | ||
if (node | ||
case VariableDeclaration( | ||
isFinal: true, | ||
declaredElement: VariableElement(id: final id, isPrivate: true) | ||
) when id == _getterId) { | ||
_variable = node; | ||
} | ||
|
||
super.visitVariableDeclaration(node); | ||
} | ||
} | ||
|
||
extension on MethodDeclaration { | ||
int? get getterReferenceId => switch (body) { | ||
ExpressionFunctionBody( | ||
expression: SimpleIdentifier( | ||
staticElement: Element( | ||
declaration: PropertyAccessorElement( | ||
variable: PropertyInducingElement(:final id) | ||
) | ||
) | ||
) | ||
) => | ||
id, | ||
_ => null, | ||
}; | ||
} |
56 changes: 0 additions & 56 deletions
56
lib/src/lints/avoid_final_with_getter/visitors/variable_getter_visitor.dart
This file was deleted.
Oops, something went wrong.
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