mirror of
https://github.com/dart-lang/sdk
synced 2024-10-07 09:11:55 +00:00
[dart2js] Manually add braces for dangling else.
Rather than emit a different node than what is expected (the Block rather than the enclosed statement), we can manually emit the necessary braces. Change-Id: I0bf2a27a56828cb6d292479c73fdaec8f4072116 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/336980 Reviewed-by: Stephen Adams <sra@google.com> Commit-Queue: Nate Biggs <natebiggs@google.com>
This commit is contained in:
parent
765c0b3ab8
commit
1941a8f463
|
@ -291,12 +291,18 @@ class Printer implements NodeVisitor<void> {
|
|||
}
|
||||
|
||||
bool blockBody(Statement body,
|
||||
{required bool needsSeparation, required bool needsNewline}) {
|
||||
{required bool needsSeparation,
|
||||
required bool needsNewline,
|
||||
bool needsBraces = false}) {
|
||||
if (body is Block) {
|
||||
spaceOut();
|
||||
blockOut(body, shouldIndent: false, needsNewline: needsNewline);
|
||||
return true;
|
||||
}
|
||||
if (needsBraces) {
|
||||
spaceOut();
|
||||
out('{');
|
||||
}
|
||||
if (shouldCompressOutput && needsSeparation) {
|
||||
// If [shouldCompressOutput] is false, then the 'lineOut' will insert
|
||||
// the separation.
|
||||
|
@ -307,6 +313,11 @@ class Printer implements NodeVisitor<void> {
|
|||
indentMore();
|
||||
visit(body);
|
||||
indentLess();
|
||||
if (needsBraces) {
|
||||
indent();
|
||||
out('}');
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -369,12 +380,8 @@ class Printer implements NodeVisitor<void> {
|
|||
// Handle dangling elses and a workaround 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 = then.accept(danglingElseVisitor) || then is Do;
|
||||
if (needsBraces) {
|
||||
then = Block(<Statement>[then]);
|
||||
}
|
||||
}
|
||||
bool needsBraces =
|
||||
hasElse && (then is Do || then.accept(danglingElseVisitor));
|
||||
if (shouldIndent) indent();
|
||||
out('if');
|
||||
spaceOut();
|
||||
|
@ -382,8 +389,10 @@ class Printer implements NodeVisitor<void> {
|
|||
visitNestedExpression(node.condition, EXPRESSION,
|
||||
newInForInit: false, newAtStatementBegin: false);
|
||||
out(')');
|
||||
bool thenWasBlock =
|
||||
blockBody(then, needsSeparation: false, needsNewline: !hasElse);
|
||||
bool thenWasBlock = blockBody(then,
|
||||
needsSeparation: false,
|
||||
needsNewline: !hasElse,
|
||||
needsBraces: needsBraces);
|
||||
if (hasElse) {
|
||||
if (thenWasBlock) {
|
||||
spaceOut();
|
||||
|
|
Loading…
Reference in a new issue