Permalink
Browse files

add brackets around splice nodes when they're directly under "for",

"for-in", "if", "while", "do" or "with" nodes
  • Loading branch information...
mishoo committed Apr 24, 2011
1 parent e83767e commit b5d994a2d81e266f0520fe415b29b17ea06c83ec
Showing with 13 additions and 5 deletions.
  1. +13 −5 lib/process.js
View
@@ -1072,6 +1072,8 @@ function to_ascii(str) {
});
};
+var SPLICE_NEEDS_BRACKETS = jsp.array_to_hash([ "if", "while", "do", "for", "for-in", "with" ]);
+
function gen_code(ast, options) {
options = defaults(options, {
indent_start : 0,
@@ -1203,11 +1205,17 @@ function gen_code(ast, options) {
.join(newline + newline);
},
"splice": function(statements) {
- var a = make_block_statements(statements, true);
- return MAP(a, function(line, i) {
- // the first line is already indented
- return i > 0 ? indent(line) : line;
- }).join(newline);
+ var parent = $stack[$stack.length - 2][0];
+ if (HOP(SPLICE_NEEDS_BRACKETS, parent)) {
+ // we need block brackets in this case
+ return make_block.apply(this, arguments);
+ } else {
+ return MAP(make_block_statements(statements, true),
+ function(line, i) {
+ // the first line is already indented
+ return i > 0 ? indent(line) : line;
+ }).join(newline);
+ }
},
"block": make_block,
"var": function(defs) {

0 comments on commit b5d994a

Please sign in to comment.