Skip to content

Commit 611baaf

Browse files
crisbetoAndrewKushnir
authored andcommitted
feat(core): remove InjectFlags from public API (angular#60318)
Removes the deprecated `InjectFlags` symbol from the `@angular/core` public API, as well as all the places that accept it. The previous commit includes an automated migration to switch over to the new way of passing in flags. BREAKING CHANGE: * `InjectFlags` has been removed. * `inject` no longer accepts `InjectFlags`. * `Injector.get` no longer accepts `InjectFlags`. * `EnvironmentInjector.get` no longer accepts `InjectFlags`. * `TestBed.get` no longer accepts `InjectFlags`. * `TestBed.inject` no longer accepts `InjectFlags`. PR Close angular#60318
1 parent e170d24 commit 611baaf

File tree

22 files changed

+93
-205
lines changed

22 files changed

+93
-205
lines changed

goldens/public-api/core/index.api.md

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -692,8 +692,6 @@ export abstract class EnvironmentInjector implements Injector {
692692
}): T;
693693
abstract get<T>(token: ProviderToken<T>, notFoundValue: null | undefined, options: InjectOptions): T | null;
694694
abstract get<T>(token: ProviderToken<T>, notFoundValue?: T, options?: InjectOptions): T;
695-
// @deprecated
696-
abstract get<T>(token: ProviderToken<T>, notFoundValue?: T, flags?: InjectFlags): T;
697695
// @deprecated (undocumented)
698696
abstract get<T>(token: string | ProviderToken<T>, notFoundValue?: any): any;
699697
// @deprecated
@@ -853,9 +851,6 @@ export const Inject: InjectDecorator;
853851
// @public (undocumented)
854852
export function inject<T>(token: ProviderToken<T>): T;
855853

