-
Notifications
You must be signed in to change notification settings - Fork 24
refactor: migrate prefer last #287
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
andrew-bekhiet-solid
merged 8 commits into
analysis_server_migration
from
279-migrate-prefer_last
Jun 25, 2026
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7792e68
refactor: migrate prefer_last rule & visitor
andrew-bekhiet-solid 2a13b7e
refactor: migrate prefer_last test
andrew-bekhiet-solid de117fb
refactor: migrate prefer_last fix
andrew-bekhiet-solid 4ebc514
fix: handle null awareness
andrew-bekhiet-solid 8ca7315
fix: prefer firstOrNull
andrew-bekhiet-solid 34cc2c9
feat: handle cascade & null aware expressions
andrew-bekhiet-solid 9de7a17
refactor: use AutoTestLintOffsets
andrew-bekhiet-solid 679b6f8
refactor: use return with switch expression
andrew-bekhiet-solid File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,67 +1,70 @@ | ||
| import 'package:analysis_server_plugin/edit/dart/correction_producer.dart'; | ||
| import 'package:analysis_server_plugin/edit/dart/dart_fix_kind_priority.dart'; | ||
| import 'package:analyzer/dart/ast/ast.dart'; | ||
| import 'package:analyzer/diagnostic/diagnostic.dart'; | ||
| import 'package:analyzer/source/source_range.dart'; | ||
| import 'package:custom_lint_builder/custom_lint_builder.dart'; | ||
| import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart'; | ||
| import 'package:analyzer_plugin/utilities/fixes/fixes.dart'; | ||
| import 'package:solid_lints/src/lints/prefer_last/prefer_last_rule.dart'; | ||
|
|
||
| /// A Quick fix for `prefer_last` rule | ||
| /// Suggests to replace iterable access expressions | ||
| class PreferLastFix extends DartFix { | ||
| class PreferLastFix extends ParsedCorrectionProducer { | ||
| static const _replaceComment = "Replace with 'last'."; | ||
|
|
||
| /// Creates a new instance of [PreferLastFix] | ||
| PreferLastFix({required super.context}); | ||
|
|
||
| @override | ||
| void run( | ||
| CustomLintResolver resolver, | ||
| ChangeReporter reporter, | ||
| CustomLintContext context, | ||
| Diagnostic analysisError, | ||
| List<Diagnostic> others, | ||
| ) { | ||
| context.registry.addMethodInvocation((node) { | ||
| if (analysisError.sourceRange.intersects(node.sourceRange)) { | ||
| final correction = _createCorrection(node); | ||
| FixKind get fixKind => const FixKind( | ||
| 'solid_lints.fix.${PreferLastRule.lintName}', | ||
| DartFixKindPriority.standard, | ||
| _replaceComment, | ||
| ); | ||
|
|
||
| _addReplacement(reporter, node, correction); | ||
| } | ||
| }); | ||
| @override | ||
| FixKind get multiFixKind => const FixKind( | ||
| 'solid_lints.fix.multi.${PreferLastRule.lintName}', | ||
| DartFixKindPriority.standard, | ||
| '$_replaceComment across files', | ||
| ); | ||
|
|
||
| context.registry.addIndexExpression((node) { | ||
| if (analysisError.sourceRange.intersects(node.sourceRange)) { | ||
| final correction = _createCorrection(node); | ||
| @override | ||
| CorrectionApplicability get applicability => | ||
| CorrectionApplicability.automatically; | ||
|
|
||
| _addReplacement(reporter, node, correction); | ||
| } | ||
| }); | ||
| @override | ||
| Future<void> compute(ChangeBuilder builder) async { | ||
| final targetNode = node.thisOrAncestorMatching( | ||
| (n) => n is MethodInvocation || n is IndexExpression, | ||
| ); | ||
| if (targetNode is! Expression) return; | ||
|
|
||
| final correction = _createCorrection(targetNode); | ||
| await _addReplacement(builder, targetNode, correction); | ||
| } | ||
|
|
||
| String _createCorrection(Expression expression) { | ||
| if (expression is MethodInvocation) { | ||
| return expression.isCascaded | ||
| ? '..last' | ||
| : '${expression.target ?? ''}.last'; | ||
| } else if (expression is IndexExpression) { | ||
| return expression.isCascaded | ||
| ? '..last' | ||
| : '${expression.target ?? ''}.last'; | ||
| } else { | ||
| return '.last'; | ||
| switch (expression) { | ||
| case MethodInvocation(isCascaded: true, :final isNullAware): | ||
| case IndexExpression(isCascaded: true, :final isNullAware): | ||
| return isNullAware ? '?.last' : '..last'; | ||
|
|
||
| case MethodInvocation(:final target?, :final isNullAware): | ||
| case IndexExpression(:final target?, :final isNullAware): | ||
| return isNullAware ? '$target?.last' : '$target.last'; | ||
|
|
||
| default: | ||
| return '.last'; | ||
| } | ||
| } | ||
|
andrew-bekhiet-solid marked this conversation as resolved.
|
||
|
|
||
| void _addReplacement( | ||
| ChangeReporter reporter, | ||
| Expression node, | ||
| Future<void> _addReplacement( | ||
| ChangeBuilder builder, | ||
| AstNode node, | ||
| String correction, | ||
| ) { | ||
| final changeBuilder = reporter.createChangeBuilder( | ||
| message: _replaceComment, | ||
| priority: 1, | ||
| ) async { | ||
| await builder.addDartFileEdit( | ||
| file, | ||
| (builder) => builder.addSimpleReplacement(node.sourceRange, correction), | ||
| ); | ||
|
|
||
| changeBuilder.addDartFileEdit((builder) { | ||
| builder.addSimpleReplacement( | ||
| SourceRange(node.offset, node.length), | ||
| correction, | ||
| ); | ||
| }); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.