From e04ab6938a9ad017f9c71e40553455a440e1a5b3 Mon Sep 17 00:00:00 2001 From: Alexander T Date: Sat, 16 May 2020 11:56:07 +0300 Subject: [PATCH] fix(38295): handle duplicate object literal keys which contain '+' and '-' tokens --- src/compiler/utilities.ts | 6 + ...ectLiteralProperty_computedName.errors.txt | 56 ++++++++++ ...icateObjectLiteralProperty_computedName.js | 81 ++++++++++++++ ...ObjectLiteralProperty_computedName.symbols | 74 +++++++++++++ ...teObjectLiteralProperty_computedName.types | 103 ++++++++++++++++++ ...icateObjectLiteralProperty_computedName.ts | 34 ++++++ 6 files changed, 354 insertions(+) create mode 100644 tests/baselines/reference/duplicateObjectLiteralProperty_computedName.errors.txt create mode 100644 tests/baselines/reference/duplicateObjectLiteralProperty_computedName.js create mode 100644 tests/baselines/reference/duplicateObjectLiteralProperty_computedName.symbols create mode 100644 tests/baselines/reference/duplicateObjectLiteralProperty_computedName.types create mode 100644 tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 17ce056212..06d4c90201 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -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); diff --git a/tests/baselines/reference/duplicateObjectLiteralProperty_computedName.errors.txt b/tests/baselines/reference/duplicateObjectLiteralProperty_computedName.errors.txt new file mode 100644 index 0000000000..84fae4c8fb --- /dev/null +++ b/tests/baselines/reference/duplicateObjectLiteralProperty_computedName.errors.txt @@ -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"]'. + } + \ No newline at end of file diff --git a/tests/baselines/reference/duplicateObjectLiteralProperty_computedName.js b/tests/baselines/reference/duplicateObjectLiteralProperty_computedName.js new file mode 100644 index 0000000000..2eedb465be --- /dev/null +++ b/tests/baselines/reference/duplicateObjectLiteralProperty_computedName.js @@ -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); diff --git a/tests/baselines/reference/duplicateObjectLiteralProperty_computedName.symbols b/tests/baselines/reference/duplicateObjectLiteralProperty_computedName.symbols new file mode 100644 index 0000000000..9ed2226656 --- /dev/null +++ b/tests/baselines/reference/duplicateObjectLiteralProperty_computedName.symbols @@ -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)) +} + diff --git a/tests/baselines/reference/duplicateObjectLiteralProperty_computedName.types b/tests/baselines/reference/duplicateObjectLiteralProperty_computedName.types new file mode 100644 index 0000000000..e597508a0c --- /dev/null +++ b/tests/baselines/reference/duplicateObjectLiteralProperty_computedName.types @@ -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 +} + diff --git a/tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts b/tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts new file mode 100644 index 0000000000..9bc5e2e3d9 --- /dev/null +++ b/tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts @@ -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 +}