Skip to content

Commit 993a2b8

Browse files
committed
fix(javascript): keep a sloppy block's function in its block under a let of its name in a catch body
A `catch` body's statements sit in the catch scope with no block of their own, so a `let` there was missed and the function still hoisted. Also covers labeled, `while`, `do` and `with` bodies, arrow, generator and async functions, renamed, defaulted, nested and `for-of` patterns, an inlined function, `toplevel`, and a `let` in an outer function, where the function still hoists.
1 parent ca904cc commit 993a2b8

2 files changed

Lines changed: 45 additions & 4 deletions

File tree

‎lib/javascript/ScopeAnalyzer.js‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2236,10 +2236,14 @@ class MinifierScopeAnalyzer extends ScopeAnalyzer {
22362236
break;
22372237
case "CatchClause":
22382238
// B.3.5 lets a `var` redeclare a plain catch parameter, not a pattern's.
2239+
// The body's statements sit in this scope, with no block of their own.
22392240
if (
2240-
node.param &&
2241-
node.param.type !== "Identifier" &&
2242-
bindsName(node.param, name)
2241+
(node.param &&
2242+
node.param.type !== "Identifier" &&
2243+
bindsName(node.param, name)) ||
2244+
node.body.body.some((statement) =>
2245+
declaresLexically(statement, name)
2246+
)
22432247
) {
22442248
return true;
22452249
}

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

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2318,6 +2318,41 @@ const CORRECTED_CASES = [
23182318
"a sloppy block's function under an array or object pattern's name",
23192319
"var r = []; (function () { { let [, f] = [0, 1]; { function f() {} } } try { f; r.push(\"set\"); } catch (e) { r.push(e.name); } r.push(typeof f); })(); (function () { { let [...f] = []; { function f() {} } } try { f; r.push(\"set\"); } catch (e) { r.push(e.name); } r.push(typeof f); })(); (function () { { let { ...f } = {}; { function f() {} } } try { f; r.push(\"set\"); } catch (e) { r.push(e.name); } r.push(typeof f); })(); (function ([, f]) { { function f() {} } r.push(typeof f); })([0, 1]); console.log(r.join());",
23202320
{ compress: {}, mangle: false }
2321+
],
2322+
[
2323+
"a sloppy block's function under a `let` of its name in a `catch` body",
2324+
"var r = []; (function () { try { throw 1; } catch { let f = 1; { function f() {} } } try { f; r.push(\"set\"); } catch (e) { r.push(e.name); } r.push(typeof f); })(); (function () { try { throw 1; } catch (e) { let f = 1; { function f() {} } } try { f; r.push(\"set\"); } catch (e) { r.push(e.name); } r.push(typeof f); })(); (function () { try { throw {}; } catch ({ e }) { let f = 1; { function f() {} } } try { f; r.push(\"set\"); } catch (e) { r.push(e.name); } r.push(typeof f); })(); console.log(r.join());",
2325+
{ compress: {}, mangle: false }
2326+
],
2327+
[
2328+
"a sloppy block's function under a `let` of its name in a labeled block, a `while` or a `do` body",
2329+
"var r = []; (function () { L: { let f = 1; { function f() {} } } try { f; r.push(\"set\"); } catch (e) { r.push(e.name); } r.push(typeof f); })(); (function () { while (true) { let f = 1; { function f() {} } break; } try { f; r.push(\"set\"); } catch (e) { r.push(e.name); } r.push(typeof f); })(); (function () { do { let f = 1; { function f() {} } } while (false); try { f; r.push(\"set\"); } catch (e) { r.push(e.name); } r.push(typeof f); })(); console.log(r.join());",
2330+
{ compress: {}, mangle: false }
2331+
],
2332+
[
2333+
"a sloppy block's function under a `let` of its name in an arrow, a generator and an async function",
2334+
"var r = []; (() => { { let f = 1; { function f() {} } } try { f; r.push(\"set\"); } catch (e) { r.push(e.name); } r.push(typeof f); })(); function* g() { { let f = 1; { function f() {} } } try { f; r.push(\"set\"); } catch (e) { r.push(e.name); } r.push(typeof f); } g().next(); (async function () { { let f = 1; { function f() {} } } try { f; r.push(\"set\"); } catch (e) { r.push(e.name); } r.push(typeof f); })(); console.log(r.join());",
2335+
{ compress: {}, mangle: false }
2336+
],
2337+
[
2338+
"a sloppy block's function under a renamed, defaulted, nested or `for-of` pattern's name",
2339+
"var r = []; (function () { { let { a: f } = { a: 1 }; { function f() {} } } try { f; r.push(\"set\"); } catch (e) { r.push(e.name); } r.push(typeof f); })(); (function () { { let { f = 1 } = {}; { function f() {} } } try { f; r.push(\"set\"); } catch (e) { r.push(e.name); } r.push(typeof f); })(); (function () { { let { a: [{ f }] } = { a: [{ f: 1 }] }; { function f() {} } } try { f; r.push(\"set\"); } catch (e) { r.push(e.name); } r.push(typeof f); })(); (function () { for (let [f] of [[1]]) { { function f() {} } } try { f; r.push(\"set\"); } catch (e) { r.push(e.name); } r.push(typeof f); })(); console.log(r.join());",
2340+
{ compress: {}, mangle: false }
2341+
],
2342+
[
2343+
"a sloppy block's function under a `let` of its name, in a function inlined at its one call",
2344+
"var r = []; function h() { { let f = 1; { function f() { return 7; } r.push(f()); } r.push(f); } } h(); console.log(r.join());",
2345+
{ compress: { passes: 2 }, mangle: true }
2346+
],
2347+
[
2348+
"a sloppy block's function under a `let` of its name at a script's top level, with `toplevel`",
2349+
"var r = []; { let f = 1; { function f() {} } } try { f; r.push(\"set\"); } catch (e) { r.push(e.name); } r.push(typeof f); console.log(r.join());",
2350+
{ compress: {}, mangle: true, toplevel: true }
2351+
],
2352+
[
2353+
"a sloppy block's function under a `let` of its name in a `with` body",
2354+
"var r = []; (function () { with ({}) { let f = 1; { function f() {} } } try { f; r.push(\"set\"); } catch (e) { r.push(e.name); } r.push(typeof f); })(); console.log(r.join());",
2355+
{ compress: {}, mangle: false }
23212356
]
23222357
];
23232358

@@ -6387,7 +6422,9 @@ describe("syntax-printer", () => {
63876422
"var r = []; (function () { { let f = 1; r.push(f); } { function f() {} } try { f; r.push(\"set\"); } catch (e) { r.push(e.name); } r.push(typeof f); })(); console.log(r.join());",
63886423
"var r = []; (function () { { function f() { let f = 1; return f; } } r.push(f()); })(); console.log(r.join());",
63896424
"var r = []; try { f; r.push(\"set\"); } catch (e) { r.push(e.name); } r.push(typeof f); { function f() {} } try { f; r.push(\"set\"); } catch (e) { r.push(e.name); } r.push(typeof f); console.log(r.join());",
6390-
"var r = []; (function () { for (let g; ; ) { { function f() {} } break; } try { f; r.push(\"set\"); } catch (e) { r.push(e.name); } r.push(typeof f); })(); (function () { for (let g in { a: 1 }) { { function f() {} } } try { f; r.push(\"set\"); } catch (e) { r.push(e.name); } r.push(typeof f); })(); (function () { for (let g of [0]) { { function f() {} } } try { f; r.push(\"set\"); } catch (e) { r.push(e.name); } r.push(typeof f); })(); console.log(r.join());"
6425+
"var r = []; (function () { for (let g; ; ) { { function f() {} } break; } try { f; r.push(\"set\"); } catch (e) { r.push(e.name); } r.push(typeof f); })(); (function () { for (let g in { a: 1 }) { { function f() {} } } try { f; r.push(\"set\"); } catch (e) { r.push(e.name); } r.push(typeof f); })(); (function () { for (let g of [0]) { { function f() {} } } try { f; r.push(\"set\"); } catch (e) { r.push(e.name); } r.push(typeof f); })(); console.log(r.join());",
6426+
"var r = []; (function () { let f = 1; (function () { { function f() {} } r.push(typeof f); })(); r.push(typeof f); })(); console.log(r.join());",
6427+
"var r = []; (function () { { let { g } = { g: 1 }; { function f() {} } } try { f; r.push(\"set\"); } catch (e) { r.push(e.name); } r.push(typeof f); })(); console.log(r.join());"
63916428
]) {
63926429
for (const options of [
63936430
{ compress: {}, mangle: false },

0 commit comments

Comments
 (0)