@@ -7,6 +7,7 @@ import 'dart:collection';
7
7
import 'package:analyzer/dart/ast/ast.dart' ;
8
8
import 'package:analyzer/dart/ast/visitor.dart' ;
9
9
import 'package:analyzer/dart/element/element.dart' ;
10
+ import 'package:analyzer/dart/element/type.dart' ;
10
11
import 'package:collection/collection.dart' ;
11
12
12
13
import '../analyzer.dart' ;
@@ -19,7 +20,7 @@ const _details = r'''
19
20
Top-level members in an executable library should be used directly inside this
20
21
library. An executable library is a library that contains a `main` top-level
21
22
function or that contains a top-level function annotated with
22
- `@pragma('vm:entry-point')`). Executable libraries are usually never imported
23
+ `@pragma('vm:entry-point')`). Executable libraries are not usually imported
23
24
and it's better to avoid defining unused members.
24
25
25
26
This rule assumes that an executable library isn't imported by other files
@@ -168,10 +169,9 @@ class _Visitor extends SimpleAstVisitor<void> {
168
169
(e.name.name == 'main' || e.metadata.any (_isPragmaVmEntry));
169
170
170
171
bool _isPragmaVmEntry (Annotation annotation) {
172
+ if (! annotation.isPragma) return false ;
171
173
var value = annotation.elementAnnotation? .computeConstantValue ();
172
174
if (value == null ) return false ;
173
- var element = value.type? .element;
174
- if (element == null || ! element.isPragma) return false ;
175
175
var name = value.getField ('name' );
176
176
return name != null &&
177
177
name.hasKnownValue &&
@@ -182,3 +182,19 @@ class _Visitor extends SimpleAstVisitor<void> {
182
182
extension on Element {
183
183
bool get isPragma => (library? .isDartCore ?? false ) && name == 'pragma' ;
184
184
}
185
+
186
+ extension on Annotation {
187
+ bool get isPragma {
188
+ var element = elementAnnotation? .element;
189
+ DartType type;
190
+ if (element is ConstructorElement ) {
191
+ type = element.returnType;
192
+ } else if (element is PropertyAccessorElement && element.isGetter) {
193
+ type = element.returnType;
194
+ } else {
195
+ // Dunno what this is.
196
+ return false ;
197
+ }
198
+ return type.element? .isPragma ?? false ;
199
+ }
200
+ }
0 commit comments