feat(ivy): add getBaseClassIdentifier() to ReflectionHost (#31544)

This method will be useful for writing ngcc `Migrations` that
need to be able to find base classes.

PR Close #31544
This commit is contained in:
Pete Bacon Darwin
2019-07-18 21:05:31 +01:00
committed by Misko Hevery
parent 399935c32b
commit 8a470b9af9
8 changed files with 408 additions and 4 deletions

View File

@ -505,6 +505,16 @@ export interface ReflectionHost {
*/
hasBaseClass(clazz: ClassDeclaration): boolean;
/**
* Get an expression representing the base class (if any) of the given `clazz`.
*
* This expression is most commonly an Identifier, but is possible to inherit from a more dynamic
* expression.
*
* @param clazz the class whose base we want to get.
*/
getBaseClassExpression(clazz: ClassDeclaration): ts.Expression|null;
/**
* Get the number of generic type parameters of a given class.
*

View File

@ -121,10 +121,28 @@ export class TypeScriptReflectionHost implements ReflectionHost {
}
hasBaseClass(clazz: ClassDeclaration): boolean {
return ts.isClassDeclaration(clazz) && clazz.heritageClauses !== undefined &&
return (ts.isClassDeclaration(clazz) || ts.isClassExpression(clazz)) &&
clazz.heritageClauses !== undefined &&
clazz.heritageClauses.some(clause => clause.token === ts.SyntaxKind.ExtendsKeyword);
}
getBaseClassExpression(clazz: ClassDeclaration): ts.Expression|null {
if (!(ts.isClassDeclaration(clazz) || ts.isClassExpression(clazz)) ||
clazz.heritageClauses === undefined) {
return null;
}
const extendsClause =
clazz.heritageClauses.find(clause => clause.token === ts.SyntaxKind.ExtendsKeyword);
if (extendsClause === undefined) {
return null;
}
const extendsType = extendsClause.types[0];
if (extendsType === undefined) {
return null;
}
return extendsType.expression;
}
getDeclarationOfIdentifier(id: ts.Identifier): Declaration|null {
// Resolve the identifier to a Symbol, and return the declaration of that.
let symbol: ts.Symbol|undefined = this.checker.getSymbolAtLocation(id);