Protected ctors are accessible in subclass static methods

Previously, it was an error to refer to a protected constructor from a
base class, even in a static method where the semantics work. Now it is
not an error in static methods.
This commit is contained in:
Nathan Shively-Sanders 2016-07-22 13:48:43 -07:00
parent 25525607d5
commit 97ef839a03

View File

@ -11544,8 +11544,23 @@ namespace ts {
const declaringClassDeclaration = <ClassLikeDeclaration>getClassLikeDeclarationOfSymbol(declaration.parent.symbol);
const declaringClass = <InterfaceType>getDeclaredTypeOfSymbol(declaration.parent.symbol);
// A private or protected constructor can only be instantiated within it's own class
// A private or protected constructor can only be instantiated within its own class or a static method of a subclass
if (!isNodeWithinClass(node, declaringClassDeclaration)) {
const containingFunction = getContainingFunction(node);
const containingClass = getContainingClass(node);
if (containingClass) {
const containingType = getTypeOfNode(containingClass);
const baseTypes = getBaseTypes(<InterfaceType>containingType);
if (baseTypes.length) {
const baseType = baseTypes[0];
if (containingFunction &&
containingFunction.flags & NodeFlags.Static &&
flags & NodeFlags.Protected &&
baseType.symbol === declaration.parent.symbol) {
return true;
}
}
}
if (flags & NodeFlags.Private) {
error(node, Diagnostics.Constructor_of_class_0_is_private_and_only_accessible_within_the_class_declaration, typeToString(declaringClass));
}