Skip to content

Commit 2395fda

Browse files
committed
fix(38295): handle duplicate object literal keys which contain '+' and '-' tokens
1 parent 7fc456f commit 2395fda

6 files changed

+158
-0
lines changed

src/compiler/utilities.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3092,6 +3092,9 @@ namespace ts {
30923092
else if (isStringOrNumericLiteralLike(nameExpression)) {
30933093
return escapeLeadingUnderscores(nameExpression.text);
30943094
}
3095+
else if (isSignedNumericLiteral(nameExpression)) {
3096+
return tokenToString(nameExpression.operator) + nameExpression.operand.text as __String;
3097+
}
30953098
return undefined;
30963099
default:
30973100
return Debug.assertNever(name);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts(3,5): error TS2300: Duplicate identifier '[1]'.
2+
tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts(8,5): error TS2300: Duplicate identifier '[-1]'.
3+
tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts(13,5): error TS2300: Duplicate identifier '[+1]'.
4+
5+
6+
==== tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts (3 errors) ====
7+
const a = {
8+
1: 0,
9+
[1]: 0 // Duplicate
10+
~~~
11+
!!! error TS2300: Duplicate identifier '[1]'.
12+
}
13+
14+
const b = {
15+
"-1": 0,
16+
[-1]: 0 // Duplicate
17+
~~~~
18+
!!! error TS2300: Duplicate identifier '[-1]'.
19+
}
20+
21+
const c = {
22+
"+1": 0,
23+
[+1]: 0 // Duplicate
24+
~~~~
25+
!!! error TS2300: Duplicate identifier '[+1]'.
26+
}
27+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//// [duplicateObjectLiteralProperty_computedName.ts]
2+
const a = {
3+
1: 0,
4+
[1]: 0 // Duplicate
5+
}
6+
7+
const b = {
8+
"-1": 0,
9+
[-1]: 0 // Duplicate
10+
}
11+
12+
const c = {
13+
"+1": 0,
14+
[+1]: 0 // Duplicate
15+
}
16+
17+
18+
//// [duplicateObjectLiteralProperty_computedName.js]
19+
var _a, _b, _c;
20+
var a = (_a = {
21+
1: 0
22+
},
23+
_a[1] = 0 // Duplicate
24+
,
25+
_a);
26+
var b = (_b = {
27+
"-1": 0
28+
},
29+
_b[-1] = 0 // Duplicate
30+
,
31+
_b);
32+
var c = (_c = {
33+
"+1": 0
34+
},
35+
_c[+1] = 0 // Duplicate
36+
,
37+
_c);
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
=== tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts ===
2+
const a = {
3+
>a : Symbol(a, Decl(duplicateObjectLiteralProperty_computedName.ts, 0, 5))
4+
5+
1: 0,
6+
>1 : Symbol(1, Decl(duplicateObjectLiteralProperty_computedName.ts, 0, 11), Decl(duplicateObjectLiteralProperty_computedName.ts, 1, 9))
7+
8+
[1]: 0 // Duplicate
9+
>[1] : Symbol(1, Decl(duplicateObjectLiteralProperty_computedName.ts, 0, 11), Decl(duplicateObjectLiteralProperty_computedName.ts, 1, 9))
10+
>1 : Symbol(1, Decl(duplicateObjectLiteralProperty_computedName.ts, 0, 11), Decl(duplicateObjectLiteralProperty_computedName.ts, 1, 9))
11+
}
12+
13+
const b = {
14+
>b : Symbol(b, Decl(duplicateObjectLiteralProperty_computedName.ts, 5, 5))
15+
16+
"-1": 0,
17+
>"-1" : Symbol("-1", Decl(duplicateObjectLiteralProperty_computedName.ts, 5, 11), Decl(duplicateObjectLiteralProperty_computedName.ts, 6, 12))
18+
19+
[-1]: 0 // Duplicate
20+
>[-1] : Symbol("-1", Decl(duplicateObjectLiteralProperty_computedName.ts, 5, 11), Decl(duplicateObjectLiteralProperty_computedName.ts, 6, 12))
21+
}
22+
23+
const c = {
24+
>c : Symbol(c, Decl(duplicateObjectLiteralProperty_computedName.ts, 10, 5))
25+
26+
"+1": 0,
27+
>"+1" : Symbol("+1", Decl(duplicateObjectLiteralProperty_computedName.ts, 10, 11), Decl(duplicateObjectLiteralProperty_computedName.ts, 11, 12))
28+
29+
[+1]: 0 // Duplicate
30+
>[+1] : Symbol("+1", Decl(duplicateObjectLiteralProperty_computedName.ts, 10, 11), Decl(duplicateObjectLiteralProperty_computedName.ts, 11, 12))
31+
}
32+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
=== tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts ===
2+
const a = {
3+
>a : { 1: number; }
4+
>{ 1: 0, [1]: 0 // Duplicate} : { 1: number; }
5+
6+
1: 0,
7+
>1 : number
8+
>0 : 0
9+
10+
[1]: 0 // Duplicate
11+
>[1] : number
12+
>1 : 1
13+
>0 : 0
14+
}
15+
16+
const b = {
17+
>b : { [-1]: number; }
18+
>{ "-1": 0, [-1]: 0 // Duplicate} : { [-1]: number; }
19+
20+
"-1": 0,
21+
>"-1" : number
22+
>0 : 0
23+
24+
[-1]: 0 // Duplicate
25+
>[-1] : number
26+
>-1 : -1
27+
>1 : 1
28+
>0 : 0
29+
}
30+
31+
const c = {
32+
>c : { "+1": number; 1: number; }
33+
>{ "+1": 0, [+1]: 0 // Duplicate} : { "+1": number; 1: number; }
34+
35+
"+1": 0,
36+
>"+1" : number
37+
>0 : 0
38+
39+
[+1]: 0 // Duplicate
40+
>[+1] : number
41+
>+1 : 1
42+
>1 : 1
43+
>0 : 0
44+
}
45+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const a = {
2+
1: 0,
3+
[1]: 0 // Duplicate
4+
}
5+
6+
const b = {
7+
"-1": 0,
8+
[-1]: 0 // Duplicate
9+
}
10+
11+
const c = {
12+
"+1": 0,
13+
[+1]: 0 // Duplicate
14+
}

0 commit comments

Comments
 (0)