Skip to content

Commit 2f336f1

Browse files
mheveryjasonaden
authored andcommitted
refactor(core): unify setPreviousOrParentTNode and setIsParent into single setPreviousOrParentTNode (angular#30453)
PR Close angular#30453
1 parent e122b44 commit 2f336f1

File tree

12 files changed

+48
-57
lines changed

12 files changed

+48
-57
lines changed

packages/core/src/render3/i18n.ts

+6-10
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {SanitizerFn} from './interfaces/sanitization';
2626
import {StylingContext} from './interfaces/styling';
2727
import {BINDING_INDEX, HEADER_OFFSET, LView, RENDERER, TVIEW, TView, T_HOST} from './interfaces/view';
2828
import {appendChild, createTextNode, nativeRemoveNode} from './node_manipulation';
29-
import {getIsParent, getLView, getPreviousOrParentTNode, setIsParent, setPreviousOrParentTNode} from './state';
29+
import {getIsParent, getLView, getPreviousOrParentTNode, setIsNotParent, setPreviousOrParentTNode} from './state';
3030
import {NO_CHANGE} from './tokens';
3131
import {renderStringify} from './util/misc_utils';
3232
import {getNativeByIndex, getNativeByTNode, getTNode, isLContainer} from './util/view_utils';
@@ -701,7 +701,7 @@ function readCreateOpCodes(
701701
previousTNode = currentTNode;
702702
currentTNode = createDynamicNodeAtIndex(textNodeIndex, TNodeType.Element, textRNode, null);
703703
visitedNodes.push(textNodeIndex);
704-
setIsParent(false);
704+
setIsNotParent();
705705
} else if (typeof opCode == 'number') {
706706
switch (opCode & I18nMutateOpCode.MASK_OPCODE) {
707707
case I18nMutateOpCode.AppendChild:
@@ -726,17 +726,13 @@ function readCreateOpCodes(
726726
previousTNode = currentTNode;
727727
currentTNode = getTNode(nodeIndex, viewData);
728728
if (currentTNode) {
729-
setPreviousOrParentTNode(currentTNode);
730-
if (currentTNode.type === TNodeType.Element) {
731-
setIsParent(true);
732-
}
729+
setPreviousOrParentTNode(currentTNode, currentTNode.type === TNodeType.Element);
733730
}
734731
break;
735732
case I18nMutateOpCode.ElementEnd:
736733
const elementIndex = opCode >>> I18nMutateOpCode.SHIFT_REF;
737734
previousTNode = currentTNode = getTNode(elementIndex, viewData);
738-
setPreviousOrParentTNode(currentTNode);
739-
setIsParent(false);
735+
setPreviousOrParentTNode(currentTNode, false);
740736
break;
741737
case I18nMutateOpCode.Attr:
742738
const elementNodeIndex = opCode >>> I18nMutateOpCode.SHIFT_REF;
@@ -764,7 +760,7 @@ function readCreateOpCodes(
764760
attachPatchData(commentRNode, viewData);
765761
(currentTNode as TIcuContainerNode).activeCaseIndex = null;
766762
// We will add the case nodes later, during the update phase
767-
setIsParent(false);
763+
setIsNotParent();
768764
break;
769765
case ELEMENT_MARKER:
770766
const tagNameValue = createOpCodes[++i] as string;
@@ -785,7 +781,7 @@ function readCreateOpCodes(
785781
}
786782
}
787783

788-
setIsParent(false);
784+
setIsNotParent();
789785

790786
return visitedNodes;
791787
}

packages/core/src/render3/instructions/container.ts

+6-8
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {LocalRefExtractor, TAttributes, TContainerNode, TNode, TNodeType} from '
1515
import {BINDING_INDEX, HEADER_OFFSET, LView, QUERIES, RENDERER, TVIEW} from '../interfaces/view';
1616
import {assertNodeType} from '../node_assert';
1717
import {appendChild, removeView} from '../node_manipulation';
18-
import {getCheckNoChangesMode, getIsParent, getLView, getPreviousOrParentTNode, setIsParent, setPreviousOrParentTNode} from '../state';
18+
import {getCheckNoChangesMode, getIsParent, getLView, getPreviousOrParentTNode, setIsNotParent, setPreviousOrParentTNode} from '../state';
1919
import {getNativeByTNode, loadInternal} from '../util/view_utils';
2020
import {addToViewTree, createDirectivesAndLocals, createLContainer, createNodeAtIndex, createTView} from './shared';
2121

@@ -37,7 +37,7 @@ export function ɵɵcontainer(index: number): void {
3737
tNode.tViews = [];
3838
}
3939
addTContainerToQueries(lView, tNode);
40-
setIsParent(false);
40+
setIsNotParent();
4141
}
4242

4343
/**
@@ -77,7 +77,7 @@ export function ɵɵtemplate(
7777
addTContainerToQueries(lView, tContainerNode);
7878
attachPatchData(getNativeByTNode(tContainerNode, lView), lView);
7979
registerPostOrderHooks(tView, tContainerNode);
80-
setIsParent(false);
80+
setIsNotParent();
8181
}
8282

8383
/**
@@ -91,10 +91,8 @@ export function ɵɵcontainerRefreshStart(index: number): void {
9191
const lView = getLView();
9292
const tView = lView[TVIEW];
9393
let previousOrParentTNode = loadInternal(tView.data, index) as TNode;
94-
setPreviousOrParentTNode(previousOrParentTNode);
95-
9694
ngDevMode && assertNodeType(previousOrParentTNode, TNodeType.Container);
97-
setIsParent(true);
95+
setPreviousOrParentTNode(previousOrParentTNode, true);
9896

9997
lView[index + HEADER_OFFSET][ACTIVE_INDEX] = 0;
10098

@@ -113,12 +111,12 @@ export function ɵɵcontainerRefreshStart(index: number): void {
113111
export function ɵɵcontainerRefreshEnd(): void {
114112
let previousOrParentTNode = getPreviousOrParentTNode();
115113
if (getIsParent()) {
116-
setIsParent(false);
114+
setIsNotParent();
117115
} else {
118116
ngDevMode && assertNodeType(previousOrParentTNode, TNodeType.View);
119117
ngDevMode && assertHasParent(previousOrParentTNode);
120118
previousOrParentTNode = previousOrParentTNode.parent !;
121-
setPreviousOrParentTNode(previousOrParentTNode);
119+
setPreviousOrParentTNode(previousOrParentTNode, false);
122120
}
123121

124122
ngDevMode && assertNodeType(previousOrParentTNode, TNodeType.Container);

packages/core/src/render3/instructions/element.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88
import {validateAgainstEventAttributes} from '../../sanitization/sanitization';
9-
import {assertDataInRange, assertEqual} from '../../util/assert';
9+
import {assertDataInRange, assertDefined, assertEqual} from '../../util/assert';
1010
import {assertHasParent} from '../assert';
1111
import {attachPatchData} from '../context_discovery';
1212
import {registerPostOrderHooks} from '../hooks';
@@ -18,7 +18,7 @@ import {BINDING_INDEX, QUERIES, RENDERER, TVIEW} from '../interfaces/view';
1818
import {assertNodeType} from '../node_assert';
1919
import {appendChild} from '../node_manipulation';
2020
import {applyOnCreateInstructions} from '../node_util';
21-
import {decreaseElementDepthCount, getElementDepthCount, getIsParent, getLView, getPreviousOrParentTNode, getSelectedIndex, increaseElementDepthCount, setIsParent, setPreviousOrParentTNode} from '../state';
21+
import {decreaseElementDepthCount, getElementDepthCount, getIsParent, getLView, getPreviousOrParentTNode, getSelectedIndex, increaseElementDepthCount, setIsNotParent, setPreviousOrParentTNode} from '../state';
2222
import {getInitialClassNameValue, getInitialStyleStringValue, initializeStaticContext, patchContextWithStaticAttrs, renderInitialClasses, renderInitialStyles} from '../styling/class_and_style_bindings';
2323
import {getStylingContextFromLView, hasClassInput, hasStyleInput} from '../styling/util';
2424
import {registerInitialStylingIntoContext} from '../styling_next/instructions';
@@ -139,12 +139,13 @@ export function ɵɵelementStart(
139139
*/
140140
export function ɵɵelementEnd(): void {
141141
let previousOrParentTNode = getPreviousOrParentTNode();
142+
ngDevMode && assertDefined(previousOrParentTNode, 'No parent node to close.');
142143
if (getIsParent()) {
143-
setIsParent(false);
144+
setIsNotParent();
144145
} else {
145146
ngDevMode && assertHasParent(getPreviousOrParentTNode());
146147
previousOrParentTNode = previousOrParentTNode.parent !;
147-
setPreviousOrParentTNode(previousOrParentTNode);
148+
setPreviousOrParentTNode(previousOrParentTNode, false);
148149
}
149150

150151
// this is required for all host-level styling-related instructions to run

packages/core/src/render3/instructions/element_container.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import {BINDING_INDEX, QUERIES, RENDERER, TVIEW} from '../interfaces/view';
1414
import {assertNodeType} from '../node_assert';
1515
import {appendChild} from '../node_manipulation';
1616
import {applyOnCreateInstructions} from '../node_util';
17-
import {getIsParent, getLView, getPreviousOrParentTNode, setIsParent, setPreviousOrParentTNode} from '../state';
17+
import {getIsParent, getLView, getPreviousOrParentTNode, setIsNotParent, setPreviousOrParentTNode} from '../state';
18+
1819
import {createDirectivesAndLocals, createNodeAtIndex, executeContentQueries, setNodeStylingTemplate} from './shared';
1920

2021
/**
@@ -77,11 +78,11 @@ export function ɵɵelementContainerEnd(): void {
7778
const lView = getLView();
7879
const tView = lView[TVIEW];
7980
if (getIsParent()) {
80-
setIsParent(false);
81+
setIsNotParent();
8182
} else {
8283
ngDevMode && assertHasParent(previousOrParentTNode);
8384
previousOrParentTNode = previousOrParentTNode.parent !;
84-
setPreviousOrParentTNode(previousOrParentTNode);
85+
setPreviousOrParentTNode(previousOrParentTNode, false);
8586
}
8687

8788
ngDevMode && assertNodeType(previousOrParentTNode, TNodeType.ElementContainer);

packages/core/src/render3/instructions/embedded_view.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export function ɵɵembeddedViewStart(
4040
let viewToRender = scanForView(lContainer, lContainer[ACTIVE_INDEX] !, viewBlockId);
4141

4242
if (viewToRender) {
43-
setIsParent(true);
43+
setIsParent();
4444
enterView(viewToRender, viewToRender[TVIEW].node);
4545
} else {
4646
// When we create a new LView, we always reset the state of the instructions.
@@ -141,6 +141,5 @@ export function ɵɵembeddedViewEnd(): void {
141141
const lContainer = lView[PARENT] as LContainer;
142142
ngDevMode && assertLContainerOrUndefined(lContainer);
143143
leaveView(lContainer[PARENT] !);
144-
setPreviousOrParentTNode(viewHost !);
145-
setIsParent(false);
144+
setPreviousOrParentTNode(viewHost !, false);
146145
}

packages/core/src/render3/instructions/projection.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {CssSelectorList} from '../interfaces/projection';
1010
import {T_HOST} from '../interfaces/view';
1111
import {appendProjectedNodes} from '../node_manipulation';
1212
import {matchingProjectionSelectorIndex} from '../node_selector_matcher';
13-
import {getLView, setIsParent} from '../state';
13+
import {getLView, setIsNotParent} from '../state';
1414
import {findComponentView} from '../util/view_traversal_utils';
1515

1616
import {createNodeAtIndex} from './shared';
@@ -88,7 +88,7 @@ export function ɵɵprojection(
8888
if (tProjectionNode.projection === null) tProjectionNode.projection = selectorIndex;
8989

9090
// `<ng-content>` has no content
91-
setIsParent(false);
91+
setIsNotParent();
9292

9393
// re-distribution of projectable nodes is stored on a component's view level
9494
appendProjectedNodes(lView, tProjectionNode, selectorIndex, findComponentView(lView));

packages/core/src/render3/instructions/shared.ts

+11-19
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,7 @@ export function createNodeAtIndex(
287287
}
288288
}
289289

290-
setPreviousOrParentTNode(tNode);
291-
setIsParent(true);
290+
setPreviousOrParentTNode(tNode, true);
292291
return tNode as TElementNode & TViewNode & TContainerNode & TElementContainerNode &
293292
TProjectionNode & TIcuContainerNode;
294293
}
@@ -351,8 +350,7 @@ export function createEmbeddedViewAndNode<T>(
351350
injectorIndex: number): LView {
352351
const _isParent = getIsParent();
353352
const _previousOrParentTNode = getPreviousOrParentTNode();
354-
setIsParent(true);
355-
setPreviousOrParentTNode(null !);
353+
setPreviousOrParentTNode(null !, true);
356354

357355
const lView = createLView(declarationView, tView, context, LViewFlags.CheckAlways, null, null);
358356
lView[DECLARATION_VIEW] = declarationView;
@@ -366,8 +364,7 @@ export function createEmbeddedViewAndNode<T>(
366364
tView.node !.injectorIndex = injectorIndex;
367365
}
368366

369-
setIsParent(_isParent);
370-
setPreviousOrParentTNode(_previousOrParentTNode);
367+
setPreviousOrParentTNode(_previousOrParentTNode, _isParent);
371368
return lView;
372369
}
373370

@@ -390,8 +387,7 @@ export function renderEmbeddedTemplate<T>(viewToRender: LView, tView: TView, con
390387
tickRootContext(getRootContext(viewToRender));
391388
} else {
392389
try {
393-
setIsParent(true);
394-
setPreviousOrParentTNode(null !);
390+
setPreviousOrParentTNode(null !, true);
395391

396392
oldView = enterView(viewToRender, viewToRender[T_HOST]);
397393
resetPreOrderHookFlags(viewToRender);
@@ -406,8 +402,7 @@ export function renderEmbeddedTemplate<T>(viewToRender: LView, tView: TView, con
406402
refreshDescendantViews(viewToRender);
407403
} finally {
408404
leaveView(oldView !);
409-
setIsParent(_isParent);
410-
setPreviousOrParentTNode(_previousOrParentTNode);
405+
setPreviousOrParentTNode(_previousOrParentTNode, _isParent);
411406
}
412407
}
413408
}
@@ -1254,7 +1249,7 @@ function addComponentLogic<T>(
12541249
lView, createLView(
12551250
lView, tView, null, def.onPush ? LViewFlags.Dirty : LViewFlags.CheckAlways,
12561251
lView[previousOrParentTNode.index], previousOrParentTNode as TElementNode,
1257-
rendererFactory, lView[RENDERER_FACTORY].createRenderer(native as RElement, def)));
1252+
rendererFactory, rendererFactory.createRenderer(native as RElement, def)));
12581253

12591254
componentView[T_HOST] = previousOrParentTNode as TElementNode;
12601255

@@ -1695,17 +1690,14 @@ export function storeBindingMetadata(lView: LView, prefix = '', suffix = ''): st
16951690

16961691
export const CLEAN_PROMISE = _CLEAN_PROMISE;
16971692

1698-
export function initializeTNodeInputs(tNode: TNode | null): PropertyAliases|null {
1693+
export function initializeTNodeInputs(tNode: TNode): PropertyAliases|null {
16991694
// If tNode.inputs is undefined, a listener has created outputs, but inputs haven't
17001695
// yet been checked.
1701-
if (tNode) {
1702-
if (tNode.inputs === undefined) {
1703-
// mark inputs as checked
1704-
tNode.inputs = generatePropertyAliases(tNode, BindingDirection.Input);
1705-
}
1706-
return tNode.inputs;
1696+
if (tNode.inputs === undefined) {
1697+
// mark inputs as checked
1698+
tNode.inputs = generatePropertyAliases(tNode, BindingDirection.Input);
17071699
}
1708-
return null;
1700+
return tNode.inputs;
17091701
}
17101702

17111703

packages/core/src/render3/instructions/text.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {TNodeType} from '../interfaces/node';
1010
import {RText, isProceduralRenderer} from '../interfaces/renderer';
1111
import {BINDING_INDEX, HEADER_OFFSET, RENDERER, TVIEW} from '../interfaces/view';
1212
import {appendChild, createTextNode} from '../node_manipulation';
13-
import {getLView, setIsParent} from '../state';
13+
import {getLView, setIsNotParent} from '../state';
1414
import {NO_CHANGE} from '../tokens';
1515
import {renderStringify} from '../util/misc_utils';
1616
import {getNativeByIndex} from '../util/view_utils';
@@ -34,7 +34,7 @@ export function ɵɵtext(index: number, value?: any): void {
3434
const tNode = createNodeAtIndex(index, TNodeType.Element, textNative, null, null);
3535

3636
// Text nodes are self closing.
37-
setIsParent(false);
37+
setIsNotParent();
3838
appendChild(textNative, tNode, lView);
3939
}
4040

packages/core/src/render3/state.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,9 @@ export function getPreviousOrParentTNode(): TNode {
303303
return previousOrParentTNode;
304304
}
305305

306-
export function setPreviousOrParentTNode(tNode: TNode) {
306+
export function setPreviousOrParentTNode(tNode: TNode, _isParent: boolean) {
307307
previousOrParentTNode = tNode;
308+
isParent = _isParent;
308309
}
309310

310311
export function setTNodeAndViewData(tNode: TNode, view: LView) {
@@ -325,8 +326,11 @@ export function getIsParent(): boolean {
325326
return isParent;
326327
}
327328

328-
export function setIsParent(value: boolean): void {
329-
isParent = value;
329+
export function setIsNotParent(): void {
330+
isParent = false;
331+
}
332+
export function setIsParent(): void {
333+
isParent = true;
330334
}
331335

332336

packages/core/test/bundling/cyclic_import/bundle.golden_symbols.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,7 @@
693693
"name": "setInputsFromAttrs"
694694
},
695695
{
696-
"name": "setIsParent"
696+
"name": "setIsNotParent"
697697
},
698698
{
699699
"name": "setNodeStylingTemplate"

packages/core/test/bundling/hello_world/bundle.golden_symbols.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@
468468
"name": "setInjectImplementation"
469469
},
470470
{
471-
"name": "setIsParent"
471+
"name": "setIsNotParent"
472472
},
473473
{
474474
"name": "setPreviousOrParentTNode"

packages/core/test/bundling/todo/bundle.golden_symbols.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1386,7 +1386,7 @@
13861386
"name": "setInputsFromAttrs"
13871387
},
13881388
{
1389-
"name": "setIsParent"
1389+
"name": "setIsNotParent"
13901390
},
13911391
{
13921392
"name": "setNodeStylingTemplate"

0 commit comments

Comments
 (0)