2017-12-01 22:23:03 +00:00
|
|
|
/**
|
|
|
|
|
* @license
|
2020-05-19 19:08:49 +00:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2017-12-01 22:23:03 +00:00
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
|
*/
|
|
|
|
|
|
2020-09-14 20:43:44 +00:00
|
|
|
import {assertDefined, assertEqual, assertNumber, throwError} from '../util/assert';
|
|
|
|
|
import {getComponentDef, getNgModuleDef} from './definition';
|
|
|
|
|
import {LContainer} from './interfaces/container';
|
|
|
|
|
import {DirectiveDef} from './interfaces/definition';
|
|
|
|
|
import {PARENT_INJECTOR} from './interfaces/injector';
|
|
|
|
|
import {TNode} from './interfaces/node';
|
|
|
|
|
import {isLContainer, isLView} from './interfaces/type_checks';
|
|
|
|
|
import {HEADER_OFFSET, LView, TVIEW, TView} from './interfaces/view';
|
2020-09-14 18:21:15 +00:00
|
|
|
|
2018-08-29 23:34:44 +00:00
|
|
|
|
2020-03-09 16:54:51 +00:00
|
|
|
// [Assert functions do not constraint type when they are guarded by a truthy
|
|
|
|
|
// expression.](https://github.com/microsoft/TypeScript/issues/37295)
|
|
|
|
|
|
|
|
|
|
|
2019-06-14 05:55:17 +00:00
|
|
|
export function assertTNodeForLView(tNode: TNode, lView: LView) {
|
2020-09-04 05:46:11 +00:00
|
|
|
assertTNodeForTView(tNode, lView[TVIEW]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function assertTNodeForTView(tNode: TNode, tView: TView) {
|
|
|
|
|
assertDefined(tNode, 'TNode must be defined');
|
2020-04-13 23:40:21 +00:00
|
|
|
tNode.hasOwnProperty('tView_') &&
|
|
|
|
|
assertEqual(
|
2020-09-04 05:46:11 +00:00
|
|
|
(tNode as any as {tView_: TView}).tView_, tView,
|
|
|
|
|
'This TNode does not belong to this TView.');
|
2019-06-14 05:55:17 +00:00
|
|
|
}
|
2018-02-13 06:46:15 +00:00
|
|
|
|
2018-03-15 04:32:09 +00:00
|
|
|
export function assertComponentType(
|
|
|
|
|
actual: any,
|
2019-10-10 21:57:15 +00:00
|
|
|
msg: string = 'Type passed in is not ComponentType, it does not have \'ɵcmp\' property.') {
|
2018-08-29 23:34:44 +00:00
|
|
|
if (!getComponentDef(actual)) {
|
2018-07-31 05:34:15 +00:00
|
|
|
throwError(msg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function assertNgModuleType(
|
|
|
|
|
actual: any,
|
2019-10-14 14:20:26 +00:00
|
|
|
msg: string = 'Type passed in is not NgModuleType, it does not have \'ɵmod\' property.') {
|
2018-08-29 23:34:44 +00:00
|
|
|
if (!getNgModuleDef(actual)) {
|
2018-03-15 04:32:09 +00:00
|
|
|
throwError(msg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-14 20:43:44 +00:00
|
|
|
export function assertCurrentTNodeIsParent(isParent: boolean) {
|
|
|
|
|
assertEqual(isParent, true, 'currentTNode should be a parent');
|
2018-11-22 05:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
2020-04-13 23:40:21 +00:00
|
|
|
export function assertHasParent(tNode: TNode|null) {
|
2020-09-14 20:43:44 +00:00
|
|
|
assertDefined(tNode, 'currentTNode should exist!');
|
|
|
|
|
assertDefined(tNode!.parent, 'currentTNode should have a parent');
|
2018-11-22 05:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function assertDataNext(lView: LView, index: number, arr?: any[]) {
|
|
|
|
|
if (arr == null) arr = lView;
|
|
|
|
|
assertEqual(
|
|
|
|
|
arr.length, index, `index ${index} expected to be at the end of arr (length ${arr.length})`);
|
|
|
|
|
}
|
2019-01-28 22:45:31 +00:00
|
|
|
|
2020-03-09 16:54:51 +00:00
|
|
|
export function assertLContainer(value: any): asserts value is LContainer {
|
2019-01-28 22:45:31 +00:00
|
|
|
assertDefined(value, 'LContainer must be defined');
|
|
|
|
|
assertEqual(isLContainer(value), true, 'Expecting LContainer');
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-09 16:54:51 +00:00
|
|
|
export function assertLViewOrUndefined(value: any): asserts value is LView|null|undefined {
|
2019-01-28 22:45:31 +00:00
|
|
|
value && assertEqual(isLView(value), true, 'Expecting LView or undefined or null');
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-09 16:54:51 +00:00
|
|
|
export function assertLView(value: any): asserts value is LView {
|
2019-01-28 22:45:31 +00:00
|
|
|
assertDefined(value, 'LView must be defined');
|
|
|
|
|
assertEqual(isLView(value), true, 'Expecting LView');
|
|
|
|
|
}
|
2019-06-07 08:55:48 +00:00
|
|
|
|
2019-11-01 20:06:17 +00:00
|
|
|
export function assertFirstCreatePass(tView: TView, errMessage?: string) {
|
2019-08-01 12:00:23 +00:00
|
|
|
assertEqual(
|
2019-11-01 20:06:17 +00:00
|
|
|
tView.firstCreatePass, true, errMessage || 'Should only be called in first create pass.');
|
2019-06-07 08:55:48 +00:00
|
|
|
}
|
2019-11-25 04:56:18 +00:00
|
|
|
|
2019-12-17 23:40:37 +00:00
|
|
|
export function assertFirstUpdatePass(tView: TView, errMessage?: string) {
|
|
|
|
|
assertEqual(
|
|
|
|
|
tView.firstUpdatePass, true, errMessage || 'Should only be called in first update pass.');
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-25 04:56:18 +00:00
|
|
|
/**
|
|
|
|
|
* This is a basic sanity check that an object is probably a directive def. DirectiveDef is
|
|
|
|
|
* an interface, so we can't do a direct instanceof check.
|
|
|
|
|
*/
|
2020-03-09 16:54:51 +00:00
|
|
|
export function assertDirectiveDef<T>(obj: any): asserts obj is DirectiveDef<T> {
|
2019-11-25 04:56:18 +00:00
|
|
|
if (obj.type === undefined || obj.selectors == undefined || obj.inputs === undefined) {
|
|
|
|
|
throwError(
|
|
|
|
|
`Expected a DirectiveDef/ComponentDef and this object does not seem to have the expected shape.`);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-09-04 23:55:51 +00:00
|
|
|
|
|
|
|
|
export function assertIndexInDeclRange(lView: LView, index: number) {
|
|
|
|
|
const tView = lView[1];
|
|
|
|
|
assertBetween(HEADER_OFFSET, tView.bindingStartIndex, index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function assertIndexInVarsRange(lView: LView, index: number) {
|
|
|
|
|
const tView = lView[1];
|
|
|
|
|
assertBetween(
|
|
|
|
|
tView.bindingStartIndex, (tView as any as {i18nStartIndex: number}).i18nStartIndex, index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function assertIndexInI18nRange(lView: LView, index: number) {
|
|
|
|
|
const tView = lView[1];
|
|
|
|
|
assertBetween(
|
|
|
|
|
(tView as any as {i18nStartIndex: number}).i18nStartIndex, tView.expandoStartIndex, index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function assertIndexInExpandoRange(lView: LView, index: number) {
|
|
|
|
|
const tView = lView[1];
|
|
|
|
|
assertBetween(tView.expandoStartIndex, lView.length, index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function assertBetween(lower: number, upper: number, index: number) {
|
|
|
|
|
if (!(lower <= index && index < upper)) {
|
|
|
|
|
throwError(`Index out of range (expecting ${lower} <= ${index} < ${upper})`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This is a basic sanity check that the `injectorIndex` seems to point to what looks like a
|
|
|
|
|
* NodeInjector data structure.
|
|
|
|
|
*
|
|
|
|
|
* @param lView `LView` which should be checked.
|
|
|
|
|
* @param injectorIndex index into the `LView` where the `NodeInjector` is expected.
|
|
|
|
|
*/
|
|
|
|
|
export function assertNodeInjector(lView: LView, injectorIndex: number) {
|
|
|
|
|
assertIndexInExpandoRange(lView, injectorIndex);
|
|
|
|
|
assertIndexInExpandoRange(lView, injectorIndex + PARENT_INJECTOR);
|
|
|
|
|
assertNumber(lView[injectorIndex + 0], 'injectorIndex should point to a bloom filter');
|
|
|
|
|
assertNumber(lView[injectorIndex + 1], 'injectorIndex should point to a bloom filter');
|
|
|
|
|
assertNumber(lView[injectorIndex + 2], 'injectorIndex should point to a bloom filter');
|
|
|
|
|
assertNumber(lView[injectorIndex + 3], 'injectorIndex should point to a bloom filter');
|
|
|
|
|
assertNumber(lView[injectorIndex + 4], 'injectorIndex should point to a bloom filter');
|
|
|
|
|
assertNumber(lView[injectorIndex + 5], 'injectorIndex should point to a bloom filter');
|
|
|
|
|
assertNumber(lView[injectorIndex + 6], 'injectorIndex should point to a bloom filter');
|
|
|
|
|
assertNumber(lView[injectorIndex + 7], 'injectorIndex should point to a bloom filter');
|
|
|
|
|
assertNumber(
|
|
|
|
|
lView[injectorIndex + 8 /*PARENT_INJECTOR*/],
|
|
|
|
|
'injectorIndex should point to parent injector');
|
2020-09-14 18:21:15 +00:00
|
|
|
}
|