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
4 changes: 4 additions & 0 deletions internal/transformers/declarations/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -2173,6 +2173,10 @@ func (tx *DeclarationTransformer) transformJSDocOptionalType(input *ast.JSDocOpt
}

func (tx *DeclarationTransformer) getNameExpressionPreferringIdentifier(nameExpr *ast.Node) *ast.Node {
if ast.IsNumericLiteral(nameExpr) {
Copy link
Copy Markdown
Member

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 isStringOrNumericLiteralLike in the check below.

// 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) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if ast.IsStringLiteralLike(nameExpr) && scanner.IsIdentifierText(nameExpr.Text(), core.LanguageVariantStandard) {
if ast.IsStringOrNumericLiteralLike(nameExpr) && scanner.IsIdentifierText(nameExpr.Text(), core.LanguageVariantStandard) {

result := tx.Factory().NewIdentifier(nameExpr.Text()) // prefer non-string literal names where possible
kwKind := scanner.IdentifierToKeywordKind(result.AsIdentifier())
Expand Down
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"'.

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))

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 testdata/tests/cases/compiler/numericExportNameDeclaration.ts
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, {});