fix(ivy): resolve forwardRef when analyzing NgModule (#28942)

Fixes forward refs not being resolved when an NgModule is being analyzed by ngtsc.

This PR resolves FW-1094.

PR Close #28942
This commit is contained in:
Kristiyan Kostadinov 2019-02-27 19:33:18 +01:00 committed by Andrew Kushnir
parent daf8251998
commit efa10e33a9
3 changed files with 111 additions and 7 deletions

View File

@ -20,7 +20,7 @@ import {getSourceFile} from '../../util/src/typescript';
import {generateSetClassMetadataCall} from './metadata';
import {ReferencesRegistry} from './references_registry';
import {getValidConstructorDependencies, isAngularCore, toR3Reference, unwrapExpression} from './util';
import {combineResolvers, forwardRefResolver, getValidConstructorDependencies, isAngularCore, toR3Reference, unwrapExpression} from './util';
export interface NgModuleAnalysis {
ngModuleDef: R3NgModuleMetadata;
@ -83,34 +83,37 @@ export class NgModuleDecoratorHandler implements DecoratorHandler<NgModuleAnalys
return {};
}
const moduleResolvers = combineResolvers([
ref => this._extractModuleFromModuleWithProvidersFn(ref.node),
forwardRefResolver,
]);
// Extract the module declarations, imports, and exports.
let declarations: Reference<ts.Declaration>[] = [];
if (ngModule.has('declarations')) {
const expr = ngModule.get('declarations') !;
const declarationMeta = this.evaluator.evaluate(expr);
const declarationMeta = this.evaluator.evaluate(expr, forwardRefResolver);
declarations = this.resolveTypeList(expr, declarationMeta, 'declarations');
}
let imports: Reference<ts.Declaration>[] = [];
let rawImports: ts.Expression|null = null;
if (ngModule.has('imports')) {
rawImports = ngModule.get('imports') !;
const importsMeta = this.evaluator.evaluate(
rawImports, ref => this._extractModuleFromModuleWithProvidersFn(ref.node));
const importsMeta = this.evaluator.evaluate(rawImports, moduleResolvers);
imports = this.resolveTypeList(rawImports, importsMeta, 'imports');
}
let exports: Reference<ts.Declaration>[] = [];
let rawExports: ts.Expression|null = null;
if (ngModule.has('exports')) {
rawExports = ngModule.get('exports') !;
const exportsMeta = this.evaluator.evaluate(
rawExports, ref => this._extractModuleFromModuleWithProvidersFn(ref.node));
const exportsMeta = this.evaluator.evaluate(rawExports, moduleResolvers);
exports = this.resolveTypeList(rawExports, exportsMeta, 'exports');
this.referencesRegistry.add(node, ...exports);
}
let bootstrap: Reference<ts.Declaration>[] = [];
if (ngModule.has('bootstrap')) {
const expr = ngModule.get('bootstrap') !;
const bootstrapMeta = this.evaluator.evaluate(expr);
const bootstrapMeta = this.evaluator.evaluate(expr, forwardRefResolver);
bootstrap = this.resolveTypeList(expr, bootstrapMeta, 'bootstrap');
}

View File

@ -11,6 +11,7 @@ import * as ts from 'typescript';
import {ErrorCode, FatalDiagnosticError} from '../../diagnostics';
import {ImportMode, Reference, ReferenceEmitter} from '../../imports';
import {ForeignFunctionResolver} from '../../partial_evaluator';
import {ClassMemberKind, CtorParameter, Decorator, ReflectionHost} from '../../reflection';
export enum ConstructorDepErrorKind {
@ -213,3 +214,21 @@ export function forwardRefResolver(
}
return expandForwardRef(args[0]);
}
/**
* Combines an array of resolver functions into a one.
* @param resolvers Resolvers to be combined.
*/
export function combineResolvers(resolvers: ForeignFunctionResolver[]): ForeignFunctionResolver {
return (ref: Reference<ts.FunctionDeclaration|ts.MethodDeclaration>,
args: ts.Expression[]): ts.Expression |
null => {
for (const resolver of resolvers) {
const resolved = resolver(ref, args);
if (resolved !== null) {
return resolved;
}
}
return null;
};
}

View File

@ -0,0 +1,82 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {WrappedNodeExpr} from '@angular/compiler';
import {R3Reference} from '@angular/compiler/src/compiler';
import * as ts from 'typescript';
import {LocalIdentifierStrategy, ReferenceEmitter} from '../../imports';
import {PartialEvaluator} from '../../partial_evaluator';
import {TypeScriptReflectionHost} from '../../reflection';
import {LocalModuleScopeRegistry, MetadataDtsModuleScopeResolver} from '../../scope';
import {getDeclaration, makeProgram} from '../../testing/in_memory_typescript';
import {NgModuleDecoratorHandler} from '../src/ng_module';
import {NoopReferencesRegistry} from '../src/references_registry';
describe('NgModuleDecoratorHandler', () => {
it('should resolve forwardRef', () => {
const {program} = makeProgram([
{
name: 'node_modules/@angular/core/index.d.ts',
contents: `
export const Component: any;
export const NgModule: any;
export declare function forwardRef(fn: () => any): any;
`,
},
{
name: 'entry.ts',
contents: `
import {Component, forwardRef, NgModule} from '@angular/core';
@Component({
template: '',
})
export class TestComp {}
@NgModule()
export class TestModuleDependency {}
@NgModule({
declarations: [forwardRef(() => TestComp)],
exports: [forwardRef(() => TestComp)],
imports: [forwardRef(() => TestModuleDependency)]
})
export class TestModule {}
`
},
]);
const checker = program.getTypeChecker();
const reflectionHost = new TypeScriptReflectionHost(checker);
const evaluator = new PartialEvaluator(reflectionHost, checker);
const referencesRegistry = new NoopReferencesRegistry();
const scopeRegistry = new LocalModuleScopeRegistry(
new MetadataDtsModuleScopeResolver(checker, reflectionHost, null), new ReferenceEmitter([]),
null);
const refEmitter = new ReferenceEmitter([new LocalIdentifierStrategy()]);
const handler = new NgModuleDecoratorHandler(
reflectionHost, evaluator, scopeRegistry, referencesRegistry, false, null, refEmitter);
const TestModule = getDeclaration(program, 'entry.ts', 'TestModule', ts.isClassDeclaration);
const detected =
handler.detect(TestModule, reflectionHost.getDecoratorsOfDeclaration(TestModule));
if (detected === undefined) {
return fail('Failed to recognize @NgModule');
}
const moduleDef = handler.analyze(TestModule, detected.metadata).analysis !.ngModuleDef;
expect(getReferenceIdentifierTexts(moduleDef.declarations)).toEqual(['TestComp']);
expect(getReferenceIdentifierTexts(moduleDef.exports)).toEqual(['TestComp']);
expect(getReferenceIdentifierTexts(moduleDef.imports)).toEqual(['TestModuleDependency']);
function getReferenceIdentifierTexts(references: R3Reference[]) {
return references.map(ref => (ref.value as WrappedNodeExpr<ts.Identifier>).node.text);
}
});
});