diff --git a/internal/transformers/moduletransforms/commonjsmodule.go b/internal/transformers/moduletransforms/commonjsmodule.go index 0fd744bfb9a..cc716a425f6 100644 --- a/internal/transformers/moduletransforms/commonjsmodule.go +++ b/internal/transformers/moduletransforms/commonjsmodule.go @@ -1120,6 +1120,8 @@ func (tx *CommonJSModuleTransformer) transformInitializedVariable(node *ast.Vari // re-aliased or multi-exported names (where native destructuring cannot update all // targets) does `visitDestructuringAssignment` fall back to flattening. assignment := transformers.ConvertVariableDeclarationToAssignmentExpression(tx.EmitContext(), node) + grandparentNode := tx.pushNode(assignment) + defer tx.popNode(grandparentNode) return tx.visitDestructuringAssignment(assignment.AsBinaryExpression(), true /*valueIsDiscarded*/) } propertyAccess := tx.Factory().NewPropertyAccessExpression( diff --git a/testdata/baselines/reference/compiler/commonjsExportDestructuringImportedValue.js b/testdata/baselines/reference/compiler/commonjsExportDestructuringImportedValue.js new file mode 100644 index 00000000000..3770a97b643 --- /dev/null +++ b/testdata/baselines/reference/compiler/commonjsExportDestructuringImportedValue.js @@ -0,0 +1,77 @@ +//// [tests/cases/compiler/commonjsExportDestructuringImportedValue.ts] //// + +//// [enum.ts] +export class CodePriceType { + static A = "a"; + static B = "b"; +} +export default CodePriceType; +export const pair = ["a", "b"]; +export const fallback = "fallback"; + +//// [repro.ts] +import { CodePriceType } from "./enum"; +export const { A, B } = CodePriceType; + +//// [arrayPattern.ts] +import { pair } from "./enum"; +export const [ArrayA, ArrayB] = pair; + +//// [aliasedNamedObject.ts] +import { CodePriceType as PriceType } from "./enum"; +export const { A: AliasA, B: AliasB } = PriceType; + +//// [defaultObject.ts] +import CodePriceType from "./enum"; +export const { A: DefaultA, B: DefaultB } = CodePriceType; + +//// [defaultInitializer.ts] +import { CodePriceType, fallback } from "./enum"; +export const { Missing = fallback } = CodePriceType as any; + + +//// [enum.js] +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.fallback = exports.pair = exports.CodePriceType = void 0; +class CodePriceType { + static A = "a"; + static B = "b"; +} +exports.CodePriceType = CodePriceType; +exports.default = CodePriceType; +exports.pair = ["a", "b"]; +exports.fallback = "fallback"; +//// [repro.js] +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.B = exports.A = void 0; +const enum_1 = require("./enum"); +({ A: exports.A, B: exports.B } = enum_1.CodePriceType); +//// [arrayPattern.js] +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.ArrayB = exports.ArrayA = void 0; +const enum_1 = require("./enum"); +[exports.ArrayA, exports.ArrayB] = enum_1.pair; +//// [aliasedNamedObject.js] +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.AliasB = exports.AliasA = void 0; +const enum_1 = require("./enum"); +({ A: exports.AliasA, B: exports.AliasB } = enum_1.CodePriceType); +//// [defaultObject.js] +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.DefaultB = exports.DefaultA = void 0; +const enum_1 = __importDefault(require("./enum")); +({ A: exports.DefaultA, B: exports.DefaultB } = enum_1.default); +//// [defaultInitializer.js] +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.Missing = void 0; +const enum_1 = require("./enum"); +({ Missing: exports.Missing = enum_1.fallback } = enum_1.CodePriceType); diff --git a/testdata/baselines/reference/compiler/namespaceExportDestructuringReferences.js b/testdata/baselines/reference/compiler/namespaceExportDestructuringReferences.js new file mode 100644 index 00000000000..3a0f8092fad --- /dev/null +++ b/testdata/baselines/reference/compiler/namespaceExportDestructuringReferences.js @@ -0,0 +1,24 @@ +//// [tests/cases/compiler/namespaceExportDestructuringReferences.ts] //// + +//// [namespaceExportDestructuringReferences.ts] +namespace N { + export const key = "a"; + export const source: any = { a: 1, b: undefined, pair: [3, 4] }; + export const fallback = 2; + + export const { [key]: computed, b = fallback } = source; + export const [x, y] = source.pair; +} + + +//// [namespaceExportDestructuringReferences.js] +"use strict"; +var N; +(function (N) { + var _a, _b, _c, _d; + N.key = "a"; + N.source = { a: 1, b: undefined, pair: [3, 4] }; + N.fallback = 2; + _a = N.source, _b = N.key, N.computed = _a[_b], _c = _a.b, N.b = _c === void 0 ? N.fallback : _c; + _d = N.source.pair, N.x = _d[0], N.y = _d[1]; +})(N || (N = {})); diff --git a/testdata/tests/cases/compiler/commonjsExportDestructuringImportedValue.ts b/testdata/tests/cases/compiler/commonjsExportDestructuringImportedValue.ts new file mode 100644 index 00000000000..3434be72390 --- /dev/null +++ b/testdata/tests/cases/compiler/commonjsExportDestructuringImportedValue.ts @@ -0,0 +1,32 @@ +// @target: es2022 +// @module: commonjs +// @noTypesAndSymbols: true + +// @filename: /enum.ts +export class CodePriceType { + static A = "a"; + static B = "b"; +} +export default CodePriceType; +export const pair = ["a", "b"]; +export const fallback = "fallback"; + +// @filename: /repro.ts +import { CodePriceType } from "./enum"; +export const { A, B } = CodePriceType; + +// @filename: /arrayPattern.ts +import { pair } from "./enum"; +export const [ArrayA, ArrayB] = pair; + +// @filename: /aliasedNamedObject.ts +import { CodePriceType as PriceType } from "./enum"; +export const { A: AliasA, B: AliasB } = PriceType; + +// @filename: /defaultObject.ts +import CodePriceType from "./enum"; +export const { A: DefaultA, B: DefaultB } = CodePriceType; + +// @filename: /defaultInitializer.ts +import { CodePriceType, fallback } from "./enum"; +export const { Missing = fallback } = CodePriceType as any; diff --git a/testdata/tests/cases/compiler/namespaceExportDestructuringReferences.ts b/testdata/tests/cases/compiler/namespaceExportDestructuringReferences.ts new file mode 100644 index 00000000000..282e0195201 --- /dev/null +++ b/testdata/tests/cases/compiler/namespaceExportDestructuringReferences.ts @@ -0,0 +1,11 @@ +// @target: es2022 +// @noTypesAndSymbols: true + +namespace N { + export const key = "a"; + export const source: any = { a: 1, b: undefined, pair: [3, 4] }; + export const fallback = 2; + + export const { [key]: computed, b = fallback } = source; + export const [x, y] = source.pair; +}