Skip to content

Commit

Permalink
improve handling of declaration statements (#4980)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl committed May 30, 2021
1 parent 55a230d commit 06e3dbc
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 8 deletions.
25 changes: 18 additions & 7 deletions lib/compress.js
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/compress/functions.js
Expand Up @@ -5236,7 +5236,7 @@ issue_4265: {
expect: {
function f() {
return console, function() {
return console.log(a);
console.log(a);
var a;
}(), 0;
}
Expand Down
108 changes: 108 additions & 0 deletions test/compress/let.js
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 06e3dbc

Please sign in to comment.