Skip to content

Commit 6a26b75

Browse files
fix(javascript): check an aliased parameter for closures in a loop
A parameter read as its argument skipped the list of names the loop check walks, so a closure made in a loop could have read a later iteration's value. It now joins that list like every other parameter.
1 parent bef798c commit 6a26b75

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

‎lib/javascript/syntax-printer.js‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33125,7 +33125,6 @@ const createInline = (modules) => {
3312533125
) {
3312633126
// Read in place, the argument needs no `var` holding a copy.
3312733127
(state.aliases || (state.aliases = [])).push(arg);
33128-
continue;
3312933128
}
3313033129
if (inLoop) inLoop.push(arg.definition);
3313133130
}

‎test/unitCases/syntax-printer.unittest.js‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8497,6 +8497,24 @@ describe("syntax-printer", () => {
84978497
}
84988498
});
84998499

8500+
it("should keep each closure a function called in place in a loop makes reading its own argument", async () => {
8501+
const { minify } = await load();
8502+
for (const input of [
8503+
"function g() { var out = []; for (var i = 0; i < 2; i++) { var b = [i]; console.log(b); (function (a) { out.push(function () { return a; }); })(b); } return out.map(function (f) { return f(); }); } console.log(g());",
8504+
"function g(n) { var out = []; while (n--) { var b = n * 2; console.log(b, b); (function (a) { out.push(() => a); })(b); } return out.map((f) => f()); } console.log(g(2));"
8505+
]) {
8506+
for (const options of [
8507+
{ compress: { passes: 2 }, mangle: false },
8508+
{ toplevel: true, compress: { passes: 2 } }
8509+
]) {
8510+
const { code } = await minify(input, options);
8511+
expect(runProgram(/** @type {string} */ (code))).toBe(
8512+
runProgram(input)
8513+
);
8514+
}
8515+
}
8516+
});
8517+
85008518
it("should keep `arguments.length` tested, as an index past it may read a getter `Object.prototype` holds", async () => {
85018519
const { minify } = await load();
85028520
for (const input of [

0 commit comments

Comments
 (0)