Skip to content

Commit

Permalink
add quick fix to avoid_final_with_getter
Browse files Browse the repository at this point in the history
  • Loading branch information
4akloon committed Apr 19, 2024
1 parent 680359c commit 17e5b01
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 7 deletions.
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));
});
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import 'package:analyzer/error/error.dart';
import 'package:analyzer/error/listener.dart';
import 'package:analyzer/source/source_range.dart';
import 'package:custom_lint_builder/custom_lint_builder.dart';
import 'package:solid_lints/src/lints/avoid_final_with_getter/visitors/avoid_final_with_getter_visitor.dart';
import 'package:solid_lints/src/models/rule_config.dart';
import 'package:solid_lints/src/models/solid_lint_rule.dart';

part 'avoid_final_with_getter_fix.dart';

/// Avoid using final private fields with getters.
///
/// Final private variables used in a pair with a getter
Expand Down Expand Up @@ -59,9 +63,18 @@ class AvoidFinalWithGetterRule extends SolidLintRule {
final visitor = AvoidFinalWithGetterVisitor();
node.accept(visitor);

for (final getter in visitor.getters) {
reporter.reportErrorForNode(code, getter);
for (final element in visitor.getters) {
reporter.reportErrorForNode(
code,
element.getter,
null,
null,
element,
);
}
});
}

@override
List<Fix> getFixes() => [_FinalWithGetterFix()];
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import 'package:solid_lints/src/lints/avoid_final_with_getter/visitors/getter_va
/// 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 _getters = <MethodDeclaration>[];
final _getters = <FinalWithGetterInfo>{};

/// List of getters
Iterable<MethodDeclaration> get getters => _getters;
Set<FinalWithGetterInfo> get getters => _getters;

@override
void visitMethodDeclaration(MethodDeclaration node) {
Expand All @@ -24,10 +24,22 @@ class AvoidFinalWithGetterVisitor extends RecursiveAstVisitor<void> {
final visitor = GetterVariableVisitor(node);
node.parent?.accept(visitor);

if (visitor.hasVariable) {
_getters.add(node);
if (visitor.variable != null) {
_getters.add(FinalWithGetterInfo(node, visitor.variable!));
}
}
super.visitMethodDeclaration(node);
}
}

/// Information about the final private field with a getter.
class FinalWithGetterInfo {
/// The getter method declaration.
final MethodDeclaration getter;

/// The variable declaration.
final VariableDeclaration variable;

/// Creates a new instance of [FinalWithGetterInfo]
const FinalWithGetterInfo(this.getter, this.variable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class GetterVariableVisitor extends RecursiveAstVisitor<void> {
: _getterId = getter.getterReferenceId;

/// Is there a variable associated with the getter
bool get hasVariable => _variable != null;
VariableDeclaration? get variable => _variable;

@override
void visitVariableDeclaration(VariableDeclaration node) {
Expand Down

0 comments on commit 17e5b01

Please sign in to comment.