diff options
author | Alon Zakai <azakai@google.com> | 2019-05-02 16:40:20 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-02 16:40:20 -0700 |
commit | 80b509cfb8fa35d229643f35e423e6d049ab8849 (patch) | |
tree | bb978cf9ab6293876b38fd1eb476126fe34f8b5b /src/emscripten-optimizer | |
parent | 29e7e4483dc04ca8e882f12871c7eb395c82de00 (diff) | |
download | binaryen-80b509cfb8fa35d229643f35e423e6d049ab8849.tar.gz binaryen-80b509cfb8fa35d229643f35e423e6d049ab8849.tar.bz2 binaryen-80b509cfb8fa35d229643f35e423e6d049ab8849.zip |
wasm2js: don't emit obviously unnecessary parens (#2080)
A minifier would probably remove them later anyhow, but they make reading the code annoying and hard.
Diffstat (limited to 'src/emscripten-optimizer')
-rw-r--r-- | src/emscripten-optimizer/simple_ast.h | 38 |
1 files changed, 26 insertions, 12 deletions
diff --git a/src/emscripten-optimizer/simple_ast.h b/src/emscripten-optimizer/simple_ast.h index bcfce9712..be5b4fa21 100644 --- a/src/emscripten-optimizer/simple_ast.h +++ b/src/emscripten-optimizer/simple_ast.h @@ -1332,6 +1332,10 @@ struct JSPrinter { } } + static bool isBlock(Ref node) { + return node->isArray() && !node->empty() && node[0] == BLOCK; + } + static bool ifHasElse(Ref node) { assert(node->isArray() && node[0] == IF); return node->size() >= 4 && !!node[3]; @@ -1344,25 +1348,35 @@ struct JSPrinter { print(node[1]); emit(')'); space(); - emit('{'); - indent++; - newline(); - print(node[2]); - indent--; - newline(); - emit('}'); - if (ifHasElse(node)) { - space(); - emit("else"); - safeSpace(); + bool emitsBracesAnyhow = isBlock(node[2]); + if (!emitsBracesAnyhow) { emit('{'); indent++; newline(); - print(node[3]); + } + print(node[2]); + if (!emitsBracesAnyhow) { indent--; newline(); emit('}'); } + if (ifHasElse(node)) { + space(); + emit("else"); + safeSpace(); + bool emitsBracesAnyhow = isBlock(node[3]); + if (!emitsBracesAnyhow) { + emit('{'); + indent++; + newline(); + } + print(node[3]); + if (!emitsBracesAnyhow) { + indent--; + newline(); + emit('}'); + } + } } void printDo(Ref node) { |