MCPcopy Create free account
hub / github.com/angular/angular / compileInjectable

Function compileInjectable

packages/compiler/src/injectable_compiler_2.ts:38–145  ·  view source on GitHub ↗
(
  meta: R3InjectableMetadata,
  resolveForwardRefs: boolean,
)

Source from the content-addressed store, hash-verified

36}
37
38export function compileInjectable(
39 meta: R3InjectableMetadata,
40 resolveForwardRefs: boolean,
41): R3CompiledExpression {
42 let result: {expression: o.Expression; statements: o.Statement[]} | null = null;
43
44 const factoryMeta: R3FactoryMetadata = {
45 name: meta.name,
46 type: meta.type,
47 typeArgumentCount: meta.typeArgumentCount,
48 deps: [],
49 target: FactoryTarget.Injectable,
50 };
51
52 if (meta.useClass !== undefined) {
53 // meta.useClass has two modes of operation. Either deps are specified, in which case `new` is
54 // used to instantiate the class with dependencies injected, or deps are not specified and
55 // the factory of the class is used to instantiate it.
56 //
57 // A special case exists for useClass: Type where Type is the injectable type itself and no
58 // deps are specified, in which case 'useClass' is effectively ignored.
59
60 const useClassOnSelf = meta.useClass.expression.isEquivalent(meta.type.value);
61 let deps: R3DependencyMetadata[] | undefined = undefined;
62 if (meta.deps !== undefined) {
63 deps = meta.deps;
64 }
65
66 if (deps !== undefined) {
67 // factory: () => new meta.useClass(...deps)
68 result = compileFactoryFunction({
69 ...factoryMeta,
70 delegate: meta.useClass.expression,
71 delegateDeps: deps,
72 delegateType: R3FactoryDelegateType.Class,
73 });
74 } else if (useClassOnSelf) {
75 result = compileFactoryFunction(factoryMeta);
76 } else {
77 result = {
78 statements: [],
79 expression: delegateToFactory(
80 meta.type.value as o.WrappedNodeExpr<any>,
81 meta.useClass.expression as o.WrappedNodeExpr<any>,
82 resolveForwardRefs,
83 ),
84 };
85 }
86 } else if (meta.useFactory !== undefined) {
87 if (meta.deps !== undefined) {
88 result = compileFactoryFunction({
89 ...factoryMeta,
90 delegate: meta.useFactory,
91 delegateDeps: meta.deps || [],
92 delegateType: R3FactoryDelegateType.Function,
93 });
94 } else {
95 result = {statements: [], expression: o.arrowFn([], meta.useFactory.callFn([]))};

Callers 5

compileInjectableMethod · 0.90
compileFullMethod · 0.90
compileLocalMethod · 0.90

Calls 7

setMethod · 0.95
toLiteralMapMethod · 0.95
compileFactoryFunctionFunction · 0.90
delegateToFactoryFunction · 0.85
createInjectableTypeFunction · 0.85
isEquivalentMethod · 0.45

Tested by

no test coverage detected