-
Notifications
You must be signed in to change notification settings - Fork 13
Add the prefer_equatable_mixin lint
#503
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
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
116 changes: 116 additions & 0 deletions
116
packages/leancode_lint/lib/src/lints/prefer_equatable_mixin.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,116 @@ | ||
| 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/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/dart/ast/ast.dart'; | ||
| import 'package:analyzer/dart/ast/visitor.dart'; | ||
| import 'package:analyzer/error/error.dart'; | ||
| import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart'; | ||
| import 'package:analyzer_plugin/utilities/fixes/fixes.dart'; | ||
| import 'package:analyzer_plugin/utilities/range_factory.dart'; | ||
| import 'package:leancode_lint/src/type_checker.dart'; | ||
|
|
||
| class PreferEquatableMixin extends AnalysisRule { | ||
| PreferEquatableMixin() | ||
| : super(name: code.lowerCaseName, description: code.problemMessage); | ||
|
|
||
| static const code = LintCode( | ||
| 'prefer_equatable_mixin', | ||
| 'The class {0} should mix in EquatableMixin instead of extending Equatable.', | ||
| correctionMessage: 'Replace with a mixin.', | ||
| severity: .WARNING, | ||
|
Albert221 marked this conversation as resolved.
|
||
| ); | ||
|
|
||
| @override | ||
| LintCode get diagnosticCode => code; | ||
|
|
||
| @override | ||
| void registerNodeProcessors( | ||
| RuleVisitorRegistry registry, | ||
| RuleContext context, | ||
| ) { | ||
| registry.addClassDeclaration(this, _Visitor(this, context)); | ||
| } | ||
| } | ||
|
|
||
| class _Visitor extends SimpleAstVisitor<void> { | ||
| _Visitor(this.rule, this.context); | ||
|
|
||
| final AnalysisRule rule; | ||
| final RuleContext context; | ||
|
|
||
| static const equatable = TypeChecker.fromName( | ||
| 'Equatable', | ||
| packageName: 'equatable', | ||
| ); | ||
| static const equatableMixin = TypeChecker.fromName( | ||
| 'EquatableMixin', | ||
| packageName: 'equatable', | ||
| ); | ||
|
|
||
| @override | ||
| void visitClassDeclaration(ClassDeclaration node) { | ||
| final extendsClause = node.extendsClause; | ||
| if (extendsClause == null) { | ||
| return; | ||
| } | ||
|
|
||
| final superType = extendsClause.superclass.type; | ||
| final isEquatable = superType != null && equatable.isExactlyType(superType); | ||
|
|
||
| final isEquatableMixin = | ||
| node.withClause?.mixinTypes | ||
| .map((mixin) => mixin.type) | ||
| .nonNulls | ||
| .any(equatableMixin.isExactlyType) ?? | ||
| false; | ||
|
|
||
| if (isEquatable && !isEquatableMixin) { | ||
| rule.reportAtNode( | ||
| extendsClause.superclass, | ||
| arguments: [node.namePart.typeName.lexeme], | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| class ConvertToEquatableMixin extends ResolvedCorrectionProducer { | ||
| ConvertToEquatableMixin({required super.context}); | ||
|
|
||
| @override | ||
| FixKind? get fixKind => const FixKind( | ||
| 'leancode_lint.fix.convertToEquatableMixin', | ||
| DartFixKindPriority.standard, | ||
| 'Convert to EquatableMixin', | ||
| ); | ||
|
|
||
| @override | ||
| CorrectionApplicability get applicability => .automatically; | ||
|
|
||
| @override | ||
| Future<void> compute(ChangeBuilder builder) async { | ||
| final classDeclaration = node.thisOrAncestorOfType<ClassDeclaration>()!; | ||
| final extendsClause = classDeclaration.extendsClause!; | ||
| final withClause = classDeclaration.withClause; | ||
|
|
||
| await builder.addDartFileEdit(file, (builder) { | ||
| if (withClause != null) { | ||
| builder | ||
| ..addSimpleReplacement( | ||
| range.startStart(extendsClause, withClause), | ||
| '', | ||
| ) | ||
| ..addSimpleInsertion( | ||
| withClause.mixinTypes.first.offset, | ||
| 'EquatableMixin, ', | ||
| ); | ||
| } else { | ||
| builder.addSimpleReplacement( | ||
| extendsClause.sourceRange, | ||
| 'with EquatableMixin', | ||
| ); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
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 |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| part of '../mock_libraries.dart'; | ||
|
|
||
| mixin MockEquatable on AnalysisRuleTest { | ||
| @override | ||
| void setUp() { | ||
| newPackage('equatable').addFile('lib/equatable.dart', ''' | ||
| class Equatable { | ||
| const Equatable(); | ||
| List<Object?> get props; | ||
| } | ||
|
|
||
| mixin EquatableMixin { | ||
| List<Object?> get props; | ||
| } | ||
| '''); | ||
| super.setUp(); | ||
| } | ||
| } |
72 changes: 72 additions & 0 deletions
72
packages/leancode_lint/test/test_cases/prefer_equatable_mixin_test.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,72 @@ | ||
| import 'package:analyzer_testing/analysis_rule/analysis_rule.dart'; | ||
| import 'package:leancode_lint/src/lints/prefer_equatable_mixin.dart'; | ||
| import 'package:test_reflective_loader/test_reflective_loader.dart'; | ||
|
|
||
| import '../assert_ranges.dart'; | ||
| import '../mock_libraries.dart'; | ||
|
|
||
| void main() { | ||
| defineReflectiveSuite(() { | ||
| defineReflectiveTests(PreferEquatableMixinTest); | ||
| }); | ||
| } | ||
|
|
||
| @reflectiveTest | ||
| class PreferEquatableMixinTest extends AnalysisRuleTest with MockEquatable { | ||
| @override | ||
| void setUp() { | ||
| rule = PreferEquatableMixin(); | ||
|
|
||
| super.setUp(); | ||
| } | ||
|
|
||
| Future<void> test_only_directly_extending_equatable() async { | ||
| await assertDiagnosticsInRanges(''' | ||
| import 'package:equatable/equatable.dart'; | ||
|
|
||
| class MyState extends [!Equatable!] { | ||
| @override | ||
| List<Object?> get props => []; | ||
| } | ||
|
|
||
| class MyState2 extends MyState { | ||
| @override | ||
| List<Object?> get props => []; | ||
| } | ||
| '''); | ||
| } | ||
|
|
||
| Future<void> test_mixin_not_flagged() async { | ||
| await assertNoDiagnostics(''' | ||
| import 'package:equatable/equatable.dart'; | ||
|
|
||
| class MyState3 with EquatableMixin { | ||
| @override | ||
| List<Object?> get props => []; | ||
| } | ||
| '''); | ||
| } | ||
|
PiotrRogulski marked this conversation as resolved.
|
||
|
|
||
| Future<void> test_with_other_mixins() async { | ||
| await assertDiagnosticsInRanges(''' | ||
| import 'package:equatable/equatable.dart'; | ||
|
|
||
| mixin SomethingElse {} | ||
|
|
||
| class MyState extends [!Equatable!] with SomethingElse { | ||
| @override | ||
| List<Object?> get props => []; | ||
| } | ||
|
|
||
| class MyState2 extends MyState with SomethingElse { | ||
| @override | ||
| List<Object?> get props => []; | ||
| } | ||
|
|
||
| class MyState3 with SomethingElse, EquatableMixin { | ||
| @override | ||
| List<Object?> get props => []; | ||
| } | ||
| '''); | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Explaining all lints in README takes much space. Maybe we could wrap them into <details>? Out of scope though
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will do next 💯