From 42036f4b79a692dcc8321f9d62ac6c515fbe34ee Mon Sep 17 00:00:00 2001 From: Pete Bacon Darwin Date: Sat, 25 May 2019 20:53:52 +0100 Subject: [PATCH] refactor(ivy): ngcc - pass `bundle` to `DecorationAnalyzer` (#30591) Rather than passing a number of individual arguments, we can just pass an `EntryPointBundle`, which already contains them. This is also a precursor to using more of the properties in the bundle. PR Close #30591 --- .../ngcc/src/analysis/decoration_analyzer.ts | 13 +++++++++---- .../compiler-cli/ngcc/src/packages/transformer.ts | 6 ++---- .../ngcc/test/analysis/decoration_analyzer_spec.ts | 12 ++++++------ .../rendering/commonjs_rendering_formatter_spec.ts | 5 +---- .../ngcc/test/rendering/dts_renderer_spec.ts | 6 ++---- .../test/rendering/esm5_rendering_formatter_spec.ts | 6 ++---- .../test/rendering/esm_rendering_formatter_spec.ts | 6 ++---- .../ngcc/test/rendering/renderer_spec.ts | 6 ++---- .../test/rendering/umd_rendering_formatter_spec.ts | 7 ++----- 9 files changed, 28 insertions(+), 39 deletions(-) diff --git a/packages/compiler-cli/ngcc/src/analysis/decoration_analyzer.ts b/packages/compiler-cli/ngcc/src/analysis/decoration_analyzer.ts index 12b832e78c..232506d3a7 100644 --- a/packages/compiler-cli/ngcc/src/analysis/decoration_analyzer.ts +++ b/packages/compiler-cli/ngcc/src/analysis/decoration_analyzer.ts @@ -18,6 +18,7 @@ import {ClassDeclaration, ClassSymbol, Decorator} from '../../../src/ngtsc/refle import {LocalModuleScopeRegistry, MetadataDtsModuleScopeResolver} from '../../../src/ngtsc/scope'; import {CompileResult, DecoratorHandler, DetectResult, HandlerPrecedence} from '../../../src/ngtsc/transform'; import {NgccReflectionHost} from '../host/ngcc_host'; +import {EntryPointBundle} from '../packages/entry_point_bundle'; import {isDefined} from '../utils'; export interface AnalyzedFile { @@ -66,6 +67,12 @@ class NgccResourceLoader implements ResourceLoader { * This Analyzer will analyze the files that have decorated classes that need to be transformed. */ export class DecorationAnalyzer { + private program = this.bundle.src.program; + private options = this.bundle.src.options; + private host = this.bundle.src.host; + private typeChecker = this.bundle.src.program.getTypeChecker(); + private rootDirs = this.bundle.rootDirs; + private isCore = this.bundle.isCore; resourceManager = new NgccResourceLoader(this.fs); metaRegistry = new LocalMetadataRegistry(); dtsMetaReader = new DtsMetadataReader(this.typeChecker, this.reflectionHost); @@ -112,10 +119,8 @@ export class DecorationAnalyzer { ]; constructor( - private fs: FileSystem, private program: ts.Program, private options: ts.CompilerOptions, - private host: ts.CompilerHost, private typeChecker: ts.TypeChecker, - private reflectionHost: NgccReflectionHost, private referencesRegistry: ReferencesRegistry, - private rootDirs: AbsoluteFsPath[], private isCore: boolean) {} + private fs: FileSystem, private bundle: EntryPointBundle, + private reflectionHost: NgccReflectionHost, private referencesRegistry: ReferencesRegistry) {} /** * Analyze a program to find all the decorated files should be transformed. diff --git a/packages/compiler-cli/ngcc/src/packages/transformer.ts b/packages/compiler-cli/ngcc/src/packages/transformer.ts index 0d1176c48e..23b6d76bba 100644 --- a/packages/compiler-cli/ngcc/src/packages/transformer.ts +++ b/packages/compiler-cli/ngcc/src/packages/transformer.ts @@ -120,15 +120,13 @@ export class Transformer { } analyzeProgram(reflectionHost: NgccReflectionHost, bundle: EntryPointBundle): ProgramAnalyses { - const typeChecker = bundle.src.program.getTypeChecker(); const referencesRegistry = new NgccReferencesRegistry(reflectionHost); const switchMarkerAnalyzer = new SwitchMarkerAnalyzer(reflectionHost); const switchMarkerAnalyses = switchMarkerAnalyzer.analyzeProgram(bundle.src.program); - const decorationAnalyzer = new DecorationAnalyzer( - this.fs, bundle.src.program, bundle.src.options, bundle.src.host, typeChecker, - reflectionHost, referencesRegistry, bundle.rootDirs, bundle.isCore); + const decorationAnalyzer = + new DecorationAnalyzer(this.fs, bundle, reflectionHost, referencesRegistry); const decorationAnalyses = decorationAnalyzer.analyzeProgram(); const moduleWithProvidersAnalyzer = diff --git a/packages/compiler-cli/ngcc/test/analysis/decoration_analyzer_spec.ts b/packages/compiler-cli/ngcc/test/analysis/decoration_analyzer_spec.ts index 1baa420b22..bcd31c248f 100644 --- a/packages/compiler-cli/ngcc/test/analysis/decoration_analyzer_spec.ts +++ b/packages/compiler-cli/ngcc/test/analysis/decoration_analyzer_spec.ts @@ -16,7 +16,7 @@ import {CompiledClass, DecorationAnalyses, DecorationAnalyzer} from '../../src/a import {NgccReferencesRegistry} from '../../src/analysis/ngcc_references_registry'; import {Esm2015ReflectionHost} from '../../src/host/esm2015_host'; import {MockLogger} from '../helpers/mock_logger'; -import {getRootFiles, makeTestBundleProgram} from '../helpers/utils'; +import {getRootFiles, makeTestEntryPointBundle} from '../helpers/utils'; type DecoratorHandlerWithResolve = DecoratorHandler& { resolve: NonNullable['resolve']>; @@ -91,15 +91,15 @@ runInEachFileSystem(() => { loadTestFiles(testFiles); loadFakeCore(getFileSystem()); const rootFiles = getRootFiles(testFiles); - const {options, host, ...bundle} = makeTestBundleProgram(rootFiles[0]); - program = bundle.program; + const bundle = + makeTestEntryPointBundle('test-package', 'es2015', 'esm2015', false, rootFiles); + program = bundle.src.program; const reflectionHost = new Esm2015ReflectionHost(new MockLogger(), false, program.getTypeChecker()); const referencesRegistry = new NgccReferencesRegistry(reflectionHost); - const analyzer = new DecorationAnalyzer( - getFileSystem(), program, options, host, program.getTypeChecker(), reflectionHost, - referencesRegistry, [absoluteFrom('/')], false); + const analyzer = + new DecorationAnalyzer(getFileSystem(), bundle, reflectionHost, referencesRegistry); testHandler = createTestHandler(); analyzer.handlers = [testHandler]; result = analyzer.analyzeProgram(); diff --git a/packages/compiler-cli/ngcc/test/rendering/commonjs_rendering_formatter_spec.ts b/packages/compiler-cli/ngcc/test/rendering/commonjs_rendering_formatter_spec.ts index d299a7640c..567a1e5ff9 100644 --- a/packages/compiler-cli/ngcc/test/rendering/commonjs_rendering_formatter_spec.ts +++ b/packages/compiler-cli/ngcc/test/rendering/commonjs_rendering_formatter_spec.ts @@ -155,10 +155,7 @@ exports.D = D; const host = new CommonJsReflectionHost(logger, false, bundle.src.program, bundle.src.host); const referencesRegistry = new NgccReferencesRegistry(host); const decorationAnalyses = - new DecorationAnalyzer( - fs, bundle.src.program, bundle.src.options, bundle.src.host, typeChecker, host, - referencesRegistry, [absoluteFrom('/')], false) - .analyzeProgram(); + new DecorationAnalyzer(fs, bundle, host, referencesRegistry).analyzeProgram(); const switchMarkerAnalyses = new SwitchMarkerAnalyzer(host).analyzeProgram(bundle.src.program); const renderer = new CommonJsRenderingFormatter(host, false); 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 d7e8e04c64..2caf9cf0be 100644 --- a/packages/compiler-cli/ngcc/test/rendering/dts_renderer_spec.ts +++ b/packages/compiler-cli/ngcc/test/rendering/dts_renderer_spec.ts @@ -65,10 +65,8 @@ function createTestRenderer( const typeChecker = bundle.src.program.getTypeChecker(); const host = new Esm2015ReflectionHost(logger, isCore, typeChecker, bundle.dts); const referencesRegistry = new NgccReferencesRegistry(host); - const decorationAnalyses = new DecorationAnalyzer( - fs, bundle.src.program, bundle.src.options, bundle.src.host, - typeChecker, host, referencesRegistry, bundle.rootDirs, isCore) - .analyzeProgram(); + const decorationAnalyses = + new DecorationAnalyzer(fs, bundle, host, referencesRegistry).analyzeProgram(); const moduleWithProvidersAnalyses = new ModuleWithProvidersAnalyzer(host, referencesRegistry).analyzeProgram(bundle.src.program); const privateDeclarationsAnalyses = diff --git a/packages/compiler-cli/ngcc/test/rendering/esm5_rendering_formatter_spec.ts b/packages/compiler-cli/ngcc/test/rendering/esm5_rendering_formatter_spec.ts index fe310c5db9..c92974ad69 100644 --- a/packages/compiler-cli/ngcc/test/rendering/esm5_rendering_formatter_spec.ts +++ b/packages/compiler-cli/ngcc/test/rendering/esm5_rendering_formatter_spec.ts @@ -30,10 +30,8 @@ function setup(file: {name: AbsoluteFsPath, contents: string}) { const typeChecker = bundle.src.program.getTypeChecker(); const host = new Esm5ReflectionHost(logger, false, typeChecker); const referencesRegistry = new NgccReferencesRegistry(host); - const decorationAnalyses = new DecorationAnalyzer( - fs, bundle.src.program, bundle.src.options, bundle.src.host, - typeChecker, host, referencesRegistry, [absoluteFrom('/')], false) - .analyzeProgram(); + const decorationAnalyses = + new DecorationAnalyzer(fs, bundle, host, referencesRegistry).analyzeProgram(); const switchMarkerAnalyses = new SwitchMarkerAnalyzer(host).analyzeProgram(bundle.src.program); const renderer = new Esm5RenderingFormatter(host, false); const importManager = new ImportManager(new NoopImportRewriter(), IMPORT_PREFIX); diff --git a/packages/compiler-cli/ngcc/test/rendering/esm_rendering_formatter_spec.ts b/packages/compiler-cli/ngcc/test/rendering/esm_rendering_formatter_spec.ts index 2534811f04..2c52e002ea 100644 --- a/packages/compiler-cli/ngcc/test/rendering/esm_rendering_formatter_spec.ts +++ b/packages/compiler-cli/ngcc/test/rendering/esm_rendering_formatter_spec.ts @@ -35,10 +35,8 @@ function setup(files: TestFile[], dtsFiles?: TestFile[]) { const typeChecker = bundle.src.program.getTypeChecker(); const host = new Esm2015ReflectionHost(logger, false, typeChecker, bundle.dts); const referencesRegistry = new NgccReferencesRegistry(host); - const decorationAnalyses = new DecorationAnalyzer( - fs, bundle.src.program, bundle.src.options, bundle.src.host, - typeChecker, host, referencesRegistry, [absoluteFrom('/')], false) - .analyzeProgram(); + const decorationAnalyses = + new DecorationAnalyzer(fs, bundle, host, referencesRegistry).analyzeProgram(); const switchMarkerAnalyses = new SwitchMarkerAnalyzer(host).analyzeProgram(bundle.src.program); const renderer = new EsmRenderingFormatter(host, false); const importManager = new ImportManager(new NoopImportRewriter(), IMPORT_PREFIX); diff --git a/packages/compiler-cli/ngcc/test/rendering/renderer_spec.ts b/packages/compiler-cli/ngcc/test/rendering/renderer_spec.ts index e9d4ae842b..be77626357 100644 --- a/packages/compiler-cli/ngcc/test/rendering/renderer_spec.ts +++ b/packages/compiler-cli/ngcc/test/rendering/renderer_spec.ts @@ -67,10 +67,8 @@ function createTestRenderer( const typeChecker = bundle.src.program.getTypeChecker(); const host = new Esm2015ReflectionHost(logger, isCore, typeChecker, bundle.dts); const referencesRegistry = new NgccReferencesRegistry(host); - const decorationAnalyses = new DecorationAnalyzer( - fs, bundle.src.program, bundle.src.options, bundle.src.host, - typeChecker, host, referencesRegistry, bundle.rootDirs, isCore) - .analyzeProgram(); + const decorationAnalyses = + new DecorationAnalyzer(fs, bundle, host, referencesRegistry).analyzeProgram(); const switchMarkerAnalyses = new SwitchMarkerAnalyzer(host).analyzeProgram(bundle.src.program); const privateDeclarationsAnalyses = new PrivateDeclarationsAnalyzer(host, referencesRegistry).analyzeProgram(bundle.src.program); diff --git a/packages/compiler-cli/ngcc/test/rendering/umd_rendering_formatter_spec.ts b/packages/compiler-cli/ngcc/test/rendering/umd_rendering_formatter_spec.ts index 2a96bc7ef5..1286f9a081 100644 --- a/packages/compiler-cli/ngcc/test/rendering/umd_rendering_formatter_spec.ts +++ b/packages/compiler-cli/ngcc/test/rendering/umd_rendering_formatter_spec.ts @@ -27,13 +27,10 @@ function setup(file: TestFile) { const logger = new MockLogger(); const bundle = makeTestEntryPointBundle('test-package', 'esm5', 'esm5', false, [file.name]); const src = bundle.src; - const typeChecker = src.program.getTypeChecker(); const host = new UmdReflectionHost(logger, false, src.program, src.host); const referencesRegistry = new NgccReferencesRegistry(host); - const decorationAnalyses = new DecorationAnalyzer( - fs, src.program, src.options, src.host, typeChecker, host, - referencesRegistry, [absoluteFrom('/')], false) - .analyzeProgram(); + const decorationAnalyses = + new DecorationAnalyzer(fs, bundle, host, referencesRegistry).analyzeProgram(); const switchMarkerAnalyses = new SwitchMarkerAnalyzer(host).analyzeProgram(src.program); const renderer = new UmdRenderingFormatter(host, false); const importManager = new ImportManager(new NoopImportRewriter(), 'i');