Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: consistently rewrite Injector to INJECTOR #23008

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {Component, Injectable, NgModule} from '@angular/core';
import {Component, INJECTOR, Injectable, NgModule} from '@angular/core';
import {TestBed} from '@angular/core/testing';
import {renderModuleFactory} from '@angular/platform-server';
import {BasicAppModuleNgFactory} from 'app_built/src/basic.ngfactory';
Expand Down Expand Up @@ -142,4 +142,28 @@ describe('ngInjectableDef Bazel Integration', () => {
TestBed.configureTestingModule({});
expect(TestBed.get(Service).value).toEqual(true);
});

it('NgModule injector understands requests for INJECTABLE', () => {
TestBed.configureTestingModule({
providers: [{provide: 'foo', useValue: 'bar'}],
});
expect(TestBed.get(INJECTOR).get('foo')).toEqual('bar');
});

it('Component injector understands requests for INJECTABLE', () => {
@Component({
selector: 'test-cmp',
template: 'test',
providers: [{provide: 'foo', useValue: 'bar'}],
})
class TestCmp {
}

TestBed.configureTestingModule({
declarations: [TestCmp],
});

const fixture = TestBed.createComponent(TestCmp);
expect(fixture.componentRef.injector.get(INJECTOR).get('foo')).toEqual('bar');
});
});
2 changes: 1 addition & 1 deletion packages/compiler/src/injectable_compiler.ts
Expand Up @@ -63,7 +63,7 @@ export class InjectableCompiler {
let tokenExpr: o.Expression;
if (typeof token === 'string') {
tokenExpr = o.literal(token);
} else if (token === this.tokenInjector && this.alwaysGenerateDef) {
} else if (token === this.tokenInjector) {
tokenExpr = o.importExpr(Identifiers.INJECTOR);
} else {
tokenExpr = ctx.importExpr(token);
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/di/injector.ts
Expand Up @@ -145,7 +145,7 @@ export class StaticInjector implements Injector {
records.set(
Injector, <Record>{token: Injector, fn: IDENT, deps: EMPTY, value: this, useNew: false});
records.set(
INJECTOR, <Record>{token: Injector, fn: IDENT, deps: EMPTY, value: this, useNew: false});
INJECTOR, <Record>{token: INJECTOR, fn: IDENT, deps: EMPTY, value: this, useNew: false});
recursivelyProcessProviders(records, providers);
}

Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/view/ng_module.ts
Expand Up @@ -19,6 +19,7 @@ import {splitDepsDsl, tokenKey} from './util';
const UNDEFINED_VALUE = new Object();

const InjectorRefTokenKey = tokenKey(Injector);
const INJECTORRefTokenKey = tokenKey(INJECTOR);
const NgModuleRefTokenKey = tokenKey(NgModuleRef);

export function moduleProvideDef(
Expand Down Expand Up @@ -86,6 +87,7 @@ export function resolveNgModuleDep(
const tokenKey = depDef.tokenKey;
switch (tokenKey) {
case InjectorRefTokenKey:
case INJECTORRefTokenKey:
case NgModuleRefTokenKey:
return data;
}
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/view/provider.ts
Expand Up @@ -7,12 +7,13 @@
*/

import {ChangeDetectorRef, SimpleChange, SimpleChanges, WrappedValue} from '../change_detection/change_detection';
import {Injector, resolveForwardRef} from '../di';
import {INJECTOR, Injector, resolveForwardRef} from '../di';
import {ElementRef} from '../linker/element_ref';
import {TemplateRef} from '../linker/template_ref';
import {ViewContainerRef} from '../linker/view_container_ref';
import {Renderer as RendererV1, Renderer2} from '../render/api';
import {stringify} from '../util';

import {createChangeDetectorRef, createInjector, createRendererV1} from './refs';
import {BindingDef, BindingFlags, DepDef, DepFlags, NodeDef, NodeFlags, OutputDef, OutputType, ProviderData, QueryValueType, Services, ViewData, ViewFlags, ViewState, asElementData, asProviderData, shouldCallLifecycleInitHook} from './types';
import {calcBindingFlags, checkBinding, dispatchEvent, isComponentView, splitDepsDsl, splitMatchedQueriesDsl, tokenKey, viewParentEl} from './util';
Expand All @@ -24,6 +25,7 @@ const ViewContainerRefTokenKey = tokenKey(ViewContainerRef);
const TemplateRefTokenKey = tokenKey(TemplateRef);
const ChangeDetectorRefTokenKey = tokenKey(ChangeDetectorRef);
const InjectorRefTokenKey = tokenKey(Injector);
const INJECTORRefTokenKey = tokenKey(INJECTOR);

export function directiveDef(
checkIndex: number, flags: NodeFlags,
Expand Down Expand Up @@ -373,6 +375,7 @@ export function resolveDep(
return createChangeDetectorRef(cdView);
}
case InjectorRefTokenKey:
case INJECTORRefTokenKey:
return createInjector(searchView, elDef);
default:
const providerDef =
Expand Down