-
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.
add quick fix to avoid_final_with_getter (#164)
* add avoid_final_with_getter rule * Update analysis_options.yaml to exclude final fields with getters * Refactor avoid_final_with_getter_rule.dart and avoid_final_with_getter_visitor.dart * Fix incorrect test comments in avoid_final_with_getter_test.dart * change avoid_final_with_getter, now it lints getter, no getter name equality * Refactor avoid_final_with_getter_visitor.dart to remove isStatic property from declaredElement * add more tests to avoid_final_with_getter_test.dart * Add avoid_final_with_getter rule to CHANGELOG.md * add quick fix to avoid_final_with_getter * Refactor avoid_final_with_getter_visitor.dart to use local variable instead of accessing visitor.variable directly
- Loading branch information
Showing
4 changed files
with
70 additions
and
7 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
lib/src/lints/avoid_final_with_getter/avoid_final_with_getter_fix.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,36 @@ | ||
part of 'avoid_final_with_getter_rule.dart'; | ||
|
||
class _FinalWithGetterFix extends DartFix { | ||
@override | ||
void run( | ||
CustomLintResolver resolver, | ||
ChangeReporter reporter, | ||
CustomLintContext context, | ||
AnalysisError analysisError, | ||
List<AnalysisError> others, | ||
) { | ||
context.registry.addMethodDeclaration((node) { | ||
if (analysisError.sourceRange.intersects(node.sourceRange)) { | ||
final info = analysisError.data as FinalWithGetterInfo?; | ||
if (info == null) return; | ||
|
||
_addReplacement(reporter, info); | ||
} | ||
}); | ||
} | ||
|
||
void _addReplacement( | ||
ChangeReporter reporter, | ||
FinalWithGetterInfo info, | ||
) { | ||
final changeBuilder = reporter.createChangeBuilder( | ||
message: "Remove getter and make variable public.", | ||
priority: 1, | ||
); | ||
|
||
changeBuilder.addDartFileEdit((builder) { | ||
builder.addDeletion(info.getter.sourceRange); | ||
builder.addDeletion(SourceRange(info.variable.name.offset, 1)); | ||
}); | ||
} | ||
} |
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
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
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