Merge pull request #38607 from a-tarasyuk/bug/38295

fix(38295): Duplicated object key in const not detected when the key is a number preceded by `-` or `+`
This commit is contained in:
Nathan Shively-Sanders 2020-05-20 14:52:09 -07:00 committed by GitHub
commit 3340142dda
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 354 additions and 0 deletions

View file

@ -3092,6 +3092,12 @@ namespace ts {
else if (isStringOrNumericLiteralLike(nameExpression)) {
return escapeLeadingUnderscores(nameExpression.text);
}
else if (isSignedNumericLiteral(nameExpression)) {
if (nameExpression.operator === SyntaxKind.MinusToken) {
return tokenToString(nameExpression.operator) + nameExpression.operand.text as __String;
}
return nameExpression.operand.text as __String;
}
return undefined;
default:
return Debug.assertNever(name);

View file

@ -0,0 +1,56 @@
tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts(3,5): error TS2300: Duplicate identifier '[1]'.
tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts(8,5): error TS2300: Duplicate identifier '[+1]'.
tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts(13,5): error TS2300: Duplicate identifier '[+1]'.
tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts(23,5): error TS2300: Duplicate identifier '["+1"]'.
tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts(28,5): error TS2300: Duplicate identifier '[-1]'.
tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts(33,5): error TS2300: Duplicate identifier '["-1"]'.
==== tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts (6 errors) ====
const t1 = {
1: 1,
[1]: 0 // duplicate
~~~
!!! error TS2300: Duplicate identifier '[1]'.
}
const t2 = {
1: 1,
[+1]: 0 // duplicate
~~~~
!!! error TS2300: Duplicate identifier '[+1]'.
}
const t3 = {
"1": 1,
[+1]: 0 // duplicate
~~~~
!!! error TS2300: Duplicate identifier '[+1]'.
}
const t4 = {
"+1": 1,
[+1]: 0 // two different keys, "+1", "1"
}
const t5 = {
"+1": 1,
["+1"]: 0 // duplicate
~~~~~~
!!! error TS2300: Duplicate identifier '["+1"]'.
}
const t6 = {
"-1": 1,
[-1]: 0 // duplicate
~~~~
!!! error TS2300: Duplicate identifier '[-1]'.
}
const t7 = {
"-1": 1,
["-1"]: 0 // duplicate
~~~~~~
!!! error TS2300: Duplicate identifier '["-1"]'.
}

View file

@ -0,0 +1,81 @@
//// [duplicateObjectLiteralProperty_computedName.ts]
const t1 = {
1: 1,
[1]: 0 // duplicate
}
const t2 = {
1: 1,
[+1]: 0 // duplicate
}
const t3 = {
"1": 1,
[+1]: 0 // duplicate
}
const t4 = {
"+1": 1,
[+1]: 0 // two different keys, "+1", "1"
}
const t5 = {
"+1": 1,
["+1"]: 0 // duplicate
}
const t6 = {
"-1": 1,
[-1]: 0 // duplicate
}
const t7 = {
"-1": 1,
["-1"]: 0 // duplicate
}
//// [duplicateObjectLiteralProperty_computedName.js]
var _a, _b, _c, _d, _e, _f, _g;
var t1 = (_a = {
1: 1
},
_a[1] = 0 // duplicate
,
_a);
var t2 = (_b = {
1: 1
},
_b[+1] = 0 // duplicate
,
_b);
var t3 = (_c = {
"1": 1
},
_c[+1] = 0 // duplicate
,
_c);
var t4 = (_d = {
"+1": 1
},
_d[+1] = 0 // two different keys, "+1", "1"
,
_d);
var t5 = (_e = {
"+1": 1
},
_e["+1"] = 0 // duplicate
,
_e);
var t6 = (_f = {
"-1": 1
},
_f[-1] = 0 // duplicate
,
_f);
var t7 = (_g = {
"-1": 1
},
_g["-1"] = 0 // duplicate
,
_g);

View file

@ -0,0 +1,74 @@
=== tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts ===
const t1 = {
>t1 : Symbol(t1, Decl(duplicateObjectLiteralProperty_computedName.ts, 0, 5))
1: 1,
>1 : Symbol(1, Decl(duplicateObjectLiteralProperty_computedName.ts, 0, 12), Decl(duplicateObjectLiteralProperty_computedName.ts, 1, 9))
[1]: 0 // duplicate
>[1] : Symbol(1, Decl(duplicateObjectLiteralProperty_computedName.ts, 0, 12), Decl(duplicateObjectLiteralProperty_computedName.ts, 1, 9))
>1 : Symbol(1, Decl(duplicateObjectLiteralProperty_computedName.ts, 0, 12), Decl(duplicateObjectLiteralProperty_computedName.ts, 1, 9))
}
const t2 = {
>t2 : Symbol(t2, Decl(duplicateObjectLiteralProperty_computedName.ts, 5, 5))
1: 1,
>1 : Symbol(1, Decl(duplicateObjectLiteralProperty_computedName.ts, 5, 12))
[+1]: 0 // duplicate
>[+1] : Symbol([+1], Decl(duplicateObjectLiteralProperty_computedName.ts, 6, 9))
}
const t3 = {
>t3 : Symbol(t3, Decl(duplicateObjectLiteralProperty_computedName.ts, 10, 5))
"1": 1,
>"1" : Symbol("1", Decl(duplicateObjectLiteralProperty_computedName.ts, 10, 12))
[+1]: 0 // duplicate
>[+1] : Symbol([+1], Decl(duplicateObjectLiteralProperty_computedName.ts, 11, 11))
}
const t4 = {
>t4 : Symbol(t4, Decl(duplicateObjectLiteralProperty_computedName.ts, 15, 5))
"+1": 1,
>"+1" : Symbol("+1", Decl(duplicateObjectLiteralProperty_computedName.ts, 15, 12), Decl(duplicateObjectLiteralProperty_computedName.ts, 16, 12))
[+1]: 0 // two different keys, "+1", "1"
>[+1] : Symbol("+1", Decl(duplicateObjectLiteralProperty_computedName.ts, 15, 12), Decl(duplicateObjectLiteralProperty_computedName.ts, 16, 12))
}
const t5 = {
>t5 : Symbol(t5, Decl(duplicateObjectLiteralProperty_computedName.ts, 20, 5))
"+1": 1,
>"+1" : Symbol("+1", Decl(duplicateObjectLiteralProperty_computedName.ts, 20, 12), Decl(duplicateObjectLiteralProperty_computedName.ts, 21, 12))
["+1"]: 0 // duplicate
>["+1"] : Symbol("+1", Decl(duplicateObjectLiteralProperty_computedName.ts, 20, 12), Decl(duplicateObjectLiteralProperty_computedName.ts, 21, 12))
>"+1" : Symbol("+1", Decl(duplicateObjectLiteralProperty_computedName.ts, 20, 12), Decl(duplicateObjectLiteralProperty_computedName.ts, 21, 12))
}
const t6 = {
>t6 : Symbol(t6, Decl(duplicateObjectLiteralProperty_computedName.ts, 25, 5))
"-1": 1,
>"-1" : Symbol("-1", Decl(duplicateObjectLiteralProperty_computedName.ts, 25, 12), Decl(duplicateObjectLiteralProperty_computedName.ts, 26, 12))
[-1]: 0 // duplicate
>[-1] : Symbol("-1", Decl(duplicateObjectLiteralProperty_computedName.ts, 25, 12), Decl(duplicateObjectLiteralProperty_computedName.ts, 26, 12))
}
const t7 = {
>t7 : Symbol(t7, Decl(duplicateObjectLiteralProperty_computedName.ts, 30, 5))
"-1": 1,
>"-1" : Symbol("-1", Decl(duplicateObjectLiteralProperty_computedName.ts, 30, 12), Decl(duplicateObjectLiteralProperty_computedName.ts, 31, 12))
["-1"]: 0 // duplicate
>["-1"] : Symbol("-1", Decl(duplicateObjectLiteralProperty_computedName.ts, 30, 12), Decl(duplicateObjectLiteralProperty_computedName.ts, 31, 12))
>"-1" : Symbol("-1", Decl(duplicateObjectLiteralProperty_computedName.ts, 30, 12), Decl(duplicateObjectLiteralProperty_computedName.ts, 31, 12))
}

View file

@ -0,0 +1,103 @@
=== tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts ===
const t1 = {
>t1 : { 1: number; }
>{ 1: 1, [1]: 0 // duplicate} : { 1: number; }
1: 1,
>1 : number
>1 : 1
[1]: 0 // duplicate
>[1] : number
>1 : 1
>0 : 0
}
const t2 = {
>t2 : { 1: number; }
>{ 1: 1, [+1]: 0 // duplicate} : { 1: number; }
1: 1,
>1 : number
>1 : 1
[+1]: 0 // duplicate
>[+1] : number
>+1 : 1
>1 : 1
>0 : 0
}
const t3 = {
>t3 : { 1: number; }
>{ "1": 1, [+1]: 0 // duplicate} : { 1: number; }
"1": 1,
>"1" : number
>1 : 1
[+1]: 0 // duplicate
>[+1] : number
>+1 : 1
>1 : 1
>0 : 0
}
const t4 = {
>t4 : { "+1": number; 1: number; }
>{ "+1": 1, [+1]: 0 // two different keys, "+1", "1"} : { "+1": number; 1: number; }
"+1": 1,
>"+1" : number
>1 : 1
[+1]: 0 // two different keys, "+1", "1"
>[+1] : number
>+1 : 1
>1 : 1
>0 : 0
}
const t5 = {
>t5 : { "+1": number; }
>{ "+1": 1, ["+1"]: 0 // duplicate} : { "+1": number; }
"+1": 1,
>"+1" : number
>1 : 1
["+1"]: 0 // duplicate
>["+1"] : number
>"+1" : "+1"
>0 : 0
}
const t6 = {
>t6 : { [-1]: number; }
>{ "-1": 1, [-1]: 0 // duplicate} : { [-1]: number; }
"-1": 1,
>"-1" : number
>1 : 1
[-1]: 0 // duplicate
>[-1] : number
>-1 : -1
>1 : 1
>0 : 0
}
const t7 = {
>t7 : { [-1]: number; }
>{ "-1": 1, ["-1"]: 0 // duplicate} : { [-1]: number; }
"-1": 1,
>"-1" : number
>1 : 1
["-1"]: 0 // duplicate
>["-1"] : number
>"-1" : "-1"
>0 : 0
}

View file

@ -0,0 +1,34 @@
const t1 = {
1: 1,
[1]: 0 // duplicate
}
const t2 = {
1: 1,
[+1]: 0 // duplicate
}
const t3 = {
"1": 1,
[+1]: 0 // duplicate
}
const t4 = {
"+1": 1,
[+1]: 0 // two different keys, "+1", "1"
}
const t5 = {
"+1": 1,
["+1"]: 0 // duplicate
}
const t6 = {
"-1": 1,
[-1]: 0 // duplicate
}
const t7 = {
"-1": 1,
["-1"]: 0 // duplicate
}