Skip to content

Commit 7119345

Browse files
Copilotjakebailey
andauthored
Handle numeric literal export names in declaration emit (#3919)
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 902a32b commit 7119345

5 files changed

Lines changed: 100 additions & 0 deletions

File tree

internal/transformers/declarations/transform.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2174,6 +2174,10 @@ func (tx *DeclarationTransformer) transformJSDocOptionalType(input *ast.JSDocOpt
21742174
}
21752175

21762176
func (tx *DeclarationTransformer) getNameExpressionPreferringIdentifier(nameExpr *ast.Node) *ast.Node {
2177+
if ast.IsNumericLiteral(nameExpr) {
2178+
// Numeric property names are string properties in JS; convert to string literal
2179+
nameExpr = tx.Factory().NewStringLiteral(nameExpr.Text(), ast.TokenFlagsNone)
2180+
}
21772181
if ast.IsStringLiteralLike(nameExpr) && scanner.IsIdentifierText(nameExpr.Text(), core.LanguageVariantStandard) {
21782182
result := tx.Factory().NewIdentifier(nameExpr.Text()) // prefer non-string literal names where possible
21792183
kwKind := scanner.IdentifierToKeywordKind(result.AsIdentifier())
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//// [tests/cases/compiler/numericExportNameDeclaration.ts] ////
2+
3+
//// [bug.js]
4+
exports[1] = 2;
5+
module.exports[1] = 2;
6+
Object.defineProperty(exports, 1, {});
7+
8+
9+
10+
11+
//// [bug.d.ts]
12+
declare const _exported: any;
13+
export { _exported as "1" };
14+
declare const _exported_1: any;
15+
export { _exported_1 as "1" };
16+
declare const _exported_2: any;
17+
export { _exported_2 as "1" };
18+
19+
20+
//// [DtsFileErrors]
21+
22+
23+
bug.d.ts(2,23): error TS2300: Duplicate identifier '"1"'.
24+
bug.d.ts(4,25): error TS2300: Duplicate identifier '"1"'.
25+
bug.d.ts(6,25): error TS2300: Duplicate identifier '"1"'.
26+
27+
28+
==== bug.d.ts (3 errors) ====
29+
declare const _exported: any;
30+
export { _exported as "1" };
31+
~~~
32+
!!! error TS2300: Duplicate identifier '"1"'.
33+
declare const _exported_1: any;
34+
export { _exported_1 as "1" };
35+
~~~
36+
!!! error TS2300: Duplicate identifier '"1"'.
37+
declare const _exported_2: any;
38+
export { _exported_2 as "1" };
39+
~~~
40+
!!! error TS2300: Duplicate identifier '"1"'.
41+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//// [tests/cases/compiler/numericExportNameDeclaration.ts] ////
2+
3+
=== bug.js ===
4+
exports[1] = 2;
5+
>exports : Symbol(exports, Decl(bug.js, 0, 0))
6+
>1 : Symbol(1, Decl(bug.js, 0, 0), Decl(bug.js, 0, 15), Decl(bug.js, 1, 22))
7+
8+
module.exports[1] = 2;
9+
>module.exports : Symbol(exports, Decl(bug.js, 0, 0))
10+
>module : Symbol(module, Decl(bug.js, 0, 0))
11+
>exports : Symbol(exports, Decl(bug.js, 0, 0))
12+
>1 : Symbol(1, Decl(bug.js, 0, 0), Decl(bug.js, 0, 15), Decl(bug.js, 1, 22))
13+
14+
Object.defineProperty(exports, 1, {});
15+
>Object.defineProperty : Symbol(ObjectConstructor.defineProperty, Decl(lib.es5.d.ts, --, --))
16+
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
17+
>defineProperty : Symbol(ObjectConstructor.defineProperty, Decl(lib.es5.d.ts, --, --))
18+
>exports : Symbol(exports, Decl(bug.js, 0, 0))
19+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//// [tests/cases/compiler/numericExportNameDeclaration.ts] ////
2+
3+
=== bug.js ===
4+
exports[1] = 2;
5+
>exports[1] = 2 : 2
6+
>exports[1] : any
7+
>exports : typeof import("./bug")
8+
>1 : 1
9+
>2 : 2
10+
11+
module.exports[1] = 2;
12+
>module.exports[1] = 2 : 2
13+
>module.exports[1] : error
14+
>module.exports : typeof import("./bug")
15+
>module : { exports: typeof import("./bug"); }
16+
>exports : typeof import("./bug")
17+
>1 : 1
18+
>2 : 2
19+
20+
Object.defineProperty(exports, 1, {});
21+
>Object.defineProperty(exports, 1, {}) : typeof import("./bug")
22+
>Object.defineProperty : <T>(o: T, p: PropertyKey, attributes: PropertyDescriptor & ThisType<any>) => T
23+
>Object : ObjectConstructor
24+
>defineProperty : <T>(o: T, p: PropertyKey, attributes: PropertyDescriptor & ThisType<any>) => T
25+
>exports : typeof import("./bug")
26+
>1 : 1
27+
>{} : {}
28+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// @declaration: true
2+
// @emitDeclarationOnly: true
3+
// @allowJs: true
4+
5+
// @filename: bug.js
6+
exports[1] = 2;
7+
module.exports[1] = 2;
8+
Object.defineProperty(exports, 1, {});

0 commit comments

Comments
 (0)