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

Unified Diff: lib/src/codegen/js_codegen.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
« no previous file with comments | « no previous file | lib/src/utils.dart » ('j') | lib/src/utils.dart » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/src/codegen/js_codegen.dart
diff --git a/lib/src/codegen/js_codegen.dart b/lib/src/codegen/js_codegen.dart
index 654073628fd7a4eac6fa94b93fed8145f6390bf1..431d7227f0e34c970753dc1a27455a88238e13cb 100644
--- a/lib/src/codegen/js_codegen.dart
+++ b/lib/src/codegen/js_codegen.dart
@@ -33,20 +33,6 @@ import 'code_generator.dart';
import 'js_names.dart';
import 'js_metalet.dart';
-bool _isAnnotationType(Annotation m, String name) => m.name.name == name;
-
-Annotation _getAnnotation(AnnotatedNode node, String name) => node.metadata
- .firstWhere((annotation) => _isAnnotationType(annotation, name),
- orElse: () => null);
-
-Annotation _getJsNameAnnotation(AnnotatedNode node) =>
- _getAnnotation(node, "JsName");
-
-// TODO(jacobr): we would like to do something like the following
-// but we don't have summary support yet.
-// bool _supportJsExtensionMethod(AnnotatedNode node) =>
-// _getAnnotation(node, "SupportJsExtensionMethod") != null;
-
class JSCodegenVisitor extends GeneralizingAstVisitor with ConversionVisitor {
final LibraryInfo libraryInfo;
final TypeRules rules;
@@ -122,19 +108,17 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ConversionVisitor {
final JSTemporary _namedArgTemp = new JSTemporary('opts');
JS.Program emitLibrary(LibraryUnit library) {
- var jsDefaultValue = '{}';
+ String jsDefaultValue = null;
var unit = library.library;
if (unit.directives.isNotEmpty) {
- var annotation = _getJsNameAnnotation(unit.directives.first);
- if (annotation != null) {
- var arguments = annotation.arguments.arguments;
- if (!arguments.isEmpty) {
- var namedExpression = arguments.first as NamedExpression;
- var literal = namedExpression.expression as SimpleStringLiteral;
- jsDefaultValue = literal.stringValue;
- }
+ var libraryDir = unit.directives.first;
+ if (libraryDir is LibraryDirective) {
+ var jsName = getAnnotationValue(libraryDir, _isJsNameAnnotation);
+ jsDefaultValue = getStringConstantField(jsName, 'name');
}
}
+ if (jsDefaultValue == null) jsDefaultValue = '{}';
+
var body = <JS.Statement>[];
// Collect classes we need to emit, used for:
@@ -333,17 +317,16 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ConversionVisitor {
return _finishClassDef(type, classDecl);
}
- JS.Statement _emitJsType(ClassDeclaration node, Annotation jsName) {
- var dartName = node.name.name;
- var jsTypeName = _getLiteralStringNamedArg(jsName, 'name');
+ JS.Statement _emitJsType(String dartClassName, DartObjectImpl jsName) {
+ var jsTypeName = getStringConstantField(jsName, 'name');
- if (jsTypeName != null && jsTypeName != dartName) {
+ if (jsTypeName != null && jsTypeName != dartClassName) {
// We export the JS type as if it was a Dart type. For example this allows
// `dom.InputElement` to actually be HTMLInputElement.
// TODO(jmesserly): if we had the JsName on the Element, we could just
// generate it correctly when we refer to it.
- if (isPublic(dartName)) _addExport(dartName);
- return js.statement('let # = #;', [dartName, jsTypeName]);
+ if (isPublic(dartClassName)) _addExport(dartClassName);
+ return js.statement('let # = #;', [dartClassName, jsTypeName]);
}
return null;
}
@@ -354,8 +337,8 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ConversionVisitor {
var type = node.element.type;
if (_pendingClasses.remove(node.element) == null) return null;
- var jsName = _getJsNameAnnotation(node);
- if (jsName != null) return _emitJsType(node, jsName);
+ var jsName = getAnnotationValue(node, _isJsNameAnnotation);
+ if (jsName != null) return _emitJsType(node.name.name, jsName);
currentClass = node;
@@ -2594,16 +2577,14 @@ class _AssignmentFinder extends RecursiveAstVisitor {
}
}
-String _getLiteralStringNamedArg(Annotation annotation, String argName) {
- if (annotation.arguments != null) {
- var args = annotation.arguments.arguments;
- if (args.isNotEmpty && args[0] is NamedExpression) {
- NamedExpression named = args[0];
- if (named.name.label.name == argName &&
- named.expression is StringLiteral) {
- return (named.expression as StringLiteral).stringValue;
- }
- }
- }
- return null;
+bool _isJsNameAnnotation(DartObjectImpl value) {
+ var type = value.type;
+ var library = type.element.library;
+ // TODO(jmesserly): move JsName to dart:js
Jacob 2015/04/14 20:54:33 until we move it to dart:js can't we just be permi
Jennifer Messerly 2015/04/15 00:21:26 hmmm. The current situation does indeed seem odd.
+ return type.name == 'JsName' && (library.isDartCore || library.name == 'dom');
}
+
+// TODO(jacobr): we would like to do something like the following
+// but we don't have summary support yet.
+// bool _supportJsExtensionMethod(AnnotatedNode node) =>
+// _getAnnotation(node, "SupportJsExtensionMethod") != null;
« no previous file with comments | « no previous file | lib/src/utils.dart » ('j') | lib/src/utils.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698