From d04515cf537b1ce67b1f4b0da91a02e07def5186 Mon Sep 17 00:00:00 2001 From: Pete Bacon Darwin Date: Thu, 25 Feb 2021 13:48:39 +0000 Subject: [PATCH] refactor(compiler): separate `compileFactoryFunction()` from `compileInjector()` (#41022) This commit moves the creation of the injector's factory function out so that it can be more easily refactored further. PR Close #41022 --- .../src/ngtsc/annotations/src/ng_module.ts | 32 +++++++++++++------ packages/compiler/src/jit_compiler_facade.ts | 11 ++++++- .../src/render3/r3_module_compiler.ts | 20 +++--------- 3 files changed, 37 insertions(+), 26 deletions(-) diff --git a/packages/compiler-cli/src/ngtsc/annotations/src/ng_module.ts b/packages/compiler-cli/src/ngtsc/annotations/src/ng_module.ts index 76e3cc5411..fbb181a743 100644 --- a/packages/compiler-cli/src/ngtsc/annotations/src/ng_module.ts +++ b/packages/compiler-cli/src/ngtsc/annotations/src/ng_module.ts @@ -6,7 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -import {compileInjector, compileNgModule, CUSTOM_ELEMENTS_SCHEMA, Expression, ExternalExpr, InvokeFunctionExpr, LiteralArrayExpr, LiteralExpr, NO_ERRORS_SCHEMA, R3Identifiers, R3InjectorMetadata, R3NgModuleMetadata, R3Reference, SchemaMetadata, Statement, STRING_TYPE, WrappedNodeExpr} from '@angular/compiler'; +import {compileFactoryFunction, compileInjector, compileNgModule, CUSTOM_ELEMENTS_SCHEMA, Expression, ExternalExpr, Identifiers as R3, InvokeFunctionExpr, LiteralArrayExpr, LiteralExpr, NO_ERRORS_SCHEMA, R3FactoryTarget, R3Identifiers, R3InjectorMetadata, R3NgModuleMetadata, R3Reference, SchemaMetadata, Statement, STRING_TYPE, WrappedNodeExpr} from '@angular/compiler'; import * as ts from 'typescript'; import {ErrorCode, FatalDiagnosticError, makeDiagnostic, makeRelatedInformation} from '../../diagnostics'; @@ -449,21 +449,33 @@ export class NgModuleDecoratorHandler implements } compileFull( - node: ClassDeclaration, analysis: Readonly, + node: ClassDeclaration, {inj, mod, metadataStmt, declarations}: Readonly, resolution: Readonly): CompileResult[] { + const factoryFn = compileFactoryFunction({ + name: inj.name, + type: inj.type, + internalType: inj.internalType, + typeArgumentCount: 0, + deps: inj.deps, + injectFn: R3.inject, + target: R3FactoryTarget.NgModule, + }); + // Merge the injector imports (which are 'exports' that were later found to be NgModules) // computed during resolution with the ones from analysis. - const ngInjectorDef = compileInjector({ - ...analysis.inj, - imports: [...analysis.inj.imports, ...resolution.injectorImports], - }); - const ngModuleDef = compileNgModule(analysis.mod); + const ngInjectorDef = compileInjector( + { + ...inj, + imports: [...inj.imports, ...resolution.injectorImports], + }, + factoryFn); + const ngModuleDef = compileNgModule(mod); const ngModuleStatements = ngModuleDef.additionalStatements; - if (analysis.metadataStmt !== null) { - ngModuleStatements.push(analysis.metadataStmt); + if (metadataStmt !== null) { + ngModuleStatements.push(metadataStmt); } const context = getSourceFile(node); - for (const decl of analysis.declarations) { + for (const decl of declarations) { const remoteScope = this.scopeRegistry.getRemoteScope(decl.node); if (remoteScope !== null) { const directives = remoteScope.directives.map( diff --git a/packages/compiler/src/jit_compiler_facade.ts b/packages/compiler/src/jit_compiler_facade.ts index a1ec6553db..a37b036e7a 100644 --- a/packages/compiler/src/jit_compiler_facade.ts +++ b/packages/compiler/src/jit_compiler_facade.ts @@ -88,7 +88,16 @@ export class CompilerFacadeImpl implements CompilerFacade { providers: new WrappedNodeExpr(facade.providers), imports: facade.imports.map(i => new WrappedNodeExpr(i)), }; - const res = compileInjector(meta); + const factoryFn = compileFactoryFunction({ + name: meta.name, + type: meta.type, + internalType: meta.internalType, + typeArgumentCount: 0, + deps: meta.deps, + injectFn: Identifiers.inject, + target: R3FactoryTarget.NgModule, + }); + const res = compileInjector(meta, factoryFn); return this.jitExpression(res.expression, angularCoreEnv, sourceMapUrl, res.statements); } diff --git a/packages/compiler/src/render3/r3_module_compiler.ts b/packages/compiler/src/render3/r3_module_compiler.ts index c97bf31f83..ebf783be09 100644 --- a/packages/compiler/src/render3/r3_module_compiler.ts +++ b/packages/compiler/src/render3/r3_module_compiler.ts @@ -8,7 +8,7 @@ import * as o from '../output/output_ast'; -import {compileFactoryFunction, R3DependencyMetadata, R3FactoryTarget} from './r3_factory'; +import {R3DependencyMetadata, R3FactoryFn} from './r3_factory'; import {Identifiers as R3} from './r3_identifiers'; import {jitOnlyGuardedExpression, mapToMapExpression, R3Reference} from './util'; @@ -230,19 +230,9 @@ export interface R3InjectorMetadata { imports: o.Expression[]; } -export function compileInjector(meta: R3InjectorMetadata): R3InjectorDef { - const result = compileFactoryFunction({ - name: meta.name, - type: meta.type, - internalType: meta.internalType, - typeArgumentCount: 0, - deps: meta.deps, - injectFn: R3.inject, - target: R3FactoryTarget.NgModule, - }); - const definitionMap = { - factory: result.factory, - } as {factory: o.Expression, providers: o.Expression, imports: o.Expression}; +export function compileInjector( + meta: R3InjectorMetadata, {factory, statements}: R3FactoryFn): R3InjectorDef { + const definitionMap: Record = {factory}; if (meta.providers !== null) { definitionMap.providers = meta.providers; @@ -256,7 +246,7 @@ export function compileInjector(meta: R3InjectorMetadata): R3InjectorDef { o.importExpr(R3.defineInjector).callFn([mapToMapExpression(definitionMap)], undefined, true); const type = new o.ExpressionType(o.importExpr(R3.InjectorDef, [new o.ExpressionType(meta.type.type)])); - return {expression, type, statements: result.statements}; + return {expression, type, statements}; } function tupleTypeOf(exp: R3Reference[]): o.Type {