static-injector 2.1.0 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (108) hide show
  1. package/import/commonjs/index.js +403 -211
  2. package/import/{es2015 → es2022}/di/create_injector.js +5 -3
  3. package/import/{es2015 → es2022}/di/forward_ref.js +32 -5
  4. package/import/{es2015 → es2022}/di/injection_token.js +20 -11
  5. package/import/{es2015 → es2022}/di/injector.js +15 -16
  6. package/import/{es2015 → es2022}/di/injector_compatibility.js +27 -13
  7. package/import/{es2015 → es2022}/di/injector_token.js +3 -3
  8. package/import/{es2015 → es2022}/di/interface/defs.js +22 -27
  9. package/import/{es2015 → es2022}/di/interface/injector.js +1 -1
  10. package/import/{es2015 → es2022}/di/interface/provider.js +3 -1
  11. package/import/{es2015 → es2022}/di/null_injector.js +3 -3
  12. package/import/es2022/di/provider_collection.js +104 -0
  13. package/import/{es2015 → es2022}/di/r3_injector.js +71 -37
  14. package/import/es2022/error_details_base_url.js +19 -0
  15. package/import/es2022/errors.js +44 -0
  16. package/import/es2022/interface/type.js +21 -0
  17. package/import/{es2015 → es2022}/render3/definition_factory.js +2 -3
  18. package/import/{es2015 → es2022}/render3/error_code.js +1 -0
  19. package/import/{es2015 → es2022}/render3/errors_di.js +2 -3
  20. package/import/es2022/render3/fields.js +22 -0
  21. package/import/{es2015 → es2022}/render3/instructions/di.js +0 -7
  22. package/import/{es2015 → es2022}/render3/util/stringify_utils.js +5 -5
  23. package/import/{es2015 → es2022}/util/array_utils.js +0 -7
  24. package/import/es2022/util/decorators.js +53 -0
  25. package/import/{es2015 → es2022}/util/empty.js +4 -0
  26. package/import/{es2015 → es2022}/util/stringify.js +5 -5
  27. package/import/{fesm2015 → fesm2022}/index.js +399 -209
  28. package/import/typings/di/create_injector.d.ts +3 -5
  29. package/import/typings/di/forward_ref.d.ts +30 -3
  30. package/import/typings/di/inject_switch.d.ts +1 -1
  31. package/import/typings/di/injectable.d.ts +4 -3
  32. package/import/typings/di/injection_token.d.ts +21 -6
  33. package/import/typings/di/injector.d.ts +19 -3
  34. package/import/typings/di/injector_compatibility.d.ts +12 -5
  35. package/import/typings/di/injector_token.d.ts +2 -2
  36. package/import/typings/di/interface/defs.d.ts +17 -9
  37. package/import/typings/di/interface/injector.d.ts +2 -2
  38. package/import/typings/di/interface/provider.d.ts +64 -4
  39. package/import/typings/di/metadata.d.ts +4 -4
  40. package/import/typings/di/null_injector.d.ts +1 -1
  41. package/import/typings/di/provider_collection.d.ts +53 -11
  42. package/import/typings/di/provider_token.d.ts +2 -2
  43. package/import/typings/di/r3_injector.d.ts +11 -14
  44. package/import/typings/di/scope.d.ts +1 -1
  45. package/import/typings/error_details_base_url.d.ts +19 -0
  46. package/import/typings/errors.d.ts +114 -0
  47. package/import/typings/interface/type.d.ts +45 -0
  48. package/import/typings/render3/errors_di.d.ts +7 -0
  49. package/import/typings/render3/fields.d.ts +9 -0
  50. package/import/typings/render3/instructions/di.d.ts +0 -7
  51. package/import/typings/util/array_utils.d.ts +15 -7
  52. package/import/typings/util/decorators.d.ts +3 -1
  53. package/import/typings/util/empty.d.ts +1 -0
  54. package/package.json +7 -7
  55. package/readme.md +4 -1
  56. package/transform/compiler/src/injectable_compiler_2.d.ts +0 -1
  57. package/transform/compiler/src/injectable_compiler_2.js +31 -16
  58. package/transform/compiler/src/output/output_ast.d.ts +53 -10
  59. package/transform/compiler/src/output/output_ast.js +211 -12
  60. package/transform/compiler/src/parse_util.js +10 -0
  61. package/transform/compiler/src/render3/partial/api.js +1 -1
  62. package/transform/compiler/src/render3/r3_factory.d.ts +0 -8
  63. package/transform/compiler/src/render3/r3_factory.js +5 -4
  64. package/transform/compiler/src/render3/r3_identifiers.js +37 -37
  65. package/transform/compiler/src/render3/view/util.js +1 -3
  66. package/transform/compiler-cli/src/ngtsc/annotations/common/src/di.js +2 -4
  67. package/transform/compiler-cli/src/ngtsc/annotations/common/src/factory.js +1 -0
  68. package/transform/compiler-cli/src/ngtsc/annotations/common/src/util.d.ts +1 -1
  69. package/transform/compiler-cli/src/ngtsc/annotations/common/src/util.js +5 -9
  70. package/transform/compiler-cli/src/ngtsc/annotations/src/injectable.d.ts +2 -1
  71. package/transform/compiler-cli/src/ngtsc/annotations/src/injectable.js +12 -8
  72. package/transform/compiler-cli/src/ngtsc/diagnostics/error.d.ts +2 -2
  73. package/transform/compiler-cli/src/ngtsc/diagnostics/error.js +8 -4
  74. package/transform/compiler-cli/src/ngtsc/diagnostics/error_code.js +1 -1
  75. package/transform/compiler-cli/src/ngtsc/imports/src/default.js +1 -2
  76. package/transform/compiler-cli/src/ngtsc/reflection/src/host.d.ts +24 -173
  77. package/transform/compiler-cli/src/ngtsc/reflection/src/host.js +2 -51
  78. package/transform/compiler-cli/src/ngtsc/reflection/src/typescript.d.ts +5 -3
  79. package/transform/compiler-cli/src/ngtsc/reflection/src/typescript.js +5 -10
  80. package/transform/compiler-cli/src/ngtsc/transform/src/api.d.ts +5 -23
  81. package/transform/compiler-cli/src/ngtsc/transform/src/api.js +0 -21
  82. package/transform/compiler-cli/src/ngtsc/transform/src/utils.js +1 -2
  83. package/transform/compiler-cli/src/ngtsc/translator/src/api/ast_factory.d.ts +7 -0
  84. package/transform/compiler-cli/src/ngtsc/translator/src/context.js +1 -0
  85. package/transform/compiler-cli/src/ngtsc/translator/src/import_manager.js +4 -2
  86. package/transform/compiler-cli/src/ngtsc/translator/src/translator.d.ts +1 -0
  87. package/transform/compiler-cli/src/ngtsc/translator/src/translator.js +10 -4
  88. package/transform/compiler-cli/src/ngtsc/translator/src/typescript_ast_factory.d.ts +2 -1
  89. package/transform/compiler-cli/src/ngtsc/translator/src/typescript_ast_factory.js +20 -16
  90. package/transform/injectable-transform.d.ts +2 -2
  91. package/transform/injectable-transform.js +32 -6
  92. package/import/es2015/di/provider_collection.js +0 -49
  93. package/import/es2015/interface/type.js +0 -8
  94. package/import/es2015/render3/fields.js +0 -11
  95. package/import/es2015/util/decorators.js +0 -30
  96. /package/import/{es2015 → es2022}/di/initializer_token.js +0 -0
  97. /package/import/{es2015 → es2022}/di/inject_switch.js +0 -0
  98. /package/import/{es2015 → es2022}/di/injectable.js +0 -0
  99. /package/import/{es2015 → es2022}/di/injector_marker.js +0 -0
  100. /package/import/{es2015 → es2022}/di/internal_tokens.js +0 -0
  101. /package/import/{es2015 → es2022}/di/metadata.js +0 -0
  102. /package/import/{es2015 → es2022}/di/provider_token.js +0 -0
  103. /package/import/{es2015 → es2022}/di/scope.js +0 -0
  104. /package/import/{es2015 → es2022}/index.js +0 -0
  105. /package/import/{es2015 → es2022}/interface/lifecycle_hooks.js +0 -0
  106. /package/import/{es2015 → es2022}/render3/di.js +0 -0
  107. /package/import/{es2015 → es2022}/util/closure.js +0 -0
  108. /package/import/{es2015 → es2022}/util/property.js +0 -0
