Skip to content

Commit a8c44b8

Browse files
committed
"No repeated property names" error in object literals is duplicated in strict mode
1 parent 97a7901 commit a8c44b8

20 files changed

+198
-101
lines changed

src/compiler/binder.ts

-6
Original file line numberDiff line numberDiff line change
@@ -2066,12 +2066,6 @@ namespace ts {
20662066
seen.set(identifier.escapedText, currentKind);
20672067
continue;
20682068
}
2069-
2070-
if (currentKind === ElementKind.Property && existingKind === ElementKind.Property) {
2071-
const span = getErrorSpanForNode(file, identifier);
2072-
file.bindDiagnostics.push(createFileDiagnostic(file, span.start, span.length,
2073-
Diagnostics.An_object_literal_cannot_have_multiple_properties_with_the_same_name_in_strict_mode));
2074-
}
20752069
}
20762070
}
20772071

src/compiler/checker.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -43126,8 +43126,11 @@ namespace ts {
4312643126
seen.set(effectiveName, currentKind);
4312743127
}
4312843128
else {
43129-
if ((currentKind & DeclarationMeaning.PropertyAssignmentOrMethod) && (existingKind & DeclarationMeaning.PropertyAssignmentOrMethod)) {
43130-
grammarErrorOnNode(name, Diagnostics.Duplicate_identifier_0, getTextOfNode(name));
43129+
if ((currentKind & DeclarationMeaning.Method) && (existingKind & DeclarationMeaning.Method)) {
43130+
grammarErrorOnNode(name, Diagnostics.Duplicate_identifier_0, getTextOfNode(name));
43131+
}
43132+
else if ((currentKind & DeclarationMeaning.PropertyAssignment) && (existingKind & DeclarationMeaning.PropertyAssignment)) {
43133+
grammarErrorOnNode(name, Diagnostics.An_object_literal_cannot_have_multiple_properties_with_the_same_name, getTextOfNode(name));
4313143134
}
4313243135
else if ((currentKind & DeclarationMeaning.GetOrSetAccessor) && (existingKind & DeclarationMeaning.GetOrSetAccessor)) {
4313343136
if (existingKind !== DeclarationMeaning.GetOrSetAccessor && currentKind !== existingKind) {

src/compiler/diagnosticMessages.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@
335335
"category": "Error",
336336
"code": 1116
337337
},
338-
"An object literal cannot have multiple properties with the same name in strict mode.": {
338+
"An object literal cannot have multiple properties with the same name.": {
339339
"category": "Error",
340340
"code": 1117
341341
},

tests/baselines/reference/duplicateIdentifierDifferentSpelling.errors.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
tests/cases/compiler/duplicateIdentifierDifferentSpelling.ts(3,3): error TS2300: Duplicate identifier '3'.
2-
tests/cases/compiler/duplicateIdentifierDifferentSpelling.ts(6,21): error TS2300: Duplicate identifier '3'.
2+
tests/cases/compiler/duplicateIdentifierDifferentSpelling.ts(6,21): error TS1117: An object literal cannot have multiple properties with the same name.
33

44

55
==== tests/cases/compiler/duplicateIdentifierDifferentSpelling.ts (2 errors) ====
@@ -12,5 +12,5 @@ tests/cases/compiler/duplicateIdentifierDifferentSpelling.ts(6,21): error TS2300
1212

1313
var X = { 0b11: '', 3: '' };
1414
~
15-
!!! error TS2300: Duplicate identifier '3'.
15+
!!! error TS1117: An object literal cannot have multiple properties with the same name.
1616

tests/baselines/reference/duplicateObjectLiteralProperty.errors.txt

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
tests/cases/compiler/duplicateObjectLiteralProperty.ts(4,5): error TS2300: Duplicate identifier 'a'.
2-
tests/cases/compiler/duplicateObjectLiteralProperty.ts(5,5): error TS2300: Duplicate identifier '\u0061'.
3-
tests/cases/compiler/duplicateObjectLiteralProperty.ts(6,5): error TS2300: Duplicate identifier 'a'.
4-
tests/cases/compiler/duplicateObjectLiteralProperty.ts(8,9): error TS2300: Duplicate identifier '"c"'.
1+
tests/cases/compiler/duplicateObjectLiteralProperty.ts(4,5): error TS1117: An object literal cannot have multiple properties with the same name.
2+
tests/cases/compiler/duplicateObjectLiteralProperty.ts(5,5): error TS1117: An object literal cannot have multiple properties with the same name.
3+
tests/cases/compiler/duplicateObjectLiteralProperty.ts(6,5): error TS1117: An object literal cannot have multiple properties with the same name.
4+
tests/cases/compiler/duplicateObjectLiteralProperty.ts(8,9): error TS1117: An object literal cannot have multiple properties with the same name.
55
tests/cases/compiler/duplicateObjectLiteralProperty.ts(14,9): error TS2300: Duplicate identifier 'a'.
66
tests/cases/compiler/duplicateObjectLiteralProperty.ts(15,9): error TS2300: Duplicate identifier 'a'.
77
tests/cases/compiler/duplicateObjectLiteralProperty.ts(16,9): error TS1118: An object literal cannot have multiple get/set accessors with the same name.
@@ -14,17 +14,17 @@ tests/cases/compiler/duplicateObjectLiteralProperty.ts(16,9): error TS2300: Dupl
1414
b: true, // OK
1515
a: 56, // Duplicate
1616
~
17-
!!! error TS2300: Duplicate identifier 'a'.
17+
!!! error TS1117: An object literal cannot have multiple properties with the same name.
1818
\u0061: "ss", // Duplicate
1919
~~~~~~
20-
!!! error TS2300: Duplicate identifier '\u0061'.
20+
!!! error TS1117: An object literal cannot have multiple properties with the same name.
2121
a: {
2222
~
23-
!!! error TS2300: Duplicate identifier 'a'.
23+
!!! error TS1117: An object literal cannot have multiple properties with the same name.
2424
c: 1,
2525
"c": 56, // Duplicate
2626
~~~
27-
!!! error TS2300: Duplicate identifier '"c"'.
27+
!!! error TS1117: An object literal cannot have multiple properties with the same name.
2828
}
2929
};
3030

Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
1-
tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts(3,5): error TS2300: Duplicate identifier '[1]'.
2-
tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts(8,5): error TS2300: Duplicate identifier '[+1]'.
3-
tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts(13,5): error TS2300: Duplicate identifier '[+1]'.
4-
tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts(23,5): error TS2300: Duplicate identifier '["+1"]'.
5-
tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts(28,5): error TS2300: Duplicate identifier '[-1]'.
6-
tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts(33,5): error TS2300: Duplicate identifier '["-1"]'.
1+
tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts(3,5): error TS1117: An object literal cannot have multiple properties with the same name.
2+
tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts(8,5): error TS1117: An object literal cannot have multiple properties with the same name.
3+
tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts(13,5): error TS1117: An object literal cannot have multiple properties with the same name.
4+
tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts(23,5): error TS1117: An object literal cannot have multiple properties with the same name.
5+
tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts(28,5): error TS1117: An object literal cannot have multiple properties with the same name.
6+
tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts(33,5): error TS1117: An object literal cannot have multiple properties with the same name.
77

88

99
==== tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts (6 errors) ====
1010
const t1 = {
1111
1: 1,
1212
[1]: 0 // duplicate
1313
~~~
14-
!!! error TS2300: Duplicate identifier '[1]'.
14+
!!! error TS1117: An object literal cannot have multiple properties with the same name.
1515
}
1616

1717
const t2 = {
1818
1: 1,
1919
[+1]: 0 // duplicate
2020
~~~~
21-
!!! error TS2300: Duplicate identifier '[+1]'.
21+
!!! error TS1117: An object literal cannot have multiple properties with the same name.
2222
}
2323

2424
const t3 = {
2525
"1": 1,
2626
[+1]: 0 // duplicate
2727
~~~~
28-
!!! error TS2300: Duplicate identifier '[+1]'.
28+
!!! error TS1117: An object literal cannot have multiple properties with the same name.
2929
}
3030

3131
const t4 = {
@@ -37,20 +37,20 @@ tests/cases/compiler/duplicateObjectLiteralProperty_computedName.ts(33,5): error
3737
"+1": 1,
3838
["+1"]: 0 // duplicate
3939
~~~~~~
40-
!!! error TS2300: Duplicate identifier '["+1"]'.
40+
!!! error TS1117: An object literal cannot have multiple properties with the same name.
4141
}
4242

4343
const t6 = {
4444
"-1": 1,
4545
[-1]: 0 // duplicate
4646
~~~~
47-
!!! error TS2300: Duplicate identifier '[-1]'.
47+
!!! error TS1117: An object literal cannot have multiple properties with the same name.
4848
}
4949

5050
const t7 = {
5151
"-1": 1,
5252
["-1"]: 0 // duplicate
5353
~~~~~~
54-
!!! error TS2300: Duplicate identifier '["-1"]'.
54+
!!! error TS1117: An object literal cannot have multiple properties with the same name.
5555
}
5656

Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
tests/cases/compiler/duplicatePropertiesInStrictMode.ts(4,3): error TS1117: An object literal cannot have multiple properties with the same name in strict mode.
2-
tests/cases/compiler/duplicatePropertiesInStrictMode.ts(4,3): error TS2300: Duplicate identifier 'x'.
1+
tests/cases/compiler/duplicatePropertiesInStrictMode.ts(4,3): error TS1117: An object literal cannot have multiple properties with the same name.
32

43

5-
==== tests/cases/compiler/duplicatePropertiesInStrictMode.ts (2 errors) ====
4+
==== tests/cases/compiler/duplicatePropertiesInStrictMode.ts (1 errors) ====
65
"use strict";
76
var x = {
87
x: 1,
98
x: 2
109
~
11-
!!! error TS1117: An object literal cannot have multiple properties with the same name in strict mode.
12-
~
13-
!!! error TS2300: Duplicate identifier 'x'.
10+
!!! error TS1117: An object literal cannot have multiple properties with the same name.
1411
}

tests/baselines/reference/duplicatePropertyNames.errors.txt

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ tests/cases/conformance/types/members/duplicatePropertyNames.ts(35,5): error TS2
1212
tests/cases/conformance/types/members/duplicatePropertyNames.ts(36,5): error TS2300: Duplicate identifier 'foo'.
1313
tests/cases/conformance/types/members/duplicatePropertyNames.ts(38,5): error TS2300: Duplicate identifier 'bar'.
1414
tests/cases/conformance/types/members/duplicatePropertyNames.ts(39,5): error TS2300: Duplicate identifier 'bar'.
15-
tests/cases/conformance/types/members/duplicatePropertyNames.ts(44,5): error TS2300: Duplicate identifier 'foo'.
16-
tests/cases/conformance/types/members/duplicatePropertyNames.ts(46,5): error TS2300: Duplicate identifier 'bar'.
15+
tests/cases/conformance/types/members/duplicatePropertyNames.ts(44,5): error TS1117: An object literal cannot have multiple properties with the same name.
16+
tests/cases/conformance/types/members/duplicatePropertyNames.ts(46,5): error TS1117: An object literal cannot have multiple properties with the same name.
1717

1818

1919
==== tests/cases/conformance/types/members/duplicatePropertyNames.ts (16 errors) ====
@@ -90,10 +90,10 @@ tests/cases/conformance/types/members/duplicatePropertyNames.ts(46,5): error TS2
9090
foo: '',
9191
foo: '',
9292
~~~
93-
!!! error TS2300: Duplicate identifier 'foo'.
93+
!!! error TS1117: An object literal cannot have multiple properties with the same name.
9494
bar: () => { },
9595
bar: () => { }
9696
~~~
97-
!!! error TS2300: Duplicate identifier 'bar'.
97+
!!! error TS1117: An object literal cannot have multiple properties with the same name.
9898
}
9999

tests/baselines/reference/jsFileCompilationBindStrictModeErrors.errors.txt

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
tests/cases/compiler/a.js(5,5): error TS1117: An object literal cannot have multiple properties with the same name in strict mode.
2-
tests/cases/compiler/a.js(5,5): error TS2300: Duplicate identifier 'a'.
1+
tests/cases/compiler/a.js(5,5): error TS1117: An object literal cannot have multiple properties with the same name.
32
tests/cases/compiler/a.js(7,5): error TS1212: Identifier expected. 'let' is a reserved word in strict mode.
43
tests/cases/compiler/a.js(8,8): error TS1102: 'delete' cannot be called on an identifier in strict mode.
54
tests/cases/compiler/a.js(8,8): error TS2703: The operand of a 'delete' operator must be a property reference.
@@ -15,16 +14,14 @@ tests/cases/compiler/d.js(2,9): error TS1121: Octal literals are not allowed in
1514
tests/cases/compiler/d.js(2,11): error TS1005: ',' expected.
1615

1716

18-
==== tests/cases/compiler/a.js (9 errors) ====
17+
==== tests/cases/compiler/a.js (8 errors) ====
1918
"use strict";
2019
var a = {
2120
a: "hello", // error
2221
b: 10,
2322
a: 10 // error
2423
~
25-
!!! error TS1117: An object literal cannot have multiple properties with the same name in strict mode.
26-
~
27-
!!! error TS2300: Duplicate identifier 'a'.
24+
!!! error TS1117: An object literal cannot have multiple properties with the same name.
2825
};
2926
var let = 10; // error
3027
~~~

tests/baselines/reference/lastPropertyInLiteralWins.errors.txt

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
tests/cases/compiler/lastPropertyInLiteralWins.ts(8,5): error TS2322: Type '(num: number) => void' is not assignable to type '(str: string) => void'.
22
Types of parameters 'num' and 'str' are incompatible.
33
Type 'string' is not assignable to type 'number'.
4-
tests/cases/compiler/lastPropertyInLiteralWins.ts(9,5): error TS2300: Duplicate identifier 'thunk'.
4+
tests/cases/compiler/lastPropertyInLiteralWins.ts(9,5): error TS1117: An object literal cannot have multiple properties with the same name.
55
tests/cases/compiler/lastPropertyInLiteralWins.ts(9,5): error TS2322: Type '(num: number) => void' is not assignable to type '(str: string) => void'.
6-
tests/cases/compiler/lastPropertyInLiteralWins.ts(14,5): error TS2300: Duplicate identifier 'thunk'.
6+
tests/cases/compiler/lastPropertyInLiteralWins.ts(14,5): error TS1117: An object literal cannot have multiple properties with the same name.
77

88

99
==== tests/cases/compiler/lastPropertyInLiteralWins.ts (4 errors) ====
@@ -22,7 +22,7 @@ tests/cases/compiler/lastPropertyInLiteralWins.ts(14,5): error TS2300: Duplicate
2222
!!! related TS6500 tests/cases/compiler/lastPropertyInLiteralWins.ts:2:5: The expected type comes from property 'thunk' which is declared here on type 'Thing'
2323
thunk: (num: number) => {}
2424
~~~~~
25-
!!! error TS2300: Duplicate identifier 'thunk'.
25+
!!! error TS1117: An object literal cannot have multiple properties with the same name.
2626
~~~~~
2727
!!! error TS2322: Type '(num: number) => void' is not assignable to type '(str: string) => void'.
2828
!!! related TS6500 tests/cases/compiler/lastPropertyInLiteralWins.ts:2:5: The expected type comes from property 'thunk' which is declared here on type 'Thing'
@@ -32,6 +32,6 @@ tests/cases/compiler/lastPropertyInLiteralWins.ts(14,5): error TS2300: Duplicate
3232
thunk: (num: number) => {},
3333
thunk: (str: string) => {}
3434
~~~~~
35-
!!! error TS2300: Duplicate identifier 'thunk'.
35+
!!! error TS1117: An object literal cannot have multiple properties with the same name.
3636
});
3737

Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tests/cases/compiler/memberOverride.ts(5,5): error TS2300: Duplicate identifier 'a'.
1+
tests/cases/compiler/memberOverride.ts(5,5): error TS1117: An object literal cannot have multiple properties with the same name.
22

