Skip to content

Commit b5b0777

Browse files
committed
More tests for protected members
1 parent ddfe28d commit b5b0777

28 files changed

+1283
-20
lines changed

tests/baselines/reference/classConstructorAccessibility.errors.txt

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility.ts(6,5): error TS1089: 'private' modifier cannot appear on a constructor declaration.
2-
tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility.ts(18,9): error TS1089: 'private' modifier cannot appear on a constructor declaration.
2+
tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility.ts(10,5): error TS1089: 'protected' modifier cannot appear on a constructor declaration.
3+
tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility.ts(23,9): error TS1089: 'private' modifier cannot appear on a constructor declaration.
4+
tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility.ts(27,9): error TS1089: 'protected' modifier cannot appear on a constructor declaration.
35

46

5-
==== tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility.ts (2 errors) ====
7+
==== tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility.ts (4 errors) ====
68
class C {
79
public constructor(public x: number) { }
810
}
@@ -13,8 +15,15 @@ tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessib
1315
!!! error TS1089: 'private' modifier cannot appear on a constructor declaration.
1416
}
1517

18+
class E {
19+
protected constructor(public x: number) { } // error
20+
~~~~~~~~~
21+
!!! error TS1089: 'protected' modifier cannot appear on a constructor declaration.
22+
}
23+
1624
var c = new C(1);
1725
var d = new D(1);
26+
var e = new E(1);
1827

