Skip to content

fix(38295): Duplicated object key in const not detected when the key is a number preceded by - or + #38607

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 20, 2020
Merged
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
6 changes: 6 additions & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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"]'.
}

Original file line number Diff line number Diff line change
@@ -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);
Original file line number Diff line number Diff line change
@@ -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))
}

Original file line number Diff line number Diff line change
@@ -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
}

Original file line number Diff line number Diff line change
@@ -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
}