Skip to content

Commit

Permalink
fix AST_For.init patch-up in drop_unused() (#1839)
Browse files Browse the repository at this point in the history
fixes #1838
  • Loading branch information
alexlamsl committed Apr 22, 2017
1 parent ca32a09 commit 45ce369
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/compress.js
Expand Up @@ -2066,6 +2066,7 @@ merge(Compressor.prototype, {
// certain combination of unused name + side effect leads to:
// https://github.com/mishoo/UglifyJS2/issues/44
// https://github.com/mishoo/UglifyJS2/issues/1830
// https://github.com/mishoo/UglifyJS2/issues/1838
// that's an invalid AST.
// We fix it at this stage by moving the `var` outside the `for`.
if (node instanceof AST_For) {
Expand All @@ -2075,6 +2076,8 @@ merge(Compressor.prototype, {
node.init = block.body.pop();
block.body.push(node);
return in_list ? MAP.splice(block.body) : block;
} else if (node.init instanceof AST_SimpleStatement) {
node.init = node.init.body;
} else if (is_empty(node.init)) {
node.init = null;
}
Expand Down
22 changes: 22 additions & 0 deletions test/compress/drop-unused.js
Expand Up @@ -1092,3 +1092,25 @@ issue_1830_2: {
}
expect_stdout: "1"
}

issue_1838: {
options = {
join_vars: true,
loops: true,
unused: true,
}
beautify = {
beautify: true,
}
input: {
function f() {
var b = a;
while (c);
}
}
expect_exact: [
"function f() {",
" for (a; c; ) ;",
"}",
]
}

0 comments on commit 45ce369

Please sign in to comment.