Skip to content

Commit 6157576

Browse files
committed
Change some more but I am less sure about this
1 parent a958283 commit 6157576

17 files changed

+166
-289
lines changed

internal/transformers/moduletransforms/commonjsmodule.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,7 +1080,14 @@ func (tx *CommonJSModuleTransformer) visitTopLevelVariableStatement(node *ast.Va
10801080
propertyAccess,
10811081
v.Name().Clone(tx.Factory()),
10821082
))
1083+
} else if ast.IsIdentifier(v.Name()) {
1084+
expression := tx.transformInitializedVariable(v)
1085+
if expression != nil {
1086+
pushExpression(tx.Visitor().VisitNode(expression))
1087+
}
10831088
} else {
1089+
// For binding patterns, we can't do exports.{pattern} = value
1090+
// Just emit the assignment and let appendExportsOfVariableStatement handle the exports
10841091
expression := transformers.ConvertVariableDeclarationToAssignmentExpression(tx.EmitContext(), v)
10851092
if expression != nil {
10861093
pushExpression(tx.Visitor().VisitNode(expression))
@@ -1096,6 +1103,21 @@ func (tx *CommonJSModuleTransformer) visitTopLevelVariableStatement(node *ast.Va
10961103
return tx.visitTopLevelNestedVariableStatement(node)
10971104
}
10981105

1106+
func (tx *CommonJSModuleTransformer) transformInitializedVariable(node *ast.VariableDeclaration) *ast.Expression {
1107+
if node.Initializer == nil {
1108+
return nil
1109+
}
1110+
name := node.Name()
1111+
propertyAccess := tx.Factory().NewPropertyAccessExpression(
1112+
tx.Factory().NewIdentifier("exports"),
1113+
nil, /*questionDotToken*/
1114+
name,
1115+
ast.NodeFlagsNone,
1116+
)
1117+
tx.EmitContext().AssignCommentAndSourceMapRanges(propertyAccess, name)
1118+
return tx.Factory().NewAssignmentExpression(propertyAccess, node.Initializer)
1119+
}
1120+
10991121
// Visits a top-level nested variable statement as it may contain `var` declarations that are hoisted and may still be
11001122
// exported with `export {}`.
11011123
func (tx *CommonJSModuleTransformer) visitTopLevelNestedVariableStatement(node *ast.VariableStatement) *ast.Node {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//// [tests/cases/compiler/exportDestructuring.ts] ////
2+
3+
//// [exportDestructuring.ts]
4+
const arr = [1, 2];
5+
export const [a, b] = arr;
6+
7+
8+
//// [exportDestructuring.js]
9+
"use strict";
10+
Object.defineProperty(exports, "__esModule", { value: true });
11+
exports.b = exports.a = void 0;
12+
const arr = [1, 2];
13+
[exports.a, exports.b] = arr;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//// [tests/cases/compiler/exportDestructuring.ts] ////
2+
3+
=== exportDestructuring.ts ===
4+
const arr = [1, 2];
5+
>arr : Symbol(arr, Decl(exportDestructuring.ts, 0, 5))
6+
7+
export const [a, b] = arr;
8+
>a : Symbol(a, Decl(exportDestructuring.ts, 1, 14))
9+
>b : Symbol(b, Decl(exportDestructuring.ts, 1, 16))
10+
>arr : Symbol(arr, Decl(exportDestructuring.ts, 0, 5))
11+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//// [tests/cases/compiler/exportDestructuring.ts] ////
2+
3+
=== exportDestructuring.ts ===
4+
const arr = [1, 2];
5+
>arr : number[]
6+
>[1, 2] : number[]
7+
>1 : 1
8+
>2 : 2
9+
10+
export const [a, b] = arr;
11+
>a : number
12+
>b : number
13+
>arr : number[]
14+

testdata/baselines/reference/submodule/compiler/importDeclWithClassModifiers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ var b: a;
1616
Object.defineProperty(exports, "__esModule", { value: true });
1717
exports.c = exports.b = exports.a = void 0;
1818
exports.a = x.c;
19-
b = x.c;
19+
exports.b = x.c;
2020
exports.c = x.c;
2121
var b;

testdata/baselines/reference/submodule/compiler/importDeclWithClassModifiers.js.diff

Lines changed: 0 additions & 10 deletions
This file was deleted.

testdata/baselines/reference/submodule/compiler/unicodeEscapesInNames01(target=es5).js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

testdata/baselines/reference/submodule/compiler/unicodeEscapesInNames01(target=es5).js.diff

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,6 @@
11
--- old.unicodeEscapesInNames01(target=es5).js
22
+++ new.unicodeEscapesInNames01(target=es5).js
3-
@@= skipped -124, +124 lines =@@
4-
"use strict";
5-
Object.defineProperty(exports, "__esModule", { value: true });
6-
exports.x = void 0;
7-
-exports.\u0078 = 10;
8-
+exports.x = 10;
9-
exports.x++;
10-
//# sourceMappingURL=identifierVariableWithEscape1.js.map
11-
//// [identifierVariableWithEscape2.js]
12-
"use strict";
13-
Object.defineProperty(exports, "__esModule", { value: true });
14-
exports.xx = void 0;
15-
-exports.x\u0078 = 10;
16-
+exports.xx = 10;
17-
exports.xx++;
18-
//# sourceMappingURL=identifierVariableWithEscape2.js.map
19-
//// [identifierVariableWithExtendedEscape1.js]
20-
"use strict";
21-
Object.defineProperty(exports, "__esModule", { value: true });
22-
exports.x = void 0;
23-
-exports.\u{78} = 10;
24-
+exports.x = 10;
25-
exports.x++;
26-
//# sourceMappingURL=identifierVariableWithExtendedEscape1.js.map
27-
//// [identifierVariableWithExtendedEscape2.js]
28-
"use strict";
29-
Object.defineProperty(exports, "__esModule", { value: true });
30-
exports.xx = void 0;
31-
-exports.x\u{78} = 10;
32-
+exports.xx = 10;
33-
exports.xx++;
34-
//# sourceMappingURL=identifierVariableWithExtendedEscape2.js.map
35-
//// [IdentifierNameWithEscape1.js]
36-
@@= skipped -29, +29 lines =@@
3+
@@= skipped -153, +153 lines =@@
374
Object.defineProperty(exports, "__esModule", { value: true });
385
exports.IdentifierNameWithEscape1 = void 0;
396
class IdentifierNameWithEscape1 {

testdata/baselines/reference/submodule/compiler/unicodeEscapesInNames01(target=es5).js.map

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)