OLD | NEW |
| (Empty) |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 import 'package:analyzer/dart/ast/ast.dart'; | |
6 import 'package:analyzer/src/generated/constant.dart'; | |
7 import 'package:analyzer/dart/element/element.dart'; | |
8 import 'package:analyzer/src/generated/resolver.dart' show TypeProvider; | |
9 | |
10 import '../utils.dart'; | |
11 | |
12 bool _isJsLibType(String expectedName, Element e) => | |
13 e?.name == expectedName && _isJsLib(e.library); | |
14 | |
15 /// Returns true if [e] represents any library from `package:js` or is the | |
16 /// internal `dart:_js_helper` library. | |
17 bool _isJsLib(LibraryElement e) { | |
18 if (e == null) return false; | |
19 var uri = e.source.uri; | |
20 if (uri.scheme == 'package' && uri.path.startsWith('js/')) return true; | |
21 if (uri.scheme == 'dart') { | |
22 return uri.path == '_js_helper' || uri.path == '_foreign_helper'; | |
23 } | |
24 return false; | |
25 } | |
26 | |
27 /// Whether [value] is a `@rest` annotation (to be used on function parameters | |
28 /// to have them compiled as `...` rest params in ES6 outputs). | |
29 bool isJsRestAnnotation(DartObjectImpl value) => | |
30 _isJsLibType('_Rest', value.type.element); | |
31 | |
32 /// Whether [i] is a `spread` invocation (to be used on function arguments | |
33 /// to have them compiled as `...` spread args in ES6 outputs). | |
34 bool isJsSpreadInvocation(MethodInvocation i) => | |
35 _isJsLibType('spread', i.methodName?.bestElement); | |
36 | |
37 // TODO(jmesserly): Move JsPeerInterface to package:js (see issue #135). | |
38 bool isJSAnnotation(DartObjectImpl value) => | |
39 _isJsLibType('JS', value.type.element); | |
40 | |
41 bool _isBuiltinAnnotation( | |
42 DartObjectImpl value, String libraryName, String annotationName) { | |
43 var e = value?.type?.element; | |
44 if (e?.name != annotationName) return false; | |
45 var uri = e.source.uri; | |
46 var path = uri.pathSegments[0]; | |
47 return uri.scheme == 'dart' && path == libraryName; | |
48 } | |
49 | |
50 /// Whether [value] is a `@JSExportName` (internal annotation used in SDK | |
51 /// instead of `@JS` from `package:js`). | |
52 bool isJSExportNameAnnotation(DartObjectImpl value) => | |
53 _isBuiltinAnnotation(value, '_foreign_helper', 'JSExportName'); | |
54 | |
55 bool isJsName(DartObjectImpl value) => | |
56 _isBuiltinAnnotation(value, '_js_helper', 'JSName'); | |
57 | |
58 bool isJsPeerInterface(DartObjectImpl value) => | |
59 _isBuiltinAnnotation(value, '_js_helper', 'JsPeerInterface'); | |
60 | |
61 bool isNativeAnnotation(DartObjectImpl value) => | |
62 _isBuiltinAnnotation(value, '_js_helper', 'Native'); | |
63 | |
64 /// Returns the name value of the `JSExportName` annotation (when compiling | |
65 /// the SDK), or `null` if there's none. This is used to control the name | |
66 /// under which functions are compiled and exported. | |
67 String getJSExportName(Element e, TypeProvider types) { | |
68 if (!e.source.isInSystemLibrary) { | |
69 return null; | |
70 } | |
71 var jsName = findAnnotation(e, isJSExportNameAnnotation); | |
72 return getConstantField(jsName, 'name', types.stringType)?.toStringValue(); | |
73 } | |
OLD | NEW |