Skip to content

Commit 2fee5cc

Browse files
mheverykara
authored andcommitted
test(ivy): add injection canonical specs (angular#22595)
PR Close angular#22595
1 parent f13f4db commit 2fee5cc

File tree

14 files changed

+403
-128
lines changed

14 files changed

+403
-128
lines changed

packages/core/src/core_render3_private_export.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export {
1414
detectChanges as ɵdetectChanges,
1515
renderComponent as ɵrenderComponent,
1616
ComponentType as ɵComponentType,
17-
inject as ɵinject,
17+
directiveInject as ɵdirectiveInject,
1818
injectTemplateRef as ɵinjectTemplateRef,
1919
injectViewContainerRef as ɵinjectViewContainerRef,
2020
injectChangeDetectorRef as ɵinjectChangeDetectorRef,

packages/core/src/render3/definition.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ export function defineComponent<T>(componentDefinition: ComponentDefArgs<T>): Co
4747
inputs: invertObject(componentDefinition.inputs),
4848
inputsPropertyName: componentDefinition.inputsPropertyName || null,
4949
outputs: invertObject(componentDefinition.outputs),
50-
methods: invertObject(componentDefinition.methods),
5150
rendererType: resolveRendererType2(componentDefinition.rendererType) || null,
5251
exportAs: componentDefinition.exportAs,
5352
onInit: type.prototype.ngOnInit || null,

packages/core/src/render3/di.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ export function diPublic(def: DirectiveDef<any>): void {
174174
}
175175

176176
/**
177-
* Searches for an instance of the given directive type up the injector tree and returns
177+
* Searches for an instance of the given type up the injector tree and returns
178178
* that instance if found.
179179
*
180180
* If not found, it will propagate up to the next parent injector until the token
@@ -187,15 +187,18 @@ export function diPublic(def: DirectiveDef<any>): void {
187187
*
188188
* static ngDirectiveDef = defineDirective({
189189
* type: SomeDirective,
190-
* factory: () => new SomeDirective(inject(DirectiveA))
190+
* factory: () => new SomeDirective(directiveInject(DirectiveA))
191191
* });
192192
* }
193193
*
194+
* NOTE: use `directiveInject` with `@Directive`, `@Component`, and `@Pipe`. For
195+
* all other injection use `inject` which does not walk the DOM render tree.
196+
*
194197
* @param token The directive type to search for
195198
* @param flags Injection flags (e.g. CheckParent)
196199
* @returns The instance found
197200
*/
198-
export function inject<T>(token: Type<T>, flags?: InjectFlags, defaultValue?: T): T {
201+
export function directiveInject<T>(token: Type<T>, flags?: InjectFlags, defaultValue?: T): T {
199202
return getOrCreateInjectable<T>(getOrCreateNodeInjector(), token, flags, defaultValue);
200203
}
201204

packages/core/src/render3/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {NgOnChangesFeature, PublicFeature, defineComponent, defineDirective, def
1111
import {InjectFlags} from './di';
1212
import {ComponentDef, ComponentTemplate, ComponentType, DirectiveDef, DirectiveDefFlags, DirectiveType} from './interfaces/definition';
1313

14-
export {InjectFlags, QUERY_READ_CONTAINER_REF, QUERY_READ_ELEMENT_REF, QUERY_READ_FROM_NODE, QUERY_READ_TEMPLATE_REF, inject, injectAttribute, injectChangeDetectorRef, injectElementRef, injectTemplateRef, injectViewContainerRef} from './di';
14+
export {InjectFlags, QUERY_READ_CONTAINER_REF, QUERY_READ_ELEMENT_REF, QUERY_READ_FROM_NODE, QUERY_READ_TEMPLATE_REF, directiveInject, injectAttribute, injectChangeDetectorRef, injectElementRef, injectTemplateRef, injectViewContainerRef} from './di';
1515
export {CssSelector} from './interfaces/projection';
1616

1717

packages/core/src/render3/interfaces/definition.ts

Lines changed: 173 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,51 @@
88

99
import {ChangeDetectionStrategy} from '../../change_detection/constants';
1010
import {PipeTransform} from '../../change_detection/pipe_transform';
11+
import {Provider} from '../../core';
1112
import {RendererType2} from '../../render/api';
1213
import {Type} from '../../type';
1314
import {resolveRendererType2} from '../../view/util';
1415

16+
17+
1518
/**
1619
* Definition of what a template rendering function should look like.
1720
*/
1821
export type ComponentTemplate<T> = {
1922
(ctx: T, creationMode: boolean): void; ngPrivateData?: never;
2023
};
2124

25+
/**
26+
* A subclass of `Type` which has a static `ngComponentDef`:`ComponentDef` field making it
27+
* consumable for rendering.
28+
*/
2229
export interface ComponentType<T> extends Type<T> { ngComponentDef: ComponentDef<T>; }
2330

31+
/**
32+
* A subclass of `Type` which has a static `ngDirectiveDef`:`DirectiveDef` field making it
33+
* consumable for rendering.
34+
*/
2435
export interface DirectiveType<T> extends Type<T> { ngDirectiveDef: DirectiveDef<T>; }
2536

2637
export const enum DirectiveDefFlags {ContentQuery = 0b10}
2738

39+
/**
40+
* A subclass of `Type` which has a static `ngPipeDef`:`PipeDef` field making it
41+
* consumable for rendering.
42+
*/
2843
export interface PipeType<T> extends Type<T> { ngPipeDef: PipeDef<T>; }
2944

3045
/**
31-
* `DirectiveDef` is a compiled version of the Directive used by the renderer instructions.
46+
* Runtime link information for Directives.
47+
*
48+
* This is internal data structure used by the render to link
49+
* directives into templates.
50+
*
51+
* NOTE: Always use `defineDirective` function to create this object,
52+
* never create the object directly since the shape of this object
53+
* can change between versions.
54+
*
55+
* See: {@link defineDirective}
3256
*/
3357
export interface DirectiveDef<T> {
3458
/** Token representing the directive. Used by DI. */
@@ -59,11 +83,6 @@ export interface DirectiveDef<T> {
5983
*/
6084
readonly outputs: {[P in keyof T]: P};
6185

62-
/**
63-
* A dictionary mapping the methods' minified names to their original unminified ones.
64-
*/
65-
readonly methods: {[P in keyof T]: P};
66-
6786
/**
6887
* Name under which the directive is exported (for use with local references in template)
6988
*/
@@ -104,6 +123,18 @@ export interface DirectiveDef<T> {
104123
onDestroy: (() => void)|null;
105124
}
106125

126+
/**
127+
* Runtime link information for Components.
128+
*
129+
* This is internal data structure used by the render to link
130+
* components into templates.
131+
*
132+
* NOTE: Always use `defineComponent` function to create this object,
133+
* never create the object directly since the shape of this object
134+
* can change between versions.
135+
*
136+
* See: {@link defineComponent}
137+
*/
107138
export interface ComponentDef<T> extends DirectiveDef<T> {
108139
/**
109140
* The tag name which should be used by the component.
@@ -128,10 +159,31 @@ export interface ComponentDef<T> extends DirectiveDef<T> {
128159

129160
/** Whether or not this component's ChangeDetectionStrategy is OnPush */
130161
readonly onPush: boolean;
162+
163+
/**
164+
* Defines the set of injectable providers that are visible to a Directive and its content DOM
165+
* children.
166+
*/
167+
readonly providers?: Provider[];
168+
169+
/**
170+
* Defines the set of injectable providers that are visible to a Directive and its view DOM
171+
* children only.
172+
*/
173+
readonly viewProviders?: Provider[];
131174
}
132175

133176
/**
177+
* Runtime link information for Pipes.
178+
*
179+
* This is internal data structure used by the renderer to link
180+
* pipes into templates.
134181
*
182+
* NOTE: Always use `definePipe` function to create this object,
183+
* never create the object directly since the shape of this object
184+
* can change between versions.
185+
*
186+
* See: {@link definePipe}
135187
*/
136188
export interface PipeDef<T> {
137189
/**
@@ -154,30 +206,142 @@ export interface PipeDef<T> {
154206
onDestroy: (() => void)|null;
155207
}
156208

157-
209+
/**
210+
* Arguments for `defineDirective`
211+
*/
158212
export interface DirectiveDefArgs<T> {
213+
/**
214+
* Directive type, needed to configure the injector.
215+
*/
159216
type: Type<T>;
217+
218+
/**
219+
* Factory method used to create an instance of directive.
220+
*/
160221
factory: () => T | [T];
222+
223+
/**
224+
* Static attributes to set on host element.
225+
*
226+
* Even indices: attribute name
227+
* Odd indices: attribute value
228+
*/
161229
attributes?: string[];
230+
231+
/**
232+
* A map of input names.
233+
*
234+
* The format is in: `{[actualPropertyName: string]:string}`.
235+
*
236+
* Which the minifier may translate to: `{[minifiedPropertyName: string]:string}`.
237+
*
238+
* This allows the render to re-construct the minified and non-minified names
239+
* of properties.
240+
*/
162241
inputs?: {[P in keyof T]?: string};
242+
243+
/**
244+
* TODO: Remove per https://github.com/angular/angular/issues/22591
245+
*/
163246
inputsPropertyName?: {[P in keyof T]?: string};
247+
248+
/**
249+
* A map of output names.
250+
*
251+
* The format is in: `{[actualPropertyName: string]:string}`.
252+
*
253+
* Which the minifier may translate to: `{[minifiedPropertyName: string]:string}`.
254+
*
255+
* This allows the render to re-construct the minified and non-minified names
256+
* of properties.
257+
*/
164258
outputs?: {[P in keyof T]?: string};
165-
methods?: {[P in keyof T]?: string};
259+
260+
/**
261+
* A list of optional features to apply.
262+
*
263+
* See: {@link NgOnChangesFeature}, {@link PublicFeature}
264+
*/
166265
features?: DirectiveDefFeature[];
266+
267+
/**
268+
* Function executed by the parent template to allow child directive to apply host bindings.
269+
*/
167270
hostBindings?: (directiveIndex: number, elementIndex: number) => void;
271+
272+
/**
273+
* Defines the name that can be used in the template to assign this directive to a variable.
274+
*
275+
* See: {@link Directive.exportAs}
276+
*/
168277
exportAs?: string;
169278
}
170279

280+
/**
281+
* Arguments for `defineComponent`.
282+
*/
171283
export interface ComponentDefArgs<T> extends DirectiveDefArgs<T> {
284+
/**
285+
* HTML tag name to use in place where this component should be instantiated.
286+
*/
172287
tag: string;
288+
289+
/**
290+
* Template function use for rendering DOM.
291+
*
292+
* This function has following structure.
293+
*
294+
* ```
295+
* function Template<T>(ctx:T, creationMode: boolean) {
296+
* if (creationMode) {
297+
* // Contains creation mode instructions.
298+
* }
299+
* // Contains binding update instructions
300+
* }
301+
* ```
302+
*
303+
* Common instructions are:
304+
* Creation mode instructions:
305+
* - `elementStart`, `elementEnd`
306+
* - `text`
307+
* - `container`
308+
* - `listener`
309+
*
310+
* Binding update instructions:
311+
* - `bind`
312+
* - `elementAttribute`
313+
* - `elementProperty`
314+
* - `elementClass`
315+
* - `elementStyle`
316+
*
317+
*/
173318
template: ComponentTemplate<T>;
319+
320+
/**
321+
* A list of optional features to apply.
322+
*
323+
* See: {@link NgOnChancesFeature}, {@link PublicFeature}
324+
*/
174325
features?: ComponentDefFeature[];
326+
175327
rendererType?: RendererType2;
328+
176329
changeDetection?: ChangeDetectionStrategy;
330+
331+
/**
332+
* Defines the set of injectable objects that are visible to a Directive and its light DOM
333+
* children.
334+
*/
335+
providers?: Provider[];
336+
337+
/**
338+
* Defines the set of injectable objects that are visible to its view DOM children.
339+
*/
340+
viewProviders?: Provider[];
177341
}
178342

179343
export type DirectiveDefFeature = <T>(directiveDef: DirectiveDef<T>) => void;
180-
export type ComponentDefFeature = <T>(directiveDef: DirectiveDef<T>) => void;
344+
export type ComponentDefFeature = <T>(componentDef: ComponentDef<T>) => void;
181345

182346
// Note: This hack is necessary so we don't erroneously get a circular dependency
183347
// failure based on types.

packages/core/test/render3/common_with_def.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ import {NgForOf as NgForOfDef} from '@angular/common';
1010
import {IterableDiffers} from '@angular/core';
1111

1212
import {defaultIterableDiffers} from '../../src/change_detection/change_detection';
13-
import {DirectiveType, InjectFlags, NgOnChangesFeature, defineDirective, inject, injectTemplateRef, injectViewContainerRef} from '../../src/render3/index';
13+
import {DirectiveType, InjectFlags, NgOnChangesFeature, defineDirective, directiveInject, injectTemplateRef, injectViewContainerRef} from '../../src/render3/index';
1414

1515
export const NgForOf: DirectiveType<NgForOfDef<any>> = NgForOfDef as any;
1616

1717
NgForOf.ngDirectiveDef = defineDirective({
1818
type: NgForOfDef,
1919
factory: () => new NgForOfDef(
2020
injectViewContainerRef(), injectTemplateRef(),
21-
inject(IterableDiffers, InjectFlags.Default, defaultIterableDiffers)),
21+
directiveInject(IterableDiffers, InjectFlags.Default, defaultIterableDiffers)),
2222
features: [NgOnChangesFeature],
2323
inputs: {
2424
ngForOf: 'ngForOf',

packages/core/test/render3/compiler_canonical/back_patch_types_specs.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import {Component, ContentChild, Directive, Injectable, Injector, Input, NgModule, NgModuleFactory, NgModuleRef, OnDestroy, Optional, Pipe, PipeTransform, QueryList, SimpleChanges, TemplateRef, Type, ViewChild, ViewContainerRef} from '../../../src/core';
1010
import * as r3 from '../../../src/render3/index';
1111

12-
import {pending_pull_22005} from './small_app_spec';
12+
import {$pending_pr_22458$} from './pending_api_spec';
1313

1414
const details_elided = {
1515
type: Object,
@@ -60,7 +60,7 @@ export class LibBComponent {
6060
@NgModule({declarations: [LibAComponent], imports: []})
6161
export class LibBModule {
6262
// COMPILER GENERATED
63-
static ngInjectorDef = pending_pull_22005.defineInjector(details_elided);
63+
static ngInjectorDef = $pending_pr_22458$.defineInjector(details_elided);
6464
}
6565
// END FILE: node_modules/libB/module.ts
6666
// BEGIN FILE: node_modules/libB/module.metadata.json
@@ -92,7 +92,7 @@ export class AppComponent {
9292
@NgModule({declarations: [LibAComponent], imports: []})
9393
export class AppModule {
9494
// COMPILER GENERATED
95-
static ngInjectorDef = pending_pull_22005.defineInjector(details_elided);
95+
static ngInjectorDef = $pending_pr_22458$.defineInjector(details_elided);
9696
}
9797
// END FILE: src/app.ts
9898

@@ -113,7 +113,7 @@ function ngBackPatch_node_modules_libB_module_LibAComponent() {
113113
}
114114

115115
function ngBackPatch_node_modules_libB_module_LibAModule() {
116-
(LibAModule as any).ngInjectorDef = pending_pull_22005.defineInjector(details_elided);
116+
(LibAModule as any).ngInjectorDef = $pending_pr_22458$.defineInjector(details_elided);
117117
}
118118

119119
export const AppModuleFactory: NgModuleFactory<AppModule>&{patchedDeps: boolean} = {

0 commit comments

Comments
 (0)