aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2021-07-07 03:29:16 +0100
committerGitHub <noreply@github.com>2021-07-07 10:29:16 +0800
commit0c48b84540d0a19c41ecdb48c2ad5ea4c31c5acc (patch)
treea26bb4d04e86cb3b5f56653db2b971b22939f5ef
parent1fefe3f1d138f2a1dd9386e02c58f60635632611 (diff)
downloadtracifyjs-0c48b84540d0a19c41ecdb48c2ad5ea4c31c5acc.tar.gz
tracifyjs-0c48b84540d0a19c41ecdb48c2ad5ea4c31c5acc.zip
fix corner cases in `inline` & `unused` (#5058)
fixes #5057
-rw-r--r--lib/compress.js12
-rw-r--r--test/compress/default-values.js77
2 files changed, 83 insertions, 6 deletions
diff --git a/lib/compress.js b/lib/compress.js
index 75b1c8ab..de74f56c 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -3643,7 +3643,7 @@ merge(Compressor.prototype, {
stat.init = stat.init.definitions[0].name.convert_symbol(AST_SymbolRef, function(ref, name) {
defns.push(make_node(AST_VarDef, name, {
name: name,
- value: null
+ value: null,
}));
name.definition().references.push(ref);
});
@@ -7007,6 +7007,7 @@ merge(Compressor.prototype, {
var value = node.value.drop_side_effect_free(compressor);
if (!value) return null;
log(node.name, "Side effects in default value of unused variable {name}");
+ node.name.__unused = null;
node.value = value;
}
return node;
@@ -9646,7 +9647,6 @@ merge(Compressor.prototype, {
fn.each_argname(function(arg) {
if (abort) return;
if (arg.__unused) return;
- if (arg instanceof AST_DefaultValue) arg = arg.name;
if (!safe_to_inject || var_exists(defined, arg.name)) return abort = true;
used[arg.name] = true;
if (in_loop) in_loop.push(arg.definition());
@@ -9720,22 +9720,22 @@ merge(Compressor.prototype, {
function append_var(decls, expressions, name, value) {
var def = name.definition();
- scope.variables.set(name.name, def);
- scope.enclosed.push(def);
if (!scope.var_names()[name.name]) {
scope.var_names()[name.name] = true;
decls.push(make_node(AST_VarDef, name, {
name: name,
- value: null
+ value: null,
}));
}
+ scope.variables.set(name.name, def);
+ scope.enclosed.push(def);
if (!value) return;
var sym = make_node(AST_SymbolRef, name, name);
def.references.push(sym);
expressions.push(make_node(AST_Assign, self, {
operator: "=",
left: sym,
- right: value
+ right: value,
}));
}
diff --git a/test/compress/default-values.js b/test/compress/default-values.js
index 6406d4a2..ec6a4ea5 100644
--- a/test/compress/default-values.js
+++ b/test/compress/default-values.js
@@ -1416,6 +1416,7 @@ issue_4502_1: {
expect: {
(function() {
var a = "PASS";
+ void 0,
console.log(a),
a++,
void 0;
@@ -1439,6 +1440,7 @@ issue_4502_2: {
expect: {
(function() {
var a = "PASS";
+ void 0,
console.log(a),
a++,
void 0;
@@ -1754,3 +1756,78 @@ issue_4994: {
expect_stdout: "PASS"
node_version: ">=6"
}
+
+issue_5057_1: {
+ options = {
+ collapse_vars: true,
+ inline: true,
+ sequences: true,
+ unused: true,
+ }
+ input: {
+ var a = 42;
+ (function() {
+ var b = function(c = (console.log("foo"), b = a)) {
+ a && console.log("bar");
+ }();
+ })();
+ }
+ expect: {
+ var a = 42;
+ console.log("foo"),
+ void (a && console.log("bar"));
+ }
+ expect_stdout: [
+ "foo",
+ "bar",
+ ]
+ node_version: ">=6"
+}
+
+issue_5057_2: {
+ options = {
+ inline: true,
+ unused: true,
+ }
+ input: {
+ (function f(a) {
+ (function(b = console.log("FAIL")) {})(a);
+ })(42);
+ console.log(typeof b);
+ }
+ expect: {
+ (function(a) {
+ [ b = console.log("FAIL") ] = [ a ],
+ void 0;
+ var b;
+ })(42);
+ console.log(typeof b);
+ }
+ expect_stdout: "undefined"
+ node_version: ">=6"
+}
+
+issue_5057_3: {
+ options = {
+ inline: true,
+ unused: true,
+ }
+ input: {
+ (function(a) {
+ (function f(b) {
+ (function(a = console.log("FAIL 1")) {})(b);
+ console.log(a);
+ })("FAIL 2");
+ })("PASS");
+ }
+ expect: {
+ (function(a) {
+ (function(b) {
+ (function(a = console.log("FAIL 1")) {})(b);
+ console.log(a);
+ })("FAIL 2");
+ })("PASS");
+ }
+ expect_stdout: "PASS"
+ node_version: ">=6"
+}