OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 /// Helpers for Analyzer's Element model and corelib model. |
| 6 |
| 7 import 'package:analyzer/dart/ast/ast.dart' |
| 8 show Expression, MethodInvocation, SimpleIdentifier; |
| 9 import 'package:analyzer/dart/element/element.dart' |
| 10 show Element, FunctionElement; |
| 11 import 'package:analyzer/dart/element/type.dart' |
| 12 show DartType, InterfaceType, ParameterizedType; |
| 13 import 'package:analyzer/src/dart/element/type.dart' show DynamicTypeImpl; |
| 14 import 'package:analyzer/src/generated/constant.dart' show DartObject; |
| 15 |
| 16 class Tuple2<T0, T1> { |
| 17 final T0 e0; |
| 18 final T1 e1; |
| 19 Tuple2(this.e0, this.e1); |
| 20 } |
| 21 |
| 22 /*=T*/ fillDynamicTypeArgs/*<T extends DartType>*/(/*=T*/ t) { |
| 23 if (t is ParameterizedType) { |
| 24 var dyn = new List.filled(t.typeArguments.length, DynamicTypeImpl.instance); |
| 25 return t.substitute2(dyn, t.typeArguments); |
| 26 } |
| 27 return t; |
| 28 } |
| 29 |
| 30 /// Given an annotated [node] and a [test] function, returns the first matching |
| 31 /// constant valued annotation. |
| 32 /// |
| 33 /// For example if we had the ClassDeclaration node for `FontElement`: |
| 34 /// |
| 35 /// @js.JS('HTMLFontElement') |
| 36 /// @deprecated |
| 37 /// class FontElement { ... } |
| 38 /// |
| 39 /// We could match `@deprecated` with a test function like: |
| 40 /// |
| 41 /// (v) => v.type.name == 'Deprecated' && v.type.element.library.isDartCore |
| 42 /// |
| 43 DartObject findAnnotation(Element element, bool test(DartObject value)) { |
| 44 for (var metadata in element.metadata) { |
| 45 var value = metadata.constantValue; |
| 46 if (value != null && test(value)) return value; |
| 47 } |
| 48 return null; |
| 49 } |
| 50 |
| 51 /// Searches all supertype, in order of most derived members, to see if any |
| 52 /// [match] a condition. If so, returns the first match, otherwise returns null. |
| 53 InterfaceType findSupertype(InterfaceType type, bool match(InterfaceType t)) { |
| 54 for (var m in type.mixins.reversed) { |
| 55 if (match(m)) return m; |
| 56 } |
| 57 var s = type.superclass; |
| 58 if (s == null) return null; |
| 59 |
| 60 if (match(s)) return type; |
| 61 return findSupertype(s, match); |
| 62 } |
| 63 |
| 64 //TODO(leafp): Is this really necessary? In theory I think |
| 65 // the static type should always be filled in for resolved |
| 66 // ASTs. This may be a vestigial workaround. |
| 67 DartType getStaticType(Expression e) => |
| 68 e.staticType ?? DynamicTypeImpl.instance; |
| 69 |
| 70 // TODO(leafp) Factor this out or use an existing library |
| 71 /// Similar to [SimpleIdentifier] inGetterContext, inSetterContext, and |
| 72 /// inDeclarationContext, this method returns true if [node] is used in an |
| 73 /// invocation context such as a MethodInvocation. |
| 74 bool inInvocationContext(SimpleIdentifier node) { |
| 75 var parent = node.parent; |
| 76 return parent is MethodInvocation && parent.methodName == node; |
| 77 } |
| 78 |
| 79 bool isInlineJS(Element e) => |
| 80 e is FunctionElement && |
| 81 e.name == 'JS' && |
| 82 e.library.isInSdk && |
| 83 e.library.source.uri.toString() == 'dart:_foreign_helper'; |
| 84 |
| 85 /// Returns the value of the `name` field from the [match]ing annotation on |
| 86 /// [element], or `null` if we didn't find a valid match or it was not a string. |
| 87 /// |
| 88 /// For example, consider this code: |
| 89 /// |
| 90 /// class MyAnnotation { |
| 91 /// final String name; |
| 92 /// // ... |
| 93 /// const MyAnnotation(this.name/*, ... other params ... */); |
| 94 /// } |
| 95 /// |
| 96 /// @MyAnnotation('FooBar') |
| 97 /// main() { ... } |
| 98 /// |
| 99 /// If we match the annotation for the `@MyAnnotation('FooBar')` this will |
| 100 /// return the string 'FooBar'. |
| 101 String getAnnotationName(Element element, bool match(DartObject value)) => |
| 102 findAnnotation(element, match)?.getField('name')?.toStringValue(); |
OLD | NEW |