@@ -7,11 +7,10 @@
7
7
  */
8
8
  import { EMPTY_ARRAY } from '../util/empty';
9
9
  import { stringify } from '../util/stringify';
10
+ import { importProvidersFrom } from './provider_collection';
10
11
  import { getNullInjector, R3Injector } from './r3_injector';
11
12
  /**
12
13
  * Create a new `Injector` which is configured using a `defType` of `InjectorType<any>`s.
13
- *
14
- * @publicApi
15
14
  */
16
15
  export function createInjector(defType, parent = null, additionalProviders = null, name) {
17
16
  const injector = createInjectorWithoutInjectorInstances(defType, parent, additionalProviders, name);
@@ -24,7 +23,10 @@ export function createInjector(defType, parent = null, additionalProviders = nul
24
23
  * should be resolved at a later point by calling `_resolveInjectorDefTypes`.
25
24
  */
26
25
  export function createInjectorWithoutInjectorInstances(defType, parent = null, additionalProviders = null, name, scopes = new Set()) {
27
- const providers = [additionalProviders || EMPTY_ARRAY];
26
+ const providers = [
27
+ additionalProviders || EMPTY_ARRAY,
28
+ importProvidersFrom(defType),
29
+ ];
28
30
  name = name || (typeof defType === 'object' ? undefined : stringify(defType));
29
31
  return new R3Injector(providers, parent || getNullInjector(), name || null, scopes);
30
32
  }
@@ -5,8 +5,8 @@
5
5
  * Use of this source code is governed by an MIT-style license that can be
6
6
  * found in the LICENSE file at https://angular.io/license
7
7
  */
8
- import { getClosureSafeProperty } from "../util/property";
9
- import { stringify } from "../util/stringify";
8
+ import { getClosureSafeProperty } from '../util/property';
9
+ import { stringify } from '../util/stringify';
10
10
  const __forward_ref__ = getClosureSafeProperty({
11
11
  __forward_ref__: getClosureSafeProperty,
12
12
  });
@@ -17,9 +17,36 @@ const __forward_ref__ = getClosureSafeProperty({
17
17
  * DI is declared, but not yet defined. It is also used when the `token` which we use when creating
18
18
  * a query is not yet defined.
19
19
  *
20
+ * `forwardRef` is also used to break circularities in standalone components imports.
21
+ *
20
22
  * @usageNotes
21
- * ### Example
23
+ * ### Circular dependency example
22
24
  * {@example core/di/ts/forward_ref/forward_ref_spec.ts region='forward_ref'}
25
+ *
26
+ * ### Circular standalone reference import example
27
+ * ```ts
28
+ * @Component({
29
+ * standalone: true,
30
+ * imports: [ChildComponent],
31
+ * selector: 'app-parent',
32
+ * template: `<app-child [hideParent]="hideParent"></app-child>`,
33
+ * })
34
+ * export class ParentComponent {
35
+ * @Input() hideParent: boolean;
36
+ * }
37
+ *
38
+ *
39
+ * @Component({
40
+ * standalone: true,
41
+ * imports: [CommonModule, forwardRef(() => ParentComponent)],
42
+ * selector: 'app-child',
43
+ * template: `<app-parent *ngIf="!hideParent"></app-parent>`,
44
+ * })
45
+ * export class ChildComponent {
46
+ * @Input() hideParent: boolean;
47
+ * }
48
+ * ```
49
+ *
23
50
  * @publicApi
24
51
  */
25
52
  export function forwardRef(forwardRefFn) {
@@ -39,7 +66,7 @@ export function forwardRef(forwardRefFn) {
39
66
  *
40
67
  * {@example core/di/ts/forward_ref/forward_ref_spec.ts region='resolve_forward_ref'}
41
68
  *
42
- * @see `forwardRef`
69
+ * @see {@link forwardRef}
43
70
  * @publicApi
44
71
  */
45
72
  export function resolveForwardRef(type) {
@@ -47,7 +74,7 @@ export function resolveForwardRef(type) {
47
74
  }
48
75
  /** Checks whether a function is wrapped by a `forwardRef`. */
49
76
  export function isForwardRef(fn) {
50
- return (typeof fn === "function" &&
77
+ return (typeof fn === 'function' &&
51
78
  fn.hasOwnProperty(__forward_ref__) &&
52
79
  fn.__forward_ref__ === forwardRef);
53
80
  }
@@ -14,11 +14,11 @@ import { ɵɵdefineInjectable } from './interface/defs';
14
14
  * parameterized type.
15
15
  *
16
16
  * `InjectionToken` is parameterized on `T` which is the type of object which will be returned by
17
- * the `Injector`. This provides additional level of type safety.
17
+ * the `Injector`. This provides an additional level of type safety.
18
18
  *
19
19
  * ```
20
20
  * interface MyInterface {...}
21
- * var myInterface = injector.get(new InjectionToken<MyInterface>('SomeToken'));
21
+ * const myInterface = injector.get(new InjectionToken<MyInterface>('SomeToken'));
22
22
  * // myInterface is inferred to be MyInterface.
23
23
  * ```
24
24
  *
@@ -26,14 +26,18 @@ import { ɵɵdefineInjectable } from './interface/defs';
26
26
  * (possibly by creating) a default value of the parameterized type `T`. This sets up the
27
27
  * `InjectionToken` using this factory as a provider as if it was defined explicitly in the
28
28
  * application's root injector. If the factory function, which takes zero arguments, needs to inject
29
- * dependencies, it can do so using the `inject` function. See below for an example.
29
+ * dependencies, it can do so using the [`inject`](api/core/inject) function.
30
+ * As you can see in the Tree-shakable InjectionToken example below.
30
31
  *
31
32
  * Additionally, if a `factory` is specified you can also specify the `providedIn` option, which
32
- * overrides the above behavior and marks the token as belonging to a particular `@NgModule`. As
33
- * mentioned above, `'root'` is the default value for `providedIn`.
33
+ * overrides the above behavior and marks the token as belonging to a particular `@NgModule` (note:
34
+ * this option is now deprecated). As mentioned above, `'root'` is the default value for
35
+ * `providedIn`.
36
+ *
37
+ * The `providedIn: NgModule` and `providedIn: 'any'` options are deprecated.
34
38
  *
35
39
  * @usageNotes
36
- * ### Basic Example
40
+ * ### Basic Examples
37
41
  *
38
42
  * ### Plain InjectionToken
39
43
  *
@@ -47,15 +51,20 @@ import { ɵɵdefineInjectable } from './interface/defs';
47
51
  * @publicApi
48
52
  */
49
53
  export class InjectionToken {
54
+ _desc;
55
+ /** @internal */
56
+ ngMetadataName = 'InjectionToken';
57
+ ɵprov;
58
+ /**
59
+ * @param _desc Description for the token,
60
+ * used only for debugging purposes,
61
+ * it should but does not need to be unique
62
+ * @param options Options for the token's usage, as described above
63
+ */
50
64
  constructor(_desc, options) {
51
65
  this._desc = _desc;
52
66
  this.ɵprov = undefined;
53
67
  if (typeof options == 'number') {
54
- // (typeof ngDevMode === "undefined" || ngDevMode) &&
55
- // assertLessThan(options, 0, "Only negative numbers are supported here");
56
- // // This is a special hack to assign __NG_ELEMENT_ID__ to this instance.
57
- // // See `InjectorMarkers`
58
- // (this as any).__NG_ELEMENT_ID__ = options;
59
68
  }
60
69
  else if (options !== undefined) {
61
70
  this.ɵprov = ɵɵdefineInjectable({
@@ -16,7 +16,7 @@ import { NullInjector } from './null_injector';
16
16
  * dependencies of various types with [injection tokens](guide/glossary#di-token).
17
17
  *
18
18
  * @see ["DI Providers"](guide/dependency-injection-providers).
19
- * @see `StaticProvider`
19
+ * @see {@link StaticProvider}
20
20
  *
21
21
  * @usageNotes
22
22
  *
@@ -35,27 +35,26 @@ import { NullInjector } from './null_injector';
35
35
  * @publicApi
36
36
  */
37
37
  export class Injector {
38
+ static THROW_IF_NOT_FOUND = THROW_IF_NOT_FOUND;
39
+ static NULL = new NullInjector();
38
40
  static create(options, parent) {
39
- var _a;
40
41
  if (Array.isArray(options)) {
41
42
  return createInjector({ name: '' }, parent, options, '');
42
43
  }
43
44
  else {
44
- const name = (_a = options.name) !== null && _a !== void 0 ? _a : '';
45
+ const name = options.name ?? '';
45
46
  return createInjector({ name }, options.parent, options.providers, name);
46
47
  }
47
48
  }
49
+ /** @nocollapse */
50
+ static ɵprov = /** @pureOrBreakMyCode */ ɵɵdefineInjectable({
51
+ token: Injector,
52
+ providedIn: 'any',
53
+ factory: () => ɵɵinject(INJECTOR),
54
+ });
55
+ /**
56
+ * @internal
57
+ * @nocollapse
58
+ */
59
+ static __NG_ELEMENT_ID__ = -1 /* InjectorMarkers.Injector */;
48
60
  }
49
- Injector.THROW_IF_NOT_FOUND = THROW_IF_NOT_FOUND;
50
- Injector.NULL = new NullInjector();
51
- /** @nocollapse */
52
- Injector.ɵprov = ɵɵdefineInjectable({
53
- token: Injector,
54
- providedIn: 'any',
55
- factory: () => ɵɵinject(INJECTOR),
56
- });
57
- /**
58
- * @internal
59
- * @nocollapse
60
- */
61
- Injector.__NG_ELEMENT_ID__ = -1 /* InjectorMarkers.Injector */;
@@ -5,10 +5,10 @@
5
5
  * Use of this source code is governed by an MIT-style license that can be
6
6
  * found in the LICENSE file at https://angular.io/license
7
7
  */
8
- import { getClosureSafeProperty } from '../util/property';
8
+ import { RuntimeError } from '../errors';
9
9
  import { stringify } from '../util/stringify';
10
10
  import { resolveForwardRef } from './forward_ref';
11
- import { injectRootLimpMode } from './inject_switch';
11
+ import { getInjectImplementation, injectRootLimpMode } from './inject_switch';
12
12
  import { InjectFlags, } from './interface/injector';
13
13
  const _THROW_IF_NOT_FOUND = {};
14
14
  export const THROW_IF_NOT_FOUND = _THROW_IF_NOT_FOUND;
@@ -23,10 +23,6 @@ const NG_TOKEN_PATH = 'ngTokenPath';
23
23
  const NEW_LINE = /\n/gm;
24
24
  const NO_NEW_LINE = 'ɵ';
25
25
  export const SOURCE = '__source';
26
- export const USE_VALUE = getClosureSafeProperty({
27
- provide: String,
28
- useValue: getClosureSafeProperty,
29
- });
30
26
  /**
31
27
  * Current injector value used by `inject`.
32
28
  * - `undefined`: it is an error to call `inject`
@@ -34,6 +30,9 @@ export const USE_VALUE = getClosureSafeProperty({
34
30
  * - Injector instance: Use the injector for resolution.
35
31
  */
36
32
  let _currentInjector = undefined;
33
+ export function getCurrentInjector() {
34
+ return _currentInjector;
35
+ }
37
36
  export function setCurrentInjector(injector) {
38
37
  const former = _currentInjector;
39
38
  _currentInjector = injector;
@@ -41,32 +40,46 @@ export function setCurrentInjector(injector) {
41
40
  }
42
41
  export function injectInjectorOnly(token, flags = InjectFlags.Default) {
43
42
  if (_currentInjector === undefined) {
44
- throw new Error(`inject() must be called from an injection context`);
43
+ throw new RuntimeError(-203 /* RuntimeErrorCode.MISSING_INJECTION_CONTEXT */, null);
45
44
  }
46
45
  else if (_currentInjector === null) {
47
46
  return injectRootLimpMode(token, undefined, flags);
48
47
  }
49
48
  else {
50
- return _currentInjector.get(token, flags & InjectFlags.Optional ? null : undefined, flags);
49
+ const value = _currentInjector.get(token, flags & InjectFlags.Optional ? null : undefined, flags);
50
+ return value;
51
51
  }
52
52
  }
53
53
  export function ɵɵinject(token, flags = InjectFlags.Default) {
54
- return injectInjectorOnly(resolveForwardRef(token), flags);
54
+ return (getInjectImplementation() || injectInjectorOnly)(resolveForwardRef(token), flags);
55
+ }
56
+ /**
57
+ * Throws an error indicating that a factory function could not be generated by the compiler for a
58
+ * particular class.
59
+ *
60
+ * The name of the class is not mentioned here, but will be in the generated factory function name
61
+ * and thus in the stack trace.
62
+ *
63
+ * @codeGenApi
64
+ */
65
+ export function ɵɵinvalidFactoryDep(index) {
66
+ throw new RuntimeError(202 /* RuntimeErrorCode.INVALID_FACTORY_DEPENDENCY */, null);
55
67
  }
56
68
  /**
57
69
  * Injects a token from the currently active injector.
58
- * `inject` is only supported during instantiation of a dependency by the DI system. It can be used
59
- * during:
70
+ * `inject` is only supported in an [injection context](/guide/dependency-injection-context). It can
71
+ * be used during:
60
72
  * - Construction (via the `constructor`) of a class being instantiated by the DI system, such
61
73
  * as an `@Injectable` or `@Component`.
62
74
  * - In the initializer for fields of such classes.
63
75
  * - In the factory function specified for `useFactory` of a `Provider` or an `@Injectable`.
64
76
  * - In the `factory` function specified for an `InjectionToken`.
77
+ * - In a stackframe of a function call in a DI context
65
78
  *
66
79
  * @param token A token that represents a dependency that should be injected.
67
80
  * @param flags Optional flags that control how injection is executed.
68
81
  * The flags correspond to injection strategies that can be specified with
69
- * parameter decorators `@Host`, `@Self`, `@SkipSef`, and `@Optional`.
82
+ * parameter decorators `@Host`, `@Self`, `@SkipSelf`, and `@Optional`.
70
83
  * @returns the injected value if operation is successful, `null` otherwise.
71
84
  * @throws if called outside of a supported context.
72
85
  *
@@ -130,6 +143,7 @@ export function convertToBitFlags(flags) {
130
143
  // `InjectOptions` to `InjectFlags`.
131
144
  return (0 /* InternalInjectFlags.Default */ | // comment to force a line break in the formatter
132
145
  (flags.optional && 8 /* InternalInjectFlags.Optional */) |
146
+ 0 |
133
147
  (flags.self && 2 /* InternalInjectFlags.Self */) |
134
148
  (flags.skipSelf &&
135
149
  4 /* InternalInjectFlags.SkipSelf */));
@@ -140,7 +154,7 @@ export function injectArgs(types) {
140
154
  const arg = resolveForwardRef(types[i]);
141
155
  if (Array.isArray(arg)) {
142
156
  if (arg.length === 0) {
143
- throw new Error('Arguments array must have arguments.');
157
+ throw new RuntimeError(900 /* RuntimeErrorCode.INVALID_DIFFER_INPUT */, null);
144
158
  }
145
159
  let type = undefined;
146
160
  let flags = InjectFlags.Default;
@@ -5,7 +5,7 @@
5
5
  * Use of this source code is governed by an MIT-style license that can be
6
6
  * found in the LICENSE file at https://angular.io/license
7
7
  */
8
- import { InjectionToken } from "./injection_token";
8
+ import { InjectionToken } from './injection_token';
9
9
  /**
10
10
  * An InjectionToken that gets the current `Injector` for `createInjector()`-style injectors.
11
11
  *
@@ -14,8 +14,8 @@ import { InjectionToken } from "./injection_token";
14
14
  *
15
15
  * @publicApi
16
16
  */
17
- export const INJECTOR = new InjectionToken("INJECTOR",
18
- // Dissable tslint because this is const enum which gets inlined not top level prop access.
17
+ export const INJECTOR = new InjectionToken('INJECTOR',
18
+ // Disable tslint because this is const enum which gets inlined not top level prop access.
19
19
  // tslint:disable-next-line: no-toplevel-property-access
20
20
  -1 /* InjectorMarkers.Injector */ // Special value used by Ivy to identify `Injector`.
21
21
  );
@@ -18,7 +18,8 @@ import { getClosureSafeProperty } from '../../util/property';
18
18
  * with an `@NgModule` or other `InjectorType`, or by specifying that this injectable should be
19
19
  * provided in the `'root'` injector, which will be the application-level injector in most apps.
20
20
  * * `factory` gives the zero argument function which will create an instance of the injectable.
21
- * The factory can call `inject` to access the `Injector` and request injection of dependencies.
21
+ * The factory can call [`inject`](api/core/inject) to access the `Injector` and request injection
22
+ * of dependencies.
22
23
  *
23
24
  * @codeGenApi
24
25
  * @publicApi This instruction has been emitted by ViewEngine for some time and is deployed to npm.
@@ -58,7 +59,10 @@ export function ɵɵdefineInjector(options) {
58
59
  * @param type A type which may have its own (non-inherited) `ɵprov`.
59
60
  */
60
61
  export function getInjectableDef(type) {
61
- return getOwnDefinition(type, NG_PROV_DEF);
62
+ return getOwnDefinition(type, NG_PROV_DEF) || null;
63
+ }
64
+ export function isInjectable(type) {
65
+ return getInjectableDef(type) !== null;
62
66
  }
63
67
  /**
64
68
  * Return definition only if it is defined directly on `type` and is not inherited from a base
@@ -67,9 +71,6 @@ export function getInjectableDef(type) {
67
71
  function getOwnDefinition(type, field) {
68
72
  return type.hasOwnProperty(field) ? type[field] : null;
69
73
  }
70
- export const NG_PROV_DEF = getClosureSafeProperty({
71
- ɵprov: getClosureSafeProperty,
72
- });
73
74
  /**
74
75
  * Read the injectable def (`ɵprov`) for `type` or read the `ɵprov` from one of its ancestors.
75
76
  *
@@ -79,33 +80,27 @@ export const NG_PROV_DEF = getClosureSafeProperty({
79
80
  * scenario if we find the `ɵprov` on an ancestor only.
80
81
  */
81
82
  export function getInheritedInjectableDef(type) {
82
- const def = type && (type[NG_PROV_DEF] || type[NG_INJECTABLE_DEF]);
83
+ const def = type && (type[NG_PROV_DEF] || null);
83
84
  if (def) {
84
- const typeName = getTypeName(type);
85
- // TODO(FW-1307): Re-add ngDevMode when closure can handle it
86
- // ngDevMode &&
87
- console.warn(`DEPRECATED: DI is instantiating a token "${typeName}" that inherits its @Injectable decorator but does not provide one itself.\n` +
88
- `This will become an error in a future version of Angular. Please add @Injectable() to the "${typeName}" class.`);
89
85
  return def;
90
86
  }
91
87
  else {
92
88
  return null;
93
89
  }
94
90
  }
95
- export const NG_INJECTABLE_DEF = getClosureSafeProperty({
96
- ngInjectableDef: getClosureSafeProperty,
97
- });
98
- /** Gets the name of a type, accounting for some cross-browser differences. */
99
- function getTypeName(type) {
100
- // `Function.prototype.name` behaves differently between IE and other browsers. In most browsers
101
- // it'll always return the name of the function itself, no matter how many other functions it
102
- // inherits from. On IE the function doesn't have its own `name` property, but it takes it from
103
- // the lowest level in the prototype chain. E.g. if we have `class Foo extends Parent` most
104
- // browsers will evaluate `Foo.name` to `Foo` while IE will return `Parent`. We work around
105
- // the issue by converting the function to a string and parsing its name out that way via a regex.
106
- if (type.hasOwnProperty('name')) {
107
- return type.name;
108
- }
109
- const match = ('' + type).match(/^function\s*([^\s(]+)/);
110
- return match === null ? '' : match[1];
91
+ /**
92
+ * Read the injector def type in a way which is immune to accidentally reading inherited value.
93
+ *
94
+ * @param type type which may have an injector def (`ɵinj`)
95
+ */
96
+ export function getInjectorDef(type) {
97
+ return type && (type.hasOwnProperty(NG_INJ_DEF) || false)
98
+ ? type[NG_INJ_DEF]
99
+ : null;
111
100
  }
101
+ export const NG_PROV_DEF = getClosureSafeProperty({
102
+ ɵprov: getClosureSafeProperty,
103
+ });
104
+ export const NG_INJ_DEF = getClosureSafeProperty({
105
+ ɵinj: getClosureSafeProperty,
106
+ });
@@ -9,7 +9,7 @@
9
9
  * Injection flags for DI.
10
10
  *
11
11
  * @publicApi
12
- * @deprecated use an options object for `inject` instead.
12
+ * @deprecated use an options object for [`inject`](api/core/inject) instead.
13
13
  */
14
14
  export var InjectFlags;
15
15
  (function (InjectFlags) {
@@ -5,4 +5,6 @@
5
5
  * Use of this source code is governed by an MIT-style license that can be
6
6
  * found in the LICENSE file at https://angular.io/license
7
7
  */
8
- export {};
8
+ export function isEnvironmentProviders(value) {
9
+ return value && !!value.ɵproviders;
10
+ }
@@ -5,13 +5,13 @@
5
5
  * Use of this source code is governed by an MIT-style license that can be
6
6
  * found in the LICENSE file at https://angular.io/license
7
7
  */
8
- import { stringify } from "../util/stringify";
9
- import { THROW_IF_NOT_FOUND } from "./injector_compatibility";
8
+ import { stringify } from '../util/stringify';
9
+ import { THROW_IF_NOT_FOUND } from './injector_compatibility';
10
10
  export class NullInjector {
11
11
  get(token, notFoundValue = THROW_IF_NOT_FOUND) {
12
12
  if (notFoundValue === THROW_IF_NOT_FOUND) {
13
13
  const error = new Error(`NullInjectorError: No provider for ${stringify(token)}!`);
14
- error.name = "NullInjectorError";
14
+ error.name = 'NullInjectorError';
15
15
  throw error;
16
16
  }
17
17
  return notFoundValue;
@@ -0,0 +1,104 @@
1
+ /**
2
+ * @license
3
+ * Copyright Google LLC All Rights Reserved.
4
+ *
5
+ * Use of this source code is governed by an MIT-style license that can be
6
+ * found in the LICENSE file at https://angular.io/license
7
+ */
8
+ import { getClosureSafeProperty } from '../util/property';
9
+ /**
10
+ * Wrap an array of `Provider`s into `EnvironmentProviders`, preventing them from being accidentally
11
+ * referenced in `@Component` in a component injector.
12
+ */
13
+ export function makeEnvironmentProviders(providers) {
14
+ return {
15
+ ɵproviders: providers,
16
+ };
17
+ }
18
+ /**
19
+ * Collects providers from all NgModules and standalone components, including transitively imported
20
+ * ones.
21
+ *
22
+ * Providers extracted via `importProvidersFrom` are only usable in an application injector or
23
+ * another environment injector (such as a route injector). They should not be used in component
24
+ * providers.
25
+ *
26
+ * More information about standalone components can be found in [this
27
+ * guide](guide/standalone-components).
28
+ *
29
+ * @usageNotes
30
+ * The results of the `importProvidersFrom` call can be used in the `bootstrapApplication` call:
31
+ *
32
+ * ```typescript
33
+ * await bootstrapApplication(RootComponent, {
34
+ * providers: [
35
+ * importProvidersFrom(NgModuleOne, NgModuleTwo)
36
+ * ]
37
+ * });
38
+ * ```
39
+ *
40
+ * You can also use the `importProvidersFrom` results in the `providers` field of a route, when a
41
+ * standalone component is used:
42
+ *
43
+ * ```typescript
44
+ * export const ROUTES: Route[] = [
45
+ * {
46
+ * path: 'foo',
47
+ * providers: [
48
+ * importProvidersFrom(NgModuleOne, NgModuleTwo)
49
+ * ],
50
+ * component: YourStandaloneComponent
51
+ * }
52
+ * ];
53
+ * ```
54
+ *
55
+ * @returns Collected providers from the specified list of types.
56
+ * @publicApi
57
+ */
58
+ export function importProvidersFrom(...sources) {
59
+ return {
60
+ ɵproviders: internalImportProvidersFrom(true, sources),
61
+ ɵfromNgModule: true,
62
+ };
63
+ }
64
+ export function internalImportProvidersFrom(checkForStandaloneCmp, ...sources) {
65
+ const providersOut = [];
66
+ const dedup = new Set(); // already seen types
67
+ let injectorTypesWithProviders;
68
+ const collectProviders = (provider) => {
69
+ providersOut.push(provider);
70
+ };
71
+ // Collect all providers from `ModuleWithProviders` types.
72
+ if (injectorTypesWithProviders !== undefined) {
73
+ processInjectorTypesWithProviders(injectorTypesWithProviders, collectProviders);
74
+ }
75
+ return providersOut;
76
+ }
77
+ /**
78
+ * Collects all providers from the list of `ModuleWithProviders` and appends them to the provided
79
+ * array.
80
+ */
81
+ function processInjectorTypesWithProviders(typesWithProviders, visitor) {
82
+ for (let i = 0; i < typesWithProviders.length; i++) {
83
+ const { ngModule, providers } = typesWithProviders[i];
84
+ }
85
+ }
86
+ export const USE_VALUE = getClosureSafeProperty({
87
+ provide: String,
88
+ useValue: getClosureSafeProperty,
89
+ });
90
+ export function isValueProvider(value) {
91
+ return value !== null && typeof value == 'object' && USE_VALUE in value;
92
+ }
93
+ export function isExistingProvider(value) {
94
+ return !!(value && value.useExisting);
95
+ }
96
+ export function isFactoryProvider(value) {
97
+ return !!(value && value.useFactory);
98
+ }
99
+ export function isTypeProvider(value) {
100
+ return typeof value === 'function';
101
+ }
102
+ export function isClassProvider(value) {
103
+ return !!value.useClass;
104
+ }