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:
Pete Bacon Darwin
2020-05-05 10:19:28 +01:00
committed by Alex Rickabaugh
parent c9e0db55f7
commit e010f2ca54
8 changed files with 124 additions and 868 deletions

View File

@ -46,7 +46,6 @@ runInEachFileSystem(() => {
let ARITY_CLASSES: TestFile[];
let TYPINGS_SRC_FILES: TestFile[];
let TYPINGS_DTS_FILES: TestFile[];
let MODULE_WITH_PROVIDERS_PROGRAM: TestFile[];
let NAMESPACED_IMPORT_FILE: TestFile;
let INDEX_SIGNATURE_PROP_FILE: TestFile;
@ -650,77 +649,6 @@ runInEachFileSystem(() => {
{name: _('/an_external_lib/index.d.ts'), contents: 'export declare class ShadowClass {}'},
];
MODULE_WITH_PROVIDERS_PROGRAM = [
{
name: _('/src/index.js'),
contents: `
import * as functions from './functions';
import * as methods from './methods';
import * as aliased_class from './aliased_class';
`
},
{
name: _('/src/functions.js'),
contents: `
import {ExternalModule} from './module';
import * as mod from './module';
export class SomeService {}
export class InternalModule {}
export function aNumber() { return 42; }
export function aString() { return 'foo'; }
export function emptyObject() { return {}; }
export function ngModuleIdentifier() { return { ngModule: InternalModule }; }
export function ngModuleWithEmptyProviders() { return { ngModule: InternalModule, providers: [] }; }
export function ngModuleWithProviders() { return { ngModule: InternalModule, providers: [SomeService] }; }
export function onlyProviders() { return { providers: [SomeService] }; }
export function ngModuleNumber() { return { ngModule: 42 }; }
export function ngModuleString() { return { ngModule: 'foo' }; }
export function ngModuleObject() { return { ngModule: { foo: 42 } }; }
export function externalNgModule() { return { ngModule: ExternalModule }; }
export function namespacedExternalNgModule() { return { ngModule: mod.ExternalModule }; }
`
},
{
name: _('/src/methods.js'),
contents: `
import {ExternalModule} from './module';
import * as mod from './module';
export class SomeService {}
export class InternalModule {
static aNumber() { return 42; }
static aString() { return 'foo'; }
static emptyObject() { return {}; }
static ngModuleIdentifier() { return { ngModule: InternalModule }; }
static ngModuleWithEmptyProviders() { return { ngModule: InternalModule, providers: [] }; }
static ngModuleWithProviders() { return { ngModule: InternalModule, providers: [SomeService] }; }
static onlyProviders() { return { providers: [SomeService] }; }
static ngModuleNumber() { return { ngModule: 42 }; }
static ngModuleString() { return { ngModule: 'foo' }; }
static ngModuleObject() { return { ngModule: { foo: 42 } }; }
static externalNgModule() { return { ngModule: ExternalModule }; }
static namespacedExternalNgModule() { return { ngModule: mod.ExternalModule }; }
instanceNgModuleIdentifier() { return { ngModule: InternalModule }; }
instanceNgModuleWithEmptyProviders() { return { ngModule: InternalModule, providers: [] }; }
instanceNgModuleWithProviders() { return { ngModule: InternalModule, providers: [SomeService] }; }
instanceExternalNgModule() { return { ngModule: ExternalModule }; }
instanceNamespacedExternalNgModule() { return { ngModule: mod.ExternalModule }; }
}
`
},
{
name: _('/src/aliased_class.js'),
contents: `
var AliasedModule_1;
let AliasedModule = AliasedModule_1 = class AliasedModule {
static forRoot() { return { ngModule: AliasedModule_1 }; }
};
export { AliasedModule };
`
},
{name: _('/src/module.js'), contents: 'export class ExternalModule {}'},
];
NAMESPACED_IMPORT_FILE = {
name: _('/some_directive.js'),
contents: `
@ -2432,56 +2360,6 @@ runInEachFileSystem(() => {
});
});
describe('getModuleWithProvidersFunctions()', () => {
it('should find every exported function that returns an object that looks like a ModuleWithProviders object',
() => {
loadTestFiles(MODULE_WITH_PROVIDERS_PROGRAM);
const bundle = makeTestBundleProgram(getRootFiles(MODULE_WITH_PROVIDERS_PROGRAM)[0]);
const host =
createHost(bundle, new Esm2015ReflectionHost(new MockLogger(), false, bundle));
const file = getSourceFileOrError(bundle.program, _('/src/functions.js'));
const fns = host.getModuleWithProvidersFunctions(file);
expect(fns.map(fn => [fn.declaration.name!.getText(), fn.ngModule.node.name.text]))
.toEqual([
['ngModuleIdentifier', 'InternalModule'],
['ngModuleWithEmptyProviders', 'InternalModule'],
['ngModuleWithProviders', 'InternalModule'],
['externalNgModule', 'ExternalModule'],
['namespacedExternalNgModule', 'ExternalModule'],
]);
});
it('should find every static method on exported classes that return an object that looks like a ModuleWithProviders object',
() => {
loadTestFiles(MODULE_WITH_PROVIDERS_PROGRAM);
const bundle = makeTestBundleProgram(getRootFiles(MODULE_WITH_PROVIDERS_PROGRAM)[0]);
const host =
createHost(bundle, new Esm2015ReflectionHost(new MockLogger(), false, bundle));
const file = getSourceFileOrError(bundle.program, _('/src/methods.js'));
const fn = host.getModuleWithProvidersFunctions(file);
expect(fn.map(fn => [fn.declaration.name!.getText(), fn.ngModule.node.name.text]))
.toEqual([
['ngModuleIdentifier', 'InternalModule'],
['ngModuleWithEmptyProviders', 'InternalModule'],
['ngModuleWithProviders', 'InternalModule'],
['externalNgModule', 'ExternalModule'],
['namespacedExternalNgModule', 'ExternalModule'],
]);
});
// https://github.com/angular/angular/issues/29078
it('should resolve aliased module references to their original declaration', () => {
loadTestFiles(MODULE_WITH_PROVIDERS_PROGRAM);
const bundle = makeTestBundleProgram(getRootFiles(MODULE_WITH_PROVIDERS_PROGRAM)[0]);
const host = createHost(bundle, new Esm2015ReflectionHost(new MockLogger(), false, bundle));
const file = getSourceFileOrError(bundle.program, _('/src/aliased_class.js'));
const fn = host.getModuleWithProvidersFunctions(file);
expect(fn.map(fn => [fn.declaration.name!.getText(), fn.ngModule.node.name.text])).toEqual([
['forRoot', 'AliasedModule'],
]);
});
});
describe('getEndOfClass()', () => {
it('should return the last static property of the class', () => {
const testFile: TestFile = {