-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Constructor Visibility #6885
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
Constructor Visibility #6885
Changes from 1 commit
f869b41
c351ffc
bbf92ce
3d529a8
4200051
4c93eb2
5ce0202
16b54e0
037b657
a6a5a22
d0843e1
8ef9599
ec7e80e
91ea83c
208830e
2d7a0f4
ba8b168
de23e2f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5889,16 +5889,20 @@ namespace ts { | |
|
|
||
| const sourceSignatures = getSignaturesOfType(source, kind); | ||
| const targetSignatures = getSignaturesOfType(target, kind); | ||
| if (kind === SignatureKind.Construct && sourceSignatures.length && targetSignatures.length && | ||
| isAbstractConstructorType(source) && !isAbstractConstructorType(target)) { | ||
| // An abstract constructor type is not assignable to a non-abstract constructor type | ||
| // as it would otherwise be possible to new an abstract class. Note that the assignablity | ||
| // check we perform for an extends clause excludes construct signatures from the target, | ||
| // so this check never proceeds. | ||
| if (reportErrors) { | ||
| reportError(Diagnostics.Cannot_assign_an_abstract_constructor_type_to_a_non_abstract_constructor_type); | ||
| if (kind === SignatureKind.Construct && sourceSignatures.length && targetSignatures.length) { | ||
| if (isAbstractConstructorType(source) && !isAbstractConstructorType(target)) { | ||
| // An abstract constructor type is not assignable to a non-abstract constructor type | ||
| // as it would otherwise be possible to new an abstract class. Note that the assignablity | ||
| // check we perform for an extends clause excludes construct signatures from the target, | ||
| // so this check never proceeds. | ||
| if (reportErrors) { | ||
| reportError(Diagnostics.Cannot_assign_an_abstract_constructor_type_to_a_non_abstract_constructor_type); | ||
| } | ||
| return Ternary.False; | ||
| } | ||
| if (!constructorRelatedTo(sourceSignatures[0], targetSignatures[0], reportErrors)) { | ||
| return Ternary.False; | ||
| } | ||
| return Ternary.False; | ||
| } | ||
|
|
||
| let result = Ternary.True; | ||
|
|
@@ -6052,6 +6056,32 @@ namespace ts { | |
| } | ||
| return Ternary.True; | ||
| } | ||
|
|
||
| function constructorRelatedTo(sourceSignature: Signature, targetSignature: Signature, reportErrors: boolean) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like a better name would be
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
| if (sourceSignature && targetSignature && sourceSignature.declaration && targetSignature.declaration) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When would
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same with
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are cases where the declarations are |
||
| const sourceAccessibility = sourceSignature.declaration.flags & (NodeFlags.Private | NodeFlags.Protected); | ||
| const targetAccessibility = targetSignature.declaration.flags & (NodeFlags.Private | NodeFlags.Protected); | ||
|
|
||
| const isRelated = sourceAccessibility === targetAccessibility; | ||
| if (!isRelated && reportErrors) { | ||
| reportError(Diagnostics.Cannot_assign_a_0_constructor_type_to_a_1_constructor_type, flagsToString(sourceAccessibility), flagsToString(targetAccessibility)); | ||
| } | ||
|
|
||
| return isRelated; | ||
| } | ||
|
|
||
| return true; | ||
|
|
||
| function flagsToString(flags: NodeFlags) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move this outside the function and rename (
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
| if (flags === NodeFlags.Private) { | ||
| return "private"; | ||
| } | ||
| if (flags === NodeFlags.Protected) { | ||
| return "protected"; | ||
| } | ||
| return "public"; | ||
| } | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you don't get rid of the |
||
| } | ||
|
|
||
| // Return true if the given type is the constructor type for an abstract class | ||
|
|
@@ -10103,6 +10133,9 @@ namespace ts { | |
| // that the user will not add any. | ||
| const constructSignatures = getSignaturesOfType(expressionType, SignatureKind.Construct); | ||
| if (constructSignatures.length) { | ||
| if (!isConstructorAccessible(node, constructSignatures[0])) { | ||
| return resolveErrorCall(node); | ||
| } | ||
| return resolveCall(node, constructSignatures, candidatesOutArray); | ||
| } | ||
|
|
||
|
|
@@ -10123,6 +10156,37 @@ namespace ts { | |
| return resolveErrorCall(node); | ||
| } | ||
|
|
||
| function isConstructorAccessible(node: NewExpression, signature: Signature) { | ||
| if (!signature || !signature.declaration) { | ||
| return true; | ||
| } | ||
|
|
||
| const declaration = signature.declaration; | ||
| const flags = declaration.flags; | ||
|
|
||
| // Public constructor is accessible. | ||
| if (!(flags & (NodeFlags.Private | NodeFlags.Protected))) { | ||
| return true; | ||
| } | ||
|
|
||
| const declaringClass = <InterfaceType>getDeclaredTypeOfSymbol(declaration.parent.symbol); | ||
| const enclosingClassDeclaration = getContainingClass(node); | ||
| const enclosingClass = enclosingClassDeclaration ? <InterfaceType>getDeclaredTypeOfSymbol(getSymbolOfNode(enclosingClassDeclaration)) : undefined; | ||
|
|
||
| // A private or protected constructor can only be instantiated within it's own class | ||
| if (declaringClass !== enclosingClass) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as noted in , we #7059, if here is not sufficient, it should be a while loop walking up, looking at all enclosing classes, so that something like this should be legal: class B {
private constructor() { }
method() {
class C {
method() {
new B(); // should be fine
}
}
}
}
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
| if (flags & NodeFlags.Private) { | ||
| error(node, Diagnostics.Constructor_of_type_0_is_private_and_only_accessible_within_class_1, signatureToString(signature), typeToString(declaringClass)); | ||
| } | ||
| if (flags & NodeFlags.Protected) { | ||
| error(node, Diagnostics.Constructor_of_type_0_is_protected_and_only_accessible_within_class_1, signatureToString(signature), typeToString(declaringClass)); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| function resolveTaggedTemplateExpression(node: TaggedTemplateExpression, candidatesOutArray: Signature[]): Signature { | ||
| const tagType = checkExpression(node.tag); | ||
| const apparentType = getApparentType(tagType); | ||
|
|
@@ -12059,7 +12123,7 @@ namespace ts { | |
| error(o.name, Diagnostics.Overload_signatures_must_all_be_ambient_or_non_ambient); | ||
| } | ||
| else if (deviation & (NodeFlags.Private | NodeFlags.Protected)) { | ||
| error(o.name, Diagnostics.Overload_signatures_must_all_be_public_private_or_protected); | ||
| error(o.name || o, Diagnostics.Overload_signatures_must_all_be_public_private_or_protected); | ||
| } | ||
| else if (deviation & NodeFlags.Abstract) { | ||
| error(o.name, Diagnostics.Overload_signatures_must_all_be_abstract_or_not_abstract); | ||
|
|
@@ -13928,6 +13992,7 @@ namespace ts { | |
| if (baseTypes.length && produceDiagnostics) { | ||
| const baseType = baseTypes[0]; | ||
| const staticBaseType = getBaseConstructorTypeOfClass(type); | ||
| checkBaseTypeAccessibility(staticBaseType, baseTypeNode); | ||
| checkSourceElement(baseTypeNode.expression); | ||
| if (baseTypeNode.typeArguments) { | ||
| forEach(baseTypeNode.typeArguments, checkSourceElement); | ||
|
|
@@ -13983,6 +14048,16 @@ namespace ts { | |
| } | ||
| } | ||
|
|
||
| function checkBaseTypeAccessibility(type: ObjectType, node: ExpressionWithTypeArguments) { | ||
| const signatures = getSignaturesOfType(type, SignatureKind.Construct); | ||
| if (signatures.length) { | ||
| const declaration = signatures[0].declaration; | ||
| if (declaration && declaration.flags & NodeFlags.Private) { | ||
| error(node, Diagnostics.Cannot_extend_private_class_0, (<Identifier>node.expression).text); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function getTargetSymbol(s: Symbol) { | ||
| // if symbol is instantiated its flags are not copied from the 'target' | ||
| // so we'll need to get back original 'target' symbol to work with correct set of flags | ||
|
|
@@ -16348,12 +16423,6 @@ namespace ts { | |
| if (flags & NodeFlags.Abstract) { | ||
| return grammarErrorOnNode(lastStatic, Diagnostics._0_modifier_cannot_appear_on_a_constructor_declaration, "abstract"); | ||
| } | ||
| else if (flags & NodeFlags.Protected) { | ||
| return grammarErrorOnNode(lastProtected, Diagnostics._0_modifier_cannot_appear_on_a_constructor_declaration, "protected"); | ||
| } | ||
| else if (flags & NodeFlags.Private) { | ||
| return grammarErrorOnNode(lastPrivate, Diagnostics._0_modifier_cannot_appear_on_a_constructor_declaration, "private"); | ||
| } | ||
| else if (flags & NodeFlags.Async) { | ||
| return grammarErrorOnNode(lastAsync, Diagnostics._0_modifier_cannot_appear_on_a_constructor_declaration, "async"); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1823,6 +1823,22 @@ | |
| "category": "Error", | ||
| "code": 2671 | ||
| }, | ||
| "Cannot assign a '{0}' constructor type to a '{1}' constructor type.": { | ||
| "category": "Error", | ||
| "code": 2672 | ||
| }, | ||
| "Constructor of type '{0}' is private and only accessible within class '{1}'.": { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Constructor of class '{0}' is private and only accessible within the class declaration.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
| "category": "Error", | ||
| "code": 2673 | ||
| }, | ||
| "Constructor of type '{0}' is protected and only accessible within class '{1}'.": { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how about
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I have currently implemented it such that a class with a protected constructor cannot be instantiated within a derived class (only extended - thus only from it's super call).
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. never mind. my bad. |
||
| "category": "Error", | ||
| "code": 2674 | ||
| }, | ||
| "Cannot extend private class '{0}'.": { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how about:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
| "category": "Error", | ||
| "code": 2675 | ||
| }, | ||
| "Import declaration '{0}' is using private name '{1}'.": { | ||
| "category": "Error", | ||
| "code": 4000 | ||
|
|
||
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.
Why is it just comparing the first of each? Why was this change made?
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.
Only the first has to be checked because the rest must have identical visibility (it's an error for this not to be the case)