1928
module Generic {
2029
class C<T> {
@@ -27,7 +36,14 @@ tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessib
2736
!!! error TS1089: 'private' modifier cannot appear on a constructor declaration.
2837
}
2938

39+
class E<T> {
40+
protected constructor(public x: T) { } // error
41+
~~~~~~~~~
42+
!!! error TS1089: 'protected' modifier cannot appear on a constructor declaration.
43+
}
44+
3045
var c = new C(1);
3146
var d = new D(1);
47+
var e = new E(1);
3248
}
3349

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
tests/cases/conformance/classes/members/accessibility/classPropertyAsProtected.ts(3,19): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
2+
tests/cases/conformance/classes/members/accessibility/classPropertyAsProtected.ts(4,19): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
3+
tests/cases/conformance/classes/members/accessibility/classPropertyAsProtected.ts(8,26): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
4+
tests/cases/conformance/classes/members/accessibility/classPropertyAsProtected.ts(9,26): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
5+
tests/cases/conformance/classes/members/accessibility/classPropertyAsProtected.ts(15,1): error TS2445: Property 'x' is protected and only accessible within class 'C' and its subclasses.
6+
tests/cases/conformance/classes/members/accessibility/classPropertyAsProtected.ts(16,1): error TS2445: Property 'y' is protected and only accessible within class 'C' and its subclasses.
7+
tests/cases/conformance/classes/members/accessibility/classPropertyAsProtected.ts(17,1): error TS2445: Property 'y' is protected and only accessible within class 'C' and its subclasses.
8+
tests/cases/conformance/classes/members/accessibility/classPropertyAsProtected.ts(18,1): error TS2445: Property 'foo' is protected and only accessible within class 'C' and its subclasses.
9+
tests/cases/conformance/classes/members/accessibility/classPropertyAsProtected.ts(20,1): error TS2445: Property 'a' is protected and only accessible within class 'C' and its subclasses.
10+
tests/cases/conformance/classes/members/accessibility/classPropertyAsProtected.ts(21,1): error TS2445: Property 'b' is protected and only accessible within class 'C' and its subclasses.
11+
tests/cases/conformance/classes/members/accessibility/classPropertyAsProtected.ts(22,1): error TS2445: Property 'b' is protected and only accessible within class 'C' and its subclasses.
12+
tests/cases/conformance/classes/members/accessibility/classPropertyAsProtected.ts(23,1): error TS2445: Property 'foo' is protected and only accessible within class 'C' and its subclasses.
13+
14+
15+
==== tests/cases/conformance/classes/members/accessibility/classPropertyAsProtected.ts (12 errors) ====
16+
class C {
17+
protected x: string;
18+
protected get y() { return null; }
19+
~
20+
!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
21+
protected set y(x) { }
22+
~
23+
!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
24+
protected foo() { }
25+
26+
protected static a: string;
27+
protected static get b() { return null; }
28+
~
29+
!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
30+
protected static set b(x) { }
31+
~
32+
!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
33+
protected static foo() { }
34+
}
35+
36+
var c: C;
37+
// all errors
38+
c.x;
39+
~~~
40+
!!! error TS2445: Property 'x' is protected and only accessible within class 'C' and its subclasses.
41+
c.y;
42+
~~~
43+
!!! error TS2445: Property 'y' is protected and only accessible within class 'C' and its subclasses.
44+
c.y = 1;
45+
~~~
46+
!!! error TS2445: Property 'y' is protected and only accessible within class 'C' and its subclasses.
47+
c.foo();
48+
~~~~~
49+
!!! error TS2445: Property 'foo' is protected and only accessible within class 'C' and its subclasses.
50+
51+
C.a;
52+
~~~
53+
!!! error TS2445: Property 'a' is protected and only accessible within class 'C' and its subclasses.
54+
C.b();
55+
~~~
56+
!!! error TS2445: Property 'b' is protected and only accessible within class 'C' and its subclasses.
57+
C.b = 1;
58+
~~~
59+
!!! error TS2445: Property 'b' is protected and only accessible within class 'C' and its subclasses.
60+
C.foo();
61+
~~~~~
62+
!!! error TS2445: Property 'foo' is protected and only accessible within class 'C' and its subclasses.
Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
11
tests/cases/conformance/classes/constructorDeclarations/constructorParameters/constructorParameterProperties.ts(8,10): error TS2341: Property 'x' is private and only accessible within class 'C'.
2-
tests/cases/conformance/classes/constructorDeclarations/constructorParameters/constructorParameterProperties.ts(17,10): error TS2341: Property 'x' is private and only accessible within class 'D<T>'.
3-
tests/cases/conformance/classes/constructorDeclarations/constructorParameters/constructorParameterProperties.ts(18,12): error TS2339: Property 'a' does not exist on type 'D<string>'.
2+
tests/cases/conformance/classes/constructorDeclarations/constructorParameters/constructorParameterProperties.ts(9,10): error TS2445: Property 'z' is protected and only accessible within class 'C' and its subclasses.
3+
tests/cases/conformance/classes/constructorDeclarations/constructorParameters/constructorParameterProperties.ts(18,10): error TS2341: Property 'x' is private and only accessible within class 'D<T>'.
4+
tests/cases/conformance/classes/constructorDeclarations/constructorParameters/constructorParameterProperties.ts(19,12): error TS2339: Property 'a' does not exist on type 'D<string>'.
5+
tests/cases/conformance/classes/constructorDeclarations/constructorParameters/constructorParameterProperties.ts(20,10): error TS2445: Property 'z' is protected and only accessible within class 'D<T>' and its subclasses.
46

57

6-
==== tests/cases/conformance/classes/constructorDeclarations/constructorParameters/constructorParameterProperties.ts (3 errors) ====
8+
==== tests/cases/conformance/classes/constructorDeclarations/constructorParameters/constructorParameterProperties.ts (5 errors) ====
79
class C {
810
y: string;
9-
constructor(private x: string) { }
11+
constructor(private x: string, protected z: string) { }
1012
}
1113

1214
var c: C;
1315
var r = c.y;
1416
var r2 = c.x; // error
1517
~~~
1618
!!! error TS2341: Property 'x' is private and only accessible within class 'C'.
19+
var r3 = c.z; // error
20+
~~~
21+
!!! error TS2445: Property 'z' is protected and only accessible within class 'C' and its subclasses.
1722

1823
class D<T> {
1924
y: T;
20-
constructor(a: T, private x: T) { }
25+
constructor(a: T, private x: T, protected z: T) { }
2126
}
2227

