Skip to content

Commit

Permalink
refactor(ngcc): use bundle src to create reflection hosts (#34254)
Browse files Browse the repository at this point in the history
Previously individual properties of the src bundle program were
passed to the reflection host constructors. But going forward,
more properties will be required. To prevent the signature getting
continually larger and more unwieldy, this change just passes the
whole src bundle to the constructor, allowing it to extract what it
needs.

PR Close #34254
  • Loading branch information
petebacondarwin authored and kara committed Dec 18, 2019
1 parent dfecca2 commit 0b837e2
Show file tree
Hide file tree
Showing 24 changed files with 1,428 additions and 1,459 deletions.
10 changes: 6 additions & 4 deletions packages/compiler-cli/ngcc/src/host/commonjs_host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ import {NgccClassSymbol} from './ngcc_host';
export class CommonJsReflectionHost extends Esm5ReflectionHost {
protected commonJsExports = new Map<ts.SourceFile, Map<string, Declaration>|null>();
protected topLevelHelperCalls = new Map<string, Map<ts.SourceFile, ts.CallExpression[]>>();
constructor(
logger: Logger, isCore: boolean, protected program: ts.Program,
protected compilerHost: ts.CompilerHost, dts?: BundleProgram|null) {
super(logger, isCore, program.getTypeChecker(), dts);
protected program: ts.Program;
protected compilerHost: ts.CompilerHost;
constructor(logger: Logger, isCore: boolean, src: BundleProgram, dts?: BundleProgram|null) {
super(logger, isCore, src, dts);
this.program = src.program;
this.compilerHost = src.host;
}

getImportOfIdentifier(id: ts.Identifier): Import|null {
Expand Down
4 changes: 2 additions & 2 deletions packages/compiler-cli/ngcc/src/host/esm2015_host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ export class Esm2015ReflectionHost extends TypeScriptReflectionHost implements N
protected decoratorCache = new Map<ClassDeclaration, DecoratorInfo>();

constructor(
protected logger: Logger, protected isCore: boolean, checker: ts.TypeChecker,
protected logger: Logger, protected isCore: boolean, src: BundleProgram,
dts?: BundleProgram|null) {
super(checker);
super(src.program.getTypeChecker());
this.dtsDeclarationMap =
dts && this.computeDtsDeclarationMap(dts.path, dts.program, dts.package) || null;
}
Expand Down
10 changes: 6 additions & 4 deletions packages/compiler-cli/ngcc/src/host/umd_host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ export class UmdReflectionHost extends Esm5ReflectionHost {
protected umdModules = new Map<ts.SourceFile, UmdModule|null>();
protected umdExports = new Map<ts.SourceFile, Map<string, Declaration>|null>();
protected umdImportPaths = new Map<ts.ParameterDeclaration, string|null>();
constructor(
logger: Logger, isCore: boolean, protected program: ts.Program,
protected compilerHost: ts.CompilerHost, dts?: BundleProgram|null) {
super(logger, isCore, program.getTypeChecker(), dts);
protected program: ts.Program;
protected compilerHost: ts.CompilerHost;
constructor(logger: Logger, isCore: boolean, src: BundleProgram, dts?: BundleProgram|null) {
super(logger, isCore, src, dts);
this.program = src.program;
this.compilerHost = src.host;
}

getImportOfIdentifier(id: ts.Identifier): Import|null {
Expand Down
11 changes: 4 additions & 7 deletions packages/compiler-cli/ngcc/src/packages/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,15 @@ export class Transformer {
}

getHost(bundle: EntryPointBundle): NgccReflectionHost {
const typeChecker = bundle.src.program.getTypeChecker();
switch (bundle.format) {
case 'esm2015':
return new Esm2015ReflectionHost(this.logger, bundle.isCore, typeChecker, bundle.dts);
return new Esm2015ReflectionHost(this.logger, bundle.isCore, bundle.src, bundle.dts);
case 'esm5':
return new Esm5ReflectionHost(this.logger, bundle.isCore, typeChecker, bundle.dts);
return new Esm5ReflectionHost(this.logger, bundle.isCore, bundle.src, bundle.dts);
case 'umd':
return new UmdReflectionHost(
this.logger, bundle.isCore, bundle.src.program, bundle.src.host, bundle.dts);
return new UmdReflectionHost(this.logger, bundle.isCore, bundle.src, bundle.dts);
case 'commonjs':
return new CommonJsReflectionHost(
this.logger, bundle.isCore, bundle.src.program, bundle.src.host, bundle.dts);
return new CommonJsReflectionHost(this.logger, bundle.isCore, bundle.src, bundle.dts);
default:
throw new Error(`Reflection host for "${bundle.format}" not yet implemented.`);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ runInEachFileSystem(() => {
const bundle = makeTestEntryPointBundle('test-package', 'esm2015', false, rootFiles);
program = bundle.src.program;

const reflectionHost =
new Esm2015ReflectionHost(new MockLogger(), false, program.getTypeChecker());
const reflectionHost = new Esm2015ReflectionHost(new MockLogger(), false, bundle.src);
const referencesRegistry = new NgccReferencesRegistry(reflectionHost);
diagnosticLogs = [];
const analyzer = new DecorationAnalyzer(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,7 @@ runInEachFileSystem(() => {
getRootFiles(TEST_DTS_PROGRAM));
program = bundle.src.program;
dtsProgram = bundle.dts !;
const host = new Esm2015ReflectionHost(
new MockLogger(), false, program.getTypeChecker(), dtsProgram);
const host = new Esm2015ReflectionHost(new MockLogger(), false, bundle.src, dtsProgram);
referencesRegistry = new NgccReferencesRegistry(host);

const processDts = true;
Expand Down Expand Up @@ -538,8 +537,7 @@ runInEachFileSystem(() => {
getRootFiles(TEST_DTS_PROGRAM));
const program = bundle.src.program;
const dtsProgram = bundle.dts !;
const host =
new Esm2015ReflectionHost(new MockLogger(), false, program.getTypeChecker(), dtsProgram);
const host = new Esm2015ReflectionHost(new MockLogger(), false, bundle.src, dtsProgram);
const referencesRegistry = new NgccReferencesRegistry(host);

const processDts = true;
Expand Down Expand Up @@ -569,8 +567,7 @@ runInEachFileSystem(() => {
const bundle =
makeTestEntryPointBundle('test-package', 'esm2015', false, getRootFiles(TEST_PROGRAM));
const program = bundle.src.program;
const host =
new Esm2015ReflectionHost(new MockLogger(), false, program.getTypeChecker(), null);
const host = new Esm2015ReflectionHost(new MockLogger(), false, bundle.src, null);
const referencesRegistry = new NgccReferencesRegistry(host);

const processDts = false; // Emulate the scenario where typings have already been processed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,12 @@ runInEachFileSystem(() => {
function setup(jsProgram: TestFile[], dtsProgram: TestFile[]) {
loadTestFiles(jsProgram);
loadTestFiles(dtsProgram);
const {src: {program}, dts} = makeTestEntryPointBundle(
const {src, dts} = makeTestEntryPointBundle(
'test-package', 'esm2015', false, getRootFiles(jsProgram), getRootFiles(dtsProgram));
const host = new Esm2015ReflectionHost(new MockLogger(), false, program.getTypeChecker(), dts);
const host = new Esm2015ReflectionHost(new MockLogger(), false, src, dts);
const referencesRegistry = new NgccReferencesRegistry(host);
const analyzer = new PrivateDeclarationsAnalyzer(host, referencesRegistry);
return {program, referencesRegistry, analyzer};
return {program: src.program, referencesRegistry, analyzer};
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ runInEachFileSystem(() => {
const bundle = makeTestEntryPointBundle(
'test', 'esm2015', false, [_('/node_modules/test/entrypoint.js')]);
const program = bundle.src.program;
const host = new Esm2015ReflectionHost(new MockLogger(), false, program.getTypeChecker());
const host = new Esm2015ReflectionHost(new MockLogger(), false, bundle.src);
const analyzer = new SwitchMarkerAnalyzer(host, bundle.entryPoint.package);
const analysis = analyzer.analyzeProgram(program);

Expand Down Expand Up @@ -105,7 +105,7 @@ runInEachFileSystem(() => {
const bundle = makeTestEntryPointBundle(
'test', 'esm2015', false, [_('/node_modules/test/entrypoint.js')]);
const program = bundle.src.program;
const host = new Esm2015ReflectionHost(new MockLogger(), false, program.getTypeChecker());
const host = new Esm2015ReflectionHost(new MockLogger(), false, bundle.src);
const analyzer = new SwitchMarkerAnalyzer(host, bundle.entryPoint.package);
const analysis = analyzer.analyzeProgram(program);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,11 @@ exports.AliasedDirective$1 = AliasedDirective$1;
it('should find the decorators on a class at the top level', () => {
loadFakeCore(getFileSystem());
loadTestFiles([TOPLEVEL_DECORATORS_FILE]);
const {program, host: compilerHost} = makeTestBundleProgram(TOPLEVEL_DECORATORS_FILE.name);
const host = new CommonJsReflectionHost(new MockLogger(), false, program, compilerHost);
const bundle = makeTestBundleProgram(TOPLEVEL_DECORATORS_FILE.name);
const host = new CommonJsReflectionHost(new MockLogger(), false, bundle);
const classNode = getDeclaration(
program, TOPLEVEL_DECORATORS_FILE.name, 'SomeDirective', isNamedVariableDeclaration);
bundle.program, TOPLEVEL_DECORATORS_FILE.name, 'SomeDirective',
isNamedVariableDeclaration);
const decorators = host.getDecoratorsOfDeclaration(classNode) !;

expect(decorators.length).toEqual(1);
Expand All @@ -105,10 +106,10 @@ exports.AliasedDirective$1 = AliasedDirective$1;
it('should find the decorators on an aliased class at the top level', () => {
loadFakeCore(getFileSystem());
loadTestFiles([TOPLEVEL_DECORATORS_FILE]);
const {program, host: compilerHost} = makeTestBundleProgram(TOPLEVEL_DECORATORS_FILE.name);
const host = new CommonJsReflectionHost(new MockLogger(), false, program, compilerHost);
const bundle = makeTestBundleProgram(TOPLEVEL_DECORATORS_FILE.name);
const host = new CommonJsReflectionHost(new MockLogger(), false, bundle);
const classNode = getDeclaration(
program, TOPLEVEL_DECORATORS_FILE.name, 'AliasedDirective$1',
bundle.program, TOPLEVEL_DECORATORS_FILE.name, 'AliasedDirective$1',
isNamedVariableDeclaration);
const decorators = host.getDecoratorsOfDeclaration(classNode) !;

Expand Down
Loading

0 comments on commit 0b837e2

Please sign in to comment.