856-
// @public @deprecated (undocumented)
857-
export function inject<T>(token: ProviderToken<T>, flags?: InjectFlags): T | null;
858-
859854
// @public (undocumented)
860855
export function inject<T>(token: ProviderToken<T>, options: InjectOptions & {
861856
optional?: false;
@@ -915,15 +910,6 @@ export interface InjectDecorator {
915910
new (token: any): Inject;
916911
}
917912

918-
// @public @deprecated
919-
export enum InjectFlags {
920-
Default = 0,
921-
Host = 1,
922-
Optional = 8,
923-
Self = 2,
924-
SkipSelf = 4
925-
}
926-
927913
// @public
928914
export class InjectionToken<T> {
929915
constructor(_desc: string, options?: {
@@ -962,9 +948,7 @@ export abstract class Injector {
962948
optional?: false;
963949
}): T;
964950
abstract get<T>(token: ProviderToken<T>, notFoundValue: null | undefined, options: InjectOptions): T | null;
965-
abstract get<T>(token: ProviderToken<T>, notFoundValue?: T, options?: InjectOptions | InjectFlags): T;
966-
// @deprecated
967-
abstract get<T>(token: ProviderToken<T>, notFoundValue?: T, flags?: InjectFlags): T;
951+
abstract get<T>(token: ProviderToken<T>, notFoundValue?: T, options?: InjectOptions): T;
968952
// @deprecated (undocumented)
969953
abstract get<T>(token: string | ProviderToken<T>, notFoundValue?: any): any;
970954
// (undocumented)

goldens/public-api/core/testing/index.api.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import { ɵDeferBlockBehavior as DeferBlockBehavior } from '@angular/core';
1212
import { ɵDeferBlockState as DeferBlockState } from '@angular/core';
1313
import { Directive } from '@angular/core';
1414
import { ElementRef } from '@angular/core';
15-
import { InjectFlags } from '@angular/core';
1615
import { InjectionToken } from '@angular/core';
1716
import { InjectOptions } from '@angular/core';
1817
import { NgModule } from '@angular/core';
@@ -126,8 +125,6 @@ export interface TestBed {
126125
execute(tokens: any[], fn: Function, context?: any): any;
127126
flushEffects(): void;
128127
// @deprecated (undocumented)
129-
get<T>(token: ProviderToken<T>, notFoundValue?: T, flags?: InjectFlags): any;
130-
// @deprecated (undocumented)
131128
get(token: any, notFoundValue?: any): any;
132129
initTestEnvironment(ngModule: Type<any> | Type<any>[], platform: PlatformRef, options?: TestEnvironmentOptions): void;
133130
// (undocumented)
@@ -138,10 +135,6 @@ export interface TestBed {
138135
inject<T>(token: ProviderToken<T>, notFoundValue: null | undefined, options: InjectOptions): T | null;
139136
// (undocumented)
140137
inject<T>(token: ProviderToken<T>, notFoundValue?: T, options?: InjectOptions): T;
141-
// @deprecated (undocumented)
142-
inject<T>(token: ProviderToken<T>, notFoundValue?: T, flags?: InjectFlags): T;
143-
// @deprecated (undocumented)
144-
inject<T>(token: ProviderToken<T>, notFoundValue: null, flags?: InjectFlags): T | null;
145138
// (undocumented)
146139
get ngModule(): Type<any> | Type<any>[];
147140
// (undocumented)

packages/core/src/change_detection/change_detector_ref.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import {InjectFlags} from '../di';
10-
import {InternalInjectFlags} from '../di/interface/injector';
9+
import {InjectFlags, InternalInjectFlags} from '../di/interface/injector';
1110
import {TNode, TNodeType} from '../render3/interfaces/node';
1211
import {isComponentHost} from '../render3/interfaces/type_checks';
1312
import {DECLARATION_COMPONENT_VIEW, LView} from '../render3/interfaces/view';

packages/core/src/di/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
export * from './metadata';
1616
export {assertInInjectionContext, runInInjectionContext} from './contextual';
17-
export {InjectFlags} from './interface/injector';
1817
export {
1918
ɵɵdefineInjectable,
2019
defineInjectable,

packages/core/src/di/injector.ts

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {THROW_IF_NOT_FOUND, ɵɵinject} from './injector_compatibility';
1111
import {InjectorMarkers} from './injector_marker';
1212
import {INJECTOR} from './injector_token';
1313
import {ɵɵdefineInjectable} from './interface/defs';
14-
import {InjectFlags, InjectOptions} from './interface/injector';
14+
import {InjectOptions} from './interface/injector';
1515
import {Provider, StaticProvider} from './interface/provider';
1616
import {NullInjector} from './null_injector';
1717
import {ProviderToken} from './provider_token';
@@ -44,13 +44,6 @@ export abstract class Injector {
4444
static THROW_IF_NOT_FOUND = THROW_IF_NOT_FOUND;
4545
static NULL: Injector = /* @__PURE__ */ new NullInjector();
4646

47-
/**
48-
* Internal note on the `options?: InjectOptions|InjectFlags` override of the `get`
49-
* method: consider dropping the `InjectFlags` part in one of the major versions.
50-
* It can **not** be done in minor/patch, since it's breaking for custom injectors
51-
* that only implement the old `InjectorFlags` interface.
52-
*/
53-
5447
/**
5548
* Retrieves an instance from the injector based on the provided token.
5649
* @returns The instance from the injector if defined, otherwise the `notFoundValue`.
@@ -78,18 +71,7 @@ export abstract class Injector {
7871
* @returns The instance from the injector if defined, otherwise the `notFoundValue`.
7972
* @throws When the `notFoundValue` is `undefined` or `Injector.THROW_IF_NOT_FOUND`.
8073
*/
81-
abstract get<T>(
82-
token: ProviderToken<T>,
83-
notFoundValue?: T,
84-
options?: InjectOptions | InjectFlags,
85-
): T;
86-
/**
87-
* Retrieves an instance from the injector based on the provided token.
88-
* @returns The instance from the injector if defined, otherwise the `notFoundValue`.
89-
* @throws When the `notFoundValue` is `undefined` or `Injector.THROW_IF_NOT_FOUND`.
90-
* @deprecated use object-based flags (`InjectOptions`) instead.
91-
*/
92-
abstract get<T>(token: ProviderToken<T>, notFoundValue?: T, flags?: InjectFlags): T;
74+
abstract get<T>(token: ProviderToken<T>, notFoundValue?: T, options?: InjectOptions): T;
9375
/**
9476
* @deprecated from v4.0.0 use ProviderToken<T>
9577
* @suppress {duplicate}

packages/core/src/di/injector_compatibility.ts

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export class RetrievingInjector implements PrimitivesInjector {
5454
retrieve<T>(token: PrimitivesInjectionToken<T>, options: unknown): T | NotFound {
5555
const flags: InjectFlags =
5656
convertToBitFlags(options as InjectOptions | undefined) || InjectFlags.Default;
57-
return this.injector.get(
57+
return (this.injector as BackwardsCompatibleInjector).get(
5858
token as unknown as InjectionToken<T>,
5959
// When a dependency is requested with an optional flag, DI returns null as the default value.
6060
flags & InjectFlags.Optional ? null : undefined,
@@ -69,6 +69,14 @@ const NEW_LINE = /\n/gm;
6969
const NO_NEW_LINE = 'ɵ';
7070
export const SOURCE = '__source';
7171

72+
/**
73+
* Temporary type to allow internal symbols to use inject flags. This should be
74+
* removed once we consolidate the flags and the object literal approach.
75+
*/
76+
export type BackwardsCompatibleInjector = Injector & {
77+
get<T>(token: ProviderToken<T>, notFoundValue?: T, options?: InjectFlags | InjectOptions): T;
78+
};
79+
7280
export function injectInjectorOnly<T>(token: ProviderToken<T>): T;
7381
export function injectInjectorOnly<T>(token: ProviderToken<T>, flags?: InjectFlags): T | null;
7482
export function injectInjectorOnly<T>(
@@ -150,17 +158,6 @@ Please check that 1) the type for the parameter at index ${index} is correct and
150158
* @publicApi
151159
*/
152160
export function inject<T>(token: ProviderToken<T>): T;
153-
/**
154-
* @param token A token that represents a dependency that should be injected.
155-
* @param flags Control how injection is executed. The flags correspond to injection strategies that
156-
* can be specified with parameter decorators `@Host`, `@Self`, `@SkipSelf`, and `@Optional`.
157-
* @returns the injected value if operation is successful, `null` otherwise.
158-
* @throws if called outside of a supported context.
159-
*
160-
* @publicApi
161-
* @deprecated prefer an options object instead of `InjectFlags`
162-
*/
163-
export function inject<T>(token: ProviderToken<T>, flags?: InjectFlags): T | null;
164161
/**
165162
* @param token A token that represents a dependency that should be injected.
166163
* @param options Control how injection is executed. Options correspond to injection strategies
@@ -274,13 +271,10 @@ export function inject(token: HostAttributeToken, options: {optional: false}): s
274271
*
275272
* @publicApi
276273
*/
277-
export function inject<T>(
278-
token: ProviderToken<T> | HostAttributeToken,
279-
flags: InjectFlags | InjectOptions = InjectFlags.Default,
280-
) {
274+
export function inject<T>(token: ProviderToken<T> | HostAttributeToken, options?: InjectOptions) {
281275
// The `as any` here _shouldn't_ be necessary, but without it JSCompiler
282276
// throws a disambiguation error due to the multiple signatures.
283-
return ɵɵinject(token as any, convertToBitFlags(flags));
277+
return ɵɵinject(token as any, convertToBitFlags(options));
284278
}
285279

286280
// Converts object-based DI flags (`InjectOptions`) to bit flags (`InjectFlags`).

packages/core/src/di/r3_injector.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {setInjectImplementation} from './inject_switch';
3636
import {InjectionToken} from './injection_token';
3737
import type {Injector} from './injector';
3838
import {
39+
BackwardsCompatibleInjector,
3940
catchInjectorError,
4041
convertToBitFlags,
4142
injectArgs,
@@ -152,13 +153,6 @@ export abstract class EnvironmentInjector implements Injector {
152153
* @throws When the `notFoundValue` is `undefined` or `Injector.THROW_IF_NOT_FOUND`.
153154
*/
154155
abstract get<T>(token: ProviderToken<T>, notFoundValue?: T, options?: InjectOptions): T;
155-
/**
156-
* Retrieves an instance from the injector based on the provided token.
157-
* @returns The instance from the injector if defined, otherwise the `notFoundValue`.
158-
* @throws When the `notFoundValue` is `undefined` or `Injector.THROW_IF_NOT_FOUND`.
159-
* @deprecated use object-based flags (`InjectOptions`) instead.
160-
*/
161-
abstract get<T>(token: ProviderToken<T>, notFoundValue?: T, flags?: InjectFlags): T;
162156
/**
163157
* @deprecated from v4.0.0 use ProviderToken<T>
164158
* @suppress {duplicate}
@@ -238,18 +232,18 @@ export class R3Injector extends EnvironmentInjector implements PrimitivesInjecto
238232
this.scopes.add(record.value as InjectorScope);
239233
}
240234

241-
this.injectorDefTypes = new Set(this.get(INJECTOR_DEF_TYPES, EMPTY_ARRAY, InjectFlags.Self));
235+
this.injectorDefTypes = new Set(this.get(INJECTOR_DEF_TYPES, EMPTY_ARRAY, {self: true}));
242236
}
243237

244238
retrieve<T>(token: PrimitivesInjectionToken<T>, options?: unknown): T | NotFound {
245239
const flags: InjectFlags =
246240
convertToBitFlags(options as InjectOptions | undefined) || InjectFlags.Default;
247-
return this.get(
241+
return (this as BackwardsCompatibleInjector).get(
248242
token as unknown as InjectionToken<T>,
249243
// When a dependency is requested with an optional flag, DI returns null as the default value.
250244
flags & InjectFlags.Optional ? null : undefined,
251245
flags,
252-
);
246+
)!;
253247
}
254248

255249
/**
@@ -314,15 +308,15 @@ export class R3Injector extends EnvironmentInjector implements PrimitivesInjecto
314308
override get<T>(
315309
token: ProviderToken<T>,
316310
notFoundValue: any = THROW_IF_NOT_FOUND,
317-
flags: InjectFlags | InjectOptions = InjectFlags.Default,
311+
options?: InjectOptions,
318312
): T {
319313
assertNotDestroyed(this);
320314

321315
if (token.hasOwnProperty(NG_ENV_ID)) {
322316
return (token as any)[NG_ENV_ID](this);
323317
}
324318

325-
flags = convertToBitFlags(flags) as InjectFlags;
319+
const flags = convertToBitFlags(options) as InjectFlags;
326320

327321
// Set the injection context.
328322
let prevInjectContext: InjectorProfilerContext;
@@ -403,7 +397,7 @@ export class R3Injector extends EnvironmentInjector implements PrimitivesInjecto
403397
}
404398

405399
try {
406-
const initializers = this.get(ENVIRONMENT_INITIALIZER, EMPTY_ARRAY, InjectFlags.Self);
400+
const initializers = this.get(ENVIRONMENT_INITIALIZER, EMPTY_ARRAY, {self: true});
407401
if (ngDevMode && !Array.isArray(initializers)) {
408402
throw new RuntimeError(
409403
RuntimeErrorCode.INVALID_MULTI_PROVIDER,

packages/core/src/i18n/tokens.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
import {InjectionToken} from '../di/injection_token';
1010
import {inject} from '../di/injector_compatibility';
11-
import {InjectFlags} from '../di/interface/injector';
1211

1312
import {DEFAULT_LOCALE_ID, USD_CURRENCY_CODE} from './localization';
1413

@@ -69,8 +68,7 @@ export function getGlobalLocale(): string {
6968
*/
7069
export const LOCALE_ID: InjectionToken<string> = new InjectionToken(ngDevMode ? 'LocaleId' : '', {
7170
providedIn: 'root',
72-
factory: () =>
73-
inject(LOCALE_ID, InjectFlags.Optional | InjectFlags.SkipSelf) || getGlobalLocale(),
71+
factory: () => inject(LOCALE_ID, {optional: true, skipSelf: true}) || getGlobalLocale(),
7472
});
7573

7674
/**

packages/core/src/render3/chained_injector.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
*/
88

99
import {Injector} from '../di/injector';
10-
import {convertToBitFlags} from '../di/injector_compatibility';
11-
import {InjectFlags, InjectOptions} from '../di/interface/injector';
10+
import {InjectOptions} from '../di/interface/injector';
1211
import {ProviderToken} from '../di/provider_token';
1312
import {NOT_FOUND_CHECK_ONLY_ELEMENT_INJECTOR} from '../view/provider_flags';
1413

@@ -22,12 +21,11 @@ export class ChainedInjector implements Injector {
2221
public parentInjector: Injector,
2322
) {}
2423

25-
get<T>(token: ProviderToken<T>, notFoundValue?: T, flags?: InjectFlags | InjectOptions): T {
26-
flags = convertToBitFlags(flags);
24+
get<T>(token: ProviderToken<T>, notFoundValue?: T, options?: InjectOptions): T {
2725
const value = this.injector.get<T | typeof NOT_FOUND_CHECK_ONLY_ELEMENT_INJECTOR>(
2826
token,
2927
NOT_FOUND_CHECK_ONLY_ELEMENT_INJECTOR,
30-
flags,
28+
options,
3129
);
3230

3331
if (
@@ -42,6 +40,6 @@ export class ChainedInjector implements Injector {
4240
return value as T;
4341
}
4442

45-
return this.parentInjector.get(token, notFoundValue, flags);
43+
return this.parentInjector.get(token, notFoundValue, options);
4644
}
4745
}

packages/core/src/render3/di.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import {isForwardRef, resolveForwardRef} from '../di/forward_ref';
1010
import {injectRootLimpMode, setInjectImplementation} from '../di/inject_switch';
1111
import {Injector} from '../di/injector';
12-
import {convertToBitFlags} from '../di/injector_compatibility';
12+
import {BackwardsCompatibleInjector, convertToBitFlags} from '../di/injector_compatibility';
1313
import {InjectorMarkers} from '../di/injector_marker';
1414
import {InjectFlags, InjectOptions} from '../di/interface/injector';
1515
import {ProviderToken} from '../di/provider_token';
@@ -419,7 +419,11 @@ function lookupTokenUsingModuleInjector<T>(
419419
const previousInjectImplementation = setInjectImplementation(undefined);
420420
try {
421421
if (moduleInjector) {
422-
return moduleInjector.get(token, notFoundValue, flags & InjectFlags.Optional);
422+
return (moduleInjector as BackwardsCompatibleInjector).get(
423+
token,
424+
notFoundValue,
425+
flags & InjectFlags.Optional,
426+
);
423427
} else {
424428
return injectRootLimpMode(token, notFoundValue, flags & InjectFlags.Optional);
425429
}
@@ -986,7 +990,7 @@ function lookupTokenUsingEmbeddedInjector<T>(
986990
// Before we go to the next LView, check if the token exists on the current embedded injector.
987991
const embeddedViewInjector = currentLView[EMBEDDED_VIEW_INJECTOR];
988992
if (embeddedViewInjector) {
989-
const embeddedViewInjectorValue = embeddedViewInjector.get(
993+
const embeddedViewInjectorValue = (embeddedViewInjector as BackwardsCompatibleInjector).get(
990994
token,
991995
NOT_FOUND as T | {},
992996
flags,

0 commit comments

Comments
 (0)