From b0e1a52231017e6a80f274466e8773c363e7287d Mon Sep 17 00:00:00 2001 From: Oleksandr T Date: Sun, 10 Jul 2022 11:40:58 +0300 Subject: [PATCH 1/3] feat(25758): extend handling duplicate computed properties in object literal --- src/compiler/checker.ts | 48 ++++---- ...tLiteralProperty_computedName1.errors.txt} | 14 +-- ...ateObjectLiteralProperty_computedName1.js} | 4 +- ...jectLiteralProperty_computedName1.symbols} | 50 ++++----- ...ObjectLiteralProperty_computedName1.types} | 2 +- ...ctLiteralProperty_computedName2.errors.txt | 40 +++++++ ...cateObjectLiteralProperty_computedName2.js | 55 +++++++++ ...bjectLiteralProperty_computedName2.symbols | 71 ++++++++++++ ...eObjectLiteralProperty_computedName2.types | 86 ++++++++++++++ ...ctLiteralProperty_computedName3.errors.txt | 43 +++++++ ...cateObjectLiteralProperty_computedName3.js | 67 +++++++++++ ...bjectLiteralProperty_computedName3.symbols | 91 +++++++++++++++ ...eObjectLiteralProperty_computedName3.types | 106 ++++++++++++++++++ ...ateObjectLiteralProperty_computedName1.ts} | 0 ...cateObjectLiteralProperty_computedName2.ts | 24 ++++ ...cateObjectLiteralProperty_computedName3.ts | 28 +++++ 16 files changed, 675 insertions(+), 54 deletions(-) rename tests/baselines/reference/{duplicateObjectLiteralProperty_computedName.errors.txt => duplicateObjectLiteralProperty_computedName1.errors.txt} (73%) rename tests/baselines/reference/{duplicateObjectLiteralProperty_computedName.js => duplicateObjectLiteralProperty_computedName1.js} (86%) rename tests/baselines/reference/{duplicateObjectLiteralProperty_computedName.symbols => duplicateObjectLiteralProperty_computedName1.symbols} (54%) rename tests/baselines/reference/{duplicateObjectLiteralProperty_computedName.types => duplicateObjectLiteralProperty_computedName1.types} (92%) create mode 100644 tests/baselines/reference/duplicateObjectLiteralProperty_computedName2.errors.txt create mode 100644 tests/baselines/reference/duplicateObjectLiteralProperty_computedName2.js create mode 100644 tests/baselines/reference/duplicateObjectLiteralProperty_computedName2.symbols create mode 100644 tests/baselines/reference/duplicateObjectLiteralProperty_computedName2.types create mode 100644 tests/baselines/reference/duplicateObjectLiteralProperty_computedName3.errors.txt create mode 100644 tests/baselines/reference/duplicateObjectLiteralProperty_computedName3.js create mode 100644 tests/baselines/reference/duplicateObjectLiteralProperty_computedName3.symbols create mode 100644 tests/baselines/reference/duplicateObjectLiteralProperty_computedName3.types rename tests/cases/compiler/{duplicateObjectLiteralProperty_computedName.ts => duplicateObjectLiteralProperty_computedName1.ts} (100%) create mode 100644 tests/cases/compiler/duplicateObjectLiteralProperty_computedName2.ts create mode 100644 tests/cases/compiler/duplicateObjectLiteralProperty_computedName3.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 7b6d49cf8f9c4..6afb3c1279680 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -23432,27 +23432,31 @@ m2: ${(this.mapper2 as unknown as DebugTypeMapper).__debugToString().split("\n") } function tryGetElementAccessExpressionName(node: ElementAccessExpression) { - if (isStringOrNumericLiteralLike(node.argumentExpression)) { - return escapeLeadingUnderscores(node.argumentExpression.text); - } - if (isEntityNameExpression(node.argumentExpression)) { - const symbol = resolveEntityName(node.argumentExpression, SymbolFlags.Value, /*ignoreErrors*/ true); - if (!symbol || !isConstVariable(symbol)) return undefined; + return isStringOrNumericLiteralLike(node.argumentExpression) ? escapeLeadingUnderscores(node.argumentExpression.text) : + isEntityNameExpression(node.argumentExpression) ? tryGetNameFromEntityNameExpression(node.argumentExpression) : undefined; + } - const declaration = symbol.valueDeclaration; - if (declaration === undefined) return undefined; + function tryGetNameFromEntityNameExpression(node: EntityNameOrEntityNameExpression) { + const symbol = resolveEntityName(node, SymbolFlags.Value, /*ignoreErrors*/ true); + if (!symbol || !(isConstVariable(symbol) || (symbol.flags & SymbolFlags.EnumMember))) return undefined; - const type = tryGetTypeFromEffectiveTypeNode(declaration); - if (type) { - const name = tryGetNameFromType(type); - if (name !== undefined) { - return name; - } - } + const declaration = symbol.valueDeclaration; + if (declaration === undefined) return undefined; - if (hasOnlyExpressionInitializer(declaration)) { - const initializer = getEffectiveInitializer(declaration); - return initializer && tryGetNameFromType(getTypeOfExpression(initializer)); + const type = tryGetTypeFromEffectiveTypeNode(declaration); + if (type) { + const name = tryGetNameFromType(type); + if (name !== undefined) { + return name; + } + } + if (hasOnlyExpressionInitializer(declaration)) { + const initializer = getEffectiveInitializer(declaration); + if (initializer) { + return tryGetNameFromType(getTypeOfExpression(initializer)); + } + if (isEnumMember(declaration)) { + return getTextOfPropertyName(declaration.name); } } return undefined; @@ -44579,7 +44583,7 @@ m2: ${(this.mapper2 as unknown as DebugTypeMapper).__debugToString().split("\n") } if (!inDestructuring) { - const effectiveName = getPropertyNameForPropertyNameNode(name); + const effectiveName = getEffectivePropertyNameForPropertyName(name); if (effectiveName === undefined) { continue; } @@ -45593,6 +45597,12 @@ m2: ${(this.mapper2 as unknown as DebugTypeMapper).__debugToString().split("\n") } return undefined; } + + function getEffectivePropertyNameForPropertyName(node: PropertyName) { + const name = getPropertyNameForPropertyNameNode(node); + return name ? name : + isComputedPropertyName(node) && isEntityNameExpression(node.expression) ? tryGetNameFromEntityNameExpression(node.expression) : undefined; + } } function isNotAccessor(declaration: Declaration): boolean { diff --git a/tests/baselines/reference/duplicateObjectLiteralProperty_computedName.errors.txt b/tests/baselines/reference/duplicateObjectLiteralProperty_computedName1.errors.txt similarity index 73% rename from tests/baselines/reference/duplicateObjectLiteralProperty_computedName.errors.txt rename to tests/baselines/reference/duplicateObjectLiteralProperty_computedName1.errors.txt index af508272d8462..ad0ea183152a4 100644 --- a/tests/baselines/reference/duplicateObjectLiteralProperty_computedName.errors.txt +++ b/tests/baselines/reference/duplicateObjectLiteralProperty_computedName1.errors.txt @@ -1,12 +1,12 @@ -tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts(3,5): error TS1117: An object literal cannot have multiple properties with the same name. -tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts(8,5): error TS1117: An object literal cannot have multiple properties with the same name. -tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts(13,5): error TS1117: An object literal cannot have multiple properties with the same name. -tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts(23,5): error TS1117: An object literal cannot have multiple properties with the same name. -tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts(28,5): error TS1117: An object literal cannot have multiple properties with the same name. -tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts(33,5): error TS1117: An object literal cannot have multiple properties with the same name. +tests/cases/compiler/duplicateObjectLiteralProperty_computedName1.ts(3,5): error TS1117: An object literal cannot have multiple properties with the same name. +tests/cases/compiler/duplicateObjectLiteralProperty_computedName1.ts(8,5): error TS1117: An object literal cannot have multiple properties with the same name. +tests/cases/compiler/duplicateObjectLiteralProperty_computedName1.ts(13,5): error TS1117: An object literal cannot have multiple properties with the same name. +tests/cases/compiler/duplicateObjectLiteralProperty_computedName1.ts(23,5): error TS1117: An object literal cannot have multiple properties with the same name. +tests/cases/compiler/duplicateObjectLiteralProperty_computedName1.ts(28,5): error TS1117: An object literal cannot have multiple properties with the same name. +tests/cases/compiler/duplicateObjectLiteralProperty_computedName1.ts(33,5): error TS1117: An object literal cannot have multiple properties with the same name. -==== tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts (6 errors) ==== +==== tests/cases/compiler/duplicateObjectLiteralProperty_computedName1.ts (6 errors) ==== const t1 = { 1: 1, [1]: 0 // duplicate diff --git a/tests/baselines/reference/duplicateObjectLiteralProperty_computedName.js b/tests/baselines/reference/duplicateObjectLiteralProperty_computedName1.js similarity index 86% rename from tests/baselines/reference/duplicateObjectLiteralProperty_computedName.js rename to tests/baselines/reference/duplicateObjectLiteralProperty_computedName1.js index 2eedb465be592..08be4f356e376 100644 --- a/tests/baselines/reference/duplicateObjectLiteralProperty_computedName.js +++ b/tests/baselines/reference/duplicateObjectLiteralProperty_computedName1.js @@ -1,4 +1,4 @@ -//// [duplicateObjectLiteralProperty_computedName.ts] +//// [duplicateObjectLiteralProperty_computedName1.ts] const t1 = { 1: 1, [1]: 0 // duplicate @@ -35,7 +35,7 @@ const t7 = { } -//// [duplicateObjectLiteralProperty_computedName.js] +//// [duplicateObjectLiteralProperty_computedName1.js] var _a, _b, _c, _d, _e, _f, _g; var t1 = (_a = { 1: 1 diff --git a/tests/baselines/reference/duplicateObjectLiteralProperty_computedName.symbols b/tests/baselines/reference/duplicateObjectLiteralProperty_computedName1.symbols similarity index 54% rename from tests/baselines/reference/duplicateObjectLiteralProperty_computedName.symbols rename to tests/baselines/reference/duplicateObjectLiteralProperty_computedName1.symbols index 9ed2226656d0a..8fb89d715c37a 100644 --- a/tests/baselines/reference/duplicateObjectLiteralProperty_computedName.symbols +++ b/tests/baselines/reference/duplicateObjectLiteralProperty_computedName1.symbols @@ -1,74 +1,74 @@ -=== tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts === +=== tests/cases/compiler/duplicateObjectLiteralProperty_computedName1.ts === const t1 = { ->t1 : Symbol(t1, Decl(duplicateObjectLiteralProperty_computedName.ts, 0, 5)) +>t1 : Symbol(t1, Decl(duplicateObjectLiteralProperty_computedName1.ts, 0, 5)) 1: 1, ->1 : Symbol(1, Decl(duplicateObjectLiteralProperty_computedName.ts, 0, 12), Decl(duplicateObjectLiteralProperty_computedName.ts, 1, 9)) +>1 : Symbol(1, Decl(duplicateObjectLiteralProperty_computedName1.ts, 0, 12), Decl(duplicateObjectLiteralProperty_computedName1.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)) +>[1] : Symbol(1, Decl(duplicateObjectLiteralProperty_computedName1.ts, 0, 12), Decl(duplicateObjectLiteralProperty_computedName1.ts, 1, 9)) +>1 : Symbol(1, Decl(duplicateObjectLiteralProperty_computedName1.ts, 0, 12), Decl(duplicateObjectLiteralProperty_computedName1.ts, 1, 9)) } const t2 = { ->t2 : Symbol(t2, Decl(duplicateObjectLiteralProperty_computedName.ts, 5, 5)) +>t2 : Symbol(t2, Decl(duplicateObjectLiteralProperty_computedName1.ts, 5, 5)) 1: 1, ->1 : Symbol(1, Decl(duplicateObjectLiteralProperty_computedName.ts, 5, 12)) +>1 : Symbol(1, Decl(duplicateObjectLiteralProperty_computedName1.ts, 5, 12)) [+1]: 0 // duplicate ->[+1] : Symbol([+1], Decl(duplicateObjectLiteralProperty_computedName.ts, 6, 9)) +>[+1] : Symbol([+1], Decl(duplicateObjectLiteralProperty_computedName1.ts, 6, 9)) } const t3 = { ->t3 : Symbol(t3, Decl(duplicateObjectLiteralProperty_computedName.ts, 10, 5)) +>t3 : Symbol(t3, Decl(duplicateObjectLiteralProperty_computedName1.ts, 10, 5)) "1": 1, ->"1" : Symbol("1", Decl(duplicateObjectLiteralProperty_computedName.ts, 10, 12)) +>"1" : Symbol("1", Decl(duplicateObjectLiteralProperty_computedName1.ts, 10, 12)) [+1]: 0 // duplicate ->[+1] : Symbol([+1], Decl(duplicateObjectLiteralProperty_computedName.ts, 11, 11)) +>[+1] : Symbol([+1], Decl(duplicateObjectLiteralProperty_computedName1.ts, 11, 11)) } const t4 = { ->t4 : Symbol(t4, Decl(duplicateObjectLiteralProperty_computedName.ts, 15, 5)) +>t4 : Symbol(t4, Decl(duplicateObjectLiteralProperty_computedName1.ts, 15, 5)) "+1": 1, ->"+1" : Symbol("+1", Decl(duplicateObjectLiteralProperty_computedName.ts, 15, 12), Decl(duplicateObjectLiteralProperty_computedName.ts, 16, 12)) +>"+1" : Symbol("+1", Decl(duplicateObjectLiteralProperty_computedName1.ts, 15, 12), Decl(duplicateObjectLiteralProperty_computedName1.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)) +>[+1] : Symbol("+1", Decl(duplicateObjectLiteralProperty_computedName1.ts, 15, 12), Decl(duplicateObjectLiteralProperty_computedName1.ts, 16, 12)) } const t5 = { ->t5 : Symbol(t5, Decl(duplicateObjectLiteralProperty_computedName.ts, 20, 5)) +>t5 : Symbol(t5, Decl(duplicateObjectLiteralProperty_computedName1.ts, 20, 5)) "+1": 1, ->"+1" : Symbol("+1", Decl(duplicateObjectLiteralProperty_computedName.ts, 20, 12), Decl(duplicateObjectLiteralProperty_computedName.ts, 21, 12)) +>"+1" : Symbol("+1", Decl(duplicateObjectLiteralProperty_computedName1.ts, 20, 12), Decl(duplicateObjectLiteralProperty_computedName1.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)) +>["+1"] : Symbol("+1", Decl(duplicateObjectLiteralProperty_computedName1.ts, 20, 12), Decl(duplicateObjectLiteralProperty_computedName1.ts, 21, 12)) +>"+1" : Symbol("+1", Decl(duplicateObjectLiteralProperty_computedName1.ts, 20, 12), Decl(duplicateObjectLiteralProperty_computedName1.ts, 21, 12)) } const t6 = { ->t6 : Symbol(t6, Decl(duplicateObjectLiteralProperty_computedName.ts, 25, 5)) +>t6 : Symbol(t6, Decl(duplicateObjectLiteralProperty_computedName1.ts, 25, 5)) "-1": 1, ->"-1" : Symbol("-1", Decl(duplicateObjectLiteralProperty_computedName.ts, 25, 12), Decl(duplicateObjectLiteralProperty_computedName.ts, 26, 12)) +>"-1" : Symbol("-1", Decl(duplicateObjectLiteralProperty_computedName1.ts, 25, 12), Decl(duplicateObjectLiteralProperty_computedName1.ts, 26, 12)) [-1]: 0 // duplicate ->[-1] : Symbol("-1", Decl(duplicateObjectLiteralProperty_computedName.ts, 25, 12), Decl(duplicateObjectLiteralProperty_computedName.ts, 26, 12)) +>[-1] : Symbol("-1", Decl(duplicateObjectLiteralProperty_computedName1.ts, 25, 12), Decl(duplicateObjectLiteralProperty_computedName1.ts, 26, 12)) } const t7 = { ->t7 : Symbol(t7, Decl(duplicateObjectLiteralProperty_computedName.ts, 30, 5)) +>t7 : Symbol(t7, Decl(duplicateObjectLiteralProperty_computedName1.ts, 30, 5)) "-1": 1, ->"-1" : Symbol("-1", Decl(duplicateObjectLiteralProperty_computedName.ts, 30, 12), Decl(duplicateObjectLiteralProperty_computedName.ts, 31, 12)) +>"-1" : Symbol("-1", Decl(duplicateObjectLiteralProperty_computedName1.ts, 30, 12), Decl(duplicateObjectLiteralProperty_computedName1.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)) +>["-1"] : Symbol("-1", Decl(duplicateObjectLiteralProperty_computedName1.ts, 30, 12), Decl(duplicateObjectLiteralProperty_computedName1.ts, 31, 12)) +>"-1" : Symbol("-1", Decl(duplicateObjectLiteralProperty_computedName1.ts, 30, 12), Decl(duplicateObjectLiteralProperty_computedName1.ts, 31, 12)) } diff --git a/tests/baselines/reference/duplicateObjectLiteralProperty_computedName.types b/tests/baselines/reference/duplicateObjectLiteralProperty_computedName1.types similarity index 92% rename from tests/baselines/reference/duplicateObjectLiteralProperty_computedName.types rename to tests/baselines/reference/duplicateObjectLiteralProperty_computedName1.types index e597508a0c962..80687f5df3450 100644 --- a/tests/baselines/reference/duplicateObjectLiteralProperty_computedName.types +++ b/tests/baselines/reference/duplicateObjectLiteralProperty_computedName1.types @@ -1,4 +1,4 @@ -=== tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts === +=== tests/cases/compiler/duplicateObjectLiteralProperty_computedName1.ts === const t1 = { >t1 : { 1: number; } >{ 1: 1, [1]: 0 // duplicate} : { 1: number; } diff --git a/tests/baselines/reference/duplicateObjectLiteralProperty_computedName2.errors.txt b/tests/baselines/reference/duplicateObjectLiteralProperty_computedName2.errors.txt new file mode 100644 index 0000000000000..c71f79694b0e1 --- /dev/null +++ b/tests/baselines/reference/duplicateObjectLiteralProperty_computedName2.errors.txt @@ -0,0 +1,40 @@ +tests/cases/compiler/duplicateObjectLiteralProperty_computedName2.ts(8,5): error TS1117: An object literal cannot have multiple properties with the same name. +tests/cases/compiler/duplicateObjectLiteralProperty_computedName2.ts(13,5): error TS1117: An object literal cannot have multiple properties with the same name. +tests/cases/compiler/duplicateObjectLiteralProperty_computedName2.ts(18,5): error TS1117: An object literal cannot have multiple properties with the same name. +tests/cases/compiler/duplicateObjectLiteralProperty_computedName2.ts(23,5): error TS1117: An object literal cannot have multiple properties with the same name. + + +==== tests/cases/compiler/duplicateObjectLiteralProperty_computedName2.ts (4 errors) ==== + const n = 1; + const s = "s"; + enum E1 { A = "ENUM_KEY" } + enum E2 { B } + + const t1 = { + [n]: 1, + [n]: 1, // duplicate + ~~~ +!!! error TS1117: An object literal cannot have multiple properties with the same name. + } + + const t2 = { + [s]: 1, + [s]: 1, // duplicate + ~~~ +!!! error TS1117: An object literal cannot have multiple properties with the same name. + } + + const t3 = { + [E1.A]: 1, + [E1.A]: 1, // duplicate + ~~~~~~ +!!! error TS1117: An object literal cannot have multiple properties with the same name. + } + + const t4 = { + [E2.B]: 1, + [E2.B]: 1, // duplicate + ~~~~~~ +!!! error TS1117: An object literal cannot have multiple properties with the same name. + } + \ No newline at end of file diff --git a/tests/baselines/reference/duplicateObjectLiteralProperty_computedName2.js b/tests/baselines/reference/duplicateObjectLiteralProperty_computedName2.js new file mode 100644 index 0000000000000..f17f5cebc805e --- /dev/null +++ b/tests/baselines/reference/duplicateObjectLiteralProperty_computedName2.js @@ -0,0 +1,55 @@ +//// [duplicateObjectLiteralProperty_computedName2.ts] +const n = 1; +const s = "s"; +enum E1 { A = "ENUM_KEY" } +enum E2 { B } + +const t1 = { + [n]: 1, + [n]: 1, // duplicate +} + +const t2 = { + [s]: 1, + [s]: 1, // duplicate +} + +const t3 = { + [E1.A]: 1, + [E1.A]: 1, // duplicate +} + +const t4 = { + [E2.B]: 1, + [E2.B]: 1, // duplicate +} + + +//// [duplicateObjectLiteralProperty_computedName2.js] +var _a, _b, _c, _d; +var n = 1; +var s = "s"; +var E1; +(function (E1) { + E1["A"] = "ENUM_KEY"; +})(E1 || (E1 = {})); +var E2; +(function (E2) { + E2[E2["B"] = 0] = "B"; +})(E2 || (E2 = {})); +var t1 = (_a = {}, + _a[n] = 1, + _a[n] = 1, + _a); +var t2 = (_b = {}, + _b[s] = 1, + _b[s] = 1, + _b); +var t3 = (_c = {}, + _c[E1.A] = 1, + _c[E1.A] = 1, + _c); +var t4 = (_d = {}, + _d[E2.B] = 1, + _d[E2.B] = 1, + _d); diff --git a/tests/baselines/reference/duplicateObjectLiteralProperty_computedName2.symbols b/tests/baselines/reference/duplicateObjectLiteralProperty_computedName2.symbols new file mode 100644 index 0000000000000..932f80697f93e --- /dev/null +++ b/tests/baselines/reference/duplicateObjectLiteralProperty_computedName2.symbols @@ -0,0 +1,71 @@ +=== tests/cases/compiler/duplicateObjectLiteralProperty_computedName2.ts === +const n = 1; +>n : Symbol(n, Decl(duplicateObjectLiteralProperty_computedName2.ts, 0, 5)) + +const s = "s"; +>s : Symbol(s, Decl(duplicateObjectLiteralProperty_computedName2.ts, 1, 5)) + +enum E1 { A = "ENUM_KEY" } +>E1 : Symbol(E1, Decl(duplicateObjectLiteralProperty_computedName2.ts, 1, 14)) +>A : Symbol(E1.A, Decl(duplicateObjectLiteralProperty_computedName2.ts, 2, 9)) + +enum E2 { B } +>E2 : Symbol(E2, Decl(duplicateObjectLiteralProperty_computedName2.ts, 2, 26)) +>B : Symbol(E2.B, Decl(duplicateObjectLiteralProperty_computedName2.ts, 3, 9)) + +const t1 = { +>t1 : Symbol(t1, Decl(duplicateObjectLiteralProperty_computedName2.ts, 5, 5)) + + [n]: 1, +>[n] : Symbol([n], Decl(duplicateObjectLiteralProperty_computedName2.ts, 5, 12), Decl(duplicateObjectLiteralProperty_computedName2.ts, 6, 11)) +>n : Symbol(n, Decl(duplicateObjectLiteralProperty_computedName2.ts, 0, 5)) + + [n]: 1, // duplicate +>[n] : Symbol([n], Decl(duplicateObjectLiteralProperty_computedName2.ts, 5, 12), Decl(duplicateObjectLiteralProperty_computedName2.ts, 6, 11)) +>n : Symbol(n, Decl(duplicateObjectLiteralProperty_computedName2.ts, 0, 5)) +} + +const t2 = { +>t2 : Symbol(t2, Decl(duplicateObjectLiteralProperty_computedName2.ts, 10, 5)) + + [s]: 1, +>[s] : Symbol([s], Decl(duplicateObjectLiteralProperty_computedName2.ts, 10, 12), Decl(duplicateObjectLiteralProperty_computedName2.ts, 11, 11)) +>s : Symbol(s, Decl(duplicateObjectLiteralProperty_computedName2.ts, 1, 5)) + + [s]: 1, // duplicate +>[s] : Symbol([s], Decl(duplicateObjectLiteralProperty_computedName2.ts, 10, 12), Decl(duplicateObjectLiteralProperty_computedName2.ts, 11, 11)) +>s : Symbol(s, Decl(duplicateObjectLiteralProperty_computedName2.ts, 1, 5)) +} + +const t3 = { +>t3 : Symbol(t3, Decl(duplicateObjectLiteralProperty_computedName2.ts, 15, 5)) + + [E1.A]: 1, +>[E1.A] : Symbol([E1.A], Decl(duplicateObjectLiteralProperty_computedName2.ts, 15, 12), Decl(duplicateObjectLiteralProperty_computedName2.ts, 16, 14)) +>E1.A : Symbol(E1.A, Decl(duplicateObjectLiteralProperty_computedName2.ts, 2, 9)) +>E1 : Symbol(E1, Decl(duplicateObjectLiteralProperty_computedName2.ts, 1, 14)) +>A : Symbol(E1.A, Decl(duplicateObjectLiteralProperty_computedName2.ts, 2, 9)) + + [E1.A]: 1, // duplicate +>[E1.A] : Symbol([E1.A], Decl(duplicateObjectLiteralProperty_computedName2.ts, 15, 12), Decl(duplicateObjectLiteralProperty_computedName2.ts, 16, 14)) +>E1.A : Symbol(E1.A, Decl(duplicateObjectLiteralProperty_computedName2.ts, 2, 9)) +>E1 : Symbol(E1, Decl(duplicateObjectLiteralProperty_computedName2.ts, 1, 14)) +>A : Symbol(E1.A, Decl(duplicateObjectLiteralProperty_computedName2.ts, 2, 9)) +} + +const t4 = { +>t4 : Symbol(t4, Decl(duplicateObjectLiteralProperty_computedName2.ts, 20, 5)) + + [E2.B]: 1, +>[E2.B] : Symbol([E2.B], Decl(duplicateObjectLiteralProperty_computedName2.ts, 20, 12), Decl(duplicateObjectLiteralProperty_computedName2.ts, 21, 14)) +>E2.B : Symbol(E2.B, Decl(duplicateObjectLiteralProperty_computedName2.ts, 3, 9)) +>E2 : Symbol(E2, Decl(duplicateObjectLiteralProperty_computedName2.ts, 2, 26)) +>B : Symbol(E2.B, Decl(duplicateObjectLiteralProperty_computedName2.ts, 3, 9)) + + [E2.B]: 1, // duplicate +>[E2.B] : Symbol([E2.B], Decl(duplicateObjectLiteralProperty_computedName2.ts, 20, 12), Decl(duplicateObjectLiteralProperty_computedName2.ts, 21, 14)) +>E2.B : Symbol(E2.B, Decl(duplicateObjectLiteralProperty_computedName2.ts, 3, 9)) +>E2 : Symbol(E2, Decl(duplicateObjectLiteralProperty_computedName2.ts, 2, 26)) +>B : Symbol(E2.B, Decl(duplicateObjectLiteralProperty_computedName2.ts, 3, 9)) +} + diff --git a/tests/baselines/reference/duplicateObjectLiteralProperty_computedName2.types b/tests/baselines/reference/duplicateObjectLiteralProperty_computedName2.types new file mode 100644 index 0000000000000..66c09805b220f --- /dev/null +++ b/tests/baselines/reference/duplicateObjectLiteralProperty_computedName2.types @@ -0,0 +1,86 @@ +=== tests/cases/compiler/duplicateObjectLiteralProperty_computedName2.ts === +const n = 1; +>n : 1 +>1 : 1 + +const s = "s"; +>s : "s" +>"s" : "s" + +enum E1 { A = "ENUM_KEY" } +>E1 : E1 +>A : E1.A +>"ENUM_KEY" : "ENUM_KEY" + +enum E2 { B } +>E2 : E2 +>B : E2.B + +const t1 = { +>t1 : { 1: number; } +>{ [n]: 1, [n]: 1, // duplicate} : { 1: number; } + + [n]: 1, +>[n] : number +>n : 1 +>1 : 1 + + [n]: 1, // duplicate +>[n] : number +>n : 1 +>1 : 1 +} + +const t2 = { +>t2 : { s: number; } +>{ [s]: 1, [s]: 1, // duplicate} : { s: number; } + + [s]: 1, +>[s] : number +>s : "s" +>1 : 1 + + [s]: 1, // duplicate +>[s] : number +>s : "s" +>1 : 1 +} + +const t3 = { +>t3 : { ENUM_KEY: number; } +>{ [E1.A]: 1, [E1.A]: 1, // duplicate} : { ENUM_KEY: number; } + + [E1.A]: 1, +>[E1.A] : number +>E1.A : E1 +>E1 : typeof E1 +>A : E1 +>1 : 1 + + [E1.A]: 1, // duplicate +>[E1.A] : number +>E1.A : E1 +>E1 : typeof E1 +>A : E1 +>1 : 1 +} + +const t4 = { +>t4 : { 0: number; } +>{ [E2.B]: 1, [E2.B]: 1, // duplicate} : { 0: number; } + + [E2.B]: 1, +>[E2.B] : number +>E2.B : E2 +>E2 : typeof E2 +>B : E2 +>1 : 1 + + [E2.B]: 1, // duplicate +>[E2.B] : number +>E2.B : E2 +>E2 : typeof E2 +>B : E2 +>1 : 1 +} + diff --git a/tests/baselines/reference/duplicateObjectLiteralProperty_computedName3.errors.txt b/tests/baselines/reference/duplicateObjectLiteralProperty_computedName3.errors.txt new file mode 100644 index 0000000000000..0b0a5c5ded930 --- /dev/null +++ b/tests/baselines/reference/duplicateObjectLiteralProperty_computedName3.errors.txt @@ -0,0 +1,43 @@ +tests/cases/compiler/b.ts(5,5): error TS1117: An object literal cannot have multiple properties with the same name. +tests/cases/compiler/b.ts(10,5): error TS1117: An object literal cannot have multiple properties with the same name. +tests/cases/compiler/b.ts(15,5): error TS1117: An object literal cannot have multiple properties with the same name. +tests/cases/compiler/b.ts(20,5): error TS1117: An object literal cannot have multiple properties with the same name. + + +==== tests/cases/compiler/a.ts (0 errors) ==== + export const n = 1; + export const s = "s"; + export enum E1 { A = "ENUM_KEY" } + export enum E2 { B } + +==== tests/cases/compiler/b.ts (4 errors) ==== + import * as keys from "./a"; + + const t1 = { + [keys.n]: 1, + [keys.n]: 1, // duplicate + ~~~~~~~~ +!!! error TS1117: An object literal cannot have multiple properties with the same name. + } + + const t2 = { + [keys.s]: 1, + [keys.s]: 1, // duplicate + ~~~~~~~~ +!!! error TS1117: An object literal cannot have multiple properties with the same name. + } + + const t3 = { + [keys.E1.A]: 1, + [keys.E1.A]: 1, // duplicate + ~~~~~~~~~~~ +!!! error TS1117: An object literal cannot have multiple properties with the same name. + } + + const t4 = { + [keys.E2.B]: 1, + [keys.E2.B]: 1, // duplicate + ~~~~~~~~~~~ +!!! error TS1117: An object literal cannot have multiple properties with the same name. + } + \ No newline at end of file diff --git a/tests/baselines/reference/duplicateObjectLiteralProperty_computedName3.js b/tests/baselines/reference/duplicateObjectLiteralProperty_computedName3.js new file mode 100644 index 0000000000000..3ad4ac91ae4f9 --- /dev/null +++ b/tests/baselines/reference/duplicateObjectLiteralProperty_computedName3.js @@ -0,0 +1,67 @@ +//// [tests/cases/compiler/duplicateObjectLiteralProperty_computedName3.ts] //// + +//// [a.ts] +export const n = 1; +export const s = "s"; +export enum E1 { A = "ENUM_KEY" } +export enum E2 { B } + +//// [b.ts] +import * as keys from "./a"; + +const t1 = { + [keys.n]: 1, + [keys.n]: 1, // duplicate +} + +const t2 = { + [keys.s]: 1, + [keys.s]: 1, // duplicate +} + +const t3 = { + [keys.E1.A]: 1, + [keys.E1.A]: 1, // duplicate +} + +const t4 = { + [keys.E2.B]: 1, + [keys.E2.B]: 1, // duplicate +} + + +//// [a.js] +"use strict"; +exports.__esModule = true; +exports.E2 = exports.E1 = exports.s = exports.n = void 0; +exports.n = 1; +exports.s = "s"; +var E1; +(function (E1) { + E1["A"] = "ENUM_KEY"; +})(E1 = exports.E1 || (exports.E1 = {})); +var E2; +(function (E2) { + E2[E2["B"] = 0] = "B"; +})(E2 = exports.E2 || (exports.E2 = {})); +//// [b.js] +"use strict"; +var _a, _b, _c, _d; +exports.__esModule = true; +var keys = require("./a"); +var t1 = (_a = {}, + _a[keys.n] = 1, + _a[keys.n] = 1, + _a); +var t2 = (_b = {}, + _b[keys.s] = 1, + _b[keys.s] = 1, + _b); +var t3 = (_c = {}, + _c[keys.E1.A] = 1, + _c[keys.E1.A] = 1, + _c); +var t4 = (_d = {}, + _d[keys.E2.B] = 1, + _d[keys.E2.B] = 1, + _d); diff --git a/tests/baselines/reference/duplicateObjectLiteralProperty_computedName3.symbols b/tests/baselines/reference/duplicateObjectLiteralProperty_computedName3.symbols new file mode 100644 index 0000000000000..e873bfff9ec0d --- /dev/null +++ b/tests/baselines/reference/duplicateObjectLiteralProperty_computedName3.symbols @@ -0,0 +1,91 @@ +=== tests/cases/compiler/a.ts === +export const n = 1; +>n : Symbol(n, Decl(a.ts, 0, 12)) + +export const s = "s"; +>s : Symbol(s, Decl(a.ts, 1, 12)) + +export enum E1 { A = "ENUM_KEY" } +>E1 : Symbol(E1, Decl(a.ts, 1, 21)) +>A : Symbol(E1.A, Decl(a.ts, 2, 16)) + +export enum E2 { B } +>E2 : Symbol(E2, Decl(a.ts, 2, 33)) +>B : Symbol(E2.B, Decl(a.ts, 3, 16)) + +=== tests/cases/compiler/b.ts === +import * as keys from "./a"; +>keys : Symbol(keys, Decl(b.ts, 0, 6)) + +const t1 = { +>t1 : Symbol(t1, Decl(b.ts, 2, 5)) + + [keys.n]: 1, +>[keys.n] : Symbol([keys.n], Decl(b.ts, 2, 12), Decl(b.ts, 3, 16)) +>keys.n : Symbol(keys.n, Decl(a.ts, 0, 12)) +>keys : Symbol(keys, Decl(b.ts, 0, 6)) +>n : Symbol(keys.n, Decl(a.ts, 0, 12)) + + [keys.n]: 1, // duplicate +>[keys.n] : Symbol([keys.n], Decl(b.ts, 2, 12), Decl(b.ts, 3, 16)) +>keys.n : Symbol(keys.n, Decl(a.ts, 0, 12)) +>keys : Symbol(keys, Decl(b.ts, 0, 6)) +>n : Symbol(keys.n, Decl(a.ts, 0, 12)) +} + +const t2 = { +>t2 : Symbol(t2, Decl(b.ts, 7, 5)) + + [keys.s]: 1, +>[keys.s] : Symbol([keys.s], Decl(b.ts, 7, 12), Decl(b.ts, 8, 16)) +>keys.s : Symbol(keys.s, Decl(a.ts, 1, 12)) +>keys : Symbol(keys, Decl(b.ts, 0, 6)) +>s : Symbol(keys.s, Decl(a.ts, 1, 12)) + + [keys.s]: 1, // duplicate +>[keys.s] : Symbol([keys.s], Decl(b.ts, 7, 12), Decl(b.ts, 8, 16)) +>keys.s : Symbol(keys.s, Decl(a.ts, 1, 12)) +>keys : Symbol(keys, Decl(b.ts, 0, 6)) +>s : Symbol(keys.s, Decl(a.ts, 1, 12)) +} + +const t3 = { +>t3 : Symbol(t3, Decl(b.ts, 12, 5)) + + [keys.E1.A]: 1, +>[keys.E1.A] : Symbol([keys.E1.A], Decl(b.ts, 12, 12), Decl(b.ts, 13, 19)) +>keys.E1.A : Symbol(keys.E1.A, Decl(a.ts, 2, 16)) +>keys.E1 : Symbol(keys.E1, Decl(a.ts, 1, 21)) +>keys : Symbol(keys, Decl(b.ts, 0, 6)) +>E1 : Symbol(keys.E1, Decl(a.ts, 1, 21)) +>A : Symbol(keys.E1.A, Decl(a.ts, 2, 16)) + + [keys.E1.A]: 1, // duplicate +>[keys.E1.A] : Symbol([keys.E1.A], Decl(b.ts, 12, 12), Decl(b.ts, 13, 19)) +>keys.E1.A : Symbol(keys.E1.A, Decl(a.ts, 2, 16)) +>keys.E1 : Symbol(keys.E1, Decl(a.ts, 1, 21)) +>keys : Symbol(keys, Decl(b.ts, 0, 6)) +>E1 : Symbol(keys.E1, Decl(a.ts, 1, 21)) +>A : Symbol(keys.E1.A, Decl(a.ts, 2, 16)) +} + +const t4 = { +>t4 : Symbol(t4, Decl(b.ts, 17, 5)) + + [keys.E2.B]: 1, +>[keys.E2.B] : Symbol([keys.E2.B], Decl(b.ts, 17, 12), Decl(b.ts, 18, 19)) +>keys.E2.B : Symbol(keys.E2.B, Decl(a.ts, 3, 16)) +>keys.E2 : Symbol(keys.E2, Decl(a.ts, 2, 33)) +>keys : Symbol(keys, Decl(b.ts, 0, 6)) +>E2 : Symbol(keys.E2, Decl(a.ts, 2, 33)) +>B : Symbol(keys.E2.B, Decl(a.ts, 3, 16)) + + [keys.E2.B]: 1, // duplicate +>[keys.E2.B] : Symbol([keys.E2.B], Decl(b.ts, 17, 12), Decl(b.ts, 18, 19)) +>keys.E2.B : Symbol(keys.E2.B, Decl(a.ts, 3, 16)) +>keys.E2 : Symbol(keys.E2, Decl(a.ts, 2, 33)) +>keys : Symbol(keys, Decl(b.ts, 0, 6)) +>E2 : Symbol(keys.E2, Decl(a.ts, 2, 33)) +>B : Symbol(keys.E2.B, Decl(a.ts, 3, 16)) +} + diff --git a/tests/baselines/reference/duplicateObjectLiteralProperty_computedName3.types b/tests/baselines/reference/duplicateObjectLiteralProperty_computedName3.types new file mode 100644 index 0000000000000..cd5c0fb8f9125 --- /dev/null +++ b/tests/baselines/reference/duplicateObjectLiteralProperty_computedName3.types @@ -0,0 +1,106 @@ +=== tests/cases/compiler/a.ts === +export const n = 1; +>n : 1 +>1 : 1 + +export const s = "s"; +>s : "s" +>"s" : "s" + +export enum E1 { A = "ENUM_KEY" } +>E1 : E1 +>A : E1.A +>"ENUM_KEY" : "ENUM_KEY" + +export enum E2 { B } +>E2 : E2 +>B : E2.B + +=== tests/cases/compiler/b.ts === +import * as keys from "./a"; +>keys : typeof keys + +const t1 = { +>t1 : { 1: number; } +>{ [keys.n]: 1, [keys.n]: 1, // duplicate} : { 1: number; } + + [keys.n]: 1, +>[keys.n] : number +>keys.n : 1 +>keys : typeof keys +>n : 1 +>1 : 1 + + [keys.n]: 1, // duplicate +>[keys.n] : number +>keys.n : 1 +>keys : typeof keys +>n : 1 +>1 : 1 +} + +const t2 = { +>t2 : { s: number; } +>{ [keys.s]: 1, [keys.s]: 1, // duplicate} : { s: number; } + + [keys.s]: 1, +>[keys.s] : number +>keys.s : "s" +>keys : typeof keys +>s : "s" +>1 : 1 + + [keys.s]: 1, // duplicate +>[keys.s] : number +>keys.s : "s" +>keys : typeof keys +>s : "s" +>1 : 1 +} + +const t3 = { +>t3 : { ENUM_KEY: number; } +>{ [keys.E1.A]: 1, [keys.E1.A]: 1, // duplicate} : { ENUM_KEY: number; } + + [keys.E1.A]: 1, +>[keys.E1.A] : number +>keys.E1.A : keys.E1 +>keys.E1 : typeof keys.E1 +>keys : typeof keys +>E1 : typeof keys.E1 +>A : keys.E1 +>1 : 1 + + [keys.E1.A]: 1, // duplicate +>[keys.E1.A] : number +>keys.E1.A : keys.E1 +>keys.E1 : typeof keys.E1 +>keys : typeof keys +>E1 : typeof keys.E1 +>A : keys.E1 +>1 : 1 +} + +const t4 = { +>t4 : { 0: number; } +>{ [keys.E2.B]: 1, [keys.E2.B]: 1, // duplicate} : { 0: number; } + + [keys.E2.B]: 1, +>[keys.E2.B] : number +>keys.E2.B : keys.E2 +>keys.E2 : typeof keys.E2 +>keys : typeof keys +>E2 : typeof keys.E2 +>B : keys.E2 +>1 : 1 + + [keys.E2.B]: 1, // duplicate +>[keys.E2.B] : number +>keys.E2.B : keys.E2 +>keys.E2 : typeof keys.E2 +>keys : typeof keys +>E2 : typeof keys.E2 +>B : keys.E2 +>1 : 1 +} + diff --git a/tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts b/tests/cases/compiler/duplicateObjectLiteralProperty_computedName1.ts similarity index 100% rename from tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts rename to tests/cases/compiler/duplicateObjectLiteralProperty_computedName1.ts diff --git a/tests/cases/compiler/duplicateObjectLiteralProperty_computedName2.ts b/tests/cases/compiler/duplicateObjectLiteralProperty_computedName2.ts new file mode 100644 index 0000000000000..ff56ce9cfd486 --- /dev/null +++ b/tests/cases/compiler/duplicateObjectLiteralProperty_computedName2.ts @@ -0,0 +1,24 @@ +const n = 1; +const s = "s"; +enum E1 { A = "ENUM_KEY" } +enum E2 { B } + +const t1 = { + [n]: 1, + [n]: 1, // duplicate +} + +const t2 = { + [s]: 1, + [s]: 1, // duplicate +} + +const t3 = { + [E1.A]: 1, + [E1.A]: 1, // duplicate +} + +const t4 = { + [E2.B]: 1, + [E2.B]: 1, // duplicate +} diff --git a/tests/cases/compiler/duplicateObjectLiteralProperty_computedName3.ts b/tests/cases/compiler/duplicateObjectLiteralProperty_computedName3.ts new file mode 100644 index 0000000000000..56092bcb32f8f --- /dev/null +++ b/tests/cases/compiler/duplicateObjectLiteralProperty_computedName3.ts @@ -0,0 +1,28 @@ +// @filename: a.ts +export const n = 1; +export const s = "s"; +export enum E1 { A = "ENUM_KEY" } +export enum E2 { B } + +// @filename: b.ts +import * as keys from "./a"; + +const t1 = { + [keys.n]: 1, + [keys.n]: 1, // duplicate +} + +const t2 = { + [keys.s]: 1, + [keys.s]: 1, // duplicate +} + +const t3 = { + [keys.E1.A]: 1, + [keys.E1.A]: 1, // duplicate +} + +const t4 = { + [keys.E2.B]: 1, + [keys.E2.B]: 1, // duplicate +} From 41c4263ab03cb22dccdd055877ddc0b95a7bb78a Mon Sep 17 00:00:00 2001 From: Oleksandr T Date: Thu, 20 Oct 2022 21:46:02 +0300 Subject: [PATCH 2/3] rename helper --- src/compiler/checker.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 3568478447b7d..eb9ce929f5ec5 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -45080,7 +45080,7 @@ namespace ts { } if (!inDestructuring) { - const effectiveName = getEffectivePropertyNameForPropertyName(name); + const effectiveName = getEffectivePropertyNameForPropertyNameNode(name); if (effectiveName === undefined) { continue; } @@ -46101,7 +46101,7 @@ namespace ts { return undefined; } - function getEffectivePropertyNameForPropertyName(node: PropertyName) { + function getEffectivePropertyNameForPropertyNameNode(node: PropertyName) { const name = getPropertyNameForPropertyNameNode(node); return name ? name : isComputedPropertyName(node) && isEntityNameExpression(node.expression) ? tryGetNameFromEntityNameExpression(node.expression) : undefined; From 461506aa273e1f7fbe7af00b421efba7f4637208 Mon Sep 17 00:00:00 2001 From: Oleksandr T Date: Thu, 15 Dec 2022 10:04:14 +0200 Subject: [PATCH 3/3] update baseline --- .../reference/duplicateObjectLiteralProperty_computedName3.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/baselines/reference/duplicateObjectLiteralProperty_computedName3.js b/tests/baselines/reference/duplicateObjectLiteralProperty_computedName3.js index 3ad4ac91ae4f9..ebe69a874abeb 100644 --- a/tests/baselines/reference/duplicateObjectLiteralProperty_computedName3.js +++ b/tests/baselines/reference/duplicateObjectLiteralProperty_computedName3.js @@ -32,7 +32,7 @@ const t4 = { //// [a.js] "use strict"; -exports.__esModule = true; +Object.defineProperty(exports, "__esModule", { value: true }); exports.E2 = exports.E1 = exports.s = exports.n = void 0; exports.n = 1; exports.s = "s"; @@ -47,7 +47,7 @@ var E2; //// [b.js] "use strict"; var _a, _b, _c, _d; -exports.__esModule = true; +Object.defineProperty(exports, "__esModule", { value: true }); var keys = require("./a"); var t1 = (_a = {}, _a[keys.n] = 1,