Skip to content

Commit ec4558b

Browse files
authored
fix corner cases with parameter scope (#5567)
fixes #5566
1 parent 685ab35 commit ec4558b

File tree

3 files changed

+193
-35
lines changed

3 files changed

+193
-35
lines changed

lib/compress.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2647,16 +2647,17 @@ Compressor.prototype.compress = function(node) {
26472647
arg = null;
26482648
return true;
26492649
}
2650-
if (node instanceof AST_ObjectIdentity && (fn_strict || !arg_scope)) {
2651-
arg = null;
2650+
if (node instanceof AST_ObjectIdentity) {
2651+
if (fn_strict || !arg_scope) arg = null;
26522652
return true;
26532653
}
2654-
if (node instanceof AST_SymbolRef && fn.variables.has(node.name)) {
2655-
var s = node.definition().scope;
2656-
if (s !== scope) while (s = s.parent_scope) {
2657-
if (s === scope) return true;
2654+
if (node instanceof AST_SymbolRef) {
2655+
var def;
2656+
if (node.in_arg && !is_safe_lexical(node.definition())
2657+
|| (def = fn.variables.get(node.name)) && def !== node.definition()) {
2658+
arg = null;
26582659
}
2659-
arg = null;
2660+
return true;
26602661
}
26612662
if (node instanceof AST_Scope && !is_arrow(node)) {
26622663
var save_scope = arg_scope;
@@ -5985,7 +5986,7 @@ Compressor.prototype.compress = function(node) {
59855986
return true;
59865987
}
59875988
if (node instanceof AST_SymbolRef) {
5988-
if (self.inlined || node.redef) {
5989+
if (self.inlined || node.redef || node.in_arg) {
59895990
result = false;
59905991
return true;
59915992
}
@@ -7679,6 +7680,7 @@ Compressor.prototype.compress = function(node) {
76797680
var def = node.left.definition();
76807681
if (def.scope.resolve() === self) assignments.add(def.id, node);
76817682
}
7683+
if (node instanceof AST_SymbolRef && node.in_arg) var_defs[node.definition().id] = 0;
76827684
if (node instanceof AST_Unary && node.expression instanceof AST_SymbolRef) {
76837685
var def = node.expression.definition();
76847686
if (def.scope.resolve() === self) assignments.add(def.id, node);
@@ -8172,7 +8174,9 @@ Compressor.prototype.compress = function(node) {
81728174
// collect only vars which don't show up in self's arguments list
81738175
var defns = [];
81748176
if (self instanceof AST_Lambda) self.each_argname(function(argname) {
8175-
vars.del(argname.name);
8177+
if (all(argname.definition().references, function(ref) {
8178+
return !ref.in_arg;
8179+
})) vars.del(argname.name);
81768180
});
81778181
vars.each(function(defn, name) {
81788182
defn = defn.clone();

test/compress/default-values.js

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,26 @@ collapse_arg_sequence: {
180180
node_version: ">=6"
181181
}
182182

183+
collapse_in_arg: {
184+
options = {
185+
collapse_vars: true,
186+
keep_fargs: false,
187+
unused: true,
188+
}
189+
input: {
190+
(function(a, b = a) {
191+
b("PASS");
192+
})(console.log);
193+
}
194+
expect: {
195+
(function(a) {
196+
a("PASS");
197+
})(console.log);
198+
}
199+
expect_stdout: "PASS"
200+
node_version: ">=6"
201+
}
202+
183203
collapse_value_1: {
184204
options = {
185205
collapse_vars: true,
@@ -2872,3 +2892,127 @@ issue_5536: {
28722892
expect_stdout: "undefined"
28732893
node_version: ">=6"
28742894
}
2895+
2896+
issue_5566_1: {
2897+
options = {
2898+
unused: true,
2899+
}
2900+
input: {
2901+
(function(a, f = function() {
2902+
return a;
2903+
}) {
2904+
var a = "foo";
2905+
console.log(a, f());
2906+
})("bar");
2907+
}
2908+
expect: {
2909+
(function(a, f = function() {
2910+
return a;
2911+
}) {
2912+
var a = "foo";
2913+
console.log(a, f());
2914+
})("bar");
2915+
}
2916+
expect_stdout: "foo bar"
2917+
node_version: ">=6"
2918+
}
2919+
2920+
issue_5566_2: {
2921+
options = {
2922+
inline: true,
2923+
reduce_vars: true,
2924+
}
2925+
input: {
2926+
(function(a, f = function() {
2927+
return a;
2928+
}) {
2929+
function a() {}
2930+
console.log(typeof a, typeof f());
2931+
})(42);
2932+
}
2933+
expect: {
2934+
(function(a, f = function() {
2935+
return a;
2936+
}) {
2937+
function a() {}
2938+
console.log(typeof a, typeof f());
2939+
})(42);
2940+
}
2941+
expect_stdout: "function number"
2942+
node_version: ">=6"
2943+
}
2944+
2945+
issue_5566_3: {
2946+
options = {
2947+
reduce_vars: true,
2948+
unused: true,
2949+
}
2950+
input: {
2951+
(function(a, f = function() {
2952+
return a;
2953+
}) {
2954+
function a() {}
2955+
console.log(typeof a, typeof f());
2956+
})(42);
2957+
}
2958+
expect: {
2959+
(function(a, f = function() {
2960+
return a;
2961+
}) {
2962+
function a() {}
2963+
console.log(typeof a, typeof f());
2964+
})(42);
2965+
}
2966+
expect_stdout: "function number"
2967+
node_version: ">=6"
2968+
}
2969+
2970+
issue_5566_4: {
2971+
options = {
2972+
collapse_vars: true,
2973+
unused: true,
2974+
}
2975+
input: {
2976+
(function(a, b = function() {
2977+
return a;
2978+
}) {
2979+
var a = 0;
2980+
b()("PASS");
2981+
})(console.log);
2982+
}
2983+
expect: {
2984+
(function(a, b = function() {
2985+
return a;
2986+
}) {
2987+
var a = 0;
2988+
b()("PASS");
2989+
})(console.log);
2990+
}
2991+
expect_stdout: "PASS"
2992+
node_version: ">=6"
2993+
}
2994+
2995+
issue_5566_5: {
2996+
options = {
2997+
hoist_vars: true,
2998+
}
2999+
input: {
3000+
(function(a, f = function() {
3001+
return a;
3002+
}) {
3003+
var a = "foo";
3004+
var b;
3005+
console.log(a, f());
3006+
})("bar");
3007+
}
3008+
expect: {
3009+
(function(a, f = function() {
3010+
return a;
3011+
}) {
3012+
var b, a = "foo";
3013+
console.log(a, f());
3014+
})("bar");
3015+
}
3016+
expect_stdout: "foo bar"
3017+
node_version: ">=6"
3018+
}

test/reduce.js

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -241,23 +241,6 @@ module.exports = function reduce_test(testcase, minify_options, reduce_options)
241241
CHANGED = true;
242242
return node.name;
243243
}
244-
else if (node instanceof U.AST_DestructuredArray) {
245-
var expr = node.elements[0];
246-
if (expr && !(expr instanceof U.AST_Hole)) {
247-
node.start._permute++;
248-
CHANGED = true;
249-
return expr;
250-
}
251-
}
252-
else if (node instanceof U.AST_DestructuredObject) {
253-
// first property's value
254-
var expr = node.properties[0];
255-
if (expr) {
256-
node.start._permute++;
257-
CHANGED = true;
258-
return expr.value;
259-
}
260-
}
261244
else if (node instanceof U.AST_Defun) {
262245
switch (((node.start._permute += step) * steps | 0) % 2) {
263246
case 0:
@@ -275,6 +258,23 @@ module.exports = function reduce_test(testcase, minify_options, reduce_options)
275258
}
276259
}
277260
}
261+
else if (node instanceof U.AST_DestructuredArray) {
262+
var expr = node.elements[0];
263+
if (expr && !(expr instanceof U.AST_Hole)) {
264+
node.start._permute++;
265+
CHANGED = true;
266+
return expr;
267+
}
268+
}
269+
else if (node instanceof U.AST_DestructuredObject) {
270+
// first property's value
271+
var expr = node.properties[0];
272+
if (expr) {
273+
node.start._permute++;
274+
CHANGED = true;
275+
return expr.value;
276+
}
277+
}
278278
else if (node instanceof U.AST_DWLoop) {
279279
var expr = [
280280
node.condition,
@@ -296,6 +296,16 @@ module.exports = function reduce_test(testcase, minify_options, reduce_options)
296296
return to_statement(expr);
297297
}
298298
}
299+
else if (node instanceof U.AST_ExportDeclaration) {
300+
node.start._permute++;
301+
CHANGED = true;
302+
return node.body;
303+
}
304+
else if (node instanceof U.AST_ExportDefault) {
305+
node.start._permute++;
306+
CHANGED = true;
307+
return to_statement(node.body);
308+
}
299309
else if (node instanceof U.AST_Finally) {
300310
// drop finally block
301311
node.start._permute++;
@@ -351,6 +361,15 @@ module.exports = function reduce_test(testcase, minify_options, reduce_options)
351361
return to_statement(expr);
352362
}
353363
}
364+
else if (node instanceof U.AST_LabeledStatement) {
365+
if (node.body instanceof U.AST_Statement
366+
&& !has_loopcontrol(node.body, node.body, node)) {
367+
// replace labelled statement with its non-labelled body
368+
node.start._permute = REPLACEMENTS.length;
369+
CHANGED = true;
370+
return node.body;
371+
}
372+
}
354373
else if (node instanceof U.AST_Object) {
355374
// first property's value
356375
var expr = node.properties[0];
@@ -441,15 +460,6 @@ module.exports = function reduce_test(testcase, minify_options, reduce_options)
441460
return to_statement(node.definitions[0].value);
442461
}
443462
}
444-
else if (node instanceof U.AST_LabeledStatement) {
445-
if (node.body instanceof U.AST_Statement
446-
&& !has_loopcontrol(node.body, node.body, node)) {
447-
// replace labelled statement with its non-labelled body
448-
node.start._permute = REPLACEMENTS.length;
449-
CHANGED = true;
450-
return node.body;
451-
}
452-
}
453463

454464
if (in_list) {
455465
// drop switch branches

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy