@@ -8,8 +8,6 @@ import 'package:analyzer/dart/constant/value.dart';
8
8
import 'package:analyzer/dart/element/element.dart' ;
9
9
import 'package:analyzer/dart/element/type.dart' ;
10
10
import 'package:analyzer/file_system/physical_file_system.dart' ;
11
- import 'package:analyzer/src/dart/ast/ast.dart' // ignore: implementation_imports
12
- show FunctionDeclarationImpl;
13
11
import 'package:analyzer/src/dart/element/element.dart' ; // ignore: implementation_imports
14
12
import 'package:analyzer/src/dart/element/type.dart' // ignore: implementation_imports
15
13
show InvalidTypeImpl;
@@ -47,7 +45,7 @@ extension AstNodeExtension on AstNode {
47
45
EnumDeclaration () => self.augmentKeyword != null ,
48
46
ExtensionTypeDeclaration () => self.augmentKeyword != null ,
49
47
FieldDeclaration () => self.augmentKeyword != null ,
50
- FunctionDeclarationImpl () => self.augmentKeyword != null ,
48
+ FunctionDeclaration () => self.augmentKeyword != null ,
51
49
FunctionExpression () => self.parent? .isAugmentation ?? false ,
52
50
MethodDeclaration () => self.augmentKeyword != null ,
53
51
MixinDeclaration () => self.augmentKeyword != null ,
@@ -131,7 +129,7 @@ extension AstNodeNullableExtension on AstNode? {
131
129
}
132
130
133
131
extension BlockExtension on Block {
134
- /// Returns the last statement of this block, or `null` if this is empty.
132
+ /// The last statement of this block, or `null` if this is empty.
135
133
///
136
134
/// If the last immediate statement of this block is a [Block] , recurses into
137
135
/// it to find the last statement.
@@ -148,20 +146,14 @@ extension BlockExtension on Block {
148
146
}
149
147
150
148
extension ClassElementExtension on ClassElement {
151
- bool get hasImmutableAnnotation {
152
- var inheritedAndSelfElements = < InterfaceElement > [
153
- ...allSupertypes.map ((t) => t.element),
154
- this ,
155
- ];
156
-
157
- return inheritedAndSelfElements.any ((e) => e.metadata.hasImmutable);
158
-
159
- // TODO(pq): update when implemented or replace w/ a better has{*} call
160
- // https://github.com/dart-lang/linter/issues/4939
161
- //return inheritedAndSelfElements.any((e) => e.augmented.metadata.any((e) => e.isImmutable));
162
- }
163
-
164
- bool get hasSubclassInDefiningCompilationUnit {
149
+ /// Whether this [ClassElement] , or one of its supertypes, is annotated with
150
+ /// `@Immutable` .
151
+ bool get hasImmutableAnnotation => [
152
+ ...allSupertypes.map ((t) => t.element),
153
+ this ,
154
+ ].any ((e) => e.metadata.hasImmutable);
155
+
156
+ bool get _hasSubclassInDefiningCompilationUnit {
165
157
for (var cls in library.classes) {
166
158
InterfaceType ? classType = cls.thisType;
167
159
do {
@@ -232,22 +224,17 @@ extension ClassElementExtension on ClassElement {
232
224
}
233
225
234
226
// And no subclasses in the defining library.
235
- if (hasSubclassInDefiningCompilationUnit ) return null ;
227
+ if (_hasSubclassInDefiningCompilationUnit ) return null ;
236
228
237
229
return EnumLikeClassDescription (enumConstants);
238
230
}
239
231
240
232
bool isEnumLikeClass () => asEnumLikeClass () != null ;
241
233
}
242
234
243
- extension ClassMemberListExtension on List <ClassMember > {
244
- MethodDeclaration ? getMethod (String name) => whereType <MethodDeclaration >()
245
- .firstWhereOrNull ((node) => node.name.lexeme == name);
246
- }
247
-
248
235
extension ConstructorElementExtension on ConstructorElement {
249
- /// Returns whether ` this` is the same element as the [className] constructor
250
- /// named [constructorName] declared in [uri] .
236
+ /// Whether this [ConstructorElement] is the same constructor as the
237
+ /// [className] constructor named [constructorName] declared in [uri] .
251
238
bool isSameAs ({
252
239
required String uri,
253
240
required String className,
@@ -259,43 +246,41 @@ extension ConstructorElementExtension on ConstructorElement {
259
246
}
260
247
261
248
extension DartTypeExtension on DartType ? {
249
+ /// Whether this [DartType] extends [className] , declared in [library] .
262
250
bool extendsClass (String ? className, String library) {
263
251
var self = this ;
264
- if (self is InterfaceType ) {
265
- return _extendsClass (self, < InterfaceElement > {}, className, library);
266
- }
267
- return false ;
252
+ return self is InterfaceType &&
253
+ _extendsClass (self, < InterfaceElement > {}, className, library);
268
254
}
269
255
256
+ /// Whether this [DartType] implements any of [definitions] .
270
257
bool implementsAnyInterface (Iterable <InterfaceTypeDefinition > definitions) {
271
- bool isAnyInterface (InterfaceType i) =>
272
- definitions.any ((d) => i.isSameAs (d.name, d.library));
273
-
274
258
var typeToCheck = this ;
275
259
if (typeToCheck is TypeParameterType ) {
276
260
typeToCheck = typeToCheck.typeForInterfaceCheck;
277
261
}
278
- if (typeToCheck is InterfaceType ) {
279
- return isAnyInterface (typeToCheck) ||
280
- ! typeToCheck.element.isSynthetic &&
281
- typeToCheck.element.allSupertypes.any (isAnyInterface);
282
- } else {
283
- return false ;
284
- }
262
+ if (typeToCheck is ! InterfaceType ) return false ;
263
+
264
+ bool isAnyInterface (InterfaceType i) =>
265
+ definitions.any ((d) => i.isSameAs (d.name, d.library));
266
+
267
+ return isAnyInterface (typeToCheck) ||
268
+ ! typeToCheck.element.isSynthetic &&
269
+ typeToCheck.element.allSupertypes.any (isAnyInterface);
285
270
}
286
271
272
+ /// Whether this [DartType] implements [interface] , declared in [library] .
287
273
bool implementsInterface (String interface , String library) {
288
274
var self = this ;
289
- if (self is ! InterfaceType ) {
290
- return false ;
291
- }
292
- bool predicate (InterfaceType i) => i.isSameAs (interface , library);
293
- var element = self.element;
294
- return predicate (self) ||
295
- ! element.isSynthetic && element.allSupertypes.any (predicate);
275
+ if (self is ! InterfaceType ) return false ;
276
+ if (self.isSameAs (interface , library)) return true ;
277
+ if (self.element.isSynthetic) return false ;
278
+ return self.element.allSupertypes.any (
279
+ (i) => i.isSameAs (interface , library),
280
+ );
296
281
}
297
282
298
- /// Returns whether ` this` is the same element as [interface] , declared in
283
+ /// Whether this [DartType] is the same element as [interface] , declared in
299
284
/// [library] .
300
285
bool isSameAs (String ? interface , String ? library) {
301
286
var self = this ;
@@ -537,13 +522,6 @@ extension InstanceElementExtension on InstanceElement {
537
522
metadata.annotations.any ((a) => a.isReflectiveTest);
538
523
}
539
524
540
- extension InterfaceElementExtension on InterfaceElement {
541
- /// Whether this element has the exact [name] and defined in the file with
542
- /// the given [uri] .
543
- bool isExactly (String name, Uri uri) =>
544
- this .name == name && enclosingElement.uri == uri;
545
- }
546
-
547
525
extension InterfaceTypeExtension on InterfaceType {
548
526
/// Returns the collection of all interfaces that this type implements,
549
527
/// including itself.
0 commit comments