Skip to content

Commit c351ffc

Browse files
committed
Added and updated tests for constructor visibility
1 parent f869b41 commit c351ffc

31 files changed

Lines changed: 843 additions & 110 deletions

tests/baselines/reference/Protected3.errors.txt

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
=== tests/cases/conformance/parser/ecmascript5/Protected/Protected3.ts ===
2+
class C {
3+
>C : Symbol(C, Decl(Protected3.ts, 0, 0))
4+
5+
protected constructor() { }
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
=== tests/cases/conformance/parser/ecmascript5/Protected/Protected3.ts ===
2+
class C {
3+
>C : C
4+
5+
protected constructor() { }
6+
}
Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,50 @@
1-
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(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.
1+
tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility.ts(15,9): error TS2673: Constructor of type '(x: number): D' is private and only accessible within class 'D'.
2+
tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility.ts(16,9): error TS2674: Constructor of type '(x: number): E' is protected and only accessible within class 'E'.
3+
tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility.ts(32,13): error TS2673: Constructor of type '<T>(x: T): D<T>' is private and only accessible within class 'D<T>'.
4+
tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility.ts(33,13): error TS2674: Constructor of type '<T>(x: T): E<T>' is protected and only accessible within class 'E<T>'.
55

66

77
==== tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility.ts (4 errors) ====
8+
89
class C {
910
public constructor(public x: number) { }
1011
}
1112

1213
class D {
13-
private constructor(public x: number) { } // error
14-
~~~~~~~
15-
!!! error TS1089: 'private' modifier cannot appear on a constructor declaration.
14+
private constructor(public x: number) { }
1615
}
1716

1817
class E {
19-
protected constructor(public x: number) { } // error
20-
~~~~~~~~~
21-
!!! error TS1089: 'protected' modifier cannot appear on a constructor declaration.
18+
protected constructor(public x: number) { }
2219
}
2320

2421
var c = new C(1);
25-
var d = new D(1);
26-
var e = new E(1);
22+
var d = new D(1); // error
23+
~~~~~~~~
24+
!!! error TS2673: Constructor of type '(x: number): D' is private and only accessible within class 'D'.
25+
var e = new E(1); // error
26+
~~~~~~~~
27+
!!! error TS2674: Constructor of type '(x: number): E' is protected and only accessible within class 'E'.
2728

2829
module Generic {
2930
class C<T> {
3031
public constructor(public x: T) { }
3132
}
3233

3334
class D<T> {
34-
private constructor(public x: T) { } // error
35-
~~~~~~~
36-
!!! error TS1089: 'private' modifier cannot appear on a constructor declaration.
35+
private constructor(public x: T) { }
3736
}
3837

3938
class E<T> {
40-
protected constructor(public x: T) { } // error
41-
~~~~~~~~~
42-
!!! error TS1089: 'protected' modifier cannot appear on a constructor declaration.
39+
protected constructor(public x: T) { }
4340
}
4441

4542
var c = new C(1);
46-
var d = new D(1);
47-
var e = new E(1);
43+
var d = new D(1); // error
44+
~~~~~~~~
45+
!!! error TS2673: Constructor of type '<T>(x: T): D<T>' is private and only accessible within class 'D<T>'.
46+
var e = new E(1); // error
47+
~~~~~~~~
48+
!!! error TS2674: Constructor of type '<T>(x: T): E<T>' is protected and only accessible within class 'E<T>'.
4849
}
4950

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,37 @@
11
//// [classConstructorAccessibility.ts]
2+
23
class C {
34
public constructor(public x: number) { }
45
}
56

67
class D {
7-
private constructor(public x: number) { } // error
8+
private constructor(public x: number) { }
89
}
910

1011
class E {
11-
protected constructor(public x: number) { } // error
12+
protected constructor(public x: number) { }
1213
}
1314

1415
var c = new C(1);
15-
var d = new D(1);
16-
var e = new E(1);
16+
var d = new D(1); // error
17+
var e = new E(1); // error
1718

1819
module Generic {
1920
class C<T> {
2021
public constructor(public x: T) { }
2122
}
2223

2324
class D<T> {
24-
private constructor(public x: T) { } // error
25+
private constructor(public x: T) { }
2526
}
2627

2728
class E<T> {
28-
protected constructor(public x: T) { } // error
29+
protected constructor(public x: T) { }
2930
}
3031

3132
var c = new C(1);
32-
var d = new D(1);
33-
var e = new E(1);
33+
var d = new D(1); // error
34+
var e = new E(1); // error
3435
}
3536

3637

@@ -44,18 +45,18 @@ var C = (function () {
4445
var D = (function () {
4546
function D(x) {
4647
this.x = x;
47-
} // error
48+
}
4849
return D;
4950
}());
5051
var E = (function () {
5152
function E(x) {
5253
this.x = x;
53-
} // error
54+
}
5455
return E;
5556
}());
5657
var c = new C(1);
57-
var d = new D(1);
58-
var e = new E(1);
58+
var d = new D(1); // error
59+
var e = new E(1); // error
5960
var Generic;
6061
(function (Generic) {
6162
var C = (function () {
@@ -67,16 +68,36 @@ var Generic;
6768
var D = (function () {
6869
function D(x) {
6970
this.x = x;
70-
} // error
71+
}
7172
return D;
7273
}());
7374
var E = (function () {
7475
function E(x) {
7576
this.x = x;
76-
} // error
77+
}
7778
return E;
7879
}());
7980
var c = new C(1);
80-
var d = new D(1);
81-
var e = new E(1);
81+
var d = new D(1); // error
82+
var e = new E(1); // error
8283
})(Generic || (Generic = {}));
84+
85+
86+
//// [classConstructorAccessibility.d.ts]
87+
declare class C {
88+
x: number;
89+
constructor(x: number);
90+
}
91+
declare class D {
92+
x: number;
93+
constructor(x);
94+
}
95+
declare class E {
96+
x: number;
97+
constructor(x: number);
98+
}
99+
declare var c: C;
100+
declare var d: any;
101+
declare var e: any;
102+
declare module Generic {
103+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts(26,28): error TS2674: Constructor of type '(x: number): BaseB' is protected and only accessible within class 'BaseB'.
2+
tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts(29,24): error TS2675: Cannot extend private class 'BaseC'.
3+
tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts(32,28): error TS2673: Constructor of type '(x: number): BaseC' is private and only accessible within class 'BaseC'.
4+
tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts(36,10): error TS2674: Constructor of type '(x: number): BaseB' is protected and only accessible within class 'BaseB'.
5+
tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts(37,10): error TS2673: Constructor of type '(x: number): BaseC' is private and only accessible within class 'BaseC'.
6+
7+
8+
==== tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts (5 errors) ====
9+
10+
class BaseA {
11+
public constructor(public x: number) { }
12+
createInstance() { new BaseA(1); }
13+
}
14+
15+
class BaseB {
16+
protected constructor(public x: number) { }
17+
createInstance() { new BaseB(1); }
18+
}
19+
20+
class BaseC {
21+
private constructor(public x: number) { }
22+
createInstance() { new BaseC(1); }
23+
}
24+
25+
class DerivedA extends BaseA {
26+
constructor(public x: number) { super(x); }
27+
createInstance() { new DerivedA(1); }
28+
createBaseInstance() { new BaseA(1); }
29+
}
30+
31+
class DerivedB extends BaseB {
32+
constructor(public x: number) { super(x); }
33+
createInstance() { new DerivedB(1); }
34+
createBaseInstance() { new BaseB(1); } // error
35+
~~~~~~~~~~~~
36+
!!! error TS2674: Constructor of type '(x: number): BaseB' is protected and only accessible within class 'BaseB'.
37+
}
38+
39+
class DerivedC extends BaseC { // error
40+
~~~~~
41+
!!! error TS2675: Cannot extend private class 'BaseC'.
42+
constructor(public x: number) { super(x); }
43+
createInstance() { new DerivedC(1); }
44+
createBaseInstance() { new BaseC(1); } // error
45+
~~~~~~~~~~~~
46+
!!! error TS2673: Constructor of type '(x: number): BaseC' is private and only accessible within class 'BaseC'.
47+
}
48+
49+
var ba = new BaseA(1);
50+
var bb = new BaseB(1); // error
51+
~~~~~~~~~~~~
52+
!!! error TS2674: Constructor of type '(x: number): BaseB' is protected and only accessible within class 'BaseB'.
53+
var bc = new BaseC(1); // error
54+
~~~~~~~~~~~~
55+
!!! error TS2673: Constructor of type '(x: number): BaseC' is private and only accessible within class 'BaseC'.
56+
57+
var da = new DerivedA(1);
58+
var db = new DerivedB(1);
59+
var dc = new DerivedC(1);

0 commit comments

Comments
 (0)