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
This commit is contained in:
Pete Bacon Darwin 2021-03-16 09:47:54 +00:00 committed by Alex Rickabaugh
parent d35751d442
commit 9c9fd2d074
14 changed files with 25 additions and 34 deletions

View File

@ -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) {

View File

@ -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) {

View File

@ -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) {

View File

@ -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,
};

View File

@ -177,7 +177,6 @@ export class PipeDecoratorHandler implements
private compilePipe(analysis: Readonly<PipeHandlerData>, def: R3CompiledExpression) {
const factoryRes = compileNgFactoryDefField({
...analysis.meta,
injectFn: Identifiers.directiveInject,
target: R3FactoryTarget.Pipe,
});
if (analysis.metadataStmt !== null) {

View File

@ -229,7 +229,6 @@ export interface R3FactoryDefMetadataFacade {
type: any;
typeArgumentCount: number;
deps: R3DependencyMetadataFacade[]|null;
injectFn: 'directiveInject'|'inject';
target: R3FactoryTarget;
}

View File

@ -40,7 +40,6 @@ export function compileInjectable(meta: R3InjectableMetadata): InjectableDef {
internalType: meta.internalType,
typeArgumentCount: meta.typeArgumentCount,
deps: [],
injectFn: Identifiers.inject,
target: R3FactoryTarget.Injectable,
};

View File

@ -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(

View File

@ -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;
}
}

View File

@ -229,7 +229,6 @@ export interface R3FactoryDefMetadataFacade {
type: any;
typeArgumentCount: number;
deps: R3DependencyMetadataFacade[]|null;
injectFn: 'directiveInject'|'inject';
target: R3FactoryTarget;
}

View File

@ -54,7 +54,6 @@ export function compileInjectable(type: Type<any>, srcMeta?: Injectable): void {
type: metadata.type,
typeArgumentCount: metadata.typeArgumentCount,
deps: reflectDependencies(type),
injectFn: 'inject',
target: compiler.R3FactoryTarget.Injectable
});
}

View File

@ -210,11 +210,9 @@ function addDirectiveFactoryDef(type: Type<any>, 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;
},

View File

@ -149,7 +149,6 @@ export function compileNgModuleDefs(
name: moduleType.name,
type: moduleType,
deps: reflectDependencies(moduleType),
injectFn: 'inject',
target: compiler.R3FactoryTarget.NgModule,
typeArgumentCount: 0,
});

View File

@ -25,7 +25,7 @@ export function compilePipe(type: Type<any>, 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;
},