Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(613)

Unified Diff: lib/src/utils.dart

Issue 1084783005: use Analyzer's computed constants instead of pattern matching the annotation AST (Closed) Base URL: git@github.com:dart-lang/dev_compiler.git@master
Patch Set: Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« lib/src/codegen/js_codegen.dart ('K') | « lib/src/codegen/js_codegen.dart ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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.
+}
« lib/src/codegen/js_codegen.dart ('K') | « lib/src/codegen/js_codegen.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698