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:

committed by
Misko Hevery

parent
399935c32b
commit
8a470b9af9
@ -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.
|
||||
*
|
||||
|
@ -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);
|
||||
|
Reference in New Issue
Block a user