refactor(ngcc): move getModuleWithProvidersFunctions()
into the analyzer (#36948)
Previously this method was implemented on the `NgccReflectionHost`, but really it is asking too much of the host, since it actually needs to do some static evaluation of the code to be able to support a wider range of function shapes. Also there was only one implementation of the method in the `Esm2015ReflectionHost` since it has no format specific code in in. This commit moves the whole function (and supporting helpers) into the `ModuleWithProvidersAnalyzer`, which is the only place it was being used. This class will be able to do further static evaluation of the function bodies in order to support more function shapes than the host can do on its own. The commit removes a whole set of reflection host tests but these are already covered by the tests of the analyzer. PR Close #36948
This commit is contained in:

committed by
Alex Rickabaugh

parent
c9e0db55f7
commit
e010f2ca54
@ -10,7 +10,7 @@ import * as ts from 'typescript';
|
||||
import {ReferencesRegistry} from '../../../src/ngtsc/annotations';
|
||||
import {Reference} from '../../../src/ngtsc/imports';
|
||||
import {ClassDeclaration, ConcreteDeclaration} from '../../../src/ngtsc/reflection';
|
||||
import {ModuleWithProvidersFunction, NgccReflectionHost} from '../host/ngcc_host';
|
||||
import {NgccReflectionHost} from '../host/ngcc_host';
|
||||
import {hasNameIdentifier, isDefined} from '../utils';
|
||||
|
||||
export interface ModuleWithProvidersInfo {
|
||||
@ -38,7 +38,7 @@ export class ModuleWithProvidersAnalyzer {
|
||||
const analyses = new ModuleWithProvidersAnalyses();
|
||||
const rootFiles = this.getRootFiles(program);
|
||||
rootFiles.forEach(f => {
|
||||
const fns = this.host.getModuleWithProvidersFunctions(f);
|
||||
const fns = this.getModuleWithProvidersFunctions(f);
|
||||
fns && fns.forEach(fn => {
|
||||
if (fn.ngModule.viaModule === null) {
|
||||
// Record the usage of an internal module as it needs to become an exported symbol
|
||||
@ -68,6 +68,100 @@ export class ModuleWithProvidersAnalyzer {
|
||||
return program.getRootFileNames().map(f => program.getSourceFile(f)).filter(isDefined);
|
||||
}
|
||||
|
||||
private getModuleWithProvidersFunctions(f: ts.SourceFile): ModuleWithProvidersFunction[] {
|
||||
const exports = this.host.getExportsOfModule(f);
|
||||
if (!exports) return [];
|
||||
const infos: ModuleWithProvidersFunction[] = [];
|
||||
exports.forEach((declaration, name) => {
|
||||
if (declaration.node === null) {
|
||||
return;
|
||||
}
|
||||
if (this.host.isClass(declaration.node)) {
|
||||
this.host.getMembersOfClass(declaration.node).forEach(member => {
|
||||
if (member.isStatic) {
|
||||
const info = this.parseForModuleWithProviders(
|
||||
member.name, member.node, member.implementation, declaration.node);
|
||||
if (info) {
|
||||
infos.push(info);
|
||||
}
|
||||
}
|
||||
});
|
||||
} else {
|
||||
if (hasNameIdentifier(declaration.node)) {
|
||||
const info =
|
||||
this.parseForModuleWithProviders(declaration.node.name.text, declaration.node);
|
||||
if (info) {
|
||||
infos.push(info);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
return infos;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a function/method node (or its implementation), to see if it returns a
|
||||
* `ModuleWithProviders` object.
|
||||
* @param name The name of the function.
|
||||
* @param node the node to check - this could be a function, a method or a variable declaration.
|
||||
* @param implementation the actual function expression if `node` is a variable declaration.
|
||||
* @param container the class that contains the function, if it is a method.
|
||||
* @returns info about the function if it does return a `ModuleWithProviders` object; `null`
|
||||
* otherwise.
|
||||
*/
|
||||
private parseForModuleWithProviders(
|
||||
name: string, node: ts.Node|null, implementation: ts.Node|null = node,
|
||||
container: ts.Declaration|null = null): ModuleWithProvidersFunction|null {
|
||||
if (implementation === null ||
|
||||
(!ts.isFunctionDeclaration(implementation) && !ts.isMethodDeclaration(implementation) &&
|
||||
!ts.isFunctionExpression(implementation))) {
|
||||
return null;
|
||||
}
|
||||
const declaration = implementation;
|
||||
const definition = this.host.getDefinitionOfFunction(declaration);
|
||||
if (definition === null) {
|
||||
return null;
|
||||
}
|
||||
const body = definition.body;
|
||||
const lastStatement = body && body[body.length - 1];
|
||||
const returnExpression =
|
||||
lastStatement && ts.isReturnStatement(lastStatement) && lastStatement.expression || null;
|
||||
const ngModuleProperty = returnExpression && ts.isObjectLiteralExpression(returnExpression) &&
|
||||
returnExpression.properties.find(
|
||||
prop =>
|
||||
!!prop.name && ts.isIdentifier(prop.name) && prop.name.text === 'ngModule') ||
|
||||
null;
|
||||
|
||||
if (!ngModuleProperty || !ts.isPropertyAssignment(ngModuleProperty)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// The ngModuleValue could be of the form `SomeModule` or `namespace_1.SomeModule`
|
||||
let ngModuleValue = ngModuleProperty.initializer;
|
||||
if (ts.isPropertyAccessExpression(ngModuleValue)) {
|
||||
ngModuleValue = ngModuleValue.expression;
|
||||
}
|
||||
|
||||
if (!ts.isIdentifier(ngModuleValue)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const ngModuleDeclaration = this.host.getDeclarationOfIdentifier(ngModuleValue);
|
||||
if (!ngModuleDeclaration || ngModuleDeclaration.node === null) {
|
||||
throw new Error(`Cannot find a declaration for NgModule ${
|
||||
ngModuleValue.getText()} referenced in "${declaration!.getText()}"`);
|
||||
}
|
||||
if (!hasNameIdentifier(ngModuleDeclaration.node)) {
|
||||
return null;
|
||||
}
|
||||
return {
|
||||
name,
|
||||
ngModule: ngModuleDeclaration as ConcreteDeclaration<ClassDeclaration>,
|
||||
declaration,
|
||||
container
|
||||
};
|
||||
}
|
||||
|
||||
private getDtsDeclarationForFunction(fn: ModuleWithProvidersFunction) {
|
||||
let dtsFn: ts.Declaration|null = null;
|
||||
const containerClass = fn.container && this.host.getClassSymbol(fn.container);
|
||||
@ -128,3 +222,27 @@ function isFunctionOrMethod(declaration: ts.Declaration): declaration is ts.Func
|
||||
function isAnyKeyword(typeParam: ts.TypeNode): typeParam is ts.KeywordTypeNode {
|
||||
return typeParam.kind === ts.SyntaxKind.AnyKeyword;
|
||||
}
|
||||
|
||||
/**
|
||||
* A structure returned from `getModuleWithProvidersFunction` that describes functions
|
||||
* that return ModuleWithProviders objects.
|
||||
*/
|
||||
export interface ModuleWithProvidersFunction {
|
||||
/**
|
||||
* The name of the declared function.
|
||||
*/
|
||||
name: string;
|
||||
/**
|
||||
* The declaration of the function that returns the `ModuleWithProviders` object.
|
||||
*/
|
||||
declaration: ts.SignatureDeclaration;
|
||||
/**
|
||||
* Declaration of the containing class (if this is a method)
|
||||
*/
|
||||
container: ts.Declaration|null;
|
||||
/**
|
||||
* The declaration of the class that the `ngModule` property on the `ModuleWithProviders` object
|
||||
* refers to.
|
||||
*/
|
||||
ngModule: ConcreteDeclaration<ClassDeclaration>;
|
||||
}
|
||||
|
@ -11,7 +11,7 @@ import * as ts from 'typescript';
|
||||
import {ClassDeclaration, ClassMember, CtorParameter, Declaration, Decorator, FunctionDefinition, Import, ReflectionHost} from '../../../src/ngtsc/reflection';
|
||||
import {isFromDtsFile} from '../../../src/ngtsc/util/src/typescript';
|
||||
|
||||
import {ModuleWithProvidersFunction, NgccClassSymbol, NgccReflectionHost, SwitchableVariableDeclaration} from './ngcc_host';
|
||||
import {NgccClassSymbol, NgccReflectionHost, SwitchableVariableDeclaration} from './ngcc_host';
|
||||
|
||||
/**
|
||||
* A reflection host implementation that delegates reflector queries depending on whether they
|
||||
@ -149,10 +149,6 @@ export class DelegatingReflectionHost implements NgccReflectionHost {
|
||||
return this.ngccHost.getDecoratorsOfSymbol(symbol);
|
||||
}
|
||||
|
||||
getModuleWithProvidersFunctions(sf: ts.SourceFile): ModuleWithProvidersFunction[] {
|
||||
return this.ngccHost.getModuleWithProvidersFunctions(sf);
|
||||
}
|
||||
|
||||
getSwitchableDeclarations(module: ts.Node): SwitchableVariableDeclaration[] {
|
||||
return this.ngccHost.getSwitchableDeclarations(module);
|
||||
}
|
||||
|
@ -8,13 +8,13 @@
|
||||
|
||||
import * as ts from 'typescript';
|
||||
|
||||
import {ClassDeclaration, ClassMember, ClassMemberKind, ConcreteDeclaration, CtorParameter, Declaration, Decorator, EnumMember, isDecoratorIdentifier, isNamedClassDeclaration, KnownDeclaration, reflectObjectLiteral, SpecialDeclarationKind, TypeScriptReflectionHost, TypeValueReference} from '../../../src/ngtsc/reflection';
|
||||
import {ClassDeclaration, ClassMember, ClassMemberKind, CtorParameter, Declaration, Decorator, EnumMember, isDecoratorIdentifier, isNamedClassDeclaration, KnownDeclaration, reflectObjectLiteral, SpecialDeclarationKind, TypeScriptReflectionHost, TypeValueReference} from '../../../src/ngtsc/reflection';
|
||||
import {isWithinPackage} from '../analysis/util';
|
||||
import {Logger} from '../logging/logger';
|
||||
import {BundleProgram} from '../packages/bundle_program';
|
||||
import {findAll, getNameText, hasNameIdentifier, isDefined, stripDollarSuffix} from '../utils';
|
||||
|
||||
import {ClassSymbol, isSwitchableVariableDeclaration, ModuleWithProvidersFunction, NgccClassSymbol, NgccReflectionHost, PRE_R3_MARKER, SwitchableVariableDeclaration} from './ngcc_host';
|
||||
import {ClassSymbol, isSwitchableVariableDeclaration, NgccClassSymbol, NgccReflectionHost, PRE_R3_MARKER, SwitchableVariableDeclaration} from './ngcc_host';
|
||||
import {stripParentheses} from './utils';
|
||||
|
||||
export const DECORATORS = 'decorators' as ts.__String;
|
||||
@ -568,44 +568,6 @@ export class Esm2015ReflectionHost extends TypeScriptReflectionHost implements N
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Search the given source file for exported functions and static class methods that return
|
||||
* ModuleWithProviders objects.
|
||||
* @param f The source file to search for these functions
|
||||
* @returns An array of function declarations that look like they return ModuleWithProviders
|
||||
* objects.
|
||||
*/
|
||||
getModuleWithProvidersFunctions(f: ts.SourceFile): ModuleWithProvidersFunction[] {
|
||||
const exports = this.getExportsOfModule(f);
|
||||
if (!exports) return [];
|
||||
const infos: ModuleWithProvidersFunction[] = [];
|
||||
exports.forEach((declaration, name) => {
|
||||
if (declaration.node === null) {
|
||||
return;
|
||||
}
|
||||
if (this.isClass(declaration.node)) {
|
||||
this.getMembersOfClass(declaration.node).forEach(member => {
|
||||
if (member.isStatic) {
|
||||
const info = this.parseForModuleWithProviders(
|
||||
member.name, member.node, member.implementation, declaration.node);
|
||||
if (info) {
|
||||
infos.push(info);
|
||||
}
|
||||
}
|
||||
});
|
||||
} else {
|
||||
if (isNamedDeclaration(declaration.node)) {
|
||||
const info =
|
||||
this.parseForModuleWithProviders(declaration.node.name.text, declaration.node);
|
||||
if (info) {
|
||||
infos.push(info);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
return infos;
|
||||
}
|
||||
|
||||
getEndOfClass(classSymbol: NgccClassSymbol): ts.Node {
|
||||
let last: ts.Node = classSymbol.declaration.valueDeclaration;
|
||||
|
||||
@ -1711,65 +1673,6 @@ export class Esm2015ReflectionHost extends TypeScriptReflectionHost implements N
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a function/method node (or its implementation), to see if it returns a
|
||||
* `ModuleWithProviders` object.
|
||||
* @param name The name of the function.
|
||||
* @param node the node to check - this could be a function, a method or a variable declaration.
|
||||
* @param implementation the actual function expression if `node` is a variable declaration.
|
||||
* @param container the class that contains the function, if it is a method.
|
||||
* @returns info about the function if it does return a `ModuleWithProviders` object; `null`
|
||||
* otherwise.
|
||||
*/
|
||||
protected parseForModuleWithProviders(
|
||||
name: string, node: ts.Node|null, implementation: ts.Node|null = node,
|
||||
container: ts.Declaration|null = null): ModuleWithProvidersFunction|null {
|
||||
if (implementation === null ||
|
||||
(!ts.isFunctionDeclaration(implementation) && !ts.isMethodDeclaration(implementation) &&
|
||||
!ts.isFunctionExpression(implementation))) {
|
||||
return null;
|
||||
}
|
||||
const declaration = implementation;
|
||||
const definition = this.getDefinitionOfFunction(declaration);
|
||||
if (definition === null) {
|
||||
return null;
|
||||
}
|
||||
const body = definition.body;
|
||||
const lastStatement = body && body[body.length - 1];
|
||||
const returnExpression =
|
||||
lastStatement && ts.isReturnStatement(lastStatement) && lastStatement.expression || null;
|
||||
const ngModuleProperty = returnExpression && ts.isObjectLiteralExpression(returnExpression) &&
|
||||
returnExpression.properties.find(
|
||||
prop =>
|
||||
!!prop.name && ts.isIdentifier(prop.name) && prop.name.text === 'ngModule') ||
|
||||
null;
|
||||
|
||||
if (!ngModuleProperty || !ts.isPropertyAssignment(ngModuleProperty)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// The ngModuleValue could be of the form `SomeModule` or `namespace_1.SomeModule`
|
||||
const ngModuleValue = ngModuleProperty.initializer;
|
||||
if (!ts.isIdentifier(ngModuleValue) && !ts.isPropertyAccessExpression(ngModuleValue)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const ngModuleDeclaration = this.getDeclarationOfExpression(ngModuleValue);
|
||||
if (!ngModuleDeclaration || ngModuleDeclaration.node === null) {
|
||||
throw new Error(`Cannot find a declaration for NgModule ${
|
||||
ngModuleValue.getText()} referenced in "${declaration!.getText()}"`);
|
||||
}
|
||||
if (!hasNameIdentifier(ngModuleDeclaration.node)) {
|
||||
return null;
|
||||
}
|
||||
return {
|
||||
name,
|
||||
ngModule: ngModuleDeclaration as ConcreteDeclaration<ClassDeclaration>,
|
||||
declaration,
|
||||
container
|
||||
};
|
||||
}
|
||||
|
||||
protected getDeclarationOfExpression(expression: ts.Expression): Declaration|null {
|
||||
if (ts.isIdentifier(expression)) {
|
||||
return this.getDeclarationOfIdentifier(expression);
|
||||
|
@ -7,7 +7,7 @@
|
||||
*/
|
||||
import * as ts from 'typescript';
|
||||
|
||||
import {ClassDeclaration, ConcreteDeclaration, Declaration, Decorator, ReflectionHost} from '../../../src/ngtsc/reflection';
|
||||
import {ClassDeclaration, Declaration, Decorator, ReflectionHost} from '../../../src/ngtsc/reflection';
|
||||
|
||||
export const PRE_R3_MARKER = '__PRE_R3__';
|
||||
export const POST_R3_MARKER = '__POST_R3__';
|
||||
@ -19,30 +19,6 @@ export function isSwitchableVariableDeclaration(node: ts.Node):
|
||||
ts.isIdentifier(node.initializer) && node.initializer.text.endsWith(PRE_R3_MARKER);
|
||||
}
|
||||
|
||||
/**
|
||||
* A structure returned from `getModuleWithProviderInfo` that describes functions
|
||||
* that return ModuleWithProviders objects.
|
||||
*/
|
||||
export interface ModuleWithProvidersFunction {
|
||||
/**
|
||||
* The name of the declared function.
|
||||
*/
|
||||
name: string;
|
||||
/**
|
||||
* The declaration of the function that returns the `ModuleWithProviders` object.
|
||||
*/
|
||||
declaration: ts.SignatureDeclaration;
|
||||
/**
|
||||
* Declaration of the containing class (if this is a method)
|
||||
*/
|
||||
container: ts.Declaration|null;
|
||||
/**
|
||||
* The declaration of the class that the `ngModule` property on the `ModuleWithProviders` object
|
||||
* refers to.
|
||||
*/
|
||||
ngModule: ConcreteDeclaration<ClassDeclaration>;
|
||||
}
|
||||
|
||||
/**
|
||||
* The symbol corresponding to a "class" declaration. I.e. a `ts.Symbol` whose `valueDeclaration` is
|
||||
* a `ClassDeclaration`.
|
||||
@ -108,15 +84,6 @@ export interface NgccReflectionHost extends ReflectionHost {
|
||||
*/
|
||||
findClassSymbols(sourceFile: ts.SourceFile): NgccClassSymbol[];
|
||||
|
||||
/**
|
||||
* Search the given source file for exported functions and static class methods that return
|
||||
* ModuleWithProviders objects.
|
||||
* @param f The source file to search for these functions
|
||||
* @returns An array of info items about each of the functions that return ModuleWithProviders
|
||||
* objects.
|
||||
*/
|
||||
getModuleWithProvidersFunctions(f: ts.SourceFile): ModuleWithProvidersFunction[];
|
||||
|
||||
/**
|
||||
* Find the last node that is relevant to the specified class.
|
||||
*
|
||||
|
Reference in New Issue
Block a user