aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2021-02-16 15:39:06 +0000
committerGitHub <noreply@github.com>2021-02-16 23:39:06 +0800
commit6a2bda52f332b0c39f1a5f81583cc5c63f437f4c (patch)
tree270cdd4c2d95805e1542d9b7d2c4638fe3ea20de
parentfa8aa204a0bec791b00bd6ceb3682e2a06167900 (diff)
downloadtracifyjs-6a2bda52f332b0c39f1a5f81583cc5c63f437f4c.tar.gz
tracifyjs-6a2bda52f332b0c39f1a5f81583cc5c63f437f4c.zip
workaround bug in ECMAScript specification (#4656)
closes #4655
-rw-r--r--lib/compress.js12
-rw-r--r--test/compress/functions.js27
2 files changed, 34 insertions, 5 deletions
diff --git a/lib/compress.js b/lib/compress.js
index b4f9c3a8..a2a28ab9 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -5017,20 +5017,22 @@ merge(Compressor.prototype, {
return self;
});
- function trim_block(node, in_list) {
+ function trim_block(node, parent, in_list) {
switch (node.body.length) {
case 0:
return in_list ? List.skip : make_node(AST_EmptyStatement, node);
case 1:
var stat = node.body[0];
- if (!(stat instanceof AST_Const || stat instanceof AST_Let)) return stat;
+ if (stat instanceof AST_Const || stat instanceof AST_Let) return node;
+ if (parent instanceof AST_IterationStatement && stat instanceof AST_LambdaDefinition) return node;
+ return stat;
}
return node;
}
OPT(AST_BlockStatement, function(self, compressor) {
self.body = tighten_body(self.body, compressor);
- return trim_block(self);
+ return trim_block(self, compressor.parent());
});
function drop_rest_farg(fn, compressor) {
@@ -6123,7 +6125,7 @@ merge(Compressor.prototype, {
}
}, function(node, in_list) {
if (node instanceof AST_BlockStatement) {
- return trim_block(node, in_list);
+ return trim_block(node, tt.parent(), in_list);
} else if (node instanceof AST_For) {
// Certain combination of unused name + side effect leads to invalid AST:
// https://github.com/mishoo/UglifyJS/issues/44
@@ -7282,7 +7284,7 @@ merge(Compressor.prototype, {
break;
}
}
- self.body = trim_block(self.body);
+ self.body = trim_block(self.body, compressor.parent());
}
if (self.body instanceof AST_EmptyStatement) return make_node(AST_For, self, self).optimize(compressor);
if (self.body instanceof AST_SimpleStatement) return make_node(AST_For, self, {
diff --git a/test/compress/functions.js b/test/compress/functions.js
index 77dd256e..7e2b45db 100644
--- a/test/compress/functions.js
+++ b/test/compress/functions.js
@@ -5409,3 +5409,30 @@ issue_4612_4: {
}
expect_stdout: "undefined"
}
+
+issue_4655: {
+ options = {
+ functions: true,
+ loops: true,
+ reduce_vars: true,
+ side_effects: true,
+ unused: true,
+ }
+ input: {
+ (function f() {
+ while (console.log("PASS")) {
+ var g = function() {};
+ for (var a in g)
+ g();
+ }
+ })();
+ }
+ expect: {
+ (function() {
+ for (; console.log("PASS");) {
+ function g() {};
+ }
+ })();
+ }
+ expect_stdout: "PASS"
+}