improve handling of declaration statements (#4980)

This commit is contained in:
Alex Lam S.L
2021-05-30 09:32:48 +01:00
committed by GitHub
parent 55a230daa8
commit 06e3dbc089
3 changed files with 127 additions and 8 deletions

View File

@@ -325,9 +325,12 @@ merge(Compressor.prototype, {
} else if (insert === "awaits" && node instanceof AST_Try) {
if (node.bfinally) return node;
}
var index = node.body.length - 1;
if (index >= 0) {
node.body[index] = node.body[index].transform(tt);
for (var index = node.body.length; --index >= 0;) {
var stat = node.body[index];
if (!is_declaration(stat, true)) {
node.body[index] = stat.transform(tt);
break;
}
}
} else if (node instanceof AST_If) {
node.body = node.body.transform(tt);
@@ -1695,8 +1698,16 @@ merge(Compressor.prototype, {
});
}
function is_declaration(stat) {
return stat instanceof AST_Defun || stat instanceof AST_Var && declarations_only(stat);
function is_declaration(stat, lexical) {
if (stat instanceof AST_DefClass) return lexical && !stat.extends && all(stat.properties, function(prop) {
if (prop.key instanceof AST_Node) return false;
if (prop instanceof AST_ClassField && prop.static && prop.value) return false;
return true;
});
if (stat instanceof AST_Definitions) return (lexical || stat instanceof AST_Var) && declarations_only(stat);
if (stat instanceof AST_ExportDeclaration) return is_declaration(stat.body, lexical);
if (stat instanceof AST_ExportDefault) return is_declaration(stat.body, lexical);
return stat instanceof AST_LambdaDefinition;
}
function tighten_body(statements, compressor) {
@@ -3157,7 +3168,7 @@ merge(Compressor.prototype, {
var index = body.lastIndexOf(stat);
if (index < 0) return false;
while (++index < body.length) {
if (!is_declaration(body[index])) return false;
if (!is_declaration(body[index], true)) return false;
}
return true;
}
@@ -7970,7 +7981,7 @@ merge(Compressor.prototype, {
self.condition,
]);
body.splice(i, 1);
} else if (!is_declaration(stat)) {
} else if (!is_declaration(stat, true)) {
break;
}
}

View File

@@ -5236,7 +5236,7 @@ issue_4265: {
expect: {
function f() {
return console, function() {
return console.log(a);
console.log(a);
var a;
}(), 0;
}

View File

@@ -569,6 +569,38 @@ loop_block_2: {
node_version: ">=4"
}
do_break: {
options = {
loops: true,
}
input: {
"use strict";
try {
do {
if (a)
break;
let a;
} while (!console);
} catch (e) {
console.log("PASS");
}
}
expect: {
"use strict";
try {
do {
if (a)
break;
let a;
} while (!console);
} catch (e) {
console.log("PASS");
}
}
expect_stdout: "PASS"
node_version: ">=4"
}
do_continue: {
options = {
loops: true,
@@ -629,6 +661,82 @@ dead_block_after_return: {
node_version: ">=4"
}
if_return_1: {
options = {
if_return: true,
}
input: {
"use strict";
function f(a) {
function g() {
return b = "PASS";
}
if (a)
return g();
let b;
return g();
};
console.log(f());
}
expect: {
"use strict";
function f(a) {
function g() {
return b = "PASS";
}
if (a)
return g();
let b;
return g();
};
console.log(f());
}
expect_stdout: "PASS"
node_version: ">=4"
}
if_return_2: {
options = {
if_return: true,
}
input: {
"use strict";
function f(a) {
function g() {
return b = "FAIL";
}
if (a)
return g();
let b;
return g();
};
try {
console.log(f(42));
} catch (e) {
console.log("PASS");
}
}
expect: {
"use strict";
function f(a) {
function g() {
return b = "FAIL";
}
if (a)
return g();
let b;
return g();
};
try {
console.log(f(42));
} catch (e) {
console.log("PASS");
}
}
expect_stdout: "PASS"
node_version: ">=4"
}
do_if_continue_1: {
options = {
if_return: true,