2328
var d: D<string>;
@@ -27,4 +32,8 @@ tests/cases/conformance/classes/constructorDeclarations/constructorParameters/co
2732
!!! error TS2341: Property 'x' is private and only accessible within class 'D<T>'.
2833
var r3 = d.a; // error
2934
~
30-
!!! error TS2339: Property 'a' does not exist on type 'D<string>'.
35+
!!! error TS2339: Property 'a' does not exist on type 'D<string>'.
36+
var r4 = d.z; // error
37+
~~~
38+
!!! error TS2445: Property 'z' is protected and only accessible within class 'D<T>' and its subclasses.
39+
Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,47 @@
11
//// [constructorParameterProperties.ts]
22
class C {
33
y: string;
4-
constructor(private x: string) { }
4+
constructor(private x: string, protected z: string) { }
55
}
66

77
var c: C;
88
var r = c.y;
99
var r2 = c.x; // error
10+
var r3 = c.z; // error
1011

1112
class D<T> {
1213
y: T;
13-
constructor(a: T, private x: T) { }
14+
constructor(a: T, private x: T, protected z: T) { }
1415
}
1516

1617
var d: D<string>;
1718
var r = d.y;
1819
var r2 = d.x; // error
19-
var r3 = d.a; // error
20+
var r3 = d.a; // error
21+
var r4 = d.z; // error
22+
2023

2124
//// [constructorParameterProperties.js]
2225
var C = (function () {
23-
function C(x) {
26+
function C(x, z) {
2427
this.x = x;
28+
this.z = z;
2529
}
2630
return C;
2731
})();
2832
var c;
2933
var r = c.y;
3034
var r2 = c.x; // error
35+
var r3 = c.z; // error
3136
var D = (function () {
32-
function D(a, x) {
37+
function D(a, x, z) {
3338
this.x = x;
39+
this.z = z;
3440
}
3541
return D;
3642
})();
3743
var d;
3844
var r = d.y;
3945
var r2 = d.x; // error
4046
var r3 = d.a; // error
47+
var r4 = d.z; // error

tests/baselines/reference/constructorParameterProperties2.errors.txt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
tests/cases/conformance/classes/constructorDeclarations/constructorParameters/constructorParameterProperties2.ts(11,24): error TS2300: Duplicate identifier 'y'.
22
tests/cases/conformance/classes/constructorDeclarations/constructorParameters/constructorParameterProperties2.ts(19,25): error TS2300: Duplicate identifier 'y'.
3+
tests/cases/conformance/classes/constructorDeclarations/constructorParameters/constructorParameterProperties2.ts(27,27): error TS2300: Duplicate identifier 'y'.
34

45

5-
==== tests/cases/conformance/classes/constructorDeclarations/constructorParameters/constructorParameterProperties2.ts (2 errors) ====
6+
==== tests/cases/conformance/classes/constructorDeclarations/constructorParameters/constructorParameterProperties2.ts (3 errors) ====
67
class C {
78
y: number;
89
constructor(y: number) { } // ok
@@ -29,4 +30,15 @@ tests/cases/conformance/classes/constructorDeclarations/constructorParameters/co
2930
}
3031

3132
var e: E;
32-
var r3 = e.y; // error
33+
var r3 = e.y; // error
34+
35+
class F {
36+
y: number;
37+
constructor(protected y: number) { } // error
38+
~
39+
!!! error TS2300: Duplicate identifier 'y'.
40+
}
41+
42+
var f: F;
43+
var r4 = f.y; // error
44+

tests/baselines/reference/constructorParameterProperties2.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,16 @@ class E {
2121
}
2222

2323
var e: E;
24-
var r3 = e.y; // error
24+
var r3 = e.y; // error
25+
26+
class F {
27+
y: number;
28+
constructor(protected y: number) { } // error
29+
}
30+
31+
var f: F;
32+
var r4 = f.y; // error
33+
2534

2635
//// [constructorParameterProperties2.js]
2736
var C = (function () {
@@ -47,3 +56,11 @@ var E = (function () {
4756
})();
4857
var e;
4958
var r3 = e.y; // error
59+
var F = (function () {
60+
function F(y) {
61+
this.y = y;
62+
} // error
63+
return F;
64+
})();
65+
var f;
66+
var r4 = f.y; // error
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
tests/cases/conformance/interfaces/interfacesExtendingClasses/implementingAnInterfaceExtendingClassWithProtecteds.ts(9,7): error TS2421: Class 'Bar' incorrectly implements interface 'I':
2+
Property 'y' is missing in type 'Bar'.
3+
tests/cases/conformance/interfaces/interfacesExtendingClasses/implementingAnInterfaceExtendingClassWithProtecteds.ts(12,7): error TS2421: Class 'Bar2' incorrectly implements interface 'I':
4+
Property 'x' is missing in type 'Bar2'.
5+
tests/cases/conformance/interfaces/interfacesExtendingClasses/implementingAnInterfaceExtendingClassWithProtecteds.ts(16,7): error TS2421: Class 'Bar3' incorrectly implements interface 'I':
6+
Property 'x' is protected but type 'Bar3' is not a class derived from 'Foo'.
7+
tests/cases/conformance/interfaces/interfacesExtendingClasses/implementingAnInterfaceExtendingClassWithProtecteds.ts(21,7): error TS2421: Class 'Bar4' incorrectly implements interface 'I':
8+
Property 'x' is protected but type 'Bar4' is not a class derived from 'Foo'.
9+
tests/cases/conformance/interfaces/interfacesExtendingClasses/implementingAnInterfaceExtendingClassWithProtecteds.ts(26,7): error TS2421: Class 'Bar5' incorrectly implements interface 'I':
10+
Property 'y' is missing in type 'Bar5'.
11+
tests/cases/conformance/interfaces/interfacesExtendingClasses/implementingAnInterfaceExtendingClassWithProtecteds.ts(29,7): error TS2421: Class 'Bar6' incorrectly implements interface 'I':
12+
Property 'y' is protected in type 'Bar6' but public in type 'I'.
13+
14+
15+
==== tests/cases/conformance/interfaces/interfacesExtendingClasses/implementingAnInterfaceExtendingClassWithProtecteds.ts (6 errors) ====
16+
class Foo {
17+
protected x: string;
18+
}
19+
20+
interface I extends Foo {
21+
y: number;
22+
}
23+
24+
class Bar implements I { // error
25+
~~~
26+
!!! error TS2421: Class 'Bar' incorrectly implements interface 'I':
27+
!!! error TS2421: Property 'y' is missing in type 'Bar'.
28+
}
29+
30+
class Bar2 implements I { // error
31+
~~~~
32+
!!! error TS2421: Class 'Bar2' incorrectly implements interface 'I':
33+
!!! error TS2421: Property 'x' is missing in type 'Bar2'.
34+
y: number;
35+
}
36+
37+
class Bar3 implements I { // error
38+
~~~~
39+
!!! error TS2421: Class 'Bar3' incorrectly implements interface 'I':
40+
!!! error TS2421: Property 'x' is protected but type 'Bar3' is not a class derived from 'Foo'.
41+
x: string;
42+
y: number;
43+
}
44+
45+
class Bar4 implements I { // error
46+
~~~~
47+
!!! error TS2421: Class 'Bar4' incorrectly implements interface 'I':
48+
!!! error TS2421: Property 'x' is protected but type 'Bar4' is not a class derived from 'Foo'.
49+
protected x: string;
50+
y: number;
51+
}
52+
53+
class Bar5 extends Foo implements I { // error
54+
~~~~
55+
!!! error TS2421: Class 'Bar5' incorrectly implements interface 'I':
56+
!!! error TS2421: Property 'y' is missing in type 'Bar5'.
57+
}
58+
59+
class Bar6 extends Foo implements I { // error
60+
~~~~
61+
!!! error TS2421: Class 'Bar6' incorrectly implements interface 'I':
62+
!!! error TS2421: Property 'y' is protected in type 'Bar6' but public in type 'I'.
63+
protected y: number;
64+
}
65+
66+
class Bar7 extends Foo implements I {
67+
y: number;
68+
}
69+
70+
class Bar8 extends Foo implements I {
71+
x: string;
72+
y: number;
73+
}
74+

0 commit comments

Comments
 (0)