33

44
==== tests/cases/compiler/memberOverride.ts (1 errors) ====
@@ -8,7 +8,7 @@ tests/cases/compiler/memberOverride.ts(5,5): error TS2300: Duplicate identifier
88
a: "",
99
a: 5
1010
~
11-
!!! error TS2300: Duplicate identifier 'a'.
11+
!!! error TS1117: An object literal cannot have multiple properties with the same name.
1212
}
1313

1414
var n: number = x.a;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
tests/cases/compiler/noRepeatedPropertyNames.ts(2,23): error TS1117: An object literal cannot have multiple properties with the same name.
2+
tests/cases/compiler/noRepeatedPropertyNames.ts(5,32): error TS1117: An object literal cannot have multiple properties with the same name.
3+
4+
5+
==== tests/cases/compiler/noRepeatedPropertyNames.ts (2 errors) ====
6+
// https://github.com/microsoft/TypeScript/issues/46815
7+
const first = { a: 1, a: 2 };
8+
~
9+
!!! error TS1117: An object literal cannot have multiple properties with the same name.
10+
class C {
11+
m() {
12+
const second = { a: 1, a: 2 };
13+
~
14+
!!! error TS1117: An object literal cannot have multiple properties with the same name.
15+
return second.a;
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//// [noRepeatedPropertyNames.ts]
2+
// https://github.com/microsoft/TypeScript/issues/46815
3+
const first = { a: 1, a: 2 };
4+
class C {
5+
m() {
6+
const second = { a: 1, a: 2 };
7+
return second.a;
8+
}
9+
}
10+
11+
12+
13+
//// [noRepeatedPropertyNames.js]
14+
// https://github.com/microsoft/TypeScript/issues/46815
15+
var first = { a: 1, a: 2 };
16+
var C = /** @class */ (function () {
17+
function C() {
18+
}
19+
C.prototype.m = function () {
20+
var second = { a: 1, a: 2 };
21+
return second.a;
22+
};
23+
return C;
24+
}());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
=== tests/cases/compiler/noRepeatedPropertyNames.ts ===
2+
// https://github.com/microsoft/TypeScript/issues/46815
3+
const first = { a: 1, a: 2 };
4+
>first : Symbol(first, Decl(noRepeatedPropertyNames.ts, 1, 5))
5+
>a : Symbol(a, Decl(noRepeatedPropertyNames.ts, 1, 15), Decl(noRepeatedPropertyNames.ts, 1, 21))
6+
>a : Symbol(a, Decl(noRepeatedPropertyNames.ts, 1, 15), Decl(noRepeatedPropertyNames.ts, 1, 21))
7+
8+
class C {
9+
>C : Symbol(C, Decl(noRepeatedPropertyNames.ts, 1, 29))
10+
11+
m() {
12+
>m : Symbol(C.m, Decl(noRepeatedPropertyNames.ts, 2, 9))
13+
14+
const second = { a: 1, a: 2 };
15+
>second : Symbol(second, Decl(noRepeatedPropertyNames.ts, 4, 13))
16+
>a : Symbol(a, Decl(noRepeatedPropertyNames.ts, 4, 24), Decl(noRepeatedPropertyNames.ts, 4, 30))
17+
>a : Symbol(a, Decl(noRepeatedPropertyNames.ts, 4, 24), Decl(noRepeatedPropertyNames.ts, 4, 30))
18+
19+
return second.a;
20+
>second.a : Symbol(a, Decl(noRepeatedPropertyNames.ts, 4, 24), Decl(noRepeatedPropertyNames.ts, 4, 30))
21+
>second : Symbol(second, Decl(noRepeatedPropertyNames.ts, 4, 13))
22+
>a : Symbol(a, Decl(noRepeatedPropertyNames.ts, 4, 24), Decl(noRepeatedPropertyNames.ts, 4, 30))
23+
}
24+
}
25+
26+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
=== tests/cases/compiler/noRepeatedPropertyNames.ts ===
2+
// https://github.com/microsoft/TypeScript/issues/46815
3+
const first = { a: 1, a: 2 };
4+
>first : { a: number; }
5+
>{ a: 1, a: 2 } : { a: number; }
6+
>a : number
7+
>1 : 1
8+
>a : number
9+
>2 : 2
10+
11+
class C {
12+
>C : C
13+
14+
m() {
15+
>m : () => number
16+
17+
const second = { a: 1, a: 2 };
18+
>second : { a: number; }
19+
>{ a: 1, a: 2 } : { a: number; }
20+
>a : number
21+
>1 : 1
22+
>a : number
23+
>2 : 2
24+
25+
return second.a;
26+
>second.a : number
27+
>second : { a: number; }
28+
>a : number
29+
}
30+
}

tests/baselines/reference/numericStringNamedPropertyEquivalence.errors.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ tests/cases/conformance/types/objectTypeLiteral/propertySignatures/numericString
44
tests/cases/conformance/types/objectTypeLiteral/propertySignatures/numericStringNamedPropertyEquivalence.ts(16,5): error TS2300: Duplicate identifier '1'.
55
tests/cases/conformance/types/objectTypeLiteral/propertySignatures/numericStringNamedPropertyEquivalence.ts(17,5): error TS2300: Duplicate identifier '1'.
66
tests/cases/conformance/types/objectTypeLiteral/propertySignatures/numericStringNamedPropertyEquivalence.ts(17,5): error TS2717: Subsequent property declarations must have the same type. Property '1.0' must be of type 'number', but here has type 'string'.
7-
tests/cases/conformance/types/objectTypeLiteral/propertySignatures/numericStringNamedPropertyEquivalence.ts(22,5): error TS2300: Duplicate identifier '0'.
7+
tests/cases/conformance/types/objectTypeLiteral/propertySignatures/numericStringNamedPropertyEquivalence.ts(22,5): error TS1117: An object literal cannot have multiple properties with the same name.
88

99

1010
==== tests/cases/conformance/types/objectTypeLiteral/propertySignatures/numericStringNamedPropertyEquivalence.ts (7 errors) ====
@@ -44,5 +44,5 @@ tests/cases/conformance/types/objectTypeLiteral/propertySignatures/numericString
4444
"0": '',
4545
0: ''
4646
~
47-
!!! error TS2300: Duplicate identifier '0'.
47+
!!! error TS1117: An object literal cannot have multiple properties with the same name.
4848
}

0 commit comments

Comments
 (0)