OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 /// Helpers for Analyzer's Element model and corelib model. | 5 /// Helpers for Analyzer's Element model and corelib model. |
6 | 6 |
7 import 'package:analyzer/dart/ast/ast.dart' | 7 import 'package:analyzer/dart/ast/ast.dart' |
8 show | 8 show |
9 ConstructorDeclaration, | 9 ConstructorDeclaration, |
10 Expression, | 10 Expression, |
11 FunctionBody, | 11 FunctionBody, |
12 FunctionExpression, | 12 FunctionExpression, |
13 MethodDeclaration, | 13 MethodDeclaration, |
14 MethodInvocation, | 14 MethodInvocation, |
15 SimpleIdentifier; | 15 SimpleIdentifier; |
16 import 'package:analyzer/dart/element/element.dart' | 16 import 'package:analyzer/dart/element/element.dart' |
17 show Element, ExecutableElement, FunctionElement; | 17 show Element, ExecutableElement, FunctionElement; |
18 import 'package:analyzer/dart/element/type.dart' | 18 import 'package:analyzer/dart/element/type.dart' |
19 show DartType, InterfaceType, ParameterizedType; | 19 show DartType, InterfaceType, ParameterizedType; |
20 import 'package:analyzer/src/dart/element/type.dart' show DynamicTypeImpl; | 20 import 'package:analyzer/src/dart/element/type.dart' show DynamicTypeImpl; |
21 import 'package:analyzer/src/generated/constant.dart' show DartObject; | 21 import 'package:analyzer/src/generated/constant.dart' |
| 22 show DartObject, DartObjectImpl; |
22 | 23 |
23 class Tuple2<T0, T1> { | 24 class Tuple2<T0, T1> { |
24 final T0 e0; | 25 final T0 e0; |
25 final T1 e1; | 26 final T1 e1; |
26 Tuple2(this.e0, this.e1); | 27 Tuple2(this.e0, this.e1); |
27 } | 28 } |
28 | 29 |
29 /*=T*/ fillDynamicTypeArgs/*<T extends DartType>*/(/*=T*/ t) { | 30 /*=T*/ fillDynamicTypeArgs/*<T extends DartType>*/(/*=T*/ t) { |
30 if (t is ParameterizedType) { | 31 if (t is ParameterizedType) { |
31 var dyn = new List.filled(t.typeArguments.length, DynamicTypeImpl.instance); | 32 var pt = t as ParameterizedType; |
32 return t.substitute2(dyn, t.typeArguments); | 33 var dyn = new List<DartType>.filled( |
| 34 pt.typeArguments.length, DynamicTypeImpl.instance); |
| 35 return pt.substitute2(dyn, pt.typeArguments) as dynamic/*=T*/; |
33 } | 36 } |
34 return t; | 37 return t; |
35 } | 38 } |
36 | 39 |
37 /// Given an annotated [node] and a [test] function, returns the first matching | 40 /// Given an annotated [node] and a [test] function, returns the first matching |
38 /// constant valued annotation. | 41 /// constant valued annotation. |
39 /// | 42 /// |
40 /// For example if we had the ClassDeclaration node for `FontElement`: | 43 /// For example if we had the ClassDeclaration node for `FontElement`: |
41 /// | 44 /// |
42 /// @js.JS('HTMLFontElement') | 45 /// @js.JS('HTMLFontElement') |
43 /// @deprecated | 46 /// @deprecated |
44 /// class FontElement { ... } | 47 /// class FontElement { ... } |
45 /// | 48 /// |
46 /// We could match `@deprecated` with a test function like: | 49 /// We could match `@deprecated` with a test function like: |
47 /// | 50 /// |
48 /// (v) => v.type.name == 'Deprecated' && v.type.element.library.isDartCore | 51 /// (v) => v.type.name == 'Deprecated' && v.type.element.library.isDartCore |
49 /// | 52 /// |
50 DartObject findAnnotation(Element element, bool test(DartObject value)) { | 53 DartObject findAnnotation(Element element, bool test(DartObjectImpl value)) { |
51 for (var metadata in element.metadata) { | 54 for (var metadata in element.metadata) { |
52 var value = metadata.constantValue; | 55 var value = metadata.constantValue; |
53 if (value != null && test(value)) return value; | 56 if (value != null && test(value)) return value; |
54 } | 57 } |
55 return null; | 58 return null; |
56 } | 59 } |
57 | 60 |
58 /// Searches all supertype, in order of most derived members, to see if any | 61 /// Searches all supertype, in order of most derived members, to see if any |
59 /// [match] a condition. If so, returns the first match, otherwise returns null. | 62 /// [match] a condition. If so, returns the first match, otherwise returns null. |
60 InterfaceType findSupertype(InterfaceType type, bool match(InterfaceType t)) { | 63 InterfaceType findSupertype(InterfaceType type, bool match(InterfaceType t)) { |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 /// final String name; | 112 /// final String name; |
110 /// // ... | 113 /// // ... |
111 /// const MyAnnotation(this.name/*, ... other params ... */); | 114 /// const MyAnnotation(this.name/*, ... other params ... */); |
112 /// } | 115 /// } |
113 /// | 116 /// |
114 /// @MyAnnotation('FooBar') | 117 /// @MyAnnotation('FooBar') |
115 /// main() { ... } | 118 /// main() { ... } |
116 /// | 119 /// |
117 /// If we match the annotation for the `@MyAnnotation('FooBar')` this will | 120 /// If we match the annotation for the `@MyAnnotation('FooBar')` this will |
118 /// return the string 'FooBar'. | 121 /// return the string 'FooBar'. |
119 String getAnnotationName(Element element, bool match(DartObject value)) => | 122 String getAnnotationName(Element element, bool match(DartObjectImpl value)) => |
120 findAnnotation(element, match)?.getField('name')?.toStringValue(); | 123 findAnnotation(element, match)?.getField('name')?.toStringValue(); |
OLD | NEW |