Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions internal/transformers/moduletransforms/commonjsmodule.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
Original file line number Diff line number Diff line change
@@ -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 = {}));
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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;
}