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:

committed by
Andrew Kushnir

parent
daf8251998
commit
efa10e33a9
@ -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);
|
||||
}
|
||||
});
|
||||
|
||||
});
|
Reference in New Issue
Block a user