Skip to content

Commit 2563f39

Browse files
Revert "refactor(core): remove unused injection props (angular#59209)" (angular#59386)
This reverts commit 91b5388. PR Close angular#59386
1 parent e0401ec commit 2563f39

File tree

14 files changed

+73
-6
lines changed

14 files changed

+73
-6
lines changed

packages/compiler-cli/integrationtest/bazel/injectable_def/app/test/app_spec.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,33 @@ describe('ngInjectableDef Bazel Integration', () => {
158158
expect(() => TestBed.inject(ChildService).value).toThrowError(/ChildService/);
159159
});
160160

161+
it('uses legacy `ngInjectable` property even if it inherits from a class that has `ɵprov` property', () => {
162+
@Injectable({
163+
providedIn: 'root',
164+
useValue: new ParentService('parent'),
165+
})
166+
class ParentService {
167+
constructor(public value: string) {}
168+
}
169+
170+
// ChildServices extends ParentService but does not have @Injectable
171+
class ChildService extends ParentService {
172+
constructor(value: string) {
173+
super(value);
174+
}
175+
static ngInjectableDef = {
176+
providedIn: 'root',
177+
factory: () => new ChildService('child'),
178+
token: ChildService,
179+
};
180+
}
181+
182+
TestBed.configureTestingModule({});
183+
// We are asserting that system throws an error, rather than taking the inherited
184+
// annotation.
185+
expect(TestBed.inject(ChildService).value).toEqual('child');
186+
});
187+
161188
it('NgModule injector understands requests for INJECTABLE', () => {
162189
TestBed.configureTestingModule({
163190
providers: [{provide: 'foo', useValue: 'bar'}],

packages/core/src/di/interface/defs.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ export function ɵɵdefineInjector(options: {providers?: any[]; imports?: any[]}
212212
* @param type A type which may have its own (non-inherited) `ɵprov`.
213213
*/
214214
export function getInjectableDef<T>(type: any): ɵɵInjectableDeclaration<T> | null {
215-
return getOwnDefinition(type, NG_PROV_DEF);
215+
return getOwnDefinition(type, NG_PROV_DEF) || getOwnDefinition(type, NG_INJECTABLE_DEF);
216216
}
217217

218218
export function isInjectable(type: any): boolean {
@@ -224,8 +224,7 @@ export function isInjectable(type: any): boolean {
224224
* class of `type`.
225225
*/
226226
function getOwnDefinition<T>(type: any, field: string): ɵɵInjectableDeclaration<T> | null {
227-
// if the ɵprov prop exist but is undefined we still want to return null
228-
return (type.hasOwnProperty(field) && type[field]) || null;
227+
return type.hasOwnProperty(field) ? type[field] : null;
229228
}
230229

231230
/**
@@ -237,8 +236,7 @@ function getOwnDefinition<T>(type: any, field: string): ɵɵInjectableDeclaratio
237236
* scenario if we find the `ɵprov` on an ancestor only.
238237
*/
239238
export function getInheritedInjectableDef<T>(type: any): ɵɵInjectableDeclaration<T> | null {
240-
// if the ɵprov prop exist but is undefined we still want to return null
241-
const def = type?.[NG_PROV_DEF] ?? null;
239+
const def = type && (type[NG_PROV_DEF] || type[NG_INJECTABLE_DEF]);
242240

243241
if (def) {
244242
ngDevMode &&
@@ -258,8 +256,14 @@ export function getInheritedInjectableDef<T>(type: any): ɵɵInjectableDeclarati
258256
* @param type type which may have an injector def (`ɵinj`)
259257
*/
260258
export function getInjectorDef<T>(type: any): ɵɵInjectorDef<T> | null {
261-
return type && type.hasOwnProperty(NG_INJ_DEF) ? (type as any)[NG_INJ_DEF] : null;
259+
return type && (type.hasOwnProperty(NG_INJ_DEF) || type.hasOwnProperty(NG_INJECTOR_DEF))
260+
? (type as any)[NG_INJ_DEF]
261+
: null;
262262
}
263263

264264
export const NG_PROV_DEF = getClosureSafeProperty({ɵprov: getClosureSafeProperty});
265265
export const NG_INJ_DEF = getClosureSafeProperty({ɵinj: getClosureSafeProperty});
266+
267+
// We need to keep these around so we can read off old defs if new defs are unavailable
268+
export const NG_INJECTABLE_DEF = getClosureSafeProperty({ngInjectableDef: getClosureSafeProperty});
269+
export const NG_INJECTOR_DEF = getClosureSafeProperty({ngInjectorDef: getClosureSafeProperty});

packages/core/test/bundling/animations-standalone/bundle.golden_symbols.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@
103103
"NG_ELEMENT_ID",
104104
"NG_ENV_ID",
105105
"NG_FACTORY_DEF",
106+
"NG_INJECTABLE_DEF",
107+
"NG_INJECTOR_DEF",
106108
"NG_INJ_DEF",
107109
"NG_PIPE_DEF",
108110
"NG_PROV_DEF",
@@ -323,6 +325,7 @@
323325
"getOrCreateNodeInjectorForNode",
324326
"getOrCreateTNode",
325327
"getOrSetDefaultValue",
328+
"getOwnDefinition",
326329
"getParentElement",
327330
"getParentInjectorIndex",
328331
"getParentInjectorLocation",

packages/core/test/bundling/animations/bundle.golden_symbols.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@
110110
"NG_ELEMENT_ID",
111111
"NG_ENV_ID",
112112
"NG_FACTORY_DEF",
113+
"NG_INJECTABLE_DEF",
114+
"NG_INJECTOR_DEF",
113115
"NG_INJ_DEF",
114116
"NG_MOD_DEF",
115117
"NG_PIPE_DEF",
@@ -346,6 +348,7 @@
346348
"getOrCreateNodeInjectorForNode",
347349
"getOrCreateTNode",
348350
"getOrSetDefaultValue",
351+
"getOwnDefinition",
349352
"getParentElement",
350353
"getParentInjectorIndex",
351354
"getParentInjectorLocation",

packages/core/test/bundling/cyclic_import/bundle.golden_symbols.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@
7979
"NG_ELEMENT_ID",
8080
"NG_ENV_ID",
8181
"NG_FACTORY_DEF",
82+
"NG_INJECTABLE_DEF",
83+
"NG_INJECTOR_DEF",
8284
"NG_INJ_DEF",
8385
"NG_MOD_DEF",
8486
"NG_PIPE_DEF",
@@ -273,6 +275,7 @@
273275
"getOrCreateInjectable",
274276
"getOrCreateNodeInjectorForNode",
275277
"getOrCreateTNode",
278+
"getOwnDefinition",
276279
"getParentInjectorIndex",
277280
"getParentInjectorLocation",
278281
"getParentInjectorView",

packages/core/test/bundling/defer/bundle.golden_symbols.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@
105105
"NG_ELEMENT_ID",
106106
"NG_ENV_ID",
107107
"NG_FACTORY_DEF",
108+
"NG_INJECTABLE_DEF",
109+
"NG_INJECTOR_DEF",
108110
"NG_INJ_DEF",
109111
"NG_PIPE_DEF",
110112
"NG_PROV_DEF",
@@ -328,6 +330,7 @@
328330
"getOrCreateInjectable",
329331
"getOrCreateNodeInjectorForNode",
330332
"getOrCreateTNode",
333+
"getOwnDefinition",
331334
"getParentInjectorIndex",
332335
"getParentInjectorLocation",
333336
"getParentInjectorView",

packages/core/test/bundling/forms_reactive/bundle.golden_symbols.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@
112112
"NG_ELEMENT_ID",
113113
"NG_ENV_ID",
114114
"NG_FACTORY_DEF",
115+
"NG_INJECTABLE_DEF",
116+
"NG_INJECTOR_DEF",
115117
"NG_INJ_DEF",
116118
"NG_MODEL_WITH_FORM_CONTROL_WARNING",
117119
"NG_MOD_DEF",
@@ -391,6 +393,7 @@
391393
"getOrCreateNodeInjectorForNode",
392394
"getOrCreateTNode",
393395
"getOrCreateViewRefs",
396+
"getOwnDefinition",
394397
"getParentInjectorIndex",
395398
"getParentInjectorLocation",
396399
"getParentInjectorView",

packages/core/test/bundling/forms_template_driven/bundle.golden_symbols.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@
105105
"NG_ELEMENT_ID",
106106
"NG_ENV_ID",
107107
"NG_FACTORY_DEF",
108+
"NG_INJECTABLE_DEF",
109+
"NG_INJECTOR_DEF",
108110
"NG_INJ_DEF",
109111
"NG_MOD_DEF",
110112
"NG_PIPE_DEF",
@@ -377,6 +379,7 @@
377379
"getOrCreateNodeInjectorForNode",
378380
"getOrCreateTNode",
379381
"getOrCreateViewRefs",
382+
"getOwnDefinition",
380383
"getParentInjectorIndex",
381384
"getParentInjectorLocation",
382385
"getParentInjectorView",

packages/core/test/bundling/hello_world/bundle.golden_symbols.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353
"NG_ELEMENT_ID",
5454
"NG_ENV_ID",
5555
"NG_FACTORY_DEF",
56+
"NG_INJECTABLE_DEF",
57+
"NG_INJECTOR_DEF",
5658
"NG_INJ_DEF",
5759
"NG_MOD_DEF",
5860
"NG_PROV_DEF",
@@ -213,6 +215,7 @@
213215
"getOrCreateInjectable",
214216
"getOrCreateNodeInjectorForNode",
215217
"getOrCreateTNode",
218+
"getOwnDefinition",
216219
"getParentInjectorIndex",
217220
"getParentInjectorLocation",
218221
"getParentInjectorView",

packages/core/test/bundling/hydration/bundle.golden_symbols.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@
8585
"NG_ELEMENT_ID",
8686
"NG_ENV_ID",
8787
"NG_FACTORY_DEF",
88+
"NG_INJECTABLE_DEF",
89+
"NG_INJECTOR_DEF",
8890
"NG_INJ_DEF",
8991
"NG_PIPE_DEF",
9092
"NG_PROV_DEF",
@@ -280,6 +282,7 @@
280282
"getOrCreateInjectable",
281283
"getOrCreateNodeInjectorForNode",
282284
"getOrCreateTNode",
285+
"getOwnDefinition",
283286
"getParentInjectorIndex",
284287
"getParentInjectorLocation",
285288
"getParentInjectorView",

0 commit comments

Comments
 (0)