-
Notifications
You must be signed in to change notification settings - Fork 958
Handle numeric literal export names in declaration emit #3919
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
Open
Copilot
wants to merge
6
commits into
main
Choose a base branch
from
copilot/fix-tsgo-printer-panics
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
682fd6d
Initial plan
Copilot 1da664f
fix: handle numeric literal in emitModuleExportName to prevent panic
Copilot 0019d09
fix: convert numeric literal export names to string literals in decla…
Copilot ab43a4a
Merge branch 'main' into copilot/fix-tsgo-printer-panics
jakebailey 4304c9f
fix: emit numeric literals as string literals in emitModuleExportName…
Copilot 95b66cc
Remove dead KindNumericLiteral case from emitModuleExportName
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -2173,6 +2173,10 @@ func (tx *DeclarationTransformer) transformJSDocOptionalType(input *ast.JSDocOpt | |||||
| } | ||||||
|
|
||||||
| func (tx *DeclarationTransformer) getNameExpressionPreferringIdentifier(nameExpr *ast.Node) *ast.Node { | ||||||
| if ast.IsNumericLiteral(nameExpr) { | ||||||
| // Numeric property names are string properties in JS; convert to string literal | ||||||
| nameExpr = tx.Factory().NewStringLiteral(nameExpr.Text(), ast.TokenFlagsNone) | ||||||
| } | ||||||
| if ast.IsStringLiteralLike(nameExpr) && scanner.IsIdentifierText(nameExpr.Text(), core.LanguageVariantStandard) { | ||||||
|
Member
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.
Suggested change
|
||||||
| result := tx.Factory().NewIdentifier(nameExpr.Text()) // prefer non-string literal names where possible | ||||||
| kwKind := scanner.IdentifierToKeywordKind(result.AsIdentifier()) | ||||||
|
|
||||||
41 changes: 41 additions & 0 deletions
41
testdata/baselines/reference/compiler/numericExportNameDeclaration.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| //// [tests/cases/compiler/numericExportNameDeclaration.ts] //// | ||
|
|
||
| //// [bug.js] | ||
| exports[1] = 2; | ||
| module.exports[1] = 2; | ||
| Object.defineProperty(exports, 1, {}); | ||
|
|
||
|
|
||
|
|
||
|
|
||
| //// [bug.d.ts] | ||
| declare const _exported: any; | ||
| export { _exported as "1" }; | ||
| declare const _exported_1: any; | ||
| export { _exported_1 as "1" }; | ||
| declare const _exported_2: any; | ||
| export { _exported_2 as "1" }; | ||
|
|
||
|
|
||
| //// [DtsFileErrors] | ||
|
|
||
|
|
||
| bug.d.ts(2,23): error TS2300: Duplicate identifier '"1"'. | ||
| bug.d.ts(4,25): error TS2300: Duplicate identifier '"1"'. | ||
| bug.d.ts(6,25): error TS2300: Duplicate identifier '"1"'. | ||
|
|
||
|
|
||
| ==== bug.d.ts (3 errors) ==== | ||
| declare const _exported: any; | ||
| export { _exported as "1" }; | ||
| ~~~ | ||
| !!! error TS2300: Duplicate identifier '"1"'. | ||
| declare const _exported_1: any; | ||
| export { _exported_1 as "1" }; | ||
| ~~~ | ||
| !!! error TS2300: Duplicate identifier '"1"'. | ||
| declare const _exported_2: any; | ||
| export { _exported_2 as "1" }; | ||
| ~~~ | ||
| !!! error TS2300: Duplicate identifier '"1"'. | ||
|
|
19 changes: 19 additions & 0 deletions
19
testdata/baselines/reference/compiler/numericExportNameDeclaration.symbols
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| //// [tests/cases/compiler/numericExportNameDeclaration.ts] //// | ||
|
|
||
| === bug.js === | ||
| exports[1] = 2; | ||
| >exports : Symbol(exports, Decl(bug.js, 0, 0)) | ||
| >1 : Symbol(1, Decl(bug.js, 0, 0), Decl(bug.js, 0, 15), Decl(bug.js, 1, 22)) | ||
|
|
||
| module.exports[1] = 2; | ||
| >module.exports : Symbol(exports, Decl(bug.js, 0, 0)) | ||
| >module : Symbol(module, Decl(bug.js, 0, 0)) | ||
| >exports : Symbol(exports, Decl(bug.js, 0, 0)) | ||
| >1 : Symbol(1, Decl(bug.js, 0, 0), Decl(bug.js, 0, 15), Decl(bug.js, 1, 22)) | ||
|
|
||
| Object.defineProperty(exports, 1, {}); | ||
| >Object.defineProperty : Symbol(ObjectConstructor.defineProperty, Decl(lib.es5.d.ts, --, --)) | ||
| >Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
| >defineProperty : Symbol(ObjectConstructor.defineProperty, Decl(lib.es5.d.ts, --, --)) | ||
| >exports : Symbol(exports, Decl(bug.js, 0, 0)) | ||
|
|
28 changes: 28 additions & 0 deletions
28
testdata/baselines/reference/compiler/numericExportNameDeclaration.types
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| //// [tests/cases/compiler/numericExportNameDeclaration.ts] //// | ||
|
|
||
| === bug.js === | ||
| exports[1] = 2; | ||
| >exports[1] = 2 : 2 | ||
| >exports[1] : any | ||
| >exports : typeof import("./bug") | ||
| >1 : 1 | ||
| >2 : 2 | ||
|
|
||
| module.exports[1] = 2; | ||
| >module.exports[1] = 2 : 2 | ||
| >module.exports[1] : error | ||
| >module.exports : typeof import("./bug") | ||
| >module : { exports: typeof import("./bug"); } | ||
| >exports : typeof import("./bug") | ||
| >1 : 1 | ||
| >2 : 2 | ||
|
|
||
| Object.defineProperty(exports, 1, {}); | ||
| >Object.defineProperty(exports, 1, {}) : typeof import("./bug") | ||
| >Object.defineProperty : <T>(o: T, p: PropertyKey, attributes: PropertyDescriptor & ThisType<any>) => T | ||
| >Object : ObjectConstructor | ||
| >defineProperty : <T>(o: T, p: PropertyKey, attributes: PropertyDescriptor & ThisType<any>) => T | ||
| >exports : typeof import("./bug") | ||
| >1 : 1 | ||
| >{} : {} | ||
|
|
8 changes: 8 additions & 0 deletions
8
testdata/tests/cases/compiler/numericExportNameDeclaration.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| // @declaration: true | ||
| // @emitDeclarationOnly: true | ||
| // @allowJs: true | ||
|
|
||
| // @filename: bug.js | ||
| exports[1] = 2; | ||
| module.exports[1] = 2; | ||
| Object.defineProperty(exports, 1, {}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This change isn't correct or necessary. Just use
isStringOrNumericLiteralLikein the check below.