diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 2515d81a58f7e..cc7275f16b3fc 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -25269,32 +25269,31 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { } 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) || (symbol.flags & SymbolFlags.EnumMember))) 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) && isBlockScopedNameDeclaredBeforeUse(declaration, node.argumentExpression)) { - const initializer = getEffectiveInitializer(declaration); - if (initializer) { - return tryGetNameFromType(getTypeOfExpression(initializer)); - } - if (isEnumMember(declaration)) { - return getTextOfPropertyName(declaration.name); - } + const type = tryGetTypeFromEffectiveTypeNode(declaration); + if (type) { + const name = tryGetNameFromType(type); + if (name !== undefined) { + return name; + } + } + if (hasOnlyExpressionInitializer(declaration) && isBlockScopedNameDeclaredBeforeUse(declaration, node)) { + const initializer = getEffectiveInitializer(declaration); + if (initializer) { + return tryGetNameFromType(getTypeOfExpression(initializer)); + } + if (isEnumMember(declaration)) { + return getTextOfPropertyName(declaration.name); } } return undefined; @@ -47548,7 +47547,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { } if (!inDestructuring) { - const effectiveName = getPropertyNameForPropertyNameNode(name); + const effectiveName = getEffectivePropertyNameForPropertyNameNode(name); if (effectiveName === undefined) { continue; } @@ -48570,6 +48569,12 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { } return undefined; } + + function getEffectivePropertyNameForPropertyNameNode(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..ebe69a874abeb --- /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"; +Object.defineProperty(exports, "__esModule", { value: 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; +Object.defineProperty(exports, "__esModule", { value: 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 +}