Chromium Code Reviews| Index: lib/src/utils.dart |
| diff --git a/lib/src/utils.dart b/lib/src/utils.dart |
| index ea28a6df6b4c751a9ebb0d332718ab6da4c913d7..a5725711e23ad861f6f19ce8a2e37af6355252f8 100644 |
| --- a/lib/src/utils.dart |
| +++ b/lib/src/utils.dart |
| @@ -23,6 +23,7 @@ import 'package:analyzer/src/generated/engine.dart' |
| show ParseDartTask, AnalysisContext; |
| import 'package:analyzer/src/generated/source.dart' show Source; |
| import 'package:analyzer/src/generated/element.dart'; |
| +import 'package:analyzer/src/generated/constant.dart' show DartObjectImpl; |
| import 'package:analyzer/analyzer.dart' show parseDirectives; |
| import 'package:crypto/crypto.dart' show CryptoUtils, MD5; |
| import 'package:source_span/source_span.dart'; |
| @@ -344,3 +345,43 @@ String resourceOutputPath(Uri resourceUri, Uri entryUri) { |
| if (pubspec['name'] != 'dev_compiler') return null; |
| return path.join('dev_compiler', 'runtime', filename); |
| } |
| + |
| +/// Given an annotated [node] and a [test] function, returns the first matching |
| +/// constant valued annotation. |
| +/// |
| +/// For example if we had the ClassDeclaration node for `FontElement`: |
| +/// |
| +/// @JsName('HTMLFontElement') |
| +/// @deprecated |
| +/// class FontElement { ... } |
| +/// |
| +/// We could match `@deprecated` with a test function like: |
| +/// |
| +/// (v) => v.type.name == 'Deprecated' && v.type.element.library.isDartCore |
| +/// |
| +DartObjectImpl getAnnotationValue( |
| + AnnotatedNode node, bool test(DartObjectImpl value)) { |
| + for (var metadata in node.metadata) { |
| + ElementAnnotationImpl element = metadata.elementAnnotation; |
| + if (element == null) continue; |
| + |
| + var evalResult = element.evaluationResult; |
| + if (evalResult == null) continue; |
| + |
| + var value = evalResult.value; |
| + if (value != null && test(value)) return value; |
| + } |
| + return null; |
| +} |
| + |
| +/// Given a constant [value] and a [fieldName], returns the string value of |
| +/// that field. |
| +/// |
| +/// If the field is missing or is not a string, returns null. |
| +String getStringConstantField(DartObjectImpl value, String fieldName) { |
| + if (value == null) return null; |
| + var fieldValue = value.fields[fieldName]; |
| + if (fieldValue == null) return null; |
| + var result = fieldValue.value; |
| + return result is String ? result : null; |
|
Paul Berry
2015/04/14 17:51:27
Not sure if it matters, but the analyzer stores bo
Jennifer Messerly
2015/04/15 00:21:26
ah good catch! I'll update it to fix that.
|
| +} |