|
| 1 | +import 'package:analysis_server_plugin/edit/dart/correction_producer.dart'; |
| 2 | +import 'package:analysis_server_plugin/edit/dart/dart_fix_kind_priority.dart'; |
| 3 | +import 'package:analyzer/analysis_rule/analysis_rule.dart'; |
| 4 | +import 'package:analyzer/analysis_rule/rule_context.dart'; |
| 5 | +import 'package:analyzer/analysis_rule/rule_visitor_registry.dart'; |
| 6 | +import 'package:analyzer/dart/ast/ast.dart'; |
| 7 | +import 'package:analyzer/dart/ast/visitor.dart'; |
| 8 | +import 'package:analyzer/error/error.dart'; |
| 9 | +import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart'; |
| 10 | +import 'package:analyzer_plugin/utilities/fixes/fixes.dart'; |
| 11 | +import 'package:analyzer_plugin/utilities/range_factory.dart'; |
| 12 | +import 'package:leancode_lint/src/type_checker.dart'; |
| 13 | + |
| 14 | +class PreferEquatableMixin extends AnalysisRule { |
| 15 | + PreferEquatableMixin() |
| 16 | + : super(name: code.lowerCaseName, description: code.problemMessage); |
| 17 | + |
| 18 | + static const code = LintCode( |
| 19 | + 'prefer_equatable_mixin', |
| 20 | + 'The class {0} should mix in EquatableMixin instead of extending Equatable.', |
| 21 | + correctionMessage: 'Replace with a mixin.', |
| 22 | + severity: .WARNING, |
| 23 | + ); |
| 24 | + |
| 25 | + @override |
| 26 | + LintCode get diagnosticCode => code; |
| 27 | + |
| 28 | + @override |
| 29 | + void registerNodeProcessors( |
| 30 | + RuleVisitorRegistry registry, |
| 31 | + RuleContext context, |
| 32 | + ) { |
| 33 | + registry.addClassDeclaration(this, _Visitor(this, context)); |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +class _Visitor extends SimpleAstVisitor<void> { |
| 38 | + _Visitor(this.rule, this.context); |
| 39 | + |
| 40 | + final AnalysisRule rule; |
| 41 | + final RuleContext context; |
| 42 | + |
| 43 | + static const equatable = TypeChecker.fromName( |
| 44 | + 'Equatable', |
| 45 | + packageName: 'equatable', |
| 46 | + ); |
| 47 | + static const equatableMixin = TypeChecker.fromName( |
| 48 | + 'EquatableMixin', |
| 49 | + packageName: 'equatable', |
| 50 | + ); |
| 51 | + |
| 52 | + @override |
| 53 | + void visitClassDeclaration(ClassDeclaration node) { |
| 54 | + final extendsClause = node.extendsClause; |
| 55 | + if (extendsClause == null) { |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + final superType = extendsClause.superclass.type; |
| 60 | + final isEquatable = superType != null && equatable.isExactlyType(superType); |
| 61 | + |
| 62 | + final isEquatableMixin = |
| 63 | + node.withClause?.mixinTypes |
| 64 | + .map((mixin) => mixin.type) |
| 65 | + .nonNulls |
| 66 | + .any(equatableMixin.isExactlyType) ?? |
| 67 | + false; |
| 68 | + |
| 69 | + if (isEquatable && !isEquatableMixin) { |
| 70 | + rule.reportAtNode( |
| 71 | + extendsClause.superclass, |
| 72 | + arguments: [node.namePart.typeName.lexeme], |
| 73 | + ); |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +class ConvertToEquatableMixin extends ResolvedCorrectionProducer { |
| 79 | + ConvertToEquatableMixin({required super.context}); |
| 80 | + |
| 81 | + @override |
| 82 | + FixKind? get fixKind => const FixKind( |
| 83 | + 'leancode_lint.fix.convertToEquatableMixin', |
| 84 | + DartFixKindPriority.standard, |
| 85 | + 'Convert to EquatableMixin', |
| 86 | + ); |
| 87 | + |
| 88 | + @override |
| 89 | + CorrectionApplicability get applicability => .automatically; |
| 90 | + |
| 91 | + @override |
| 92 | + Future<void> compute(ChangeBuilder builder) async { |
| 93 | + final classDeclaration = node.thisOrAncestorOfType<ClassDeclaration>()!; |
| 94 | + final extendsClause = classDeclaration.extendsClause!; |
| 95 | + final withClause = classDeclaration.withClause; |
| 96 | + |
| 97 | + await builder.addDartFileEdit(file, (builder) { |
| 98 | + if (withClause != null) { |
| 99 | + builder |
| 100 | + ..addSimpleReplacement( |
| 101 | + range.startStart(extendsClause, withClause), |
| 102 | + '', |
| 103 | + ) |
| 104 | + ..addSimpleInsertion( |
| 105 | + withClause.mixinTypes.first.offset, |
| 106 | + 'EquatableMixin, ', |
| 107 | + ); |
| 108 | + } else { |
| 109 | + builder.addSimpleReplacement( |
| 110 | + extendsClause.sourceRange, |
| 111 | + 'with EquatableMixin', |
| 112 | + ); |
| 113 | + } |
| 114 | + }); |
| 115 | + } |
| 116 | +} |
0 commit comments