Handle intersection types when looking up base types for visibility (#25418)

* Handle intersection types when looking up base types for visibility

* Extract protected constructor check to function and recur on intersections

* Remove unneeded cast
This commit is contained in:
Wesley Wigham
2018-07-09 16:58:55 -07:00
committed by GitHub
parent 6b4a3b2bbb
commit 3d64b9d7ac
6 changed files with 261 additions and 9 deletions

View File

@@ -19345,6 +19345,38 @@ namespace ts {
return resolveErrorCall(node);
}
function typeHasProtectedAccessibleBase(target: Symbol, type: InterfaceType): boolean {
const baseTypes = getBaseTypes(type);
if (!length(baseTypes)) {
return false;
}
const firstBase = baseTypes[0];
if (firstBase.flags & TypeFlags.Intersection) {
const types = (firstBase as IntersectionType).types;
const mixinCount = countWhere(types, isMixinConstructorType);
let i = 0;
for (const intersectionMember of (firstBase as IntersectionType).types) {
i++;
// We want to ignore mixin ctors
if (mixinCount === 0 || mixinCount === types.length && i === 0 || !isMixinConstructorType(intersectionMember)) {
if (getObjectFlags(intersectionMember) & (ObjectFlags.Class | ObjectFlags.Interface)) {
if (intersectionMember.symbol === target) {
return true;
}
if (typeHasProtectedAccessibleBase(target, intersectionMember as InterfaceType)) {
return true;
}
}
}
}
return false;
}
if (firstBase.symbol === target) {
return true;
}
return typeHasProtectedAccessibleBase(target, firstBase as InterfaceType);
}
function isConstructorAccessible(node: NewExpression, signature: Signature) {
if (!signature || !signature.declaration) {
return true;
@@ -19364,16 +19396,10 @@ namespace ts {
// A private or protected constructor can only be instantiated within its own class (or a subclass, for protected)
if (!isNodeWithinClass(node, declaringClassDeclaration)) {
const containingClass = getContainingClass(node);
if (containingClass) {
if (containingClass && modifiers & ModifierFlags.Protected) {
const containingType = getTypeOfNode(containingClass);
let baseTypes = getBaseTypes(containingType as InterfaceType);
while (baseTypes.length) {
const baseType = baseTypes[0];
if (modifiers & ModifierFlags.Protected &&
baseType.symbol === declaration.parent.symbol) {
return true;
}
baseTypes = getBaseTypes(baseType as InterfaceType);
if (typeHasProtectedAccessibleBase(declaration.parent.symbol, containingType as InterfaceType)) {
return true;
}
}
if (modifiers & ModifierFlags.Private) {