-
Notifications
You must be signed in to change notification settings - Fork 24
refactor: migrate prefer_first #286
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
Open
andrew-bekhiet-solid
wants to merge
5
commits into
analysis_server_migration
Choose a base branch
from
278-migrate-prefer_first
base: analysis_server_migration
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fc1222f
refactor: migrate prefer_first and tests
andrew-bekhiet-solid 7c5db64
refactor: migrate prefer_first fix
andrew-bekhiet-solid 0b02723
refactor: prefer firstOrNull
andrew-bekhiet-solid 65e23a5
fix: only apply one fix to one node at a time
andrew-bekhiet-solid f0acb31
fix: handle null awareness
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_first/prefer_first_rule.dart'; | ||
|
|
||
| /// A Quick fix for `prefer_first` rule | ||
| /// Suggests to replace iterable access expressions | ||
| class PreferFirstFix extends DartFix { | ||
| class PreferFirstFix extends ParsedCorrectionProducer { | ||
| static const _replaceComment = "Replace with 'first'."; | ||
|
|
||
| /// Creates a new instance of [PreferFirstFix] | ||
| PreferFirstFix({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.${PreferFirstRule.lintName}', | ||
| DartFixKindPriority.standard, | ||
| _replaceComment, | ||
| ); | ||
|
|
||
| _addReplacement(reporter, node, correction); | ||
| } | ||
| }); | ||
| @override | ||
| FixKind get multiFixKind => const FixKind( | ||
| 'solid_lints.fix.multi.${PreferFirstRule.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 | ||
| ? '..first' | ||
| : '${expression.target ?? ''}.first'; | ||
| } else if (expression is IndexExpression) { | ||
| return expression.isCascaded | ||
| ? '..first' | ||
| : '${expression.target ?? ''}.first'; | ||
| } else { | ||
| return '.first'; | ||
| switch (expression) { | ||
| case MethodInvocation(isCascaded: true, :final isNullAware): | ||
| case IndexExpression(isCascaded: true, :final isNullAware): | ||
| return isNullAware ? '?.first' : '..first'; | ||
|
|
||
| case MethodInvocation(:final target?, :final isNullAware): | ||
| case IndexExpression(:final target?, :final isNullAware): | ||
| return isNullAware ? '$target?.first' : '$target.first'; | ||
|
|
||
| default: | ||
| return '.first'; | ||
| } | ||
| } | ||
|
|
||
| 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
26 changes: 13 additions & 13 deletions
26
lib/src/lints/prefer_first/visitors/prefer_first_visitor.dart
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,40 +1,40 @@ | ||
| import 'package:analyzer/dart/ast/ast.dart'; | ||
| import 'package:analyzer/dart/ast/visitor.dart'; | ||
| import 'package:solid_lints/src/lints/prefer_first/prefer_first_rule.dart'; | ||
| import 'package:solid_lints/src/utils/types_utils.dart'; | ||
|
|
||
| /// The AST visitor that will collect all Iterable access expressions | ||
| /// which can be replaced with .first | ||
| class PreferFirstVisitor extends RecursiveAstVisitor<void> { | ||
| final _expressions = <Expression>[]; | ||
| final PreferFirstRule _rule; | ||
|
|
||
| /// List of all Iterable access expressions | ||
| Iterable<Expression> get expressions => _expressions; | ||
| /// Creates a new instance of [PreferFirstVisitor] | ||
| PreferFirstVisitor(this._rule); | ||
|
|
||
| @override | ||
| void visitMethodInvocation(MethodInvocation node) { | ||
| super.visitMethodInvocation(node); | ||
| final isIterable = isIterableOrSubclass(node.realTarget?.staticType); | ||
| final isElementAt = node.methodName.name == 'elementAt'; | ||
|
|
||
| if (isIterable && isElementAt) { | ||
| final arg = node.argumentList.arguments.first; | ||
| if (!isIterable || !isElementAt) return; | ||
|
|
||
| if (arg is IntegerLiteral && arg.value == 0) { | ||
| _expressions.add(node); | ||
| } | ||
| final arg = node.argumentList.arguments.firstOrNull; | ||
| if (arg case IntegerLiteral(value: 0)) { | ||
| _rule.reportAtNode(node); | ||
| } | ||
| } | ||
|
|
||
| @override | ||
| void visitIndexExpression(IndexExpression node) { | ||
| super.visitIndexExpression(node); | ||
|
|
||
| if (isListOrSubclass(node.realTarget.staticType)) { | ||
| final index = node.index; | ||
| if (!isListOrSubclass(node.realTarget.staticType)) return; | ||
|
|
||
| if (index is IntegerLiteral && index.value == 0) { | ||
| _expressions.add(node); | ||
| } | ||
| final index = node.index; | ||
|
|
||
| if (index case IntegerLiteral(value: 0)) { | ||
| _rule.reportAtNode(node); | ||
| } | ||
| } | ||
| } |
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.