Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added and updated tests for constructor visibility
  • Loading branch information
AbubakerB committed Feb 3, 2016
commit c351ffcc75c1d257f72a3e86faaa640d8daefdf3
9 changes: 0 additions & 9 deletions tests/baselines/reference/Protected3.errors.txt

This file was deleted.

6 changes: 6 additions & 0 deletions tests/baselines/reference/Protected3.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
=== tests/cases/conformance/parser/ecmascript5/Protected/Protected3.ts ===
class C {
>C : Symbol(C, Decl(Protected3.ts, 0, 0))

protected constructor() { }
}
6 changes: 6 additions & 0 deletions tests/baselines/reference/Protected3.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
=== tests/cases/conformance/parser/ecmascript5/Protected/Protected3.ts ===
class C {
>C : C

protected constructor() { }
}
41 changes: 21 additions & 20 deletions tests/baselines/reference/classConstructorAccessibility.errors.txt
Original file line number Diff line number Diff line change
@@ -1,49 +1,50 @@
tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility.ts(6,5): error TS1089: 'private' modifier cannot appear on a constructor declaration.
tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility.ts(10,5): error TS1089: 'protected' modifier cannot appear on a constructor declaration.
tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility.ts(23,9): error TS1089: 'private' modifier cannot appear on a constructor declaration.
tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility.ts(27,9): error TS1089: 'protected' modifier cannot appear on a constructor declaration.
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'.
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'.
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>'.
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>'.


==== tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility.ts (4 errors) ====

class C {
public constructor(public x: number) { }
}

class D {
private constructor(public x: number) { } // error
~~~~~~~
!!! error TS1089: 'private' modifier cannot appear on a constructor declaration.
private constructor(public x: number) { }
}

class E {
protected constructor(public x: number) { } // error
~~~~~~~~~
!!! error TS1089: 'protected' modifier cannot appear on a constructor declaration.
protected constructor(public x: number) { }
}

var c = new C(1);
var d = new D(1);
var e = new E(1);
var d = new D(1); // error
~~~~~~~~
!!! error TS2673: Constructor of type '(x: number): D' is private and only accessible within class 'D'.
var e = new E(1); // error
~~~~~~~~
!!! error TS2674: Constructor of type '(x: number): E' is protected and only accessible within class 'E'.

module Generic {
class C<T> {
public constructor(public x: T) { }
}

class D<T> {
private constructor(public x: T) { } // error
~~~~~~~
!!! error TS1089: 'private' modifier cannot appear on a constructor declaration.
private constructor(public x: T) { }
}

class E<T> {
protected constructor(public x: T) { } // error
~~~~~~~~~
!!! error TS1089: 'protected' modifier cannot appear on a constructor declaration.
protected constructor(public x: T) { }
}

var c = new C(1);
var d = new D(1);
var e = new E(1);
var d = new D(1); // error
~~~~~~~~
!!! error TS2673: Constructor of type '<T>(x: T): D<T>' is private and only accessible within class 'D<T>'.
var e = new E(1); // error
~~~~~~~~
!!! error TS2674: Constructor of type '<T>(x: T): E<T>' is protected and only accessible within class 'E<T>'.
}

53 changes: 37 additions & 16 deletions tests/baselines/reference/classConstructorAccessibility.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,37 @@
//// [classConstructorAccessibility.ts]

class C {
public constructor(public x: number) { }
}

class D {
private constructor(public x: number) { } // error
private constructor(public x: number) { }
}

class E {
protected constructor(public x: number) { } // error
protected constructor(public x: number) { }
}

var c = new C(1);
var d = new D(1);
var e = new E(1);
var d = new D(1); // error
var e = new E(1); // error

module Generic {
class C<T> {
public constructor(public x: T) { }
}

class D<T> {
private constructor(public x: T) { } // error
private constructor(public x: T) { }
}

class E<T> {
protected constructor(public x: T) { } // error
protected constructor(public x: T) { }
}

var c = new C(1);
var d = new D(1);
var e = new E(1);
var d = new D(1); // error
var e = new E(1); // error
}


Expand All @@ -44,18 +45,18 @@ var C = (function () {
var D = (function () {
function D(x) {
this.x = x;
} // error
}
return D;
}());
var E = (function () {
function E(x) {
this.x = x;
} // error
}
return E;
}());
var c = new C(1);
var d = new D(1);
var e = new E(1);
var d = new D(1); // error
var e = new E(1); // error
var Generic;
(function (Generic) {
var C = (function () {
Expand All @@ -67,16 +68,36 @@ var Generic;
var D = (function () {
function D(x) {
this.x = x;
} // error
}
return D;
}());
var E = (function () {
function E(x) {
this.x = x;
} // error
}
return E;
}());
var c = new C(1);
var d = new D(1);
var e = new E(1);
var d = new D(1); // error
var e = new E(1); // error
})(Generic || (Generic = {}));


//// [classConstructorAccessibility.d.ts]
declare class C {
x: number;
constructor(x: number);
}
declare class D {
x: number;
constructor(x);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll need to update the declaration emitter to emit private / protected appropriately

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}
declare class E {
x: number;
constructor(x: number);
}
declare var c: C;
declare var d: any;
declare var e: any;
declare module Generic {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
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'.
tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts(29,24): error TS2675: Cannot extend private class 'BaseC'.
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'.
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'.
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'.


==== tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility2.ts (5 errors) ====

class BaseA {
public constructor(public x: number) { }
createInstance() { new BaseA(1); }
}

class BaseB {
protected constructor(public x: number) { }
createInstance() { new BaseB(1); }
}

class BaseC {
private constructor(public x: number) { }
createInstance() { new BaseC(1); }
}

class DerivedA extends BaseA {
constructor(public x: number) { super(x); }
createInstance() { new DerivedA(1); }
createBaseInstance() { new BaseA(1); }
}

class DerivedB extends BaseB {
constructor(public x: number) { super(x); }
createInstance() { new DerivedB(1); }
createBaseInstance() { new BaseB(1); } // error
~~~~~~~~~~~~
!!! error TS2674: Constructor of type '(x: number): BaseB' is protected and only accessible within class 'BaseB'.
}

class DerivedC extends BaseC { // error
~~~~~
!!! error TS2675: Cannot extend private class 'BaseC'.
constructor(public x: number) { super(x); }
createInstance() { new DerivedC(1); }
createBaseInstance() { new BaseC(1); } // error
~~~~~~~~~~~~
!!! error TS2673: Constructor of type '(x: number): BaseC' is private and only accessible within class 'BaseC'.
}

var ba = new BaseA(1);
var bb = new BaseB(1); // error
~~~~~~~~~~~~
!!! error TS2674: Constructor of type '(x: number): BaseB' is protected and only accessible within class 'BaseB'.
var bc = new BaseC(1); // error
~~~~~~~~~~~~
!!! error TS2673: Constructor of type '(x: number): BaseC' is private and only accessible within class 'BaseC'.

var da = new DerivedA(1);
var db = new DerivedB(1);
var dc = new DerivedC(1);
Loading