Skip to content

Commit c14d512

Browse files
ensure let, const, class, have braces in if, for, while statements. closes #917
1 parent 8b966d6 commit c14d512

3 files changed

Lines changed: 19 additions & 9 deletions

File tree

lib/compress/common.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,10 @@ export function is_func_expr(node) {
241241
return node instanceof AST_Arrow || node instanceof AST_Function;
242242
}
243243

244+
/**
245+
* Used to determine whether the node can benefit from negation.
246+
* Not the case with arrow functions (you need an extra set of parens). */
244247
export function is_iife_call(node) {
245-
// Used to determine whether the node can benefit from negation.
246-
// Not the case with arrow functions (you need an extra set of parens).
247248
if (node.TYPE != "Call") return false;
248249
return node.expression instanceof AST_Function || is_iife_call(node.expression);
249250
}

lib/output.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,7 +1225,7 @@ function OutputStream(options) {
12251225
}
12261226

12271227
AST_StatementWithBody.DEFMETHOD("_do_print_body", function(output) {
1228-
force_statement(this.body, output);
1228+
print_maybe_braced_body(this.body, output);
12291229
});
12301230

12311231
DEFPRINT(AST_Statement, function(self, output) {
@@ -1554,7 +1554,7 @@ function OutputStream(options) {
15541554
b = b.body;
15551555
} else break;
15561556
}
1557-
force_statement(self.body, output);
1557+
print_maybe_braced_body(self.body, output);
15581558
}
15591559
DEFPRINT(AST_If, function(self, output) {
15601560
output.print("if");
@@ -1571,7 +1571,7 @@ function OutputStream(options) {
15711571
if (self.alternative instanceof AST_If)
15721572
self.alternative.print(output);
15731573
else
1574-
force_statement(self.alternative, output);
1574+
print_maybe_braced_body(self.alternative, output);
15751575
} else {
15761576
self._do_print_body(output);
15771577
}
@@ -2266,12 +2266,15 @@ function OutputStream(options) {
22662266
}
22672267
});
22682268

2269-
function force_statement(stat, output) {
2269+
/** if, for, while, may or may not have braces surrounding its body */
2270+
function print_maybe_braced_body(stat, output) {
22702271
if (output.option("braces")) {
22712272
make_block(stat, output);
22722273
} else {
22732274
if (!stat || stat instanceof AST_EmptyStatement)
22742275
output.force_semicolon();
2276+
else if (stat instanceof AST_Let || stat instanceof AST_Const || stat instanceof AST_Class)
2277+
make_block(stat, output);
22752278
else
22762279
stat.print(output);
22772280
}

lib/parse.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2089,14 +2089,20 @@ function parse($TEXT, options) {
20892089
});
20902090
}
20912091

2092+
/**
2093+
* var
2094+
* vardef1 = 2,
2095+
* vardef2 = 3;
2096+
*/
20922097
function vardefs(no_in, kind) {
2093-
var a = [];
2098+
var var_defs = [];
20942099
var def;
20952100
for (;;) {
20962101
var sym_type =
20972102
kind === "var" ? AST_SymbolVar :
20982103
kind === "const" ? AST_SymbolConst :
20992104
kind === "let" ? AST_SymbolLet : null;
2105+
// var { a } = b
21002106
if (is("punc", "{") || is("punc", "[")) {
21012107
def = new AST_VarDef({
21022108
start: S.token,
@@ -2116,12 +2122,12 @@ function parse($TEXT, options) {
21162122
});
21172123
if (def.name.name == "import") croak("Unexpected token: import");
21182124
}
2119-
a.push(def);
2125+
var_defs.push(def);
21202126
if (!is("punc", ","))
21212127
break;
21222128
next();
21232129
}
2124-
return a;
2130+
return var_defs;
21252131
}
21262132

21272133
var var_ = function(no_in) {

0 commit comments

Comments
 (0)