aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2020-05-07 13:53:05 +0100
committerGitHub <noreply@github.com>2020-05-07 20:53:05 +0800
commit88985a46ed771f312f799853740ec2ba4f87ea03 (patch)
treed30b9c78a267aaf7ffeb26a8b1f210e3b2f054c5
parent34ead0430bf24323495f47751e726dcc6bd8d5e7 (diff)
downloadtracifyjs-88985a46ed771f312f799853740ec2ba4f87ea03.tar.gz
tracifyjs-88985a46ed771f312f799853740ec2ba4f87ea03.zip
fix corner case in `inline` (#3853)
fixes #3852
-rw-r--r--lib/compress.js19
-rw-r--r--test/compress/drop-unused.js2
-rw-r--r--test/compress/functions.js21
-rw-r--r--test/compress/ie8.js2
4 files changed, 35 insertions, 9 deletions
diff --git a/lib/compress.js b/lib/compress.js
index 249b551a..ffdd5ccc 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -4179,7 +4179,7 @@ merge(Compressor.prototype, {
});
}
var drop_fn_name = compressor.option("keep_fnames") ? return_false : compressor.option("ie8") ? function(def) {
- return !compressor.exposed(def) && !def.references.length;
+ return !compressor.exposed(def) && def.references.length == def.replaced;
} : function(def) {
// any declarations with same name will overshadow
// name of this anonymous function and can therefore
@@ -4733,7 +4733,7 @@ merge(Compressor.prototype, {
}
function all_bool(def, bool_returns, compressor) {
- return def.bool_fn + (bool_returns[def.id] || 0) === def.references.length
+ return def.bool_fn + (bool_returns[def.id] || 0) === def.references.length - def.replaced
&& !compressor.exposed(def);
}
@@ -4919,7 +4919,7 @@ merge(Compressor.prototype, {
if (def.assignments != count) return;
if (def.direct_access) return;
if (def.escaped.depth == 1) return;
- if (def.references.length == count) return;
+ if (def.references.length - def.replaced == count) return;
if (def.single_use) return;
if (top_retain(def)) return;
if (sym.fixed_value() !== right) return;
@@ -5009,7 +5009,11 @@ merge(Compressor.prototype, {
exprs = trim(exprs, compressor, first_in_statement);
return exprs && make_sequence(this, exprs);
}
- if (exp instanceof AST_Function && (!exp.name || !exp.name.definition().references.length)) {
+ if (exp instanceof AST_Function) {
+ if (exp.name) {
+ var def = exp.name.definition();
+ if (def.references.length > def.replaced) return this;
+ }
exp.process_expression(false, function(node) {
var value = node.value && node.value.drop_side_effect_free(compressor, true);
return value ? make_node(AST_SimpleStatement, node, {
@@ -6177,7 +6181,7 @@ merge(Compressor.prototype, {
if (best_of(compressor, self, node) === node) return node;
}
var scope, in_loop, level = -1;
- if ((exp === fn || compressor.option("unused") && exp.definition().references.length == 1)
+ if ((exp === fn || compressor.option("unused") && def.references.length - def.replaced == 1)
&& can_inject_symbols()) {
fn._squeezed = true;
if (exp !== fn) fn.parent_scope = exp.scope;
@@ -6261,7 +6265,7 @@ merge(Compressor.prototype, {
if (var_assigned) return;
if (compressor.option("inline") < 2 && fn.argnames.length) return;
if (!fn.variables.all(function(def) {
- return def.references.length < 2 && def.orig[0] instanceof AST_SymbolFunarg;
+ return def.references.length - def.replaced < 2 && def.orig[0] instanceof AST_SymbolFunarg;
})) return;
var abort = false;
var begin;
@@ -7515,7 +7519,8 @@ merge(Compressor.prototype, {
}
var name_length = def.name.length;
if (compressor.option("unused") && !compressor.exposed(def)) {
- name_length += (name_length + 2 + value_length) / (def.references.length - def.assignments);
+ var referenced = def.references.length - def.replaced;
+ name_length += (name_length + 2 + value_length) / (referenced - def.assignments);
}
var delta = value_length - Math.floor(name_length);
def.should_replace = delta < compressor.eval_threshold ? fn : false;
diff --git a/test/compress/drop-unused.js b/test/compress/drop-unused.js
index d7d92bb2..ff230ae2 100644
--- a/test/compress/drop-unused.js
+++ b/test/compress/drop-unused.js
@@ -2366,7 +2366,7 @@ function_parameter_ie8: {
}
expect: {
(function() {
- (function f() {
+ (function() {
console.log("PASS");
})();
})();
diff --git a/test/compress/functions.js b/test/compress/functions.js
index 439c073a..ae113c84 100644
--- a/test/compress/functions.js
+++ b/test/compress/functions.js
@@ -4660,3 +4660,24 @@ issue_3836: {
}
expect_stdout: "PASS"
}
+
+issue_3852: {
+ options = {
+ collapse_vars: true,
+ inline: true,
+ unused: true,
+ }
+ input: {
+ console.log(function(a) {
+ return function(b) {
+ return b && (b[0] = 0), "PASS";
+ }(a);
+ }(42));
+ }
+ expect: {
+ console.log(function(a) {
+ return a && (a[0] = 0), "PASS";
+ }(42));
+ }
+ expect_stdout: "PASS"
+}
diff --git a/test/compress/ie8.js b/test/compress/ie8.js
index 0c79ece0..53f78f1b 100644
--- a/test/compress/ie8.js
+++ b/test/compress/ie8.js
@@ -2389,7 +2389,7 @@ issue_3703: {
var a = "PASS";
(function() {
var b;
- var c = function g() {
+ var c = function() {
a = "FAIL";
};
a ? b |= c : b.p;