fix(ngcc): Esm5ReflectionHost.getDeclarationOfIdentifier should handle aliased inner declarations (#33252)

In ES5 modules, the class declarations consist of an IIFE with inner
and outer declarations that represent the class. The `EsmReflectionHost`
has logic to ensure that `getDeclarationOfIdentifier()` always returns the
outer declaration.

Before this commit, if an identifier referred to an alias of the inner
declaration, then `getDeclarationOfIdentifier()` was failing to find
the outer declaration - instead returning the inner declaration.

Now the identifier is correctly resolved up to the outer declaration
as expected.

This should fix some of the failing 3rd party packages discussed in
https://github.com/angular/ngcc-validation/issues/57.

PR Close #33252
This commit is contained in:
Pete Bacon Darwin
2019-10-18 14:45:52 +01:00
committed by Matias Niemelä
parent 01d3599f32
commit bfd07b3c94
2 changed files with 76 additions and 15 deletions

View File

@ -1817,8 +1817,58 @@ runInEachFileSystem(() => {
superGetDeclarationOfIdentifierSpy.calls.reset();
expect(host.getDeclarationOfIdentifier(innerIdentifier) !.node).toBe(outerDeclaration);
expect(superGetDeclarationOfIdentifierSpy).toHaveBeenCalledWith(innerIdentifier);
expect(superGetDeclarationOfIdentifierSpy).toHaveBeenCalledWith(outerIdentifier);
expect(superGetDeclarationOfIdentifierSpy).toHaveBeenCalledTimes(1);
expect(superGetDeclarationOfIdentifierSpy).toHaveBeenCalledTimes(2);
});
it('should return the correct outer declaration for an aliased inner class declaration inside an ES5 IIFE',
() => {
// Note that the inner class declaration `function FroalaEditorModule() {}` is aliased
// internally to `FroalaEditorModule_1`, which is used in the object returned from
// `forRoot()`.
const PROGRAM_FILE: TestFile = {
name: _('/test.js'),
contents: `
var FroalaEditorModule = /** @class */ (function () {
function FroalaEditorModule() {
}
FroalaEditorModule_1 = FroalaEditorModule;
FroalaEditorModule.forRoot = function () {
return { ngModule: FroalaEditorModule_1, providers: [] };
};
var FroalaEditorModule_1;
FroalaEditorModule = FroalaEditorModule_1 = __decorate([
NgModule({
declarations: [FroalaEditorDirective],
exports: [FroalaEditorDirective]
})
], FroalaEditorModule);
return FroalaEditorModule;
}());
export { FroalaEditorModule };
`
};
loadTestFiles([PROGRAM_FILE]);
const {program} = makeTestBundleProgram(PROGRAM_FILE.name);
const host = new Esm5ReflectionHost(new MockLogger(), false, program.getTypeChecker());
const expectedDeclaration = getDeclaration(
program, PROGRAM_FILE.name, 'FroalaEditorModule', isNamedVariableDeclaration);
// Grab the `FroalaEditorModule_1` identifier returned from the `forRoot()` method
const forRootMethod = ((((expectedDeclaration.initializer as ts.ParenthesizedExpression)
.expression as ts.CallExpression)
.expression as ts.FunctionExpression)
.body.statements[2] as ts.ExpressionStatement);
const identifier =
(((((forRootMethod.expression as ts.BinaryExpression).right as ts.FunctionExpression)
.body.statements[0] as ts.ReturnStatement)
.expression as ts.ObjectLiteralExpression)
.properties[0] as ts.PropertyAssignment)
.initializer as ts.Identifier;
const actualDeclaration = host.getDeclarationOfIdentifier(identifier) !;
expect(actualDeclaration.node !.getText()).toBe(expectedDeclaration.getText());
});
});