From 74f637f98d25cd5e560ed9fb8688001042aaf854 Mon Sep 17 00:00:00 2001 From: Pete Bacon Darwin Date: Sat, 25 May 2019 20:38:33 +0100 Subject: [PATCH] refactor(ivy): ngcc - no need to pass `isCore` explicitly (#30591) It is part of `EntryPointBundle` so we can just use that, which is generally already passed around. PR Close #30591 --- .../ngcc/src/packages/transformer.ts | 40 +++++++++---------- .../ngcc/src/rendering/dts_renderer.ts | 6 +-- .../ngcc/src/rendering/renderer.ts | 7 ++-- .../ngcc/test/rendering/dts_renderer_spec.ts | 2 +- .../ngcc/test/rendering/renderer_spec.ts | 2 +- 5 files changed, 26 insertions(+), 31 deletions(-) diff --git a/packages/compiler-cli/ngcc/src/packages/transformer.ts b/packages/compiler-cli/ngcc/src/packages/transformer.ts index 823600fb41..0d1176c48e 100644 --- a/packages/compiler-cli/ngcc/src/packages/transformer.ts +++ b/packages/compiler-cli/ngcc/src/packages/transformer.ts @@ -58,25 +58,23 @@ export class Transformer { * @returns information about the files that were transformed. */ transform(bundle: EntryPointBundle): FileToWrite[] { - const isCore = bundle.isCore; - const reflectionHost = this.getHost(isCore, bundle); + const reflectionHost = this.getHost(bundle); // Parse and analyze the files. const {decorationAnalyses, switchMarkerAnalyses, privateDeclarationsAnalyses, - moduleWithProvidersAnalyses} = this.analyzeProgram(reflectionHost, isCore, bundle); + moduleWithProvidersAnalyses} = this.analyzeProgram(reflectionHost, bundle); // Transform the source files and source maps. - const srcFormatter = this.getRenderingFormatter(reflectionHost, isCore, bundle); + const srcFormatter = this.getRenderingFormatter(reflectionHost, bundle); - const renderer = - new Renderer(srcFormatter, this.fs, this.logger, reflectionHost, isCore, bundle); + const renderer = new Renderer(srcFormatter, this.fs, this.logger, bundle); let renderedFiles = renderer.renderProgram( decorationAnalyses, switchMarkerAnalyses, privateDeclarationsAnalyses); if (bundle.dts) { - const dtsFormatter = new EsmRenderingFormatter(reflectionHost, isCore); + const dtsFormatter = new EsmRenderingFormatter(reflectionHost, bundle.isCore); const dtsRenderer = - new DtsRenderer(dtsFormatter, this.fs, this.logger, reflectionHost, isCore, bundle); + new DtsRenderer(dtsFormatter, this.fs, this.logger, reflectionHost, bundle); const renderedDtsFiles = dtsRenderer.renderProgram( decorationAnalyses, privateDeclarationsAnalyses, moduleWithProvidersAnalyses); renderedFiles = renderedFiles.concat(renderedDtsFiles); @@ -85,45 +83,43 @@ export class Transformer { return renderedFiles; } - getHost(isCore: boolean, bundle: EntryPointBundle): NgccReflectionHost { + getHost(bundle: EntryPointBundle): NgccReflectionHost { const typeChecker = bundle.src.program.getTypeChecker(); switch (bundle.format) { case 'esm2015': - return new Esm2015ReflectionHost(this.logger, isCore, typeChecker, bundle.dts); + return new Esm2015ReflectionHost(this.logger, bundle.isCore, typeChecker, bundle.dts); case 'esm5': - return new Esm5ReflectionHost(this.logger, isCore, typeChecker, bundle.dts); + return new Esm5ReflectionHost(this.logger, bundle.isCore, typeChecker, bundle.dts); case 'umd': return new UmdReflectionHost( - this.logger, isCore, bundle.src.program, bundle.src.host, bundle.dts); + this.logger, bundle.isCore, bundle.src.program, bundle.src.host, bundle.dts); case 'commonjs': return new CommonJsReflectionHost( - this.logger, isCore, bundle.src.program, bundle.src.host, bundle.dts); + this.logger, bundle.isCore, bundle.src.program, bundle.src.host, bundle.dts); default: throw new Error(`Reflection host for "${bundle.format}" not yet implemented.`); } } - getRenderingFormatter(host: NgccReflectionHost, isCore: boolean, bundle: EntryPointBundle): - RenderingFormatter { + getRenderingFormatter(host: NgccReflectionHost, bundle: EntryPointBundle): RenderingFormatter { switch (bundle.format) { case 'esm2015': - return new EsmRenderingFormatter(host, isCore); + return new EsmRenderingFormatter(host, bundle.isCore); case 'esm5': - return new Esm5RenderingFormatter(host, isCore); + return new Esm5RenderingFormatter(host, bundle.isCore); case 'umd': if (!(host instanceof UmdReflectionHost)) { throw new Error('UmdRenderer requires a UmdReflectionHost'); } - return new UmdRenderingFormatter(host, isCore); + return new UmdRenderingFormatter(host, bundle.isCore); case 'commonjs': - return new CommonJsRenderingFormatter(host, isCore); + return new CommonJsRenderingFormatter(host, bundle.isCore); default: throw new Error(`Renderer for "${bundle.format}" not yet implemented.`); } } - analyzeProgram(reflectionHost: NgccReflectionHost, isCore: boolean, bundle: EntryPointBundle): - ProgramAnalyses { + analyzeProgram(reflectionHost: NgccReflectionHost, bundle: EntryPointBundle): ProgramAnalyses { const typeChecker = bundle.src.program.getTypeChecker(); const referencesRegistry = new NgccReferencesRegistry(reflectionHost); @@ -132,7 +128,7 @@ export class Transformer { const decorationAnalyzer = new DecorationAnalyzer( this.fs, bundle.src.program, bundle.src.options, bundle.src.host, typeChecker, - reflectionHost, referencesRegistry, bundle.rootDirs, isCore); + reflectionHost, referencesRegistry, bundle.rootDirs, bundle.isCore); const decorationAnalyses = decorationAnalyzer.analyzeProgram(); const moduleWithProvidersAnalyzer = diff --git a/packages/compiler-cli/ngcc/src/rendering/dts_renderer.ts b/packages/compiler-cli/ngcc/src/rendering/dts_renderer.ts index dba0baac80..6059fd8bfc 100644 --- a/packages/compiler-cli/ngcc/src/rendering/dts_renderer.ts +++ b/packages/compiler-cli/ngcc/src/rendering/dts_renderer.ts @@ -53,8 +53,7 @@ export interface DtsClassInfo { export class DtsRenderer { constructor( private dtsFormatter: RenderingFormatter, private fs: FileSystem, private logger: Logger, - private host: NgccReflectionHost, private isCore: boolean, private bundle: EntryPointBundle) { - } + private host: NgccReflectionHost, private bundle: EntryPointBundle) {} renderProgram( decorationAnalyses: DecorationAnalyses, @@ -84,7 +83,8 @@ export class DtsRenderer { const outputText = new MagicString(input.source); const printer = ts.createPrinter(); const importManager = new ImportManager( - getImportRewriter(this.bundle.dts !.r3SymbolsFile, this.isCore, false), IMPORT_PREFIX); + getImportRewriter(this.bundle.dts !.r3SymbolsFile, this.bundle.isCore, false), + IMPORT_PREFIX); renderInfo.classInfo.forEach(dtsClass => { const endOfClass = dtsClass.dtsDeclaration.getEnd(); diff --git a/packages/compiler-cli/ngcc/src/rendering/renderer.ts b/packages/compiler-cli/ngcc/src/rendering/renderer.ts index 0f4b7d8a91..1e3588f55e 100644 --- a/packages/compiler-cli/ngcc/src/rendering/renderer.ts +++ b/packages/compiler-cli/ngcc/src/rendering/renderer.ts @@ -15,7 +15,6 @@ import {PrivateDeclarationsAnalyses} from '../analysis/private_declarations_anal import {SwitchMarkerAnalyses, SwitchMarkerAnalysis} from '../analysis/switch_marker_analyzer'; import {IMPORT_PREFIX} from '../constants'; import {FileSystem} from '../../../src/ngtsc/file_system'; -import {NgccReflectionHost} from '../host/ngcc_host'; import {EntryPointBundle} from '../packages/entry_point_bundle'; import {Logger} from '../logging/logger'; import {FileToWrite, getImportRewriter, stripExtension} from './utils'; @@ -31,8 +30,7 @@ import {extractSourceMap, renderSourceAndMap} from './source_maps'; export class Renderer { constructor( private srcFormatter: RenderingFormatter, private fs: FileSystem, private logger: Logger, - private host: NgccReflectionHost, private isCore: boolean, private bundle: EntryPointBundle) { - } + private bundle: EntryPointBundle) {} renderProgram( decorationAnalyses: DecorationAnalyses, switchMarkerAnalyses: SwitchMarkerAnalyses, @@ -72,7 +70,8 @@ export class Renderer { } const importManager = new ImportManager( - getImportRewriter(this.bundle.src.r3SymbolsFile, this.isCore, this.bundle.isFlatCore), + getImportRewriter( + this.bundle.src.r3SymbolsFile, this.bundle.isCore, this.bundle.isFlatCore), IMPORT_PREFIX); if (compiledFile) { diff --git a/packages/compiler-cli/ngcc/test/rendering/dts_renderer_spec.ts b/packages/compiler-cli/ngcc/test/rendering/dts_renderer_spec.ts index 235972906e..d7e8e04c64 100644 --- a/packages/compiler-cli/ngcc/test/rendering/dts_renderer_spec.ts +++ b/packages/compiler-cli/ngcc/test/rendering/dts_renderer_spec.ts @@ -82,7 +82,7 @@ function createTestRenderer( spyOn(testFormatter, 'rewriteSwitchableDeclarations').and.callThrough(); spyOn(testFormatter, 'addModuleWithProvidersParams').and.callThrough(); - const renderer = new DtsRenderer(testFormatter, fs, logger, host, isCore, bundle); + const renderer = new DtsRenderer(testFormatter, fs, logger, host, bundle); return {renderer, testFormatter, diff --git a/packages/compiler-cli/ngcc/test/rendering/renderer_spec.ts b/packages/compiler-cli/ngcc/test/rendering/renderer_spec.ts index 63d92b9a84..e9d4ae842b 100644 --- a/packages/compiler-cli/ngcc/test/rendering/renderer_spec.ts +++ b/packages/compiler-cli/ngcc/test/rendering/renderer_spec.ts @@ -83,7 +83,7 @@ function createTestRenderer( spyOn(testFormatter, 'rewriteSwitchableDeclarations').and.callThrough(); spyOn(testFormatter, 'addModuleWithProvidersParams').and.callThrough(); - const renderer = new Renderer(testFormatter, fs, logger, host, isCore, bundle); + const renderer = new Renderer(testFormatter, fs, logger, bundle); return {renderer, testFormatter,