Skip to content

Commit 0482b61

Browse files
committed
error on set accessor without get accessor
Fixes: #11596
1 parent f6af618 commit 0482b61

File tree

114 files changed

+3239
-345
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

114 files changed

+3239
-345
lines changed

src/compiler/checker.ts

+3
Original file line numberDiff line numberDiff line change
@@ -22563,6 +22563,9 @@ namespace ts {
2256322563
checkAccessorDeclarationTypesIdentical(node, otherAccessor, getAnnotatedAccessorType, Diagnostics.get_and_set_accessor_must_have_the_same_type);
2256422564
checkAccessorDeclarationTypesIdentical(node, otherAccessor, getThisTypeOfDeclaration, Diagnostics.get_and_set_accessor_must_have_the_same_this_type);
2256522565
}
22566+
else if (node.kind === SyntaxKind.SetAccessor) {
22567+
error(node.name, Diagnostics.A_set_accessor_must_have_a_corresponding_get_accessor);
22568+
}
2256622569
}
2256722570
const returnType = getTypeOfAccessors(getSymbolOfNode(node));
2256822571
if (node.kind === SyntaxKind.GetAccessor) {

src/compiler/diagnosticMessages.json

+4
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@
155155
"category": "Error",
156156
"code": 1049
157157
},
158+
"A 'set' accessor must have a corresponding 'get' accessor.": {
159+
"category": "Error",
160+
"code": 1050
161+
},
158162
"A 'set' accessor cannot have an optional parameter.": {
159163
"category": "Error",
160164
"code": 1051

tests/baselines/reference/MemberAccessorDeclaration15.errors.txt

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1+
tests/cases/compiler/MemberAccessorDeclaration15.ts(2,8): error TS1050: A 'set' accessor must have a corresponding 'get' accessor.
12
tests/cases/compiler/MemberAccessorDeclaration15.ts(2,8): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
23
tests/cases/compiler/MemberAccessorDeclaration15.ts(2,12): error TS2369: A parameter property is only allowed in a constructor implementation.
34

45

5-
==== tests/cases/compiler/MemberAccessorDeclaration15.ts (2 errors) ====
6+
==== tests/cases/compiler/MemberAccessorDeclaration15.ts (3 errors) ====
67
class C {
78
set Foo(public a: number) { }
89
~~~
10+
!!! error TS1050: A 'set' accessor must have a corresponding 'get' accessor.
11+
~~~
912
!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
1013
~~~~~~~~~~~~~~~~
1114
!!! error TS2369: A parameter property is only allowed in a constructor implementation.

tests/baselines/reference/accessibilityModifiers.errors.txt

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,51 @@
1+
tests/cases/conformance/classes/propertyMemberDeclarations/accessibilityModifiers.ts(6,24): error TS1050: A 'set' accessor must have a corresponding 'get' accessor.
2+
tests/cases/conformance/classes/propertyMemberDeclarations/accessibilityModifiers.ts(11,26): error TS1050: A 'set' accessor must have a corresponding 'get' accessor.
3+
tests/cases/conformance/classes/propertyMemberDeclarations/accessibilityModifiers.ts(16,23): error TS1050: A 'set' accessor must have a corresponding 'get' accessor.
14
tests/cases/conformance/classes/propertyMemberDeclarations/accessibilityModifiers.ts(21,12): error TS1029: 'private' modifier must precede 'static' modifier.
25
tests/cases/conformance/classes/propertyMemberDeclarations/accessibilityModifiers.ts(22,12): error TS1029: 'private' modifier must precede 'static' modifier.
36
tests/cases/conformance/classes/propertyMemberDeclarations/accessibilityModifiers.ts(23,12): error TS1029: 'private' modifier must precede 'static' modifier.
47
tests/cases/conformance/classes/propertyMemberDeclarations/accessibilityModifiers.ts(24,12): error TS1029: 'private' modifier must precede 'static' modifier.
8+
tests/cases/conformance/classes/propertyMemberDeclarations/accessibilityModifiers.ts(24,24): error TS1050: A 'set' accessor must have a corresponding 'get' accessor.
59
tests/cases/conformance/classes/propertyMemberDeclarations/accessibilityModifiers.ts(26,12): error TS1029: 'protected' modifier must precede 'static' modifier.
610
tests/cases/conformance/classes/propertyMemberDeclarations/accessibilityModifiers.ts(27,12): error TS1029: 'protected' modifier must precede 'static' modifier.
711
tests/cases/conformance/classes/propertyMemberDeclarations/accessibilityModifiers.ts(28,12): error TS1029: 'protected' modifier must precede 'static' modifier.
812
tests/cases/conformance/classes/propertyMemberDeclarations/accessibilityModifiers.ts(29,12): error TS1029: 'protected' modifier must precede 'static' modifier.
13+
tests/cases/conformance/classes/propertyMemberDeclarations/accessibilityModifiers.ts(29,26): error TS1050: A 'set' accessor must have a corresponding 'get' accessor.
914
tests/cases/conformance/classes/propertyMemberDeclarations/accessibilityModifiers.ts(31,12): error TS1029: 'public' modifier must precede 'static' modifier.
1015
tests/cases/conformance/classes/propertyMemberDeclarations/accessibilityModifiers.ts(32,12): error TS1029: 'public' modifier must precede 'static' modifier.
1116
tests/cases/conformance/classes/propertyMemberDeclarations/accessibilityModifiers.ts(33,12): error TS1029: 'public' modifier must precede 'static' modifier.
1217
tests/cases/conformance/classes/propertyMemberDeclarations/accessibilityModifiers.ts(34,12): error TS1029: 'public' modifier must precede 'static' modifier.
18+
tests/cases/conformance/classes/propertyMemberDeclarations/accessibilityModifiers.ts(34,23): error TS1050: A 'set' accessor must have a corresponding 'get' accessor.
1319
tests/cases/conformance/classes/propertyMemberDeclarations/accessibilityModifiers.ts(39,13): error TS1028: Accessibility modifier already seen.
1420
tests/cases/conformance/classes/propertyMemberDeclarations/accessibilityModifiers.ts(40,12): error TS1028: Accessibility modifier already seen.
1521
tests/cases/conformance/classes/propertyMemberDeclarations/accessibilityModifiers.ts(41,13): error TS1028: Accessibility modifier already seen.
1622
tests/cases/conformance/classes/propertyMemberDeclarations/accessibilityModifiers.ts(42,12): error TS1028: Accessibility modifier already seen.
23+
tests/cases/conformance/classes/propertyMemberDeclarations/accessibilityModifiers.ts(42,23): error TS1050: A 'set' accessor must have a corresponding 'get' accessor.
1724

1825

19-
==== tests/cases/conformance/classes/propertyMemberDeclarations/accessibilityModifiers.ts (16 errors) ====
26+
==== tests/cases/conformance/classes/propertyMemberDeclarations/accessibilityModifiers.ts (23 errors) ====
2027
// No errors
2128
class C {
2229
private static privateProperty;
2330
private static privateMethod() { }
2431
private static get privateGetter() { return 0; }
2532
private static set privateSetter(a: number) { }
33+
~~~~~~~~~~~~~
34+
!!! error TS1050: A 'set' accessor must have a corresponding 'get' accessor.
2635

2736
protected static protectedProperty;
2837
protected static protectedMethod() { }
2938
protected static get protectedGetter() { return 0; }
3039
protected static set protectedSetter(a: number) { }
40+
~~~~~~~~~~~~~~~
41+
!!! error TS1050: A 'set' accessor must have a corresponding 'get' accessor.
3142

3243
public static publicProperty;
3344
public static publicMethod() { }
3445
public static get publicGetter() { return 0; }
3546
public static set publicSetter(a: number) { }
47+
~~~~~~~~~~~~
48+
!!! error TS1050: A 'set' accessor must have a corresponding 'get' accessor.
3649
}
3750

3851
// Errors, accessibility modifiers must precede static
@@ -49,6 +62,8 @@ tests/cases/conformance/classes/propertyMemberDeclarations/accessibilityModifier
4962
static private set privateSetter(a: number) { }
5063
~~~~~~~
5164
!!! error TS1029: 'private' modifier must precede 'static' modifier.
65+
~~~~~~~~~~~~~
66+
!!! error TS1050: A 'set' accessor must have a corresponding 'get' accessor.
5267

5368
static protected protectedProperty;
5469
~~~~~~~~~
@@ -62,6 +77,8 @@ tests/cases/conformance/classes/propertyMemberDeclarations/accessibilityModifier
6277
static protected set protectedSetter(a: number) { }
6378
~~~~~~~~~
6479
!!! error TS1029: 'protected' modifier must precede 'static' modifier.
80+
~~~~~~~~~~~~~~~
81+
!!! error TS1050: A 'set' accessor must have a corresponding 'get' accessor.
6582

6683
static public publicProperty;
6784
~~~~~~
@@ -75,6 +92,8 @@ tests/cases/conformance/classes/propertyMemberDeclarations/accessibilityModifier
7592
static public set publicSetter(a: number) { }
7693
~~~~~~
7794
!!! error TS1029: 'public' modifier must precede 'static' modifier.
95+
~~~~~~~~~~~~
96+
!!! error TS1050: A 'set' accessor must have a corresponding 'get' accessor.
7897
}
7998

8099
// Errors, multiple accessibility modifier
@@ -91,5 +110,7 @@ tests/cases/conformance/classes/propertyMemberDeclarations/accessibilityModifier
91110
public public set setter(a: number) { }
92111
~~~~~~
93112
!!! error TS1028: Accessibility modifier already seen.
113+
~~~~~~
114+
!!! error TS1050: A 'set' accessor must have a corresponding 'get' accessor.
94115
}
95116

Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1+
tests/cases/compiler/accessorParameterAccessibilityModifier.ts(2,9): error TS1050: A 'set' accessor must have a corresponding 'get' accessor.
12
tests/cases/compiler/accessorParameterAccessibilityModifier.ts(2,11): error TS2369: A parameter property is only allowed in a constructor implementation.
3+
tests/cases/compiler/accessorParameterAccessibilityModifier.ts(3,16): error TS1050: A 'set' accessor must have a corresponding 'get' accessor.
24
tests/cases/compiler/accessorParameterAccessibilityModifier.ts(3,18): error TS2369: A parameter property is only allowed in a constructor implementation.
35

46

5-
==== tests/cases/compiler/accessorParameterAccessibilityModifier.ts (2 errors) ====
7+
==== tests/cases/compiler/accessorParameterAccessibilityModifier.ts (4 errors) ====
68
class C {
79
set X(public v) { }
10+
~
11+
!!! error TS1050: A 'set' accessor must have a corresponding 'get' accessor.
812
~~~~~~~~
913
!!! error TS2369: A parameter property is only allowed in a constructor implementation.
1014
static set X(public v2) { }
15+
~
16+
!!! error TS1050: A 'set' accessor must have a corresponding 'get' accessor.
1117
~~~~~~~~~
1218
!!! error TS2369: A parameter property is only allowed in a constructor implementation.
1319
}

tests/baselines/reference/accessorWithES3.errors.txt

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
tests/cases/conformance/classes/propertyMemberDeclarations/memberAccessorDeclarations/accessorWithES3.ts(4,9): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
2+
tests/cases/conformance/classes/propertyMemberDeclarations/memberAccessorDeclarations/accessorWithES3.ts(10,9): error TS1050: A 'set' accessor must have a corresponding 'get' accessor.
23
tests/cases/conformance/classes/propertyMemberDeclarations/memberAccessorDeclarations/accessorWithES3.ts(10,9): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
34
tests/cases/conformance/classes/propertyMemberDeclarations/memberAccessorDeclarations/accessorWithES3.ts(15,9): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
5+
tests/cases/conformance/classes/propertyMemberDeclarations/memberAccessorDeclarations/accessorWithES3.ts(19,9): error TS1050: A 'set' accessor must have a corresponding 'get' accessor.
46
tests/cases/conformance/classes/propertyMemberDeclarations/memberAccessorDeclarations/accessorWithES3.ts(19,9): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
57

68

7-
==== tests/cases/conformance/classes/propertyMemberDeclarations/memberAccessorDeclarations/accessorWithES3.ts (4 errors) ====
9+
==== tests/cases/conformance/classes/propertyMemberDeclarations/memberAccessorDeclarations/accessorWithES3.ts (6 errors) ====
810
// error to use accessors in ES3 mode
911

1012
class C {
@@ -18,6 +20,8 @@ tests/cases/conformance/classes/propertyMemberDeclarations/memberAccessorDeclara
1820
class D {
1921
set x(v) {
2022
~
23+
!!! error TS1050: A 'set' accessor must have a corresponding 'get' accessor.
24+
~
2125
!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
2226
}
2327
}
@@ -31,5 +35,7 @@ tests/cases/conformance/classes/propertyMemberDeclarations/memberAccessorDeclara
3135
var y = {
3236
set b(v) { }
3337
~
38+
!!! error TS1050: A 'set' accessor must have a corresponding 'get' accessor.
39+
~
3440
!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
3541
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
tests/cases/conformance/classes/propertyMemberDeclarations/memberAccessorDeclarations/accessorWithES5.ts(8,9): error TS1050: A 'set' accessor must have a corresponding 'get' accessor.
2+
tests/cases/conformance/classes/propertyMemberDeclarations/memberAccessorDeclarations/accessorWithES5.ts(17,9): error TS1050: A 'set' accessor must have a corresponding 'get' accessor.
3+
4+
5+
==== tests/cases/conformance/classes/propertyMemberDeclarations/memberAccessorDeclarations/accessorWithES5.ts (2 errors) ====
6+
class C {
7+
get x() {
8+
return 1;
9+
}
10+
}
11+
12+
class D {
13+
set x(v) {
14+
~
15+
!!! error TS1050: A 'set' accessor must have a corresponding 'get' accessor.
16+
}
17+
}
18+
19+
var x = {
20+
get a() { return 1 }
21+
}
22+
23+
var y = {
24+
set b(v) { }
25+
~
26+
!!! error TS1050: A 'set' accessor must have a corresponding 'get' accessor.
27+
}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1+
tests/cases/compiler/accessorWithInitializer.ts(2,9): error TS1050: A 'set' accessor must have a corresponding 'get' accessor.
12
tests/cases/compiler/accessorWithInitializer.ts(2,9): error TS1052: A 'set' accessor parameter cannot have an initializer.
3+
tests/cases/compiler/accessorWithInitializer.ts(3,16): error TS1050: A 'set' accessor must have a corresponding 'get' accessor.
24
tests/cases/compiler/accessorWithInitializer.ts(3,16): error TS1052: A 'set' accessor parameter cannot have an initializer.
35

46

5-
==== tests/cases/compiler/accessorWithInitializer.ts (2 errors) ====
7+
==== tests/cases/compiler/accessorWithInitializer.ts (4 errors) ====
68
class C {
79
set X(v = 0) { }
810
~
11+
!!! error TS1050: A 'set' accessor must have a corresponding 'get' accessor.
12+
~
913
!!! error TS1052: A 'set' accessor parameter cannot have an initializer.
1014
static set X(v2 = 0) { }
1115
~
16+
!!! error TS1050: A 'set' accessor must have a corresponding 'get' accessor.
17+
~
1218
!!! error TS1052: A 'set' accessor parameter cannot have an initializer.
1319
}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1+
tests/cases/compiler/accessorWithRestParam.ts(2,9): error TS1050: A 'set' accessor must have a corresponding 'get' accessor.
12
tests/cases/compiler/accessorWithRestParam.ts(2,11): error TS1053: A 'set' accessor cannot have rest parameter.
3+
tests/cases/compiler/accessorWithRestParam.ts(3,16): error TS1050: A 'set' accessor must have a corresponding 'get' accessor.
24
tests/cases/compiler/accessorWithRestParam.ts(3,18): error TS1053: A 'set' accessor cannot have rest parameter.
35

46

5-
==== tests/cases/compiler/accessorWithRestParam.ts (2 errors) ====
7+
==== tests/cases/compiler/accessorWithRestParam.ts (4 errors) ====
68
class C {
79
set X(...v) { }
10+
~
11+
!!! error TS1050: A 'set' accessor must have a corresponding 'get' accessor.
812
~~~
913
!!! error TS1053: A 'set' accessor cannot have rest parameter.
1014
static set X(...v2) { }
15+
~
16+
!!! error TS1050: A 'set' accessor must have a corresponding 'get' accessor.
1117
~~~
1218
!!! error TS1053: A 'set' accessor cannot have rest parameter.
1319
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
tests/cases/compiler/accessorWithoutBody2.ts(1,15): error TS1050: A 'set' accessor must have a corresponding 'get' accessor.
12
tests/cases/compiler/accessorWithoutBody2.ts(1,20): error TS1005: '{' expected.
23

34

4-
==== tests/cases/compiler/accessorWithoutBody2.ts (1 errors) ====
5+
==== tests/cases/compiler/accessorWithoutBody2.ts (2 errors) ====
56
var v = { set foo(a) }
7+
~~~
8+
!!! error TS1050: A 'set' accessor must have a corresponding 'get' accessor.
69
~
710
!!! error TS1005: '{' expected.
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
tests/cases/conformance/async/es5/asyncSetter_es5.ts(2,3): error TS1042: 'async' modifier cannot be used here.
2+
tests/cases/conformance/async/es5/asyncSetter_es5.ts(2,13): error TS1050: A 'set' accessor must have a corresponding 'get' accessor.
23

34

4-
==== tests/cases/conformance/async/es5/asyncSetter_es5.ts (1 errors) ====
5+
==== tests/cases/conformance/async/es5/asyncSetter_es5.ts (2 errors) ====
56
class C {
67
async set foo(value) {
78
~~~~~
89
!!! error TS1042: 'async' modifier cannot be used here.
10+
~~~
11+
!!! error TS1050: A 'set' accessor must have a corresponding 'get' accessor.
912
}
1013
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
tests/cases/conformance/async/es6/asyncSetter_es6.ts(2,3): error TS1042: 'async' modifier cannot be used here.
2+
tests/cases/conformance/async/es6/asyncSetter_es6.ts(2,13): error TS1050: A 'set' accessor must have a corresponding 'get' accessor.
23

34

4-
==== tests/cases/conformance/async/es6/asyncSetter_es6.ts (1 errors) ====
5+
==== tests/cases/conformance/async/es6/asyncSetter_es6.ts (2 errors) ====
56
class C {
67
async set foo(value) {
78
~~~~~
89
!!! error TS1042: 'async' modifier cannot be used here.
10+
~~~
11+
!!! error TS1050: A 'set' accessor must have a corresponding 'get' accessor.
912
}
1013
}
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
tests/cases/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractAccessor.ts(3,17): error TS1318: An abstract accessor cannot have an implementation.
2+
tests/cases/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractAccessor.ts(4,17): error TS1050: A 'set' accessor must have a corresponding 'get' accessor.
3+
tests/cases/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractAccessor.ts(5,17): error TS1050: A 'set' accessor must have a corresponding 'get' accessor.
24
tests/cases/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractAccessor.ts(5,17): error TS1318: An abstract accessor cannot have an implementation.
35

46

5-
==== tests/cases/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractAccessor.ts (2 errors) ====
7+
==== tests/cases/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractAccessor.ts (4 errors) ====
68
abstract class A {
79
abstract get a();
810
abstract get aa() { return 1; } // error
911
~~
1012
!!! error TS1318: An abstract accessor cannot have an implementation.
1113
abstract set b(x: string);
14+
~
15+
!!! error TS1050: A 'set' accessor must have a corresponding 'get' accessor.
1216
abstract set bb(x: string) {} // error
1317
~~
18+
!!! error TS1050: A 'set' accessor must have a corresponding 'get' accessor.
19+
~~
1420
!!! error TS1318: An abstract accessor cannot have an implementation.
1521
}
1622

0 commit comments

Comments
 (0)