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[] { {expression: initializer, statements, type}: R3CompiledExpression): CompileResult[] {
const factoryRes = compileNgFactoryDefField({ const factoryRes = compileNgFactoryDefField({
...analysis.meta, ...analysis.meta,
injectFn: Identifiers.directiveInject,
target: R3FactoryTarget.Component, target: R3FactoryTarget.Component,
}); });
if (analysis.metadataStmt !== null) { if (analysis.metadataStmt !== null) {

View File

@ -318,7 +318,6 @@ export class DirectiveDecoratorHandler implements
{expression: initializer, statements, type}: R3CompiledExpression): CompileResult[] { {expression: initializer, statements, type}: R3CompiledExpression): CompileResult[] {
const factoryRes = compileNgFactoryDefField({ const factoryRes = compileNgFactoryDefField({
...analysis.meta, ...analysis.meta,
injectFn: Identifiers.directiveInject,
target: R3FactoryTarget.Directive, target: R3FactoryTarget.Directive,
}); });
if (analysis.metadataStmt !== null) { if (analysis.metadataStmt !== null) {

View File

@ -107,7 +107,6 @@ export class InjectableDecoratorHandler implements
internalType: meta.internalType, internalType: meta.internalType,
typeArgumentCount: meta.typeArgumentCount, typeArgumentCount: meta.typeArgumentCount,
deps: analysis.ctorDeps, deps: analysis.ctorDeps,
injectFn: Identifiers.inject,
target: R3FactoryTarget.Injectable, target: R3FactoryTarget.Injectable,
}); });
if (analysis.metadataStmt !== null) { if (analysis.metadataStmt !== null) {

View File

@ -351,7 +351,6 @@ export class NgModuleDecoratorHandler implements
typeArgumentCount: 0, typeArgumentCount: 0,
deps: getValidConstructorDependencies( deps: getValidConstructorDependencies(
node, this.reflector, this.defaultImportRecorder, this.isCore), node, this.reflector, this.defaultImportRecorder, this.isCore),
injectFn: R3.inject,
target: R3FactoryTarget.NgModule, target: R3FactoryTarget.NgModule,
}; };

View File

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

View File

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

View File

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

View File

@ -207,8 +207,6 @@ export class CompilerFacadeImpl implements CompilerFacade {
internalType: new WrappedNodeExpr(meta.type), internalType: new WrappedNodeExpr(meta.type),
typeArgumentCount: meta.typeArgumentCount, typeArgumentCount: meta.typeArgumentCount,
deps: convertR3DependencyMetadataArray(meta.deps), deps: convertR3DependencyMetadataArray(meta.deps),
injectFn: meta.injectFn === 'directiveInject' ? Identifiers.directiveInject :
Identifiers.inject,
target: meta.target, target: meta.target,
}); });
return this.jitExpression( return this.jitExpression(

View File

@ -56,12 +56,6 @@ export interface R3ConstructorFactoryMetadata {
*/ */
deps: R3DependencyMetadata[]|'invalid'|null; 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. * Type of the target being created by the factory.
*/ */
@ -190,9 +184,7 @@ export function compileFactoryFunction(meta: R3FactoryMetadata): R3CompiledExpre
if (meta.deps !== null) { if (meta.deps !== null) {
// There is a constructor (either explicitly or implicitly defined). // There is a constructor (either explicitly or implicitly defined).
if (meta.deps !== 'invalid') { if (meta.deps !== 'invalid') {
ctorExpr = new o.InstantiateExpr( ctorExpr = new o.InstantiateExpr(typeForCtor, injectDependencies(meta.deps, meta.target));
typeForCtor,
injectDependencies(meta.deps, meta.injectFn, meta.target === R3FactoryTarget.Pipe));
ctorDepsType = createCtorDepsType(meta.deps); ctorDepsType = createCtorDepsType(meta.deps);
} }
@ -230,8 +222,7 @@ export function compileFactoryFunction(meta: R3FactoryMetadata): R3CompiledExpre
if (isDelegatedMetadata(meta)) { if (isDelegatedMetadata(meta)) {
// This type is created with a delegated factory. If a type parameter is not specified, call // This type is created with a delegated factory. If a type parameter is not specified, call
// the factory instead. // the factory instead.
const delegateArgs = const delegateArgs = injectDependencies(meta.delegateDeps, meta.target);
injectDependencies(meta.delegateDeps, meta.injectFn, meta.target === R3FactoryTarget.Pipe);
// Either call `new delegate(...)` or `delegate(...)` depending on meta.delegateType. // Either call `new delegate(...)` or `delegate(...)` depending on meta.delegateType.
const factoryExpr = new ( const factoryExpr = new (
meta.delegateType === R3FactoryDelegateType.Class ? meta.delegateType === R3FactoryDelegateType.Class ?
@ -262,14 +253,14 @@ export function compileFactoryFunction(meta: R3FactoryMetadata): R3CompiledExpre
}; };
} }
function injectDependencies( function injectDependencies(deps: R3DependencyMetadata[], target: R3FactoryTarget): o.Expression[] {
deps: R3DependencyMetadata[], injectFn: o.ExternalReference, isPipe: boolean): o.Expression[] { return deps.map((dep, index) => compileInjectDependency(dep, target, index));
return deps.map((dep, index) => compileInjectDependency(dep, injectFn, isPipe, index));
} }
function compileInjectDependency( function compileInjectDependency(
dep: R3DependencyMetadata, injectFn: o.ExternalReference, isPipe: boolean, dep: R3DependencyMetadata, target: R3FactoryTarget, index: number): o.Expression {
index: number): o.Expression { const isPipe = target === R3FactoryTarget.Pipe;
// Interpret the dependency according to its resolved type. // Interpret the dependency according to its resolved type.
switch (dep.resolved) { switch (dep.resolved) {
case R3ResolvedDependencyType.Token: case R3ResolvedDependencyType.Token:
@ -295,6 +286,7 @@ function compileInjectDependency(
if (flagsParam) { if (flagsParam) {
injectArgs.push(flagsParam); injectArgs.push(flagsParam);
} }
const injectFn = getInjectFn(target);
return o.importExpr(injectFn).callFn(injectArgs); return o.importExpr(injectFn).callFn(injectArgs);
case R3ResolvedDependencyType.Attribute: case R3ResolvedDependencyType.Attribute:
// In the case of attributes, the attribute name in question is given as the token. // 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 { function isExpressionFactoryMetadata(meta: R3FactoryMetadata): meta is R3ExpressionFactoryMetadata {
return (meta as any).expression !== undefined; 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; type: any;
typeArgumentCount: number; typeArgumentCount: number;
deps: R3DependencyMetadataFacade[]|null; deps: R3DependencyMetadataFacade[]|null;
injectFn: 'directiveInject'|'inject';
target: R3FactoryTarget; target: R3FactoryTarget;
} }

View File

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

View File

@ -210,11 +210,9 @@ function addDirectiveFactoryDef(type: Type<any>, metadata: Directive|Component)
if (ngFactoryDef === null) { if (ngFactoryDef === null) {
const meta = getDirectiveMetadata(type, metadata); const meta = getDirectiveMetadata(type, metadata);
const compiler = getCompilerFacade(); const compiler = getCompilerFacade();
ngFactoryDef = compiler.compileFactory(angularCoreEnv, `ng:///${type.name}/ɵfac.js`, { ngFactoryDef = compiler.compileFactory(
...meta.metadata, angularCoreEnv, `ng:///${type.name}/ɵfac.js`,
injectFn: 'directiveInject', {...meta.metadata, target: compiler.R3FactoryTarget.Directive});
target: compiler.R3FactoryTarget.Directive
});
} }
return ngFactoryDef; return ngFactoryDef;
}, },

View File

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

View File

@ -25,7 +25,7 @@ export function compilePipe(type: Type<any>, meta: Pipe): void {
const compiler = getCompilerFacade(); const compiler = getCompilerFacade();
ngFactoryDef = compiler.compileFactory( ngFactoryDef = compiler.compileFactory(
angularCoreEnv, `ng:///${metadata.name}/ɵfac.js`, angularCoreEnv, `ng:///${metadata.name}/ɵfac.js`,
{...metadata, injectFn: 'directiveInject', target: compiler.R3FactoryTarget.Pipe}); {...metadata, target: compiler.R3FactoryTarget.Pipe});
} }
return ngFactoryDef; return ngFactoryDef;
}, },