feat(ivy): implement NgccReflectionHost.getSwitchableDeclarations() (#25534)

This method will be used to find all the places where the "ivy switch"
will occur. See #25238

PR Close #25534
This commit is contained in:
Pete Bacon Darwin
2018-08-17 07:50:55 +01:00
committed by Misko Hevery
parent a469c2c412
commit 6f168b7a0f
5 changed files with 119 additions and 2 deletions

View File

@ -32,6 +32,24 @@ const CLASSES = [
},
];
const MARKER_FILE = {
name: '/marker.js',
contents: `
let compileNgModuleFactory = compileNgModuleFactory__PRE_NGCC__;
function compileNgModuleFactory__PRE_NGCC__(injector, options, moduleType) {
const compilerFactory = injector.get(CompilerFactory);
const compiler = compilerFactory.createCompiler([options]);
return compiler.compileModuleAsync(moduleType);
}
function compileNgModuleFactory__POST_NGCC__(injector, options, moduleType) {
ngDevMode && assertNgModuleType(moduleType);
return Promise.resolve(new R3NgModuleFactory(moduleType));
}
`
};
describe('Esm2015ReflectionHost', () => {
describe('getGenericArityOfClass()', () => {
it('should properly count type parameters', () => {
@ -52,4 +70,18 @@ describe('Esm2015ReflectionHost', () => {
expect(host.getGenericArityOfClass(twoTypeParamsClass)).toBe(2);
});
});
describe('getSwitchableDeclarations()', () => {
it('should return a collection of all the switchable variable declarations in the given module',
() => {
const program = makeProgram(MARKER_FILE);
const dtsMapper = new DtsMapper('/src', '/typings');
const host = new Esm2015ReflectionHost(program.getTypeChecker(), dtsMapper);
const file = program.getSourceFile(MARKER_FILE.name) !;
const declarations = host.getSwitchableDeclarations(file);
expect(declarations.map(d => [d.name.getText(), d.initializer !.getText()])).toEqual([
['compileNgModuleFactory', 'compileNgModuleFactory__PRE_NGCC__']
]);
});
});
});

View File

@ -385,6 +385,24 @@ const FUNCTION_BODY_FILE = {
`
};
const MARKER_FILE = {
name: '/marker.js',
contents: `
var compileNgModuleFactory = compileNgModuleFactory__PRE_NGCC__;
function compileNgModuleFactory__PRE_NGCC__(injector, options, moduleType) {
var compilerFactory = injector.get(CompilerFactory);
var compiler = compilerFactory.createCompiler([options]);
return compiler.compileModuleAsync(moduleType);
}
function compileNgModuleFactory__POST_NGCC__(injector, options, moduleType) {
ngDevMode && assertNgModuleType(moduleType);
return Promise.resolve(new R3NgModuleFactory(moduleType));
}
`
};
describe('Fesm2015ReflectionHost', () => {
describe('getDecoratorsOfDeclaration()', () => {
@ -1120,4 +1138,17 @@ describe('Fesm2015ReflectionHost', () => {
expect(host.getGenericArityOfClass(node)).toBe(0);
});
});
describe('getSwitchableDeclarations()', () => {
it('should return a collection of all the switchable variable declarations in the given module',
() => {
const program = makeProgram(MARKER_FILE);
const host = new Fesm2015ReflectionHost(program.getTypeChecker());
const file = program.getSourceFile(MARKER_FILE.name) !;
const declarations = host.getSwitchableDeclarations(file);
expect(declarations.map(d => [d.name.getText(), d.initializer !.getText()])).toEqual([
['compileNgModuleFactory', 'compileNgModuleFactory__PRE_NGCC__']
]);
});
});
});