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

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 1fc1d446d68bbade0a62160b80f6fd7ef266d36a..6f5ac0675a169dfe74aa619d26b4fe4721869330 100644
--- a/lib/src/utils.dart
+++ b/lib/src/utils.dart
@@ -19,6 +19,7 @@ import 'package:analyzer/src/generated/ast.dart'
AstNode,
Expression,
SimpleIdentifier;
+import 'package:analyzer/src/generated/constant.dart' show DartObjectImpl;
import 'package:analyzer/src/generated/element.dart';
import 'package:analyzer/src/generated/engine.dart'
show ParseDartTask, AnalysisContext;
@@ -346,5 +347,44 @@ String resourceOutputPath(Uri resourceUri, Uri entryUri) {
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], a [fieldName], and an [expectedType], returns the
+/// value of that field.
+///
+/// If the field is missing or is not [expectedType], returns null.
+Object getConstantField(
+ DartObjectImpl value, String fieldName, DartType expectedType) {
+ if (value == null) return null;
+ var f = value.fields[fieldName];
+ return (f == null || f.type != expectedType) ? null : f.value;
+}
+
InterfaceType fillDynamicTypeArgs(InterfaceType t, TypeProvider types) =>
t.substitute4(new List.filled(t.typeArguments.length, types.dynamicType));
« 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