forked from dart-code-checker/dart-code-metrics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_utils.dart
59 lines (51 loc) · 1.96 KB
/
node_utils.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/syntactic_entity.dart';
import 'package:source_span/source_span.dart';
import '../analyzers/lint_analyzer/models/internal_resolved_unit_result.dart';
/// Returns [SourceSpan] with information about original code for [node] from [source]
SourceSpan nodeLocation({
required SyntacticEntity node,
required InternalResolvedUnitResult source,
SyntacticEntity? endNode,
bool withCommentOrMetadata = false,
}) {
final offset = !withCommentOrMetadata && node is AnnotatedNode
? node.firstTokenAfterCommentAndMetadata.offset
: node.offset;
final end = endNode?.end ?? node.end;
final sourceUrl = Uri.file(source.path);
final offsetLocation = source.lineInfo.getLocation(offset);
final endLocation = source.lineInfo.getLocation(end);
return SourceSpan(
SourceLocation(
offset,
sourceUrl: sourceUrl,
line: offsetLocation.lineNumber,
column: offsetLocation.columnNumber,
),
SourceLocation(
end,
sourceUrl: sourceUrl,
line: endLocation.lineNumber,
column: endLocation.columnNumber,
),
source.content.substring(offset, end),
);
}
bool isEntrypoint(String name, NodeList<Annotation> metadata) =>
name == 'main' ||
_hasPragmaAnnotation(metadata) ||
_flutterInternalEntryFunctions.contains(name);
const _flutterInternalEntryFunctions = {'registerPlugins', 'testExecutable'};
/// See https://github.com/dart-lang/sdk/blob/master/runtime/docs/compiler/aot/entry_point_pragma.md
bool _hasPragmaAnnotation(Iterable<Annotation> metadata) =>
metadata.where((annotation) {
final arguments = annotation.arguments;
return annotation.name.name == 'pragma' &&
arguments != null &&
arguments.arguments
.where((argument) =>
argument is SimpleStringLiteral &&
argument.stringValue == 'vm:entry-point')
.isNotEmpty;
}).isNotEmpty;