feat(ivy): require 'token' for ngInjectableDef (#30855)

The compiler generates a 'token' field when it emits an ngInjectableDef,
but this field was not required by defineInjectable or the InjectableDef
interface, nor was it added by InjectionToken.

This commit makes 'token' required and adds it where missing.

PR Close #30855
This commit is contained in:
Alex Rickabaugh 2019-06-07 10:12:07 -07:00 committed by Andrew Kushnir
parent b0866769b0
commit a4b4f35533
16 changed files with 77 additions and 14 deletions

View File

@ -22,6 +22,7 @@ export abstract class ViewportScroller {
// See #23917 // See #23917
/** @nocollapse */ /** @nocollapse */
static ngInjectableDef = ɵɵdefineInjectable({ static ngInjectableDef = ɵɵdefineInjectable({
token: ViewportScroller,
providedIn: 'root', providedIn: 'root',
factory: () => new BrowserViewportScroller(ɵɵinject(DOCUMENT), window, ɵɵinject(ErrorHandler)) factory: () => new BrowserViewportScroller(ɵɵinject(DOCUMENT), window, ɵɵinject(ErrorHandler))
}); });

View File

@ -144,6 +144,7 @@ export interface IterableDifferFactory {
export class IterableDiffers { export class IterableDiffers {
/** @nocollapse */ /** @nocollapse */
static ngInjectableDef = ɵɵdefineInjectable({ static ngInjectableDef = ɵɵdefineInjectable({
token: IterableDiffers,
providedIn: 'root', providedIn: 'root',
factory: () => new IterableDiffers([new DefaultIterableDifferFactory()]) factory: () => new IterableDiffers([new DefaultIterableDifferFactory()])
}); });

View File

@ -119,6 +119,7 @@ export interface KeyValueDifferFactory {
export class KeyValueDiffers { export class KeyValueDiffers {
/** @nocollapse */ /** @nocollapse */
static ngInjectableDef = ɵɵdefineInjectable({ static ngInjectableDef = ɵɵdefineInjectable({
token: KeyValueDiffers,
providedIn: 'root', providedIn: 'root',
factory: () => new KeyValueDiffers([new DefaultKeyValueDifferFactory()]) factory: () => new KeyValueDiffers([new DefaultKeyValueDifferFactory()])
}); });

View File

@ -90,6 +90,7 @@ function render2CompileInjectable(
options: {providedIn?: Type<any>| 'root' | null} & InjectableProvider): void { options: {providedIn?: Type<any>| 'root' | null} & InjectableProvider): void {
if (options && options.providedIn !== undefined && !getInjectableDef(injectableType)) { if (options && options.providedIn !== undefined && !getInjectableDef(injectableType)) {
injectableType.ngInjectableDef = ɵɵdefineInjectable({ injectableType.ngInjectableDef = ɵɵdefineInjectable({
token: injectableType,
providedIn: options.providedIn, providedIn: options.providedIn,
factory: convertInjectableProviderToFactory(injectableType, options), factory: convertInjectableProviderToFactory(injectableType, options),
}); });

View File

@ -68,6 +68,7 @@ export class InjectionToken<T> {
(this as any).__NG_ELEMENT_ID__ = options; (this as any).__NG_ELEMENT_ID__ = options;
} else if (options !== undefined) { } else if (options !== undefined) {
this.ngInjectableDef = ɵɵdefineInjectable({ this.ngInjectableDef = ɵɵdefineInjectable({
token: this,
providedIn: options.providedIn || 'root', providedIn: options.providedIn || 'root',
factory: options.factory, factory: options.factory,
}); });

View File

@ -89,6 +89,7 @@ export abstract class Injector {
/** @nocollapse */ /** @nocollapse */
static ngInjectableDef = ɵɵdefineInjectable({ static ngInjectableDef = ɵɵdefineInjectable({
token: Injector,
providedIn: 'any' as any, providedIn: 'any' as any,
factory: () => ɵɵinject(INJECTOR), factory: () => ɵɵinject(INJECTOR),
}); });

View File

@ -37,6 +37,13 @@ export interface ɵɵInjectableDef<T> {
*/ */
providedIn: InjectorType<any>|'root'|'any'|null; providedIn: InjectorType<any>|'root'|'any'|null;
/**
* The token to which this definition belongs.
*
* Note that this may not be the same as the type that the `factory` will create.
*/
token: unknown;
/** /**
* Factory method to execute to create an instance of the injectable. * Factory method to execute to create an instance of the injectable.
*/ */
@ -132,11 +139,13 @@ export interface InjectorTypeWithProviders<T> {
* @codeGenApi * @codeGenApi
*/ */
export function ɵɵdefineInjectable<T>(opts: { export function ɵɵdefineInjectable<T>(opts: {
token: unknown,
providedIn?: Type<any>| 'root' | 'any' | null, providedIn?: Type<any>| 'root' | 'any' | null,
factory: () => T, factory: () => T,
}): never { }): never {
return ({ return ({
providedIn: opts.providedIn as any || null, factory: opts.factory, value: undefined, token: opts.token, providedIn: opts.providedIn as any || null, factory: opts.factory,
value: undefined,
} as ɵɵInjectableDef<T>) as never; } as ɵɵInjectableDef<T>) as never;
} }

View File

@ -307,7 +307,7 @@ export function ɵɵdefineComponent<T>(componentDefinition: {
// be retrieved through the node injector, so this isn't a problem. // be retrieved through the node injector, so this isn't a problem.
if (!type.hasOwnProperty(NG_INJECTABLE_DEF)) { if (!type.hasOwnProperty(NG_INJECTABLE_DEF)) {
(type as any)[NG_INJECTABLE_DEF] = (type as any)[NG_INJECTABLE_DEF] =
ɵɵdefineInjectable<T>({factory: componentDefinition.factory as() => T}); ɵɵdefineInjectable<T>({token: type, factory: componentDefinition.factory as() => T});
} }
}) as never; }) as never;

View File

@ -10,6 +10,7 @@ import {Injector, ɵcreateInjector as createInjector, ɵɵdefineInjectable, ɵɵ
export class RootService { export class RootService {
static ngInjectableDef = ɵɵdefineInjectable({ static ngInjectableDef = ɵɵdefineInjectable({
token: RootService,
providedIn: 'root', providedIn: 'root',
factory: () => new RootService(), factory: () => new RootService(),
}); });
@ -17,6 +18,7 @@ export class RootService {
export class ScopedService { export class ScopedService {
static ngInjectableDef = ɵɵdefineInjectable({ static ngInjectableDef = ɵɵdefineInjectable({
token: ScopedService,
providedIn: null, providedIn: null,
factory: () => new ScopedService(), factory: () => new ScopedService(),
}); });

View File

@ -13,6 +13,7 @@ import {expect} from '@angular/platform-browser/testing/src/matchers';
describe('InjectorDef-based createInjector()', () => { describe('InjectorDef-based createInjector()', () => {
class CircularA { class CircularA {
static ngInjectableDef = ɵɵdefineInjectable({ static ngInjectableDef = ɵɵdefineInjectable({
token: CircularA,
providedIn: null, providedIn: null,
factory: () => ɵɵinject(CircularB), factory: () => ɵɵinject(CircularB),
}); });
@ -20,6 +21,7 @@ describe('InjectorDef-based createInjector()', () => {
class CircularB { class CircularB {
static ngInjectableDef = ɵɵdefineInjectable({ static ngInjectableDef = ɵɵdefineInjectable({
token: CircularB,
providedIn: null, providedIn: null,
factory: () => ɵɵinject(CircularA), factory: () => ɵɵinject(CircularA),
}); });
@ -27,6 +29,7 @@ describe('InjectorDef-based createInjector()', () => {
class Service { class Service {
static ngInjectableDef = ɵɵdefineInjectable({ static ngInjectableDef = ɵɵdefineInjectable({
token: Service,
providedIn: null, providedIn: null,
factory: () => new Service(), factory: () => new Service(),
}); });
@ -34,6 +37,7 @@ describe('InjectorDef-based createInjector()', () => {
class OptionalService { class OptionalService {
static ngInjectableDef = ɵɵdefineInjectable({ static ngInjectableDef = ɵɵdefineInjectable({
token: OptionalService,
providedIn: null, providedIn: null,
factory: () => new OptionalService(), factory: () => new OptionalService(),
}); });
@ -56,6 +60,7 @@ describe('InjectorDef-based createInjector()', () => {
constructor(readonly service: Service) {} constructor(readonly service: Service) {}
static ngInjectableDef = ɵɵdefineInjectable({ static ngInjectableDef = ɵɵdefineInjectable({
token: ServiceWithDep,
providedIn: null, providedIn: null,
factory: () => new ServiceWithDep(ɵɵinject(Service)), factory: () => new ServiceWithDep(ɵɵinject(Service)),
}); });
@ -65,6 +70,7 @@ describe('InjectorDef-based createInjector()', () => {
constructor(@Optional() readonly service: OptionalService|null) {} constructor(@Optional() readonly service: OptionalService|null) {}
static ngInjectableDef = ɵɵdefineInjectable({ static ngInjectableDef = ɵɵdefineInjectable({
token: ServiceWithOptionalDep,
providedIn: null, providedIn: null,
factory: () => new ServiceWithOptionalDep(ɵɵinject(OptionalService, InjectFlags.Optional)), factory: () => new ServiceWithOptionalDep(ɵɵinject(OptionalService, InjectFlags.Optional)),
}); });
@ -74,6 +80,7 @@ describe('InjectorDef-based createInjector()', () => {
constructor(readonly service: Service) {} constructor(readonly service: Service) {}
static ngInjectableDef = ɵɵdefineInjectable({ static ngInjectableDef = ɵɵdefineInjectable({
token: ServiceWithMissingDep,
providedIn: null, providedIn: null,
factory: () => new ServiceWithMissingDep(ɵɵinject(Service)), factory: () => new ServiceWithMissingDep(ɵɵinject(Service)),
}); });
@ -83,6 +90,7 @@ describe('InjectorDef-based createInjector()', () => {
constructor(readonly locale: string[]) {} constructor(readonly locale: string[]) {}
static ngInjectableDef = ɵɵdefineInjectable({ static ngInjectableDef = ɵɵdefineInjectable({
token: ServiceWithMultiDep,
providedIn: null, providedIn: null,
factory: () => new ServiceWithMultiDep(ɵɵinject(LOCALE)), factory: () => new ServiceWithMultiDep(ɵɵinject(LOCALE)),
}); });
@ -90,6 +98,7 @@ describe('InjectorDef-based createInjector()', () => {
class ServiceTwo { class ServiceTwo {
static ngInjectableDef = ɵɵdefineInjectable({ static ngInjectableDef = ɵɵdefineInjectable({
token: ServiceTwo,
providedIn: null, providedIn: null,
factory: () => new ServiceTwo(), factory: () => new ServiceTwo(),
}); });
@ -98,6 +107,7 @@ describe('InjectorDef-based createInjector()', () => {
let deepServiceDestroyed = false; let deepServiceDestroyed = false;
class DeepService { class DeepService {
static ngInjectableDef = ɵɵdefineInjectable({ static ngInjectableDef = ɵɵdefineInjectable({
token: DeepService,
providedIn: null, providedIn: null,
factory: () => new DeepService(), factory: () => new DeepService(),
}); });
@ -108,6 +118,7 @@ describe('InjectorDef-based createInjector()', () => {
let eagerServiceCreated: boolean = false; let eagerServiceCreated: boolean = false;
class EagerService { class EagerService {
static ngInjectableDef = ɵɵdefineInjectable({ static ngInjectableDef = ɵɵdefineInjectable({
token: EagerService,
providedIn: undefined, providedIn: undefined,
factory: () => new EagerService(), factory: () => new EagerService(),
}); });
@ -203,6 +214,7 @@ describe('InjectorDef-based createInjector()', () => {
let scopedServiceDestroyed = false; let scopedServiceDestroyed = false;
class ScopedService { class ScopedService {
static ngInjectableDef = ɵɵdefineInjectable({ static ngInjectableDef = ɵɵdefineInjectable({
token: ScopedService,
providedIn: Module, providedIn: Module,
factory: () => new ScopedService(), factory: () => new ScopedService(),
}); });
@ -212,6 +224,7 @@ describe('InjectorDef-based createInjector()', () => {
class WrongScopeService { class WrongScopeService {
static ngInjectableDef = ɵɵdefineInjectable({ static ngInjectableDef = ɵɵdefineInjectable({
token: WrongScopeService,
providedIn: OtherModule, providedIn: OtherModule,
factory: () => new WrongScopeService(), factory: () => new WrongScopeService(),
}); });

View File

@ -1397,6 +1397,7 @@ function declareTests(config?: {useJit: boolean}) {
class Bar { class Bar {
static ngInjectableDef: ɵɵInjectableDef<Bar> = ɵɵdefineInjectable({ static ngInjectableDef: ɵɵInjectableDef<Bar> = ɵɵdefineInjectable({
token: Bar,
factory: () => new Bar(), factory: () => new Bar(),
providedIn: SomeModule, providedIn: SomeModule,
}); });
@ -1429,6 +1430,7 @@ function declareTests(config?: {useJit: boolean}) {
class Bar { class Bar {
static ngInjectableDef: ɵɵInjectableDef<Bar> = ɵɵdefineInjectable({ static ngInjectableDef: ɵɵInjectableDef<Bar> = ɵɵdefineInjectable({
token: Bar,
factory: () => new Bar(), factory: () => new Bar(),
providedIn: SomeModule, providedIn: SomeModule,
}); });

View File

@ -63,8 +63,11 @@ describe('component', () => {
class MyService { class MyService {
constructor(public value: string) {} constructor(public value: string) {}
static ngInjectableDef = static ngInjectableDef = ɵɵdefineInjectable({
ɵɵdefineInjectable({providedIn: 'root', factory: () => new MyService('no-injector')}); token: MyService,
providedIn: 'root',
factory: () => new MyService('no-injector'),
});
} }
class MyComponent { class MyComponent {
constructor(public myService: MyService) {} constructor(public myService: MyService) {}

View File

@ -168,6 +168,7 @@ ivyEnabled && describe('render3 jit', () => {
it('compiles a module to an ngInjectorDef with the providers', () => { it('compiles a module to an ngInjectorDef with the providers', () => {
class Token { class Token {
static ngInjectableDef = ɵɵdefineInjectable({ static ngInjectableDef = ɵɵdefineInjectable({
token: Token,
providedIn: 'root', providedIn: 'root',
factory: () => 'default', factory: () => 'default',
}); });

View File

@ -58,8 +58,10 @@ describe('providers', () => {
public greet: string; public greet: string;
constructor(private provider: GreeterProvider) { this.greet = this.provider.provide(); } constructor(private provider: GreeterProvider) { this.greet = this.provider.provide(); }
static ngInjectableDef = static ngInjectableDef = ɵɵdefineInjectable({
ɵɵdefineInjectable({factory: () => new GreeterInj(ɵɵinject(GreeterProvider as any))}); token: GreeterInj,
factory: () => new GreeterInj(ɵɵinject(GreeterProvider as any)),
});
} }
it('TypeProvider', () => { it('TypeProvider', () => {
@ -814,8 +816,11 @@ describe('providers', () => {
it('should work with root', () => { it('should work with root', () => {
@Injectable({providedIn: 'root'}) @Injectable({providedIn: 'root'})
class FooForRoot { class FooForRoot {
static ngInjectableDef = static ngInjectableDef = ɵɵdefineInjectable({
ɵɵdefineInjectable({factory: () => new FooForRoot(), providedIn: 'root'}); token: FooForRoot,
factory: () => new FooForRoot(),
providedIn: 'root',
});
} }
expectProvidersScenario({ expectProvidersScenario({
@ -836,8 +841,11 @@ describe('providers', () => {
@Injectable({providedIn: MyModule}) @Injectable({providedIn: MyModule})
class FooForModule { class FooForModule {
static ngInjectableDef = static ngInjectableDef = ɵɵdefineInjectable({
ɵɵdefineInjectable({factory: () => new FooForModule(), providedIn: MyModule}); token: FooForModule,
factory: () => new FooForModule(),
providedIn: MyModule,
});
} }
expectProvidersScenario({ expectProvidersScenario({
@ -1153,8 +1161,10 @@ describe('providers', () => {
class MyService { class MyService {
constructor(public value: String) {} constructor(public value: String) {}
static ngInjectableDef = static ngInjectableDef = ɵɵdefineInjectable({
ɵɵdefineInjectable({factory: () => new MyService(ɵɵinject(String))}); token: MyService,
factory: () => new MyService(ɵɵinject(String)),
});
} }
expectProvidersScenario({ expectProvidersScenario({
@ -1171,7 +1181,10 @@ describe('providers', () => {
it('should make sure that parent service does not see overrides in child directives', () => { it('should make sure that parent service does not see overrides in child directives', () => {
class Greeter { class Greeter {
static ngInjectableDef = ɵɵdefineInjectable({factory: () => new Greeter(ɵɵinject(String))}); static ngInjectableDef = ɵɵdefineInjectable({
token: Greeter,
factory: () => new Greeter(ɵɵinject(String)),
});
constructor(public greeting: String) {} constructor(public greeting: String) {}
} }
@ -1213,7 +1226,10 @@ describe('providers', () => {
class SomeInj implements Some { class SomeInj implements Some {
constructor(public location: String) {} constructor(public location: String) {}
static ngInjectableDef = ɵɵdefineInjectable({factory: () => new SomeInj(ɵɵinject(String))}); static ngInjectableDef = ɵɵdefineInjectable({
token: SomeInj,
factory: () => new SomeInj(ɵɵinject(String)),
});
} }
@Component({ @Component({

View File

@ -28,6 +28,7 @@ class NotMyModule {}
class Bar { class Bar {
static ngInjectableDef: ɵɵInjectableDef<Bar> = ɵɵdefineInjectable({ static ngInjectableDef: ɵɵInjectableDef<Bar> = ɵɵdefineInjectable({
token: Bar,
factory: () => new Bar(), factory: () => new Bar(),
providedIn: MyModule, providedIn: MyModule,
}); });
@ -35,6 +36,7 @@ class Bar {
class Baz { class Baz {
static ngInjectableDef: ɵɵInjectableDef<Baz> = ɵɵdefineInjectable({ static ngInjectableDef: ɵɵInjectableDef<Baz> = ɵɵdefineInjectable({
token: Baz,
factory: () => new Baz(), factory: () => new Baz(),
providedIn: NotMyModule, providedIn: NotMyModule,
}); });
@ -44,6 +46,7 @@ class HasNormalDep {
constructor(public foo: Foo) {} constructor(public foo: Foo) {}
static ngInjectableDef: ɵɵInjectableDef<HasNormalDep> = ɵɵdefineInjectable({ static ngInjectableDef: ɵɵInjectableDef<HasNormalDep> = ɵɵdefineInjectable({
token: HasNormalDep,
factory: () => new HasNormalDep(inject(Foo)), factory: () => new HasNormalDep(inject(Foo)),
providedIn: MyModule, providedIn: MyModule,
}); });
@ -53,6 +56,7 @@ class HasDefinedDep {
constructor(public bar: Bar) {} constructor(public bar: Bar) {}
static ngInjectableDef: ɵɵInjectableDef<HasDefinedDep> = ɵɵdefineInjectable({ static ngInjectableDef: ɵɵInjectableDef<HasDefinedDep> = ɵɵdefineInjectable({
token: HasDefinedDep,
factory: () => new HasDefinedDep(inject(Bar)), factory: () => new HasDefinedDep(inject(Bar)),
providedIn: MyModule, providedIn: MyModule,
}); });
@ -62,6 +66,7 @@ class HasOptionalDep {
constructor(public baz: Baz|null) {} constructor(public baz: Baz|null) {}
static ngInjectableDef: ɵɵInjectableDef<HasOptionalDep> = ɵɵdefineInjectable({ static ngInjectableDef: ɵɵInjectableDef<HasOptionalDep> = ɵɵdefineInjectable({
token: HasOptionalDep,
factory: () => new HasOptionalDep(inject(Baz, InjectFlags.Optional)), factory: () => new HasOptionalDep(inject(Baz, InjectFlags.Optional)),
providedIn: MyModule, providedIn: MyModule,
}); });
@ -69,6 +74,7 @@ class HasOptionalDep {
class ChildDep { class ChildDep {
static ngInjectableDef: ɵɵInjectableDef<ChildDep> = ɵɵdefineInjectable({ static ngInjectableDef: ɵɵInjectableDef<ChildDep> = ɵɵdefineInjectable({
token: ChildDep,
factory: () => new ChildDep(), factory: () => new ChildDep(),
providedIn: MyChildModule, providedIn: MyChildModule,
}); });
@ -77,6 +83,7 @@ class ChildDep {
class FromChildWithOptionalDep { class FromChildWithOptionalDep {
constructor(public baz: Baz|null) {} constructor(public baz: Baz|null) {}
static ngInjectableDef: ɵɵInjectableDef<FromChildWithOptionalDep> = ɵɵdefineInjectable({ static ngInjectableDef: ɵɵInjectableDef<FromChildWithOptionalDep> = ɵɵdefineInjectable({
token: FromChildWithOptionalDep,
factory: () => new FromChildWithOptionalDep(inject(Baz, InjectFlags.Default)), factory: () => new FromChildWithOptionalDep(inject(Baz, InjectFlags.Default)),
providedIn: MyChildModule, providedIn: MyChildModule,
}); });
@ -87,6 +94,7 @@ class FromChildWithSkipSelfDep {
public skipSelfChildDep: ChildDep|null, public selfChildDep: ChildDep|null, public skipSelfChildDep: ChildDep|null, public selfChildDep: ChildDep|null,
public optionalSelfBar: Bar|null) {} public optionalSelfBar: Bar|null) {}
static ngInjectableDef: ɵɵInjectableDef<FromChildWithSkipSelfDep> = ɵɵdefineInjectable({ static ngInjectableDef: ɵɵInjectableDef<FromChildWithSkipSelfDep> = ɵɵdefineInjectable({
token: FromChildWithSkipSelfDep,
factory: () => new FromChildWithSkipSelfDep( factory: () => new FromChildWithSkipSelfDep(
inject(ChildDep, InjectFlags.SkipSelf|InjectFlags.Optional), inject(ChildDep, InjectFlags.SkipSelf|InjectFlags.Optional),
inject(ChildDep, InjectFlags.Self), inject(ChildDep, InjectFlags.Self),
@ -210,6 +218,7 @@ describe('NgModuleRef_ injector', () => {
ngOnDestroy(): void { Service.destroyed++; } ngOnDestroy(): void { Service.destroyed++; }
static ngInjectableDef: ɵɵInjectableDef<Service> = ɵɵdefineInjectable({ static ngInjectableDef: ɵɵInjectableDef<Service> = ɵɵdefineInjectable({
token: Service,
factory: () => new Service(), factory: () => new Service(),
providedIn: 'root', providedIn: 'root',
}); });

View File

@ -778,6 +778,7 @@ export declare const ɵɵdefineDirective: <T>(directiveDefinition: {
}) => never; }) => never;
export declare function ɵɵdefineInjectable<T>(opts: { export declare function ɵɵdefineInjectable<T>(opts: {
token: unknown;
providedIn?: Type<any> | 'root' | 'any' | null; providedIn?: Type<any> | 'root' | 'any' | null;
factory: () => T; factory: () => T;
}): never; }): never;
@ -869,6 +870,7 @@ export declare function ɵɵinject<T>(token: Type<T> | InjectionToken<T>, flags?
export interface ɵɵInjectableDef<T> { export interface ɵɵInjectableDef<T> {
factory: () => T; factory: () => T;
providedIn: InjectorType<any> | 'root' | 'any' | null; providedIn: InjectorType<any> | 'root' | 'any' | null;
token: unknown;
value: T | undefined; value: T | undefined;
} }