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

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

Issue 949753004: Avoid printing braces for one-statement blocks in js-printer. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 5 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 | « pkg/compiler/lib/src/js/nodes.dart ('k') | tests/compiler/dart2js/async_await_js_transform_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/compiler/lib/src/js/printer.dart
diff --git a/pkg/compiler/lib/src/js/printer.dart b/pkg/compiler/lib/src/js/printer.dart
index b1acebaa8f82e18b0159660b79714a7ba7637f7c..71ee7b75e2f61768da176b95b298a6c36b17e802 100644
--- a/pkg/compiler/lib/src/js/printer.dart
+++ b/pkg/compiler/lib/src/js/printer.dart
@@ -204,10 +204,14 @@ class Printer implements NodeVisitor {
visitAll(program.body);
}
- bool blockBody(Node body, {bool needsSeparation, bool needsNewline}) {
+ bool blockBody(Statement body, {bool needsSeparation, bool needsNewline}) {
+ if (body is Block && body.statements.length == 1) {
+ Block block = body;
+ body = block.statements.single;
+ }
if (body is Block) {
spaceOut();
- blockOut(body, false, needsNewline);
+ blockOut(body, shouldIndent: false, needsNewline: needsNewline);
return true;
}
if (shouldCompressOutput && needsSeparation) {
@@ -234,7 +238,7 @@ class Printer implements NodeVisitor {
}
}
- void blockOut(Block node, bool shouldIndent, bool needsNewline) {
+ void blockOut(Block node, {bool shouldIndent, bool needsNewline}) {
if (shouldIndent) indent();
context.enterNode(node);
out("{");
@@ -249,7 +253,7 @@ class Printer implements NodeVisitor {
}
visitBlock(Block block) {
- blockOut(block, true, true);
+ blockOut(block, shouldIndent: true, needsNewline: true);
}
visitExpressionStatement(ExpressionStatement expressionStatement) {
@@ -264,19 +268,17 @@ class Printer implements NodeVisitor {
}
void ifOut(If node, bool shouldIndent) {
- Node then = node.then;
- Node elsePart = node.otherwise;
+ Statement then = node.then;
+ Statement elsePart = node.otherwise;
bool hasElse = node.hasElse;
// Handle dangling elses and a work-around for Android 4.0 stock browser.
// Android 4.0 requires braces for a single do-while in the `then` branch.
// See issue 10923.
- if (hasElse) {
- bool needsBraces = node.then.accept(danglingElseVisitor) || then is Do;
- if (needsBraces) {
- then = new Block(<Statement>[then]);
- }
- }
+ bool needsBracesInThen = hasElse
floitsch 2015/02/23 13:46:08 bool needsBracesInThen = hasElse && (then.acce
sigurdm 2015/02/23 14:13:22 Acknowledged. This change has been undone.
+ ? node.then.accept(danglingElseVisitor) || then is Do
+ : false;
+
if (shouldIndent) indent();
out("if");
spaceOut();
@@ -284,8 +286,15 @@ class Printer implements NodeVisitor {
visitNestedExpression(node.condition, EXPRESSION,
newInForInit: false, newAtStatementBegin: false);
out(")");
- bool thenWasBlock =
- blockBody(then, needsSeparation: false, needsNewline: !hasElse);
+ bool thenWasBlock;
+ if (needsBracesInThen) {
+ spaceOut();
+ blockOut(new Block([then]), shouldIndent: false, needsNewline: !hasElse);
+ thenWasBlock = true;
+ } else {
floitsch 2015/02/23 13:46:08 This probably broke the dangling-else: if (x) {
sigurdm 2015/02/23 14:13:22 Yes, I just realized that. I will remove the block
+ thenWasBlock =
+ blockBody(then, needsSeparation: false, needsNewline: !hasElse);
+ }
if (hasElse) {
if (thenWasBlock) {
spaceOut();
@@ -423,14 +432,16 @@ class Printer implements NodeVisitor {
visitTry(Try node) {
outIndent("try");
- blockBody(node.body, needsSeparation: true, needsNewline: false);
+ spaceOut();
+ blockOut(node.body, shouldIndent: false, needsNewline: false);
if (node.catchPart != null) {
visit(node.catchPart);
}
if (node.finallyPart != null) {
spaceOut();
out("finally");
- blockBody(node.finallyPart, needsSeparation: true, needsNewline: true);
+ spaceOut();
+ blockOut(node.finallyPart, shouldIndent: false, needsNewline: true);
} else {
lineOut();
}
@@ -444,7 +455,8 @@ class Printer implements NodeVisitor {
visitNestedExpression(node.declaration, EXPRESSION,
newInForInit: false, newAtStatementBegin: false);
out(")");
- blockBody(node.body, needsSeparation: false, needsNewline: true);
+ spaceOut();
+ blockOut(node.body, shouldIndent: false, needsNewline: false);
}
visitSwitch(Switch node) {
@@ -517,7 +529,8 @@ class Printer implements NodeVisitor {
out(' async*');
break;
}
- blockBody(fun.body, needsSeparation: false, needsNewline: false);
+ spaceOut();
+ blockOut(fun.body, shouldIndent: false, needsNewline: false);
localNamer.leaveScope();
}
« no previous file with comments | « pkg/compiler/lib/src/js/nodes.dart ('k') | tests/compiler/dart2js/async_await_js_transform_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698