8000 refactor(core): Remove reliance on `TNodeType.View`. (#38707) · angular/angular@4645f43 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4645f43

Browse files
committed
refactor(core): Remove reliance on TNodeType.View. (#38707)
`TNodeType.View` was created to support inline views. That feature did not materialize and we have since removed the instructions for it, leave an unneeded `TNodeType.View` which was still used in a very inconsistent way. This change no longer created `TNodeType.View` (and there will be a follow up chang to completely remove it.) Also simplified the mental model so that `LView[HOST]`/`LView[T_HOST]` always point to the insertion location of the `LView`. PR Close #38707
1 parent b613639 commit 4645f43

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+348
-353
lines changed

goldens/circular-deps/packages.json

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -389,27 +389,6 @@
389389
"packages/core/src/render3/definition.ts",
390390
"packages/core/src/metadata/ng_module.ts"
391391
],
392-
[
393-
"packages/core/src/application_ref.ts",
394-
"packages/core/src/application_tokens.ts",
395-
"packages/core/src/linker/component_factory.ts",
396-
"packages/core/src/change_detection/change_detection.ts",
397-
"packages/core/src/change_detection/change_detector_ref.ts",
398-
"packages/core/src/render3/view_engine_compatibility.ts",
399-
"packages/core/src/render3/node_manipulation.ts",
400-
"packages/core/src/core.ts",
401-
"packages/core/src/metadata.ts",
402-
"packages/core/src/di.ts",
403-
"packages/core/src/di/index.ts",
404-
"packages/core/src/di/injectable.ts",
405-
"packages/core/src/di/jit/injectable.ts",
406-
"packages/core/src/di/jit/environment.ts",
407-
"packages/core/src/di/injector_compatibility.ts",
408-
"packages/core/src/di/injector.ts",
409-
"packages/core/src/di/r3_injector.ts",
410-
"packages/core/src/render3/definition.ts",
411-
"packages/core/src/metadata/ng_module.ts"
412-
],
413392
[
414393
"packages/core/src/application_ref.ts",
415394
"packages/core/src/application_tokens.ts",
@@ -904,6 +883,12 @@
904883
"packages/core/src/change_detection/differs/default_keyvalue_differ.ts",
905884
"packages/core/src/change_detection/differs/keyvalue_differs.ts"
906885
],
886+
[
887+
"packages/core/src/debug/debug_node.ts",
888+
"packages/core/src/view/index.ts",
889+
"packages/core/src/view/entrypoint.ts",
890+
"packages/core/src/view/services.ts"
891+
],
907892
[
908893
"packages/core/src/di/injectable.ts",
909894
"packages/core/src/di/jit/injectable.ts"

goldens/size-tracking/aio-payloads.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"master": {
1313
"uncompressed": {
1414
"runtime-es2015": 3037,
15-
"main-es2015": 448493,
15+
"main-es2015": 447742,
1616
"polyfills-es2015": 52415
1717
}
1818
}

goldens/size-tracking/integration-payloads.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"master": {
2222
"uncompressed": {
2323
"runtime-es2015": 1485,
24-
"main-es2015": 146989,
24+
"main-es2015": 146417,
2525
"polyfills-es2015": 36571
2626
}
2727
}
@@ -39,7 +39,7 @@
3939
"master": {
4040
"uncompressed": {
4141
"runtime-es2015": 2289,
42-
"main-es2015": 245351,
42+
"main-es2015": 244189,
4343
"polyfills-es2015": 36938,
4444
"5-es2015": 751
4545
}
@@ -49,7 +49,7 @@
4949
"master": {
5050
"uncompressed": {
5151
"runtime-es2015": 2289,
52-
"main-es2015": 221400,
52+
"main-es2015": 220768,
5353
"polyfills-es2015": 36723,
5454
"5-es2015": 781
5555
}

packages/core/src/debug/debug_node.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
import {Injector} from '../di/injector';
10+
import {assertTNodeForLView} from '../render3/assert';
1011
import {CONTAINER_HEADER_OFFSET, LContainer, NATIVE} from '../render3/interfaces/container';
1112
import {TElementNode, TNode, TNodeFlags, TNodeType} from '../render3/interfaces/node';
1213
import {isComponentHost, isLContainer} from '../render3/interfaces/type_checks';
@@ -15,7 +16,7 @@ import {getComponent, getContext, getInjectionTokens, getInjector, getListeners,
1516
import {INTERPOLATION_DELIMITER, renderStringify} from '../render3/util/misc_utils';
1617
import {getComponentLViewByIndex, getNativeByTNodeOrNull} from '../render3/util/view_utils';
1718
import {assertDomNode} from '../util/assert';
18-
import {DebugContext} from '../view/types';
19+
import {DebugContext} from '../view/index';
1920

2021

2122

@@ -525,6 +526,7 @@ function _queryAllR3(
525526
function _queryNodeChildrenR3(
526527
tNode: TNode, lView: LView, predicate: Predicate<DebugElement>|Predicate<DebugNode>,
527528
matches: DebugElement[]|DebugNode[], elementsOnly: boolean, rootNativeNode: any) {
529+
ngDevMode && assertTNodeForLView(tNode, lView);
528530
const nativeNode = getNativeByTNodeOrNull(tNode, lView);
529531
// For each type of TNode, specific logic is executed.
530532
if (tNode.type === TNodeType.Element || tNode.type === TNodeType.ElementContainer) {
@@ -616,9 +618,11 @@ function _queryNodeChildrenInContainerR3(
616618
lContainer: LContainer, predicate: Predicate<DebugElement>|Predicate<DebugNode>,
617619
matches: DebugElement[]|DebugNode[], elementsOnly: boolean, rootNativeNode: any) {
618620
for (let i = CONTAINER_HEADER_OFFSET; i < lContainer.length; i++) {
619-
const childView = lContainer[i];
620-
_queryNodeChildrenR3(
621-
childView[TVIEW].node!, childView, predicate, matches, elementsOnly, rootNativeNode);
621+
const childView = lContainer[i] as LView;
622+
const firstChild = childView[TVIEW].firstChild;
623+
if (firstChild) {
624+
_queryNodeChildrenR3(firstChild, childView, predicate, matches, elementsOnly, rootNativeNode);
625+
}
622626
}
623627
}
624628

@@ -658,7 +662,7 @@ function _addQueryMatchR3(
658662
*
659663
* @param nativeNode the current native node
660664
* @param predicate the predicate to match
661-
* @param matches the list of positive matches
665+
* @param matches the list where matches are stored
662666
* @param elementsOnly whether only elements should be searched
663667
*/
664668
function _queryNativeNodeDescendants(

packages/core/src/di/injector_compatibility.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import '../util/ng_dev_mode';
1010

1111
import {Type} from '../interface/type';
12+
import {assertNotEqual} from '../util/assert';
1213
import {getClosureSafeProperty} from '../util/property';
1314
import {stringify} from '../util/stringify';
1415

@@ -84,6 +85,19 @@ export function setInjectImplementation(
8485
return previous;
8586
}
8687

88+
/**
89+
* Assert that `_injectImplementation` is not `fn`.
90+
*
91+
* This is useful, to prevent infinite recursion.
92+
*
93+
* @param fn Function which it should not equal to
94+
*/
95+
export function assertInjectImplementationNot(
96+
fn: (<T>(token: Type<T>|InjectionToken<T>, flags?: InjectFlags) => T | null)) {
97+
ngDevMode &&
98+
assertNotEqual(_injectImplementation, fn, 'Calling ɵɵinject would cause infinite recursion');
99+
}
100+
87101
export function injectInjectorOnly<T>(token: Type<T>|InjectionToken<T>): T;
88102
export function injectInjectorOnly<T>(token: Type<T>|InjectionToken<T>, flags?: InjectFlags): T|
89103
null;

packages/core/src/render3/assert.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {assertDefined, assertEqual, assertIndexInRange, assertNumber, throwError} from '../util/assert';
9+
import {assertDefined, assertEqual, assertNumber, throwError} from '../util/assert';
1010
import {getComponentDef, getNgModuleDef} from './definition';
1111
import {LContainer} from './interfaces/container';
1212
import {DirectiveDef} from './interfaces/definition';
13-
import { PARENT_INJECTOR } from './interfaces/injector';
13+
import {PARENT_INJECTOR} from './interfaces/injector';
1414
import {TNode} from './interfaces/node';
1515
import {isLContainer, isLView} from './interfaces/type_checks';
1616
import {HEADER_OFFSET, LView, TVIEW, TView} from './interfaces/view';
1717

18+
19+
1820
// [Assert functions do not constraint type when they are guarded by a truthy
1921
// expression.](https://github.com/microsoft/TypeScript/issues/37295)
2022

@@ -147,4 +149,4 @@ export function assertNodeInjector(lView: LView, injectorIndex: number) {
147149
assertNumber(
148150
lView[injectorIndex + 8 /*PARENT_INJECTOR*/],
149151
'injectorIndex should point to parent injector');
150-
}
152+
}

packages/core/src/render3/component.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,9 @@ export function renderComponent<T>(
129129
const rootContext = createRootContext(opts.scheduler, opts.playerHandler);
130130

131131
const renderer = rendererFactory.createRenderer(hostRNode, componentDef);
132-
const rootTView = createTView(TViewType.Root, -1, null, 1, 0, null, null, null, null, null);
132+
const rootTView = createTView(TViewType.Root, null, null, 1, 0, null, null, null, null, null);
133133
const rootView: LView = createLView(
134-
null, rootTView, rootContext, rootFlags, null, null, rendererFactory, renderer, undefined,
134+
null, rootTView, rootContext, rootFlags, null, null, rendererFactory, renderer, null,
135135
opts.injector || null);
136136

137137
enterView(rootView);
@@ -193,7 +193,7 @@ export function createRootComponentView(
193193
const componentView = createLView(
194194
rootView, getOrCreateTComponentView(def), null,
195195
def.onPush ? LViewFlags.Dirty : LViewFlags.CheckAlways, rootView[HEADER_OFFSET], tNode,
196-
rendererFactory, viewRenderer, sanitizer);
196+
rendererFactory, viewRenderer, sanitizer || null, null);
197197

198198
if (tView.firstCreatePass) {
199199
diPublicInInjector(getOrCreateNodeInjectorForNode(tNode, rootView), tView, def.type);

packages/core/src/render3/component_ref.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,16 @@ import {RendererFactory2} from '../render/api';
1919
import {Sanitizer} from '../sanitization/sanitizer';
2020
import {VERSION} from '../version';
2121
import {NOT_FOUND_CHECK_ONLY_ELEMENT_INJECTOR} from '../view/provider';
22-
2322
import {assertComponentType} from './assert';
2423
import {createRootComponent, createRootComponentView, createRootContext, LifecycleHooksFeature} from './component';
2524
import {getComponentDef} from './definition';
2625
import {NodeInjector} from './di';
27-
import {assignTViewNodeToLView, createLView, createTView, elementCreate, locateHostElement, renderView} from './instructions/shared';
26+
import {createLView, createTView, elementCreate, locateHostElement, renderView} from './instructions/shared';
2827
import {ComponentDef} from './interfaces/definition';
29-
import {TContainerNode, TElementContainerNode, TElementNode, TNode, TNodeType} from './interfaces/node';
28+
import {TContainerNode, TElementContainerNode, TElementNode, TNode} from './interfaces/node';
3029
import {domRendererFactory3, RendererFactory3, RNode} from './interfaces/renderer';
31-
import {LView, LViewFlags, TVIEW, TViewType} from './interfaces/view';
30+
import {LView, LViewFlags, TViewType} from './interfaces/view';
3231
import {MATH_ML_NAMESPACE, SVG_NAMESPACE} from './namespaces';
33-
import {assertNodeOfPossibleTypes} from './node_assert';
3432
import {writeDirectClass} from './node_manipulation';
3533
import {extractAttrsAndClassesFromSelector, stringifyCSSSelectorList} from './node_selector_matcher';
3634
import {enterView, leaveView} from './state';
@@ -158,7 +156,7 @@ export class ComponentFactory<T> extends viewEngine_ComponentFactory<T> {
158156
const rootContext = createRootContext();
159157

160158
// Create the root view. Uses empty TView and ContentTemplate.
161-
const rootTView = createTView(TViewType.Root, -1, null, 1, 0, null, null, null, null, null);
159+
const rootTView = createTView(TViewType.Root, null, null, 1, 0, null, null, null, null, null);
162160
const rootLView = createLView(
163161
null, rootTView, rootContext, rootFlags, null, null, rendererFactory, hostRenderer,
164162
sanitizer, rootViewInjector);
@@ -223,11 +221,6 @@ export class ComponentFactory<T> extends viewEngine_ComponentFactory<T> {
223221
const componentRef = new ComponentRef(
224222
this.componentType, component,
225223
createElementRef(viewEngine_ElementRef, tElementNode, rootLView), rootLView, tElementNode);
226-
227-
// The host element of the internal root view is attached to the component's host view node.
228-
ngDevMode && assertNodeOfPossibleTypes(rootTView.node, [TNodeType.View]);
229-
rootTView.node!.child = tElementNode;
230-
231224
return componentRef;
232225
}
233226
}
@@ -267,7 +260,6 @@ export class ComponentRef<T> extends viewEngine_ComponentRef<T> {
267260
super();
268261
this.instance = instance;
269262
this.hostView = this.changeDetectorRef = new RootViewRef<T>(_rootLView);
270-
assignTViewNodeToLView(_rootLView[TVIEW], null, -1, _rootLView);
271263
this.componentType = componentType;
272264
}
273265

packages/core/src/render3/context_discovery.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,11 @@ export function isDirectiveInstance(instance: any): boolean {
190190
* Locates the element within the given LView and returns the matching index
191191
*/
192192
function findViaNativeElement(lView: LView, target: RElement): number {
193-
let tNode = lView[TVIEW].firstChild;
194-
while (tNode) {
195-
const native = getNativeByTNodeOrNull(tNode, lView)!;
196-
if (native === target) {
197-
return tNode.index;
193+
const tView = lView[TVIEW];
194+
for (let i = HEADER_OFFSET; i < tView.bindingStartIndex; i++) {
195+
if (unwrapRNode(lView[i]) === target) {
196+
return i;
198197
}
199-
tNode = traverseNextElement(tNode);
200198
}
201199

202200
return -1;
@@ -206,7 +204,11 @@ function findViaNativeElement(lView: LView, target: RElement): number {
206204
* Locates the next tNode (child, sibling or parent).
207205
*/
208206
function traverseNextElement(tNode: TNode): TNode|null {
209-
if (tNode.child) {
207+
if (tNode.child && tNode.child.parent === tNode) {
208+
// FIXME(misko): checking if `tNode.child.parent === tNode` should not be necessary
209+
// We have added it here because i18n creates TNode's which are not valid, so this is a work
210+
// around. The i18n code is being refactored in #??? and once it lands this extra check can be
211+
// deleted.
210212
return tNode.child;
211213
} else if (tNode.next) {
212214
return tNode.next;

0 commit comments

Comments
 (0)
0