refactor(ivy): correct typings in instantiateAllDirectives (#33322)

PR Close #33322
This commit is contained in:
Pawel Kozlowski 2019-10-18 16:22:44 +02:00 committed by atscott
parent 1d141a8ab1
commit 3ff712a0f5
5 changed files with 21 additions and 16 deletions

View File

@ -19,7 +19,7 @@ import {getFactoryDef} from './definition';
import {NG_ELEMENT_ID, NG_FACTORY_DEF} from './fields'; import {NG_ELEMENT_ID, NG_FACTORY_DEF} from './fields';
import {DirectiveDef, FactoryFn} from './interfaces/definition'; import {DirectiveDef, FactoryFn} from './interfaces/definition';
import {NO_PARENT_INJECTOR, NodeInjectorFactory, PARENT_INJECTOR, RelativeInjectorLocation, RelativeInjectorLocationFlags, TNODE, isFactory} from './interfaces/injector'; import {NO_PARENT_INJECTOR, NodeInjectorFactory, PARENT_INJECTOR, RelativeInjectorLocation, RelativeInjectorLocationFlags, TNODE, isFactory} from './interfaces/injector';
import {AttributeMarker, TContainerNode, TElementContainerNode, TElementNode, TNode, TNodeProviderIndexes, TNodeType} from './interfaces/node'; import {AttributeMarker, TContainerNode, TDirectiveHostNode, TElementContainerNode, TElementNode, TNode, TNodeProviderIndexes, TNodeType} from './interfaces/node';
import {isComponentDef, isComponentHost} from './interfaces/type_checks'; import {isComponentDef, isComponentHost} from './interfaces/type_checks';
import {DECLARATION_VIEW, INJECTOR, LView, TData, TVIEW, TView, T_HOST} from './interfaces/view'; import {DECLARATION_VIEW, INJECTOR, LView, TData, TVIEW, TView, T_HOST} from './interfaces/view';
import {assertNodeOfPossibleTypes} from './node_assert'; import {assertNodeOfPossibleTypes} from './node_assert';
@ -528,7 +528,7 @@ export function locateDirectiveOrProvider<T>(
* instantiates the `injectable` and caches the value. * instantiates the `injectable` and caches the value.
*/ */
export function getNodeInjectable( export function getNodeInjectable(
tData: TData, lView: LView, index: number, tNode: TElementNode): any { tData: TData, lView: LView, index: number, tNode: TDirectiveHostNode): any {
let value = lView[index]; let value = lView[index];
if (isFactory(value)) { if (isFactory(value)) {
const factory: NodeInjectorFactory = value; const factory: NodeInjectorFactory = value;

View File

@ -15,7 +15,7 @@ import {diPublicInInjector, getNodeInjectable, getOrCreateNodeInjectorForNode} f
import {ɵɵdirectiveInject} from './instructions/all'; import {ɵɵdirectiveInject} from './instructions/all';
import {DirectiveDef} from './interfaces/definition'; import {DirectiveDef} from './interfaces/definition';
import {NodeInjectorFactory} from './interfaces/injector'; import {NodeInjectorFactory} from './interfaces/injector';
import {TContainerNode, TElementContainerNode, TElementNode, TNodeProviderIndexes} from './interfaces/node'; import {TContainerNode, TDirectiveHostNode, TElementContainerNode, TElementNode, TNodeProviderIndexes} from './interfaces/node';
import {isComponentDef} from './interfaces/type_checks'; import {isComponentDef} from './interfaces/type_checks';
import {LView, TData, TVIEW, TView} from './interfaces/view'; import {LView, TData, TVIEW, TView} from './interfaces/view';
import {getLView, getPreviousOrParentTNode} from './state'; import {getLView, getPreviousOrParentTNode} from './state';
@ -205,7 +205,7 @@ function indexOf(item: any, arr: any[], begin: number, end: number) {
*/ */
function multiProvidersFactoryResolver( function multiProvidersFactoryResolver(
this: NodeInjectorFactory, _: undefined, tData: TData, lData: LView, this: NodeInjectorFactory, _: undefined, tData: TData, lData: LView,
tNode: TElementNode): any[] { tNode: TDirectiveHostNode): any[] {
return multiResolve(this.multi !, []); return multiResolve(this.multi !, []);
} }
@ -216,7 +216,7 @@ function multiProvidersFactoryResolver(
*/ */
function multiViewProvidersFactoryResolver( function multiViewProvidersFactoryResolver(
this: NodeInjectorFactory, _: undefined, tData: TData, lData: LView, this: NodeInjectorFactory, _: undefined, tData: TData, lData: LView,
tNode: TElementNode): any[] { tNode: TDirectiveHostNode): any[] {
const factories = this.multi !; const factories = this.multi !;
let result: any[]; let result: any[];
if (this.providerFactory) { if (this.providerFactory) {
@ -254,8 +254,8 @@ function multiResolve(factories: Array<() => any>, result: any[]): any[] {
*/ */
function multiFactory( function multiFactory(
factoryFn: ( factoryFn: (
this: NodeInjectorFactory, _: undefined, tData: TData, lData: LView, tNode: TElementNode) => this: NodeInjectorFactory, _: undefined, tData: TData, lData: LView,
any, tNode: TDirectiveHostNode) => any,
index: number, isViewProvider: boolean, isComponent: boolean, index: number, isViewProvider: boolean, isComponent: boolean,
f: () => any): NodeInjectorFactory { f: () => any): NodeInjectorFactory {
const factory = new NodeInjectorFactory(factoryFn, isViewProvider, ɵɵdirectiveInject); const factory = new NodeInjectorFactory(factoryFn, isViewProvider, ɵɵdirectiveInject);

View File

@ -23,7 +23,7 @@ import {executeCheckHooks, executeInitAndCheckHooks, incrementInitPhaseFlags, re
import {ACTIVE_INDEX, CONTAINER_HEADER_OFFSET, LContainer} from '../interfaces/container'; import {ACTIVE_INDEX, CONTAINER_HEADER_OFFSET, LContainer} from '../interfaces/container';
import {ComponentDef, ComponentTemplate, DirectiveDef, DirectiveDefListOrFactory, PipeDefListOrFactory, RenderFlags, ViewQueriesFunction} from '../interfaces/definition'; import {ComponentDef, ComponentTemplate, DirectiveDef, DirectiveDefListOrFactory, PipeDefListOrFactory, RenderFlags, ViewQueriesFunction} from '../interfaces/definition';
import {INJECTOR_BLOOM_PARENT_SIZE, NodeInjectorFactory} from '../interfaces/injector'; import {INJECTOR_BLOOM_PARENT_SIZE, NodeInjectorFactory} from '../interfaces/injector';
import {AttributeMarker, InitialInputData, InitialInputs, LocalRefExtractor, PropertyAliasValue, PropertyAliases, TAttributes, TContainerNode, TElementContainerNode, TElementNode, TIcuContainerNode, TNode, TNodeFlags, TNodeProviderIndexes, TNodeType, TProjectionNode, TViewNode} from '../interfaces/node'; import {AttributeMarker, InitialInputData, InitialInputs, LocalRefExtractor, PropertyAliasValue, PropertyAliases, TAttributes, TContainerNode, TDirectiveHostNode, TElementContainerNode, TElementNode, TIcuContainerNode, TNode, TNodeFlags, TNodeProviderIndexes, TNodeType, TProjectionNode, TViewNode} from '../interfaces/node';
import {RComment, RElement, RText, Renderer3, RendererFactory3, isProceduralRenderer} from '../interfaces/renderer'; import {RComment, RElement, RText, Renderer3, RendererFactory3, isProceduralRenderer} from '../interfaces/renderer';
import {SanitizerFn} from '../interfaces/sanitization'; import {SanitizerFn} from '../interfaces/sanitization';
import {isComponentDef, isComponentHost, isContentQueryHost, isLContainer, isRootView} from '../interfaces/type_checks'; import {isComponentDef, isComponentHost, isContentQueryHost, isLContainer, isRootView} from '../interfaces/type_checks';
@ -1090,19 +1090,19 @@ export function resolveDirectives(
/** /**
* Instantiate all the directives that were previously resolved on the current node. * Instantiate all the directives that were previously resolved on the current node.
*/ */
function instantiateAllDirectives(tView: TView, lView: LView, tNode: TNode) { function instantiateAllDirectives(tView: TView, lView: LView, tNode: TDirectiveHostNode) {
const start = tNode.directiveStart; const start = tNode.directiveStart;
const end = tNode.directiveEnd; const end = tNode.directiveEnd;
if (!tView.firstTemplatePass) { if (!tView.firstTemplatePass) {
getOrCreateNodeInjectorForNode( getOrCreateNodeInjectorForNode(tNode, lView);
tNode as TElementNode | TContainerNode | TElementContainerNode, lView);
} }
for (let i = start; i < end; i++) { for (let i = start; i < end; i++) {
const def = tView.data[i] as DirectiveDef<any>; const def = tView.data[i] as DirectiveDef<any>;
if (isComponentDef(def)) { if (isComponentDef(def)) {
addComponentLogic(lView, tNode, def as ComponentDef<any>); assertNodeOfPossibleTypes(tNode, TNodeType.Element);
addComponentLogic(lView, tNode as TElementNode, def);
} }
const directive = getNodeInjectable(tView.data, lView !, i, tNode as TElementNode); const directive = getNodeInjectable(tView.data, lView, i, tNode);
postProcessDirective(lView, tNode, directive, def, i - start); postProcessDirective(lView, tNode, directive, def, i - start);
} }
} }
@ -1306,7 +1306,7 @@ function baseResolveDirective<T>(tView: TView, viewData: LView, def: DirectiveDe
viewData.push(nodeInjectorFactory); viewData.push(nodeInjectorFactory);
} }
function addComponentLogic<T>(lView: LView, hostTNode: TNode, def: ComponentDef<T>): void { function addComponentLogic<T>(lView: LView, hostTNode: TElementNode, def: ComponentDef<T>): void {
const native = getNativeByTNode(hostTNode, lView) as RElement; const native = getNativeByTNode(hostTNode, lView) as RElement;
const tView = getOrCreateTView(def); const tView = getOrCreateTView(def);

View File

@ -10,7 +10,7 @@ import {InjectionToken} from '../../di/injection_token';
import {InjectFlags} from '../../di/interface/injector'; import {InjectFlags} from '../../di/interface/injector';
import {Type} from '../../interface/type'; import {Type} from '../../interface/type';
import {TElementNode} from './node'; import {TDirectiveHostNode} from './node';
import {LView, TData} from './view'; import {LView, TData} from './view';
export const TNODE = 8; export const TNODE = 8;
@ -230,7 +230,7 @@ export class NodeInjectorFactory {
/** /**
* The TNode of the same element injector. * The TNode of the same element injector.
*/ */
tNode: TElementNode) => any, tNode: TDirectiveHostNode) => any,
/** /**
* Set to `true` if the token is declared in `viewProviders` (or if it is component). * Set to `true` if the token is declared in `viewProviders` (or if it is component).
*/ */

View File

@ -602,6 +602,11 @@ export interface TProjectionNode extends TNode {
projection: number; projection: number;
} }
/**
* An union type representing all TNode types that can host a directive.
*/
export type TDirectiveHostNode = TElementNode | TContainerNode | TElementContainerNode;
/** /**
* This mapping is necessary so we can set input properties and output listeners * This mapping is necessary so we can set input properties and output listeners
* properly at runtime when property names are minified or aliased. * properly at runtime when property names are minified or aliased.