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

Unified Diff: lib/src/js/printer.dart

Issue 1676463002: Type annotations instead of closure comments. (Closed) Base URL: git@github.com:dart-lang/dev_compiler.git@master
Patch Set: merged master Created 4 years, 10 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 | « lib/src/js/nodes.dart ('k') | lib/src/js/template.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/src/js/printer.dart
diff --git a/lib/src/js/printer.dart b/lib/src/js/printer.dart
index 02bf379a74871a8b82bd27ef89827c59c42b1c54..cc347255cb69d824453503dc5aeac5ba30f5cda4 100644
--- a/lib/src/js/printer.dart
+++ b/lib/src/js/printer.dart
@@ -9,6 +9,7 @@ class JavaScriptPrintingOptions {
final bool shouldCompressOutput;
final bool minifyLocalVariables;
final bool preferSemicolonToNewlineInMinifiedOutput;
+ final bool shouldEmitTypes;
final bool allowSingleLineIfStatements;
/// True to allow keywords in properties, such as `obj.var` or `obj.function`
@@ -19,6 +20,7 @@ class JavaScriptPrintingOptions {
{this.shouldCompressOutput: false,
this.minifyLocalVariables: false,
this.preferSemicolonToNewlineInMinifiedOutput: false,
+ this.shouldEmitTypes: false,
this.allowKeywordsInProperties: false,
this.allowSingleLineIfStatements: false});
}
@@ -51,8 +53,9 @@ class SimpleJavaScriptPrintingContext extends JavaScriptPrintingContext {
String getText() => buffer.toString();
}
-
-class Printer implements NodeVisitor {
+// TODO(ochafik): Inline the body of [TypeScriptTypePrinter] here if/when it no
+// longer needs to share utils with [ClosureTypePrinter].
+class Printer extends TypeScriptTypePrinter implements NodeVisitor {
final JavaScriptPrintingOptions options;
final JavaScriptPrintingContext context;
final bool shouldCompressOutput;
@@ -541,12 +544,14 @@ class Printer implements NodeVisitor {
newInForInit: false, newAtStatementBegin: false);
}
localNamer.enterScope(fun);
+ outTypeParams(fun.typeParams);
out("(");
if (fun.params != null) {
visitCommaSeparated(fun.params, PRIMARY,
newInForInit: false, newAtStatementBegin: false);
}
out(")");
+ outTypeAnnotation(fun.returnType);
switch (fun.asyncModifier) {
case const AsyncModifier.sync():
break;
@@ -601,8 +606,11 @@ class Printer implements NodeVisitor {
visitVariableDeclarationList(VariableDeclarationList list) {
outClosureAnnotation(list);
- out(list.keyword);
- out(" ");
+ // Note: keyword can be null for non-static field declarations.
+ if (list.keyword != null) {
+ out(list.keyword);
+ out(" ");
+ }
visitCommaSeparated(list.declarations, ASSIGNMENT,
newInForInit: inForInit, newAtStatementBegin: false);
}
@@ -639,6 +647,7 @@ class Printer implements NodeVisitor {
}
visit(node.structure);
}
+ outTypeAnnotation(node.type);
if (node.defaultValue != null) {
spaceOut();
out("=");
@@ -869,6 +878,7 @@ class Printer implements NodeVisitor {
visitIdentifier(Identifier node) {
out(localNamer.getName(node));
+ outTypeAnnotation(node.type);
}
visitRestParameter(RestParameter node) {
@@ -934,7 +944,8 @@ class Printer implements NodeVisitor {
visitArrowFun(ArrowFun fun) {
localNamer.enterScope(fun);
- if (fun.params.length == 1) {
+ if (fun.params.length == 1 &&
+ (fun.params.single.type == null || !options.shouldEmitTypes)) {
visitNestedExpression(fun.params.single, SPREAD,
newInForInit: false, newAtStatementBegin: false);
} else {
@@ -943,6 +954,7 @@ class Printer implements NodeVisitor {
newInForInit: false, newAtStatementBegin: false);
out(")");
}
+ outTypeAnnotation(fun.returnType);
spaceOut();
out("=>");
if (fun.body is Expression) {
@@ -1081,9 +1093,23 @@ class Printer implements NodeVisitor {
lineOut();
}
+ void outTypeParams(Iterable<Identifier> typeParams) {
+ if (typeParams != null && options.shouldEmitTypes && typeParams.isNotEmpty) {
+ out("<");
+ var first = true;
+ for (var typeParam in typeParams) {
+ if (!first) out(", ");
+ first = false;
+ visit(typeParam);
+ }
+ out(">");
+ }
+ }
+
visitClassExpression(ClassExpression node) {
out('class ');
visit(node.name);
+ outTypeParams(node.typeParams);
if (node.heritage != null) {
out(' extends ');
visit(node.heritage);
@@ -1093,6 +1119,14 @@ class Printer implements NodeVisitor {
out('{');
lineOut();
indentMore();
+ if (options.shouldEmitTypes && node.fields != null) {
+ for (var field in node.fields) {
+ indent();
+ visit(field);
+ out(";");
+ lineOut();
+ }
+ }
for (var method in node.methods) {
indent();
visit(method);
@@ -1324,6 +1358,18 @@ class Printer implements NodeVisitor {
out("await ");
visit(node.expression);
}
+
+ void outTypeAnnotation(TypeRef node) {
+ if (node == null || !options.shouldEmitTypes || node.isUnknown) return;
+
+ if (node is OptionalTypeRef) {
+ out("?: ");
+ visit(node.type);
+ } else {
+ out(": ");
+ visit(node);
+ }
+ }
}
// Collects all the var declarations in the function. We need to do this in a
« no previous file with comments | « lib/src/js/nodes.dart ('k') | lib/src/js/template.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698