Skip to content

Commit 3340142

Browse files
authored
Merge pull request microsoft#38607 from a-tarasyuk/bug/38295
fix(38295): Duplicated object key in const not detected when the key is a number preceded by `-` or `+`
2 parents 4e945fb + bbf56b0 commit 3340142

6 files changed

+354
-0
lines changed

src/compiler/utilities.ts

+6
Original file line numberDiff line numberDiff line change
@@ -3092,6 +3092,12 @@ namespace ts {
30923092
else if (isStringOrNumericLiteralLike(nameExpression)) {
30933093
return escapeLeadingUnderscores(nameExpression.text);
30943094
}
3095+
else if (isSignedNumericLiteral(nameExpression)) {
3096+
if (nameExpression.operator === SyntaxKind.MinusToken) {
3097+
return tokenToString(nameExpression.operator) + nameExpression.operand.text as __String;
3098+
}
3099+
return nameExpression.operand.text as __String;
3100+
}
30953101
return undefined;
30963102
default:
30973103
return Debug.assertNever(name);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts(23,5): error TS2300: Duplicate identifier '["+1"]'.
5+
tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts(28,5): error TS2300: Duplicate identifier '[-1]'.
6+
tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts(33,5): error TS2300: Duplicate identifier '["-1"]'.
7+
8+
9+
==== tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts (6 errors) ====
10+
const t1 = {
11+
1: 1,
12+
[1]: 0 // duplicate
13+
~~~
14+
!!! error TS2300: Duplicate identifier '[1]'.
15+
}
16+
17+
const t2 = {
18+
1: 1,
19+
[+1]: 0 // duplicate
20+
~~~~
21+
!!! error TS2300: Duplicate identifier '[+1]'.
22+
}
23+
24+
const t3 = {
25+
"1": 1,
26+
[+1]: 0 // duplicate
27+
~~~~
28+
!!! error TS2300: Duplicate identifier '[+1]'.
29+
}
30+
31+
const t4 = {
32+
"+1": 1,
33+
[+1]: 0 // two different keys, "+1", "1"
34+
}
35+
36+
const t5 = {
37+
"+1": 1,
38+
["+1"]: 0 // duplicate
39+
~~~~~~
40+
!!! error TS2300: Duplicate identifier '["+1"]'.
41+
}
42+
43+
const t6 = {
44+
"-1": 1,
45+
[-1]: 0 // duplicate
46+
~~~~
47+
!!! error TS2300: Duplicate identifier '[-1]'.
48+
}
49+
50+
const t7 = {
51+
"-1": 1,
52+
["-1"]: 0 // duplicate
53+
~~~~~~
54+
!!! error TS2300: Duplicate identifier '["-1"]'.
55+
}
56+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
//// [duplicateObjectLiteralProperty_computedName.ts]
2+
const t1 = {
3+
1: 1,
4+
[1]: 0 // duplicate
5+
}
6+
7+
const t2 = {
8+
1: 1,
9+
[+1]: 0 // duplicate
10+
}
11+
12+
const t3 = {
13+
"1": 1,
14+
[+1]: 0 // duplicate
15+
}
16+
17+
const t4 = {
18+
"+1": 1,
19+
[+1]: 0 // two different keys, "+1", "1"
20+
}
21+
22+
const t5 = {
23+
"+1": 1,
24+
["+1"]: 0 // duplicate
25+
}
26+
27+
const t6 = {
28+
"-1": 1,
29+
[-1]: 0 // duplicate
30+
}
31+
32+
const t7 = {
33+
"-1": 1,
34+
["-1"]: 0 // duplicate
35+
}
36+
37+
38+
//// [duplicateObjectLiteralProperty_computedName.js]
39+
var _a, _b, _c, _d, _e, _f, _g;
40+
var t1 = (_a = {
41+
1: 1
42+
},
43+
_a[1] = 0 // duplicate
44+
,
45+
_a);
46+
var t2 = (_b = {
47+
1: 1
48+
},
49+
_b[+1] = 0 // duplicate
50+
,
51+
_b);
52+
var t3 = (_c = {
53+
"1": 1
54+
},
55+
_c[+1] = 0 // duplicate
56+
,
57+
_c);
58+
var t4 = (_d = {
59+
"+1": 1
60+
},
61+
_d[+1] = 0 // two different keys, "+1", "1"
62+
,
63+
_d);
64+
var t5 = (_e = {
65+
"+1": 1
66+
},
67+
_e["+1"] = 0 // duplicate
68+
,
69+
_e);
70+
var t6 = (_f = {
71+
"-1": 1
72+
},
73+
_f[-1] = 0 // duplicate
74+
,
75+
_f);
76+
var t7 = (_g = {
77+
"-1": 1
78+
},
79+
_g["-1"] = 0 // duplicate
80+
,
81+
_g);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
=== tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts ===
2+
const t1 = {
3+
>t1 : Symbol(t1, Decl(duplicateObjectLiteralProperty_computedName.ts, 0, 5))
4+
5+
1: 1,
6+
>1 : Symbol(1, Decl(duplicateObjectLiteralProperty_computedName.ts, 0, 12), Decl(duplicateObjectLiteralProperty_computedName.ts, 1, 9))
7+
8+
[1]: 0 // duplicate
9+
>[1] : Symbol(1, Decl(duplicateObjectLiteralProperty_computedName.ts, 0, 12), Decl(duplicateObjectLiteralProperty_computedName.ts, 1, 9))
10+
>1 : Symbol(1, Decl(duplicateObjectLiteralProperty_computedName.ts, 0, 12), Decl(duplicateObjectLiteralProperty_computedName.ts, 1, 9))
11+
}
12+
13+
const t2 = {
14+
>t2 : Symbol(t2, Decl(duplicateObjectLiteralProperty_computedName.ts, 5, 5))
15+
16+
1: 1,
17+
>1 : Symbol(1, Decl(duplicateObjectLiteralProperty_computedName.ts, 5, 12))
18+
19+
[+1]: 0 // duplicate
20+
>[+1] : Symbol([+1], Decl(duplicateObjectLiteralProperty_computedName.ts, 6, 9))
21+
}
22+
23+
const t3 = {
24+
>t3 : Symbol(t3, Decl(duplicateObjectLiteralProperty_computedName.ts, 10, 5))
25+
26+
"1": 1,
27+
>"1" : Symbol("1", Decl(duplicateObjectLiteralProperty_computedName.ts, 10, 12))
28+
29+
[+1]: 0 // duplicate
30+
>[+1] : Symbol([+1], Decl(duplicateObjectLiteralProperty_computedName.ts, 11, 11))
31+
}
32+
33+
const t4 = {
34+
>t4 : Symbol(t4, Decl(duplicateObjectLiteralProperty_computedName.ts, 15, 5))
35+
36+
"+1": 1,
37+
>"+1" : Symbol("+1", Decl(duplicateObjectLiteralProperty_computedName.ts, 15, 12), Decl(duplicateObjectLiteralProperty_computedName.ts, 16, 12))
38+
39+
[+1]: 0 // two different keys, "+1", "1"
40+
>[+1] : Symbol("+1", Decl(duplicateObjectLiteralProperty_computedName.ts, 15, 12), Decl(duplicateObjectLiteralProperty_computedName.ts, 16, 12))
41+
}
42+
43+
const t5 = {
44+
>t5 : Symbol(t5, Decl(duplicateObjectLiteralProperty_computedName.ts, 20, 5))
45+
46+
"+1": 1,
47+
>"+1" : Symbol("+1", Decl(duplicateObjectLiteralProperty_computedName.ts, 20, 12), Decl(duplicateObjectLiteralProperty_computedName.ts, 21, 12))
48+
49+
["+1"]: 0 // duplicate
50+
>["+1"] : Symbol("+1", Decl(duplicateObjectLiteralProperty_computedName.ts, 20, 12), Decl(duplicateObjectLiteralProperty_computedName.ts, 21, 12))
51+
>"+1" : Symbol("+1", Decl(duplicateObjectLiteralProperty_computedName.ts, 20, 12), Decl(duplicateObjectLiteralProperty_computedName.ts, 21, 12))
52+
}
53+
54+
const t6 = {
55+
>t6 : Symbol(t6, Decl(duplicateObjectLiteralProperty_computedName.ts, 25, 5))
56+
57+
"-1": 1,
58+
>"-1" : Symbol("-1", Decl(duplicateObjectLiteralProperty_computedName.ts, 25, 12), Decl(duplicateObjectLiteralProperty_computedName.ts, 26, 12))
59+
60+
[-1]: 0 // duplicate
61+
>[-1] : Symbol("-1", Decl(duplicateObjectLiteralProperty_computedName.ts, 25, 12), Decl(duplicateObjectLiteralProperty_computedName.ts, 26, 12))
62+
}
63+
64+
const t7 = {
65+
>t7 : Symbol(t7, Decl(duplicateObjectLiteralProperty_computedName.ts, 30, 5))
66+
67+
"-1": 1,
68+
>"-1" : Symbol("-1", Decl(duplicateObjectLiteralProperty_computedName.ts, 30, 12), Decl(duplicateObjectLiteralProperty_computedName.ts, 31, 12))
69+
70+
["-1"]: 0 // duplicate
71+
>["-1"] : Symbol("-1", Decl(duplicateObjectLiteralProperty_computedName.ts, 30, 12), Decl(duplicateObjectLiteralProperty_computedName.ts, 31, 12))
72+
>"-1" : Symbol("-1", Decl(duplicateObjectLiteralProperty_computedName.ts, 30, 12), Decl(duplicateObjectLiteralProperty_computedName.ts, 31, 12))
73+
}
74+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
=== tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts ===
2+
const t1 = {
3+
>t1 : { 1: number; }
4+
>{ 1: 1, [1]: 0 // duplicate} : { 1: number; }
5+
6+
1: 1,
7+
>1 : number
8+
>1 : 1
9+
10+
[1]: 0 // duplicate
11+
>[1] : number
12+
>1 : 1
13+
>0 : 0
14+
}
15+
16+
const t2 = {
17+
>t2 : { 1: number; }
18+
>{ 1: 1, [+1]: 0 // duplicate} : { 1: number; }
19+
20+
1: 1,
21+
>1 : number
22+
>1 : 1
23+
24+
[+1]: 0 // duplicate
25+
>[+1] : number
26+
>+1 : 1
27+
>1 : 1
28+
>0 : 0
29+
}
30+
31+
const t3 = {
32+
>t3 : { 1: number; }
33+
>{ "1": 1, [+1]: 0 // duplicate} : { 1: number; }
34+
35+
"1": 1,
36+
>"1" : number
37+
>1 : 1
38+
39+
[+1]: 0 // duplicate
40+
>[+1] : number
41+
>+1 : 1
42+
>1 : 1
43+
>0 : 0
44+
}
45+
46+
const t4 = {
47+
>t4 : { "+1": number; 1: number; }
48+
>{ "+1": 1, [+1]: 0 // two different keys, "+1", "1"} : { "+1": number; 1: number; }
49+
50+
"+1": 1,
51+
>"+1" : number
52+
>1 : 1
53+
54+
[+1]: 0 // two different keys, "+1", "1"
55+
>[+1] : number
56+
>+1 : 1
57+
>1 : 1
58+
>0 : 0
59+
}
60+
61+
const t5 = {
62+
>t5 : { "+1": number; }
63+
>{ "+1": 1, ["+1"]: 0 // duplicate} : { "+1": number; }
64+
65+
"+1": 1,
66+
>"+1" : number
67+
>1 : 1
68+
69+
["+1"]: 0 // duplicate
70+
>["+1"] : number
71+
>"+1" : "+1"
72+
>0 : 0
73+
}
74+
75+
const t6 = {
76+
>t6 : { [-1]: number; }
77+
>{ "-1": 1, [-1]: 0 // duplicate} : { [-1]: number; }
78+
79+
"-1": 1,
80+
>"-1" : number
81+
>1 : 1
82+
83+
[-1]: 0 // duplicate
84+
>[-1] : number
85+
>-1 : -1
86+
>1 : 1
87+
>0 : 0
88+
}
89+
90+
const t7 = {
91+
>t7 : { [-1]: number; }
92+
>{ "-1": 1, ["-1"]: 0 // duplicate} : { [-1]: number; }
93+
94+
"-1": 1,
95+
>"-1" : number
96+
>1 : 1
97+
98+
["-1"]: 0 // duplicate
99+
>["-1"] : number
100+
>"-1" : "-1"
101+
>0 : 0
102+
}
103+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const t1 = {
2+
1: 1,
3+
[1]: 0 // duplicate
4+
}
5+
6+
const t2 = {
7+
1: 1,
8+
[+1]: 0 // duplicate
9+
}
10+
11+
const t3 = {
12+
"1": 1,
13+
[+1]: 0 // duplicate
14+
}
15+
16+
const t4 = {
17+
"+1": 1,
18+
[+1]: 0 // two different keys, "+1", "1"
19+
}
20+
21+
const t5 = {
22+
"+1": 1,
23+
["+1"]: 0 // duplicate
24+
}
25+
26+
const t6 = {
27+
"-1": 1,
28+
[-1]: 0 // duplicate
29+
}
30+
31+
const t7 = {
32+
"-1": 1,
33+
["-1"]: 0 // duplicate
34+
}

0 commit comments

Comments
 (0)