Skip to content

Commit 90c540a

Browse files
Copilotjakebailey
andauthored
Fix CommonJS rewrites in shorthand destructuring defaults (#4111)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: jakebailey <5341706+jakebailey@users.noreply.github.com>
1 parent 336f59c commit 90c540a

3 files changed

Lines changed: 39 additions & 0 deletions

File tree

internal/transformers/utilities.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ func IsIdentifierReference(name *ast.IdentifierNode, parent *ast.Node) bool {
7777
ast.KindJsxAttribute:
7878
// only an `Initializer()` child that can be `Identifier` would be an instance of `IdentifierReference`
7979
return parent.Initializer() == name
80+
case ast.KindShorthandPropertyAssignment:
81+
return parent.AsShorthandPropertyAssignment().ObjectAssignmentInitializer == name
8082
case ast.KindForStatement:
8183
return parent.Initializer() == name ||
8284
parent.AsForStatement().Condition == name ||
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//// [tests/cases/compiler/commonjsShorthandDestructuringDefaultImportExport.ts] ////
2+
3+
//// [a.ts]
4+
import { imported } from "./b";
5+
export const exported = 2;
6+
let a, b;
7+
({ a = imported, b = exported } = {});
8+
9+
//// [b.ts]
10+
export const imported = 1;
11+
12+
13+
//// [b.js]
14+
"use strict";
15+
Object.defineProperty(exports, "__esModule", { value: true });
16+
exports.imported = void 0;
17+
exports.imported = 1;
18+
//// [a.js]
19+
"use strict";
20+
Object.defineProperty(exports, "__esModule", { value: true });
21+
exports.exported = void 0;
22+
const b_1 = require("./b");
23+
exports.exported = 2;
24+
let a, b;
25+
({ a = b_1.imported, b = exports.exported } = {});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// @target: es2015
2+
// @module: commonjs
3+
// @noTypesAndSymbols: true
4+
5+
// @filename: /a.ts
6+
import { imported } from "./b";
7+
export const exported = 2;
8+
let a, b;
9+
({ a = imported, b = exported } = {});
10+
11+
// @filename: /b.ts
12+
export const imported = 1;

0 commit comments

Comments
 (0)