-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Allow visibility on constructors #4174
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
708f423
Fixes issue #2341
AbubakerB 9dfa9c7
Add tests for issue #2341
AbubakerB 6d01766
Accept baselines
AbubakerB 7105a41
Add check on construct signature and relevant test cases.
AbubakerB 51bf395
Refactor constructor call visibility check
AbubakerB c87c3c5
Clean ups!
AbubakerB f66e66d
null check and only check signature visibility on Construct Kind.
AbubakerB 5421ac8
Accept baselines
AbubakerB ffec744
Addressed CR feedback
AbubakerB 0e8ecbd
Changed error message for mismatching ctor signature
AbubakerB 3732fcd
Addressed test PR feedback.
AbubakerB f2d3b29
Accepted new baselines
AbubakerB 35fceb9
Addressed PR feedback - Amended diagnostic messages
AbubakerB f6d8a0c
Accept baselines
AbubakerB File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { } | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { } | ||
} |
67 changes: 45 additions & 22 deletions
67
tests/baselines/reference/classConstructorAccessibility.errors.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,72 @@ | ||
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(20,9): error TS2655: Constructor of type '(x: number): D' is private and only accessible within class 'D'. | ||
tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility.ts(21,9): error TS2654: Constructor of type '(x: number): E' is protected and only accessible within class 'E' and its subclasses. | ||
tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility.ts(38,13): error TS2655: Constructor of type '(x: number): D<number>' is private and only accessible within class 'D<T>'. | ||
tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility.ts(39,13): error TS2654: Constructor of type '(x: number): E<number>' is protected and only accessible within class 'E<T>' and its subclasses. | ||
tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility.ts(46,1): error TS2322: Type 'typeof D' is not assignable to type 'new (x: number) => any'. | ||
Cannot assign a private constructor to a public constructor. | ||
tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility.ts(47,1): error TS2322: Type 'typeof E' is not assignable to type 'new (x: number) => any'. | ||
Cannot assign a protected constructor to a public constructor. | ||
|
||
|
||
==== tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility.ts (4 errors) ==== | ||
==== tests/cases/conformance/classes/constructorDeclarations/classConstructorAccessibility.ts (6 errors) ==== | ||
|
||
class B { | ||
constructor(public x: number) { } | ||
} | ||
|
||
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 b = new B(1); | ||
var c = new C(1); | ||
var d = new D(1); | ||
var e = new E(1); | ||
var d = new D(1); // error - D is private | ||
~~~~~~~~ | ||
!!! error TS2655: Constructor of type '(x: number): D' is private and only accessible within class 'D'. | ||
var e = new E(1); // error - E is protected | ||
~~~~~~~~ | ||
!!! error TS2654: Constructor of type '(x: number): E' is protected and only accessible within class 'E' and its subclasses. | ||
|
||
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 b = new B(1); | ||
var c = new C(1); | ||
var d = new D(1); | ||
var e = new E(1); | ||
var d = new D(1); // error - D is private | ||
~~~~~~~~ | ||
!!! error TS2655: Constructor of type '(x: number): D<number>' is private and only accessible within class 'D<T>'. | ||
var e = new E(1); // error - E is protected | ||
~~~~~~~~ | ||
!!! error TS2654: Constructor of type '(x: number): E<number>' is protected and only accessible within class 'E<T>' and its subclasses. | ||
} | ||
|
||
|
||
// make sure signatures are covered. | ||
let sig: new(x: number) => any; | ||
sig = B; | ||
sig = C; | ||
sig = D; // error - private to public | ||
~~~ | ||
!!! error TS2322: Type 'typeof D' is not assignable to type 'new (x: number) => any'. | ||
!!! error TS2322: Cannot assign a private constructor to a public constructor. | ||
sig = E; // error - protected to public | ||
~~~ | ||
!!! error TS2322: Type 'typeof E' is not assignable to type 'new (x: number) => any'. | ||
!!! error TS2322: Cannot assign a protected constructor to a public constructor. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be
===
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought I should follow the same structure as here.
It checks to see if its not assignable (resulted from
signatureVisibilityRelatedTo
) and returns if not.