Skip to content

Commit d9344f3

Browse files
authored
disallow parameter substitution for named IIFEs (#1596)
Self-referenced function has non-fixed values assigned to its parameters. Let `unused` & `!keep_fnames` do the scanning, then apply `reduce_vars` only to unnamed functions. fixes #1595
1 parent be80f7e commit d9344f3

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

lib/compress.js

+1
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ merge(Compressor.prototype, {
279279
}
280280
var iife;
281281
if (node instanceof AST_Function
282+
&& !node.name
282283
&& (iife = tw.parent()) instanceof AST_Call
283284
&& iife.expression === node) {
284285
// Virtually turn IIFE parameters into variable definitions:

test/compress/reduce_vars.js

+75
Original file line numberDiff line numberDiff line change
@@ -1252,3 +1252,78 @@ iife_func_side_effects: {
12521252
})(x(), 0, z());
12531253
}
12541254
}
1255+
1256+
issue_1595_1: {
1257+
options = {
1258+
evaluate: true,
1259+
reduce_vars: true,
1260+
unused: true,
1261+
}
1262+
input: {
1263+
(function f(a) {
1264+
return f(a + 1);
1265+
})(2);
1266+
}
1267+
expect: {
1268+
(function f(a) {
1269+
return f(a + 1);
1270+
})(2);
1271+
}
1272+
}
1273+
1274+
issue_1595_2: {
1275+
options = {
1276+
evaluate: true,
1277+
reduce_vars: true,
1278+
unused: true,
1279+
}
1280+
input: {
1281+
(function f(a) {
1282+
return g(a + 1);
1283+
})(2);
1284+
}
1285+
expect: {
1286+
(function(a) {
1287+
return g(a + 1);
1288+
})(2);
1289+
}
1290+
}
1291+
1292+
issue_1595_3: {
1293+
options = {
1294+
evaluate: true,
1295+
passes: 2,
1296+
reduce_vars: true,
1297+
unused: true,
1298+
}
1299+
input: {
1300+
(function f(a) {
1301+
return g(a + 1);
1302+
})(2);
1303+
}
1304+
expect: {
1305+
(function(a) {
1306+
return g(3);
1307+
})();
1308+
}
1309+
}
1310+
1311+
issue_1595_4: {
1312+
options = {
1313+
evaluate: true,
1314+
reduce_vars: true,
1315+
unused: true,
1316+
}
1317+
input: {
1318+
(function iife(a, b, c) {
1319+
console.log(a, b, c);
1320+
if (a) iife(a - 1, b, c);
1321+
})(3, 4, 5);
1322+
}
1323+
expect: {
1324+
(function iife(a, b, c) {
1325+
console.log(a, b, c);
1326+
if (a) iife(a - 1, b, c);
1327+
})(3, 4, 5);
1328+
}
1329+
}

0 commit comments

Comments
 (0)