Skip to content
This repository was archived by the owner on Nov 20, 2024. It is now read-only.

Commit 8d5f93f

Browse files
committed
address review comments
1 parent 7502bb1 commit 8d5f93f

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

lib/src/rules/unreachable_from_main.dart

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'dart:collection';
77
import 'package:analyzer/dart/ast/ast.dart';
88
import 'package:analyzer/dart/ast/visitor.dart';
99
import 'package:analyzer/dart/element/element.dart';
10+
import 'package:analyzer/dart/element/type.dart';
1011
import 'package:collection/collection.dart';
1112

1213
import '../analyzer.dart';
@@ -19,7 +20,7 @@ const _details = r'''
1920
Top-level members in an executable library should be used directly inside this
2021
library. An executable library is a library that contains a `main` top-level
2122
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
2324
and it's better to avoid defining unused members.
2425
2526
This rule assumes that an executable library isn't imported by other files
@@ -168,10 +169,9 @@ class _Visitor extends SimpleAstVisitor<void> {
168169
(e.name.name == 'main' || e.metadata.any(_isPragmaVmEntry));
169170

170171
bool _isPragmaVmEntry(Annotation annotation) {
172+
if (!annotation.isPragma) return false;
171173
var value = annotation.elementAnnotation?.computeConstantValue();
172174
if (value == null) return false;
173-
var element = value.type?.element;
174-
if (element == null || !element.isPragma) return false;
175175
var name = value.getField('name');
176176
return name != null &&
177177
name.hasKnownValue &&
@@ -182,3 +182,19 @@ class _Visitor extends SimpleAstVisitor<void> {
182182
extension on Element {
183183
bool get isPragma => (library?.isDartCore ?? false) && name == 'pragma';
184184
}
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

Comments
 (0)