-
Notifications
You must be signed in to change notification settings - Fork 725
Fix various module emit differences #1965
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1080,7 +1080,14 @@ func (tx *CommonJSModuleTransformer) visitTopLevelVariableStatement(node *ast.Va | |
| propertyAccess, | ||
| v.Name().Clone(tx.Factory()), | ||
| )) | ||
| } else if ast.IsIdentifier(v.Name()) { | ||
| expression := tx.transformInitializedVariable(v) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The old compiler had this func instead which did something special with |
||
| if expression != nil { | ||
| pushExpression(tx.Visitor().VisitNode(expression)) | ||
| } | ||
| } else { | ||
| // For binding patterns, we can't do exports.{pattern} = value | ||
| // Just emit the assignment and let appendExportsOfVariableStatement handle the exports | ||
| expression := transformers.ConvertVariableDeclarationToAssignmentExpression(tx.EmitContext(), v) | ||
| if expression != nil { | ||
| pushExpression(tx.Visitor().VisitNode(expression)) | ||
|
|
@@ -1096,6 +1103,21 @@ func (tx *CommonJSModuleTransformer) visitTopLevelVariableStatement(node *ast.Va | |
| return tx.visitTopLevelNestedVariableStatement(node) | ||
| } | ||
|
|
||
| func (tx *CommonJSModuleTransformer) transformInitializedVariable(node *ast.VariableDeclaration) *ast.Expression { | ||
| if node.Initializer == nil { | ||
| return nil | ||
| } | ||
| name := node.Name() | ||
| propertyAccess := tx.Factory().NewPropertyAccessExpression( | ||
| tx.Factory().NewIdentifier("exports"), | ||
| nil, /*questionDotToken*/ | ||
| name, | ||
| ast.NodeFlagsNone, | ||
| ) | ||
| tx.EmitContext().AssignCommentAndSourceMapRanges(propertyAccess, name) | ||
| return tx.Factory().NewAssignmentExpression(propertyAccess, node.Initializer) | ||
| } | ||
|
|
||
| // Visits a top-level nested variable statement as it may contain `var` declarations that are hoisted and may still be | ||
| // exported with `export {}`. | ||
| func (tx *CommonJSModuleTransformer) visitTopLevelNestedVariableStatement(node *ast.VariableStatement) *ast.Node { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| //// [tests/cases/compiler/exportDestructuring.ts] //// | ||
|
|
||
| //// [exportDestructuring.ts] | ||
| const arr = [1, 2]; | ||
| export const [a, b] = arr; | ||
|
|
||
|
|
||
| //// [exportDestructuring.js] | ||
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.b = exports.a = void 0; | ||
| const arr = [1, 2]; | ||
| [exports.a, exports.b] = arr; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| //// [tests/cases/compiler/exportDestructuring.ts] //// | ||
|
|
||
| === exportDestructuring.ts === | ||
| const arr = [1, 2]; | ||
| >arr : Symbol(arr, Decl(exportDestructuring.ts, 0, 5)) | ||
|
|
||
| export const [a, b] = arr; | ||
| >a : Symbol(a, Decl(exportDestructuring.ts, 1, 14)) | ||
| >b : Symbol(b, Decl(exportDestructuring.ts, 1, 16)) | ||
| >arr : Symbol(arr, Decl(exportDestructuring.ts, 0, 5)) | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| //// [tests/cases/compiler/exportDestructuring.ts] //// | ||
|
|
||
| === exportDestructuring.ts === | ||
| const arr = [1, 2]; | ||
| >arr : number[] | ||
| >[1, 2] : number[] | ||
| >1 : 1 | ||
| >2 : 2 | ||
|
|
||
| export const [a, b] = arr; | ||
| >a : number | ||
| >b : number | ||
| >arr : number[] | ||
|
|
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,3 +13,4 @@ var a; | |
| a.b = 10; | ||
| })(a || (a = {})); | ||
| var f = () => this; | ||
| var _this = a; // Error | ||
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,6 +28,7 @@ export = m; | |
| var m3; | ||
| (function (m3) { | ||
| })(m3 || (m3 = {})); | ||
| var m = m3; | ||
| module.exports = m; | ||
|
|
||
|
|
||
|
|
||
This file was deleted.
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The old compiler used a
boolean | undefinedon every symbol for this. Corsa uses a flag but didn't make the behavior match the tristate. Doing this instead to avoid adding a Tristate to all symbols.