@@ -9,6 +9,7 @@ import 'package:analyzer/dart/element/element.dart';
9
9
10
10
import '../analyzer.dart' ;
11
11
import '../ast.dart' ;
12
+ import '../extensions.dart' ;
12
13
13
14
const _desc =
14
15
r'Avoid overloading operator == and hashCode on classes not marked `@immutable`.' ;
@@ -95,24 +96,31 @@ class _Visitor extends SimpleAstVisitor<void> {
95
96
96
97
@override
97
98
void visitMethodDeclaration (MethodDeclaration node) {
99
+ if (node.isAugmentation) return ;
100
+
98
101
if (node.name.type == TokenType .EQ_EQ || isHashCode (node)) {
99
- var classElement = _getClassForMethod ( node) ;
100
- if (classElement != null && ! _hasImmutableAnnotation ( classElement) ) {
102
+ var classElement = node.classElement ;
103
+ if (classElement != null && ! classElement.hasImmutableAnnotation ) {
101
104
rule.reportLintForToken (node.firstTokenAfterCommentAndMetadata,
102
105
arguments: [node.name.lexeme]);
103
106
}
104
107
}
105
108
}
109
+ }
106
110
107
- ClassElement ? _getClassForMethod (MethodDeclaration node) =>
108
- // TODO(pq): should this be ClassOrMixinDeclaration ?
109
- node.thisOrAncestorOfType <ClassDeclaration >()? .declaredElement;
110
-
111
- bool _hasImmutableAnnotation (ClassElement clazz) {
111
+ extension on ClassElement {
112
+ bool get hasImmutableAnnotation {
113
+ // TODO(pq): consider augmentations? https://github.com/dart-lang/linter/issues/4939
112
114
var inheritedAndSelfElements = < InterfaceElement > [
113
- ...clazz. allSupertypes.map ((t) => t.element),
114
- clazz ,
115
+ ...allSupertypes.map ((t) => t.element),
116
+ this ,
115
117
];
116
118
return inheritedAndSelfElements.any ((e) => e.hasImmutable);
117
119
}
118
120
}
121
+
122
+ extension on MethodDeclaration {
123
+ ClassElement ? get classElement =>
124
+ // TODO(pq): should this be ClassOrMixinDeclaration ?
125
+ thisOrAncestorOfType <ClassDeclaration >()? .declaredElement;
126
+ }
0 commit comments