From a6f1e68da6d394c4566e5aaf50b544cf9d8c0073 Mon Sep 17 00:00:00 2001 From: Oleksandr Tarasiuk Date: Thu, 26 Nov 2026 20:37:08 +0200 Subject: [PATCH 1/2] test(65695): cover CommonJS catch binding references --- .../compiler/commonjsCatchBinding.js | 184 ++++++++++++++++++ .../cases/compiler/commonjsCatchBinding.ts | 76 ++++++++ 2 files changed, 260 insertions(+) create mode 100644 tsc/testdata/baselines/reference/compiler/commonjsCatchBinding.js create mode 100644 tsc/testdata/tests/cases/compiler/commonjsCatchBinding.ts diff --git a/tsc/testdata/baselines/reference/compiler/commonjsCatchBinding.js b/tsc/testdata/baselines/reference/compiler/commonjsCatchBinding.js new file mode 100644 index 0000000000000..92bd057cee5dc --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/commonjsCatchBinding.js @@ -0,0 +1,184 @@ +//// [tests/cases/compiler/commonjsCatchBinding.ts] //// + +//// [a.ts] +export const x = "x"; +export default "y"; + +//// [b.ts] +import y, { x } from "./a"; +export let z = "z"; + +try { + throw {}; +} catch ({ a = x }: any) { + console.log(a); +} + +try { + throw {}; +} catch ({ a = z }: any) { + console.log(a); +} + +try { + throw { a: {} }; +} catch ({ a: { b = y } }: any) { + console.log(b); +} + +try { + throw { x: 1, z: 2 }; +} catch ({ [x]: a, [z]: b }: any) { + console.log(a, b); +} + +try { + throw []; +} catch ([a = x, b = y, c = z]: any) { + console.log(a, b, c); +} + +try { + throw {}; +} catch ({ z = "a", a = z }: any) { + console.log(z, a); +} + +try { + throw "a"; +} catch (x) { + console.log(x); +} + +try { + throw 0; +} catch { + console.log(x); +} + +try { + throw {}; +} catch ({ a = x }: any) { + var w = a; +} +export { w }; + +function f1() { + try { + throw {}; + } catch ({ a = x, b = z }: any) { + console.log(a, b); + } +} +f1(); + + +//// [a.js] +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.x = void 0; +exports.x = "x"; +exports.default = "y"; +//// [b.js] +"use strict"; +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + var desc = Object.getOwnPropertyDescriptor(m, k); + if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { + desc = { enumerable: true, get: function() { return m[k]; } }; + } + Object.defineProperty(o, k2, desc); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { + Object.defineProperty(o, "default", { enumerable: true, value: v }); +}) : function(o, v) { + o["default"] = v; +}); +var __importStar = (this && this.__importStar) || (function () { + var ownKeys = function(o) { + ownKeys = Object.getOwnPropertyNames || function (o) { + var ar = []; + for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; + return ar; + }; + return ownKeys(o); + }; + return function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); + __setModuleDefault(result, mod); + return result; + }; +})(); +Object.defineProperty(exports, "__esModule", { value: true }); +exports.w = exports.z = void 0; +const a_1 = __importStar(require("./a")); +exports.z = "z"; +try { + throw {}; +} +catch ({ a = x }) { + console.log(a); +} +try { + throw {}; +} +catch ({ a = z }) { + console.log(a); +} +try { + throw { a: {} }; +} +catch ({ a: { b = y } }) { + console.log(b); +} +try { + throw { x: 1, z: 2 }; +} +catch ({ [x]: a, [z]: b }) { + console.log(a, b); +} +try { + throw []; +} +catch ([a = x, b = y, c = z]) { + console.log(a, b, c); +} +try { + throw {}; +} +catch ({ z = "a", a = z }) { + console.log(z, a); +} +try { + throw "a"; +} +catch (x) { + console.log(x); +} +try { + throw 0; +} +catch { + console.log(a_1.x); +} +try { + throw {}; +} +catch ({ a = x }) { + var w = a; + exports.w = w; +} +function f1() { + try { + throw {}; + } + catch ({ a = a_1.x, b = exports.z }) { + console.log(a, b); + } +} +f1(); diff --git a/tsc/testdata/tests/cases/compiler/commonjsCatchBinding.ts b/tsc/testdata/tests/cases/compiler/commonjsCatchBinding.ts new file mode 100644 index 0000000000000..37f1465ebd507 --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/commonjsCatchBinding.ts @@ -0,0 +1,76 @@ +// @target: es2022 +// @module: commonjs +// @strict: true +// @noTypesAndSymbols: true + +// @filename: a.ts +export const x = "x"; +export default "y"; + +// @filename: b.ts +import y, { x } from "./a"; +export let z = "z"; + +try { + throw {}; +} catch ({ a = x }: any) { + console.log(a); +} + +try { + throw {}; +} catch ({ a = z }: any) { + console.log(a); +} + +try { + throw { a: {} }; +} catch ({ a: { b = y } }: any) { + console.log(b); +} + +try { + throw { x: 1, z: 2 }; +} catch ({ [x]: a, [z]: b }: any) { + console.log(a, b); +} + +try { + throw []; +} catch ([a = x, b = y, c = z]: any) { + console.log(a, b, c); +} + +try { + throw {}; +} catch ({ z = "a", a = z }: any) { + console.log(z, a); +} + +try { + throw "a"; +} catch (x) { + console.log(x); +} + +try { + throw 0; +} catch { + console.log(x); +} + +try { + throw {}; +} catch ({ a = x }: any) { + var w = a; +} +export { w }; + +function f1() { + try { + throw {}; + } catch ({ a = x, b = z }: any) { + console.log(a, b); + } +} +f1(); From 04657358d3a331a1b723c40a83ea41403868d4a0 Mon Sep 17 00:00:00 2001 From: Oleksandr Tarasiuk Date: Thu, 26 Nov 2026 20:39:49 +0200 Subject: [PATCH 2/2] fix(65695): rewrite references in CommonJS catch bindings --- .../transformers/moduletransforms/commonjsmodule.go | 2 +- .../reference/compiler/commonjsCatchBinding.js | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/tsc/internal/transformers/moduletransforms/commonjsmodule.go b/tsc/internal/transformers/moduletransforms/commonjsmodule.go index 8def010365911..d49e6d70e2564 100644 --- a/tsc/internal/transformers/moduletransforms/commonjsmodule.go +++ b/tsc/internal/transformers/moduletransforms/commonjsmodule.go @@ -1330,7 +1330,7 @@ func (tx *CommonJSModuleTransformer) visitTopLevelNestedTryStatement(node *ast.T func (tx *CommonJSModuleTransformer) visitTopLevelNestedCatchClause(node *ast.CatchClause) *ast.Node { return tx.Factory().UpdateCatchClause( node, - node.VariableDeclaration, + tx.Visitor().VisitNode(node.VariableDeclaration), tx.topLevelNestedVisitor.VisitNode(node.Block), ) } diff --git a/tsc/testdata/baselines/reference/compiler/commonjsCatchBinding.js b/tsc/testdata/baselines/reference/compiler/commonjsCatchBinding.js index 92bd057cee5dc..8d666459dd64d 100644 --- a/tsc/testdata/baselines/reference/compiler/commonjsCatchBinding.js +++ b/tsc/testdata/baselines/reference/compiler/commonjsCatchBinding.js @@ -121,31 +121,31 @@ exports.z = "z"; try { throw {}; } -catch ({ a = x }) { +catch ({ a = a_1.x }) { console.log(a); } try { throw {}; } -catch ({ a = z }) { +catch ({ a = exports.z }) { console.log(a); } try { throw { a: {} }; } -catch ({ a: { b = y } }) { +catch ({ a: { b = a_1.default } }) { console.log(b); } try { throw { x: 1, z: 2 }; } -catch ({ [x]: a, [z]: b }) { +catch ({ [a_1.x]: a, [exports.z]: b }) { console.log(a, b); } try { throw []; } -catch ([a = x, b = y, c = z]) { +catch ([a = a_1.x, b = a_1.default, c = exports.z]) { console.log(a, b, c); } try { @@ -169,7 +169,7 @@ catch { try { throw {}; } -catch ({ a = x }) { +catch ({ a = a_1.x }) { var w = a; exports.w = w; }