-
Notifications
You must be signed in to change notification settings - Fork 24
refactor: migrate avoid_unnecessary_type_assertions #282
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
7
commits into
analysis_server_migration
Choose a base branch
from
271-migrate-avoid_unnecessary_type_assertions
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
7 commits
Select commit
Hold shift + click to select a range
cc762dd
refactor: migrate unnecessary_type_assertion
andrew-bekhiet-solid 83ac293
refactor: migrate unnecessary_type_assertion fix
andrew-bekhiet-solid ce8e401
fix: use fix kind instead of assist kind
andrew-bekhiet-solid 8187d7f
fix: use thisOrAncestorOfType to get the nearest node of type T
andrew-bekhiet-solid 598278e
feat: display removed part in the fix message
andrew-bekhiet-solid 98e8b69
fix: handle custom iterable types
andrew-bekhiet-solid 27f6bcc
test: add test for custom iterables with subtypes
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
150 changes: 34 additions & 116 deletions
150
lib/src/lints/avoid_unnecessary_type_assertions/avoid_unnecessary_type_assertions_rule.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,134 +1,52 @@ | ||
| import 'package:analyzer/dart/ast/ast.dart'; | ||
| import 'package:analyzer/dart/element/type.dart'; | ||
| import 'package:analyzer/diagnostic/diagnostic.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/models/rule_config.dart'; | ||
| import 'package:solid_lints/src/models/solid_lint_rule.dart'; | ||
| import 'package:solid_lints/src/utils/typecast_utils.dart'; | ||
| import 'package:solid_lints/src/utils/types_utils.dart'; | ||
|
|
||
| part 'fixes/avoid_unnecessary_type_assertions_fix.dart'; | ||
|
|
||
| /// The name of 'is' operator | ||
| const operatorIsName = 'is'; | ||
|
|
||
| /// The name of 'whereType' method | ||
| const whereTypeMethodName = 'whereType'; | ||
| import 'package:analyzer/analysis_rule/analysis_rule.dart'; | ||
| import 'package:analyzer/analysis_rule/rule_context.dart'; | ||
| import 'package:analyzer/analysis_rule/rule_visitor_registry.dart'; | ||
| import 'package:analyzer/error/error.dart'; | ||
| import 'package:solid_lints/src/lints/avoid_unnecessary_type_assertions/visitors/unnecessary_is_expression_visitor.dart'; | ||
| import 'package:solid_lints/src/lints/avoid_unnecessary_type_assertions/visitors/unnecessary_where_type_visitor.dart'; | ||
|
|
||
| /// Warns about unnecessary usage of `is` and `whereType` operators. | ||
| /// | ||
| /// ### Example: | ||
| /// #### BAD: | ||
| /// ```dart | ||
| /// final testList = [1.0, 2.0, 3.0]; | ||
| /// final result = testList is List<double>; // LINT | ||
| /// final negativeResult = testList is! List<double>; // LINT | ||
| /// testList.whereType<double>(); // LINT | ||
| /// | ||
| /// final double d = 2.0; | ||
| /// final casted = d is double; // LINT | ||
| /// ``` | ||
| /// | ||
| /// #### GOOD: | ||
| /// ```dart | ||
| /// final dynamicList = <dynamic>[1.0, 2.0]; | ||
| /// dynamicList.whereType<double>(); | ||
| /// | ||
| /// final double? nullableD = 2.0; | ||
| /// // casting `Type? is Type` is allowed | ||
| /// final castedD = nullableD is double; | ||
| /// ``` | ||
| class AvoidUnnecessaryTypeAssertions extends SolidLintRule { | ||
| /// {@macro solid_lints.avoid_unnecessary_type_assertions.example_is} | ||
| /// {@macro solid_lints.avoid_unnecessary_type_assertions.example_where} | ||
| class AvoidUnnecessaryTypeAssertionsRule extends AnalysisRule { | ||
| /// The name of 'is' operator | ||
| static const operatorIsName = 'is'; | ||
|
|
||
| /// The name of 'whereType' method | ||
| static const whereTypeMethodName = 'whereType'; | ||
|
|
||
| /// This lint rule represents | ||
| /// the error whether we use bad formatted double literals. | ||
| static const lintName = 'avoid_unnecessary_type_assertions'; | ||
|
|
||
| static const _unnecessaryIsCode = LintCode( | ||
| name: lintName, | ||
| problemMessage: "Unnecessary usage of the '$operatorIsName' operator.", | ||
| static const _unnecessaryTypeAssertionsCode = LintCode( | ||
| lintName, | ||
| "Unnecessary usage of the {0}.", | ||
| ); | ||
|
|
||
| static const _unnecessaryWhereTypeCode = LintCode( | ||
| name: lintName, | ||
| problemMessage: "Unnecessary usage of the '$whereTypeMethodName' method.", | ||
| ); | ||
|
|
||
| AvoidUnnecessaryTypeAssertions._(super.config); | ||
|
|
||
| /// Creates a new instance of [AvoidUnnecessaryTypeAssertions] | ||
| /// based on the lint configuration. | ||
| factory AvoidUnnecessaryTypeAssertions.createRule(CustomLintConfigs configs) { | ||
| final rule = RuleConfig( | ||
| configs: configs, | ||
| name: lintName, | ||
| problemMessage: (_) => "Unnecessary usage of typecast operators.", | ||
| ); | ||
|
|
||
| return AvoidUnnecessaryTypeAssertions._(rule); | ||
| } | ||
|
|
||
| @override | ||
| void run( | ||
| CustomLintResolver resolver, | ||
| DiagnosticReporter reporter, | ||
| CustomLintContext context, | ||
| ) { | ||
| context.registry.addIsExpression((node) { | ||
| if (_isUnnecessaryIsExpression(node)) { | ||
| reporter.atNode(node, _unnecessaryIsCode); | ||
| } | ||
| }); | ||
| DiagnosticCode get diagnosticCode => _unnecessaryTypeAssertionsCode; | ||
|
|
||
| context.registry.addMethodInvocation((node) { | ||
| if (_isUnnecessaryWhereType(node)) { | ||
| reporter.atNode(node, _unnecessaryWhereTypeCode); | ||
| } | ||
| }); | ||
| } | ||
| /// Creates a new instance of [AvoidUnnecessaryTypeAssertionsRule] | ||
| AvoidUnnecessaryTypeAssertionsRule() | ||
| : super( | ||
| name: lintName, | ||
| description: "Unnecessary usage of typecast operators.", | ||
| ); | ||
|
|
||
| @override | ||
| List<Fix> getFixes() => [_UnnecessaryTypeAssertionsFix()]; | ||
|
|
||
| bool _isUnnecessaryIsExpression(IsExpression node) { | ||
| final objectType = node.expression.staticType; | ||
| final castedType = node.type.type; | ||
|
|
||
| if (objectType == null || castedType == null) { | ||
| return false; | ||
| } | ||
| final typeCast = TypeCast( | ||
| source: objectType, | ||
| target: castedType, | ||
| isReversed: node.notOperator != null, | ||
| ); | ||
| return typeCast.isUnnecessaryTypeCheck; | ||
| } | ||
|
|
||
| bool _isUnnecessaryWhereType(MethodInvocation node) { | ||
| if (node | ||
| case MethodInvocation( | ||
| methodName: Identifier(name: whereTypeMethodName), | ||
| target: Expression(staticType: final targetType), | ||
| realTarget: Expression(staticType: final realTargetType), | ||
| typeArguments: TypeArgumentList(arguments: final arguments), | ||
| ) | ||
| when targetType is ParameterizedType && | ||
| isIterable(realTargetType) && | ||
| arguments.isNotEmpty) { | ||
| final objectType = targetType.typeArguments.first; | ||
| final castedType = arguments.first.type; | ||
|
|
||
| if (castedType == null) { | ||
| return false; | ||
| } | ||
| void registerNodeProcessors( | ||
| RuleVisitorRegistry registry, | ||
| RuleContext context, | ||
| ) { | ||
| super.registerNodeProcessors(registry, context); | ||
|
|
||
| final typeCast = TypeCast(source: objectType, target: castedType); | ||
| final unnecessaryIsExpressionVisitor = UnnecessaryIsExpressionVisitor(this); | ||
| registry.addIsExpression(this, unnecessaryIsExpressionVisitor); | ||
|
|
||
| return typeCast.isUnnecessaryTypeCheck; | ||
| } else { | ||
| return false; | ||
| } | ||
| final unnecessaryWhereTypeVisitor = UnnecessaryWhereTypeVisitor(this); | ||
| registry.addMethodInvocation(this, unnecessaryWhereTypeVisitor); | ||
| } | ||
| } |
96 changes: 57 additions & 39 deletions
96
.../lints/avoid_unnecessary_type_assertions/fixes/avoid_unnecessary_type_assertions_fix.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,50 +1,68 @@ | ||
| part of '../avoid_unnecessary_type_assertions_rule.dart'; | ||
| 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/source/source_range.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/avoid_unnecessary_type_assertions/avoid_unnecessary_type_assertions_rule.dart'; | ||
|
|
||
| /// A Quick fix for `avoid_unnecessary_type_assertions` rule | ||
| /// Suggests to remove unnecessary assertions | ||
| class _UnnecessaryTypeAssertionsFix extends DartFix { | ||
| class AvoidUnnecessaryTypeAssertionsFix extends ResolvedCorrectionProducer { | ||
| static const _avoidUnnecessaryTypeAssertionsFixKind = FixKind( | ||
| 'solid_lints.fix.${AvoidUnnecessaryTypeAssertionsRule.lintName}', | ||
| DartFixKindPriority.standard, | ||
| 'Remove the unnecessary {0}', | ||
| ); | ||
|
|
||
| SourceRange? _partToRemove; | ||
|
|
||
| @override | ||
| List<String>? get fixArguments => [ | ||
| if (_partToRemove case final partToRemove?) | ||
| '"${utils.getRangeText(partToRemove).trim()}"' | ||
| else | ||
| 'type assertion', | ||
| ]; | ||
|
|
||
| @override | ||
| void run( | ||
| CustomLintResolver resolver, | ||
| ChangeReporter reporter, | ||
| CustomLintContext context, | ||
| Diagnostic analysisError, | ||
| List<Diagnostic> others, | ||
| ) { | ||
| context.registry.addIsExpression((node) { | ||
| if (analysisError.sourceRange.intersects(node.sourceRange)) { | ||
| _addDeletion(reporter, operatorIsName, node, node.isOperator.offset); | ||
| } | ||
| }); | ||
|
|
||
| context.registry.addMethodInvocation((node) { | ||
| if (analysisError.sourceRange.intersects(node.sourceRange)) { | ||
| _addDeletion( | ||
| reporter, | ||
| whereTypeMethodName, | ||
| node, | ||
| node.operator?.offset ?? node.offset, | ||
| ); | ||
| } | ||
| }); | ||
| FixKind get fixKind => _avoidUnnecessaryTypeAssertionsFixKind; | ||
|
|
||
| @override | ||
| CorrectionApplicability get applicability => | ||
| CorrectionApplicability.automatically; | ||
|
|
||
| /// Creates a new instance of [AvoidUnnecessaryTypeAssertionsFix] | ||
| AvoidUnnecessaryTypeAssertionsFix({required super.context}); | ||
|
|
||
| @override | ||
| Future<void> compute(ChangeBuilder builder) async { | ||
| final isExpressionNode = node.thisOrAncestorOfType<IsExpression>(); | ||
| if (isExpressionNode != null) { | ||
| final operatorOffset = isExpressionNode.isOperator.offset - 1; | ||
| _partToRemove = _removedPartRange(isExpressionNode, operatorOffset); | ||
| } | ||
|
|
||
| final whereTypeNode = node.thisOrAncestorOfType<MethodInvocation>(); | ||
| if (whereTypeNode != null && _partToRemove == null) { | ||
| final operatorOffset = | ||
| whereTypeNode.operator?.offset ?? whereTypeNode.offset; | ||
| _partToRemove = _removedPartRange(whereTypeNode, operatorOffset); | ||
| } | ||
|
|
||
| final partToRemove = _partToRemove; | ||
| if (partToRemove == null) return; | ||
|
|
||
| await builder.addDartFileEdit( | ||
| file, | ||
| (builder) => builder.addDeletion(partToRemove), | ||
| ); | ||
| } | ||
|
|
||
| void _addDeletion( | ||
| ChangeReporter reporter, | ||
| String itemToDelete, | ||
| Expression node, | ||
| int operatorOffset, | ||
| ) { | ||
| SourceRange _removedPartRange(Expression node, int operatorOffset) { | ||
| final targetNameLength = operatorOffset - node.offset; | ||
| final removedPartLength = node.length - targetNameLength; | ||
|
|
||
| final changeBuilder = reporter.createChangeBuilder( | ||
| message: "Remove unnecessary '$itemToDelete'", | ||
| priority: 1, | ||
| ); | ||
|
|
||
| changeBuilder.addDartFileEdit((builder) { | ||
| builder.addDeletion(SourceRange(operatorOffset, removedPartLength)); | ||
| }); | ||
| return SourceRange(operatorOffset, removedPartLength); | ||
| } | ||
| } | ||
63 changes: 63 additions & 0 deletions
63
...c/lints/avoid_unnecessary_type_assertions/visitors/unnecessary_is_expression_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 |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import 'package:analyzer/dart/ast/ast.dart'; | ||
| import 'package:analyzer/dart/ast/visitor.dart'; | ||
| import 'package:solid_lints/src/lints/avoid_unnecessary_type_assertions/avoid_unnecessary_type_assertions_rule.dart'; | ||
| import 'package:solid_lints/src/utils/typecast_utils.dart'; | ||
|
|
||
| /// Visitor for [AvoidUnnecessaryTypeAssertionsRule]. | ||
| /// Reports on unnecessary usage of 'is' operator. | ||
| /// | ||
| /// ### Example: | ||
| /// {@template solid_lints.avoid_unnecessary_type_assertions.example_is} | ||
| /// #### BAD: | ||
| /// ```dart | ||
| /// final testList = [1.0, 2.0, 3.0]; | ||
| /// final result = testList is List<double>; // LINT | ||
| /// final negativeResult = testList is! List<double>; // LINT | ||
| /// | ||
| /// final double d = 2.0; | ||
| /// final casted = d is double; // LINT | ||
| /// ``` | ||
| /// | ||
| /// #### GOOD: | ||
| /// ```dart | ||
| /// final double? nullableD = 2.0; | ||
| /// // casting `Type? is Type` is allowed | ||
| /// final castedD = nullableD is double; | ||
| /// ``` | ||
| /// {@endtemplate} | ||
| class UnnecessaryIsExpressionVisitor extends SimpleAstVisitor<void> { | ||
| final AvoidUnnecessaryTypeAssertionsRule _rule; | ||
|
|
||
| /// Creates a new instance of [UnnecessaryIsExpressionVisitor]. | ||
| UnnecessaryIsExpressionVisitor(this._rule); | ||
|
|
||
| @override | ||
| void visitIsExpression(IsExpression node) { | ||
| super.visitIsExpression(node); | ||
|
|
||
| if (!_isUnnecessaryIsExpression(node)) return; | ||
|
|
||
| _rule.reportAtNode( | ||
| node, | ||
| arguments: [ | ||
| "'${AvoidUnnecessaryTypeAssertionsRule.operatorIsName}' operator", | ||
| ], | ||
| ); | ||
| } | ||
|
|
||
| bool _isUnnecessaryIsExpression(IsExpression node) { | ||
| final objectType = node.expression.staticType; | ||
| final castedType = node.type.type; | ||
|
|
||
| if (objectType == null || castedType == null) { | ||
| return false; | ||
| } | ||
|
|
||
| final typeCast = TypeCast( | ||
| source: objectType, | ||
| target: castedType, | ||
| isReversed: node.notOperator != null, | ||
| ); | ||
| return typeCast.isUnnecessaryTypeCheck; | ||
| } | ||
| } |
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.