From 9c9fd2d0742ff293dae1137ee9e5db05025e200f Mon Sep 17 00:00:00 2001 From: Pete Bacon Darwin Date: Tue, 16 Mar 2021 09:47:54 +0000 Subject: [PATCH] refactor(compiler): remove `injectFn` option from factory metadata (#41231) The `injectFn` reference can be inferred unamiguously from the `target` property so it is not needed. PR Close #41231 --- .../src/ngtsc/annotations/src/component.ts | 1 - .../src/ngtsc/annotations/src/directive.ts | 1 - .../src/ngtsc/annotations/src/injectable.ts | 1 - .../src/ngtsc/annotations/src/ng_module.ts | 1 - .../src/ngtsc/annotations/src/pipe.ts | 1 - .../compiler/src/compiler_facade_interface.ts | 1 - .../compiler/src/injectable_compiler_2.ts | 1 - packages/compiler/src/jit_compiler_facade.ts | 2 - packages/compiler/src/render3/r3_factory.ts | 37 +++++++++++-------- .../src/compiler/compiler_facade_interface.ts | 1 - packages/core/src/di/jit/injectable.ts | 1 - packages/core/src/render3/jit/directive.ts | 8 ++-- packages/core/src/render3/jit/module.ts | 1 - packages/core/src/render3/jit/pipe.ts | 2 +- 14 files changed, 25 insertions(+), 34 deletions(-) diff --git a/packages/compiler-cli/src/ngtsc/annotations/src/component.ts b/packages/compiler-cli/src/ngtsc/annotations/src/component.ts index 597d70d3d0..ad324a7e15 100644 --- a/packages/compiler-cli/src/ngtsc/annotations/src/component.ts +++ b/packages/compiler-cli/src/ngtsc/annotations/src/component.ts @@ -853,7 +853,6 @@ export class ComponentDecoratorHandler implements {expression: initializer, statements, type}: R3CompiledExpression): CompileResult[] { const factoryRes = compileNgFactoryDefField({ ...analysis.meta, - injectFn: Identifiers.directiveInject, target: R3FactoryTarget.Component, }); if (analysis.metadataStmt !== null) { diff --git a/packages/compiler-cli/src/ngtsc/annotations/src/directive.ts b/packages/compiler-cli/src/ngtsc/annotations/src/directive.ts index 86cc55d0d3..1b60acf84f 100644 --- a/packages/compiler-cli/src/ngtsc/annotations/src/directive.ts +++ b/packages/compiler-cli/src/ngtsc/annotations/src/directive.ts @@ -318,7 +318,6 @@ export class DirectiveDecoratorHandler implements {expression: initializer, statements, type}: R3CompiledExpression): CompileResult[] { const factoryRes = compileNgFactoryDefField({ ...analysis.meta, - injectFn: Identifiers.directiveInject, target: R3FactoryTarget.Directive, }); if (analysis.metadataStmt !== null) { diff --git a/packages/compiler-cli/src/ngtsc/annotations/src/injectable.ts b/packages/compiler-cli/src/ngtsc/annotations/src/injectable.ts index 0f420934e0..e0255ffd1d 100644 --- a/packages/compiler-cli/src/ngtsc/annotations/src/injectable.ts +++ b/packages/compiler-cli/src/ngtsc/annotations/src/injectable.ts @@ -107,7 +107,6 @@ export class InjectableDecoratorHandler implements internalType: meta.internalType, typeArgumentCount: meta.typeArgumentCount, deps: analysis.ctorDeps, - injectFn: Identifiers.inject, target: R3FactoryTarget.Injectable, }); if (analysis.metadataStmt !== null) { 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 ac8f3562cc..37a7c54eab 100644 --- a/packages/compiler-cli/src/ngtsc/annotations/src/ng_module.ts +++ b/packages/compiler-cli/src/ngtsc/annotations/src/ng_module.ts @@ -351,7 +351,6 @@ export class NgModuleDecoratorHandler implements typeArgumentCount: 0, deps: getValidConstructorDependencies( node, this.reflector, this.defaultImportRecorder, this.isCore), - injectFn: R3.inject, target: R3FactoryTarget.NgModule, }; diff --git a/packages/compiler-cli/src/ngtsc/annotations/src/pipe.ts b/packages/compiler-cli/src/ngtsc/annotations/src/pipe.ts index 102aa1bd47..edaf8913f2 100644 --- a/packages/compiler-cli/src/ngtsc/annotations/src/pipe.ts +++ b/packages/compiler-cli/src/ngtsc/annotations/src/pipe.ts @@ -177,7 +177,6 @@ export class PipeDecoratorHandler implements private compilePipe(analysis: Readonly, def: R3CompiledExpression) { const factoryRes = compileNgFactoryDefField({ ...analysis.meta, - injectFn: Identifiers.directiveInject, target: R3FactoryTarget.Pipe, }); if (analysis.metadataStmt !== null) { diff --git a/packages/compiler/src/compiler_facade_interface.ts b/packages/compiler/src/compiler_facade_interface.ts index 41c4bd7ff1..4424c416c6 100644 --- a/packages/compiler/src/compiler_facade_interface.ts +++ b/packages/compiler/src/compiler_facade_interface.ts @@ -229,7 +229,6 @@ export interface R3FactoryDefMetadataFacade { type: any; typeArgumentCount: number; deps: R3DependencyMetadataFacade[]|null; - injectFn: 'directiveInject'|'inject'; target: R3FactoryTarget; } diff --git a/packages/compiler/src/injectable_compiler_2.ts b/packages/compiler/src/injectable_compiler_2.ts index def4178fd9..4faa6ea878 100644 --- a/packages/compiler/src/injectable_compiler_2.ts +++ b/packages/compiler/src/injectable_compiler_2.ts @@ -40,7 +40,6 @@ export function compileInjectable(meta: R3InjectableMetadata): InjectableDef { internalType: meta.internalType, typeArgumentCount: meta.typeArgumentCount, deps: [], - injectFn: Identifiers.inject, target: R3FactoryTarget.Injectable, }; diff --git a/packages/compiler/src/jit_compiler_facade.ts b/packages/compiler/src/jit_compiler_facade.ts index 5b608ec418..d054dedede 100644 --- a/packages/compiler/src/jit_compiler_facade.ts +++ b/packages/compiler/src/jit_compiler_facade.ts @@ -207,8 +207,6 @@ export class CompilerFacadeImpl implements CompilerFacade { internalType: new WrappedNodeExpr(meta.type), typeArgumentCount: meta.typeArgumentCount, deps: convertR3DependencyMetadataArray(meta.deps), - injectFn: meta.injectFn === 'directiveInject' ? Identifiers.directiveInject : - Identifiers.inject, target: meta.target, }); return this.jitExpression( diff --git a/packages/compiler/src/render3/r3_factory.ts b/packages/compiler/src/render3/r3_factory.ts index d39a2ab80d..85e9f4490d 100644 --- a/packages/compiler/src/render3/r3_factory.ts +++ b/packages/compiler/src/render3/r3_factory.ts @@ -56,12 +56,6 @@ export interface R3ConstructorFactoryMetadata { */ deps: R3DependencyMetadata[]|'invalid'|null; - /** - * An expression for the function which will be used to inject dependencies. The API of this - * function could be different, and other options control how it will be invoked. - */ - injectFn: o.ExternalReference; - /** * Type of the target being created by the factory. */ @@ -190,9 +184,7 @@ export function compileFactoryFunction(meta: R3FactoryMetadata): R3CompiledExpre if (meta.deps !== null) { // There is a constructor (either explicitly or implicitly defined). if (meta.deps !== 'invalid') { - ctorExpr = new o.InstantiateExpr( - typeForCtor, - injectDependencies(meta.deps, meta.injectFn, meta.target === R3FactoryTarget.Pipe)); + ctorExpr = new o.InstantiateExpr(typeForCtor, injectDependencies(meta.deps, meta.target)); ctorDepsType = createCtorDepsType(meta.deps); } @@ -230,8 +222,7 @@ export function compileFactoryFunction(meta: R3FactoryMetadata): R3CompiledExpre if (isDelegatedMetadata(meta)) { // This type is created with a delegated factory. If a type parameter is not specified, call // the factory instead. - const delegateArgs = - injectDependencies(meta.delegateDeps, meta.injectFn, meta.target === R3FactoryTarget.Pipe); + const delegateArgs = injectDependencies(meta.delegateDeps, meta.target); // Either call `new delegate(...)` or `delegate(...)` depending on meta.delegateType. const factoryExpr = new ( meta.delegateType === R3FactoryDelegateType.Class ? @@ -262,14 +253,14 @@ export function compileFactoryFunction(meta: R3FactoryMetadata): R3CompiledExpre }; } -function injectDependencies( - deps: R3DependencyMetadata[], injectFn: o.ExternalReference, isPipe: boolean): o.Expression[] { - return deps.map((dep, index) => compileInjectDependency(dep, injectFn, isPipe, index)); +function injectDependencies(deps: R3DependencyMetadata[], target: R3FactoryTarget): o.Expression[] { + return deps.map((dep, index) => compileInjectDependency(dep, target, index)); } function compileInjectDependency( - dep: R3DependencyMetadata, injectFn: o.ExternalReference, isPipe: boolean, - index: number): o.Expression { + dep: R3DependencyMetadata, target: R3FactoryTarget, index: number): o.Expression { + const isPipe = target === R3FactoryTarget.Pipe; + // Interpret the dependency according to its resolved type. switch (dep.resolved) { case R3ResolvedDependencyType.Token: @@ -295,6 +286,7 @@ function compileInjectDependency( if (flagsParam) { injectArgs.push(flagsParam); } + const injectFn = getInjectFn(target); return o.importExpr(injectFn).callFn(injectArgs); case R3ResolvedDependencyType.Attribute: // In the case of attributes, the attribute name in question is given as the token. @@ -401,3 +393,16 @@ function isDelegatedMetadata(meta: R3FactoryMetadata): meta is R3DelegatedFnOrCl function isExpressionFactoryMetadata(meta: R3FactoryMetadata): meta is R3ExpressionFactoryMetadata { return (meta as any).expression !== undefined; } + +function getInjectFn(target: R3FactoryTarget): o.ExternalReference { + switch (target) { + case R3FactoryTarget.Component: + case R3FactoryTarget.Directive: + case R3FactoryTarget.Pipe: + return R3.directiveInject; + case R3FactoryTarget.NgModule: + case R3FactoryTarget.Injectable: + default: + return R3.inject; + } +} diff --git a/packages/core/src/compiler/compiler_facade_interface.ts b/packages/core/src/compiler/compiler_facade_interface.ts index 9489382bd4..5dcae0434d 100644 --- a/packages/core/src/compiler/compiler_facade_interface.ts +++ b/packages/core/src/compiler/compiler_facade_interface.ts @@ -229,7 +229,6 @@ export interface R3FactoryDefMetadataFacade { type: any; typeArgumentCount: number; deps: R3DependencyMetadataFacade[]|null; - injectFn: 'directiveInject'|'inject'; target: R3FactoryTarget; } diff --git a/packages/core/src/di/jit/injectable.ts b/packages/core/src/di/jit/injectable.ts index 702560897c..f3e27ca4fc 100644 --- a/packages/core/src/di/jit/injectable.ts +++ b/packages/core/src/di/jit/injectable.ts @@ -54,7 +54,6 @@ export function compileInjectable(type: Type, srcMeta?: Injectable): void { type: metadata.type, typeArgumentCount: metadata.typeArgumentCount, deps: reflectDependencies(type), - injectFn: 'inject', target: compiler.R3FactoryTarget.Injectable }); } diff --git a/packages/core/src/render3/jit/directive.ts b/packages/core/src/render3/jit/directive.ts index a1283c7350..f2f3a71489 100644 --- a/packages/core/src/render3/jit/directive.ts +++ b/packages/core/src/render3/jit/directive.ts @@ -210,11 +210,9 @@ function addDirectiveFactoryDef(type: Type, metadata: Directive|Component) if (ngFactoryDef === null) { const meta = getDirectiveMetadata(type, metadata); const compiler = getCompilerFacade(); - ngFactoryDef = compiler.compileFactory(angularCoreEnv, `ng:///${type.name}/ɵfac.js`, { - ...meta.metadata, - injectFn: 'directiveInject', - target: compiler.R3FactoryTarget.Directive - }); + ngFactoryDef = compiler.compileFactory( + angularCoreEnv, `ng:///${type.name}/ɵfac.js`, + {...meta.metadata, target: compiler.R3FactoryTarget.Directive}); } return ngFactoryDef; }, diff --git a/packages/core/src/render3/jit/module.ts b/packages/core/src/render3/jit/module.ts index 9274250c87..97dc04402a 100644 --- a/packages/core/src/render3/jit/module.ts +++ b/packages/core/src/render3/jit/module.ts @@ -149,7 +149,6 @@ export function compileNgModuleDefs( name: moduleType.name, type: moduleType, deps: reflectDependencies(moduleType), - injectFn: 'inject', target: compiler.R3FactoryTarget.NgModule, typeArgumentCount: 0, }); diff --git a/packages/core/src/render3/jit/pipe.ts b/packages/core/src/render3/jit/pipe.ts index e5f14732b1..1d419b75ee 100644 --- a/packages/core/src/render3/jit/pipe.ts +++ b/packages/core/src/render3/jit/pipe.ts @@ -25,7 +25,7 @@ export function compilePipe(type: Type, meta: Pipe): void { const compiler = getCompilerFacade(); ngFactoryDef = compiler.compileFactory( angularCoreEnv, `ng:///${metadata.name}/ɵfac.js`, - {...metadata, injectFn: 'directiveInject', target: compiler.R3FactoryTarget.Pipe}); + {...metadata, target: compiler.R3FactoryTarget.Pipe}); } return ngFactoryDef; },