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