Skip to content

Commit d497be7

Browse files
JeanMechedylhunn
authored andcommitted
refactor(core): Use the nullish coalescing assignment in render3 functions (#49698)
The usage of `??=` make the code more clear & concise. PR Close #49698
1 parent 136ffbc commit d497be7

3 files changed

Lines changed: 16 additions & 17 deletions

File tree

packages/core/src/render3/hooks.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,17 @@ export function registerPreOrderHooks(
3939

4040
if (ngOnChanges as Function | undefined) {
4141
const wrappedOnChanges = NgOnChangesFeatureImpl(directiveDef);
42-
(tView.preOrderHooks || (tView.preOrderHooks = [])).push(directiveIndex, wrappedOnChanges);
43-
(tView.preOrderCheckHooks || (tView.preOrderCheckHooks = []))
44-
.push(directiveIndex, wrappedOnChanges);
42+
(tView.preOrderHooks ??= []).push(directiveIndex, wrappedOnChanges);
43+
(tView.preOrderCheckHooks ??= []).push(directiveIndex, wrappedOnChanges);
4544
}
4645

4746
if (ngOnInit) {
48-
(tView.preOrderHooks || (tView.preOrderHooks = [])).push(0 - directiveIndex, ngOnInit);
47+
(tView.preOrderHooks ??= []).push(0 - directiveIndex, ngOnInit);
4948
}
5049

5150
if (ngDoCheck) {
52-
(tView.preOrderHooks || (tView.preOrderHooks = [])).push(directiveIndex, ngDoCheck);
53-
(tView.preOrderCheckHooks || (tView.preOrderCheckHooks = [])).push(directiveIndex, ngDoCheck);
51+
(tView.preOrderHooks ??= []).push(directiveIndex, ngDoCheck);
52+
(tView.preOrderCheckHooks ??= []).push(directiveIndex, ngDoCheck);
5453
}
5554
}
5655

@@ -91,25 +90,25 @@ export function registerPostOrderHooks(tView: TView, tNode: TNode): void {
9190
} = lifecycleHooks;
9291

9392
if (ngAfterContentInit) {
94-
(tView.contentHooks || (tView.contentHooks = [])).push(-i, ngAfterContentInit);
93+
(tView.contentHooks ??= []).push(-i, ngAfterContentInit);
9594
}
9695

9796
if (ngAfterContentChecked) {
98-
(tView.contentHooks || (tView.contentHooks = [])).push(i, ngAfterContentChecked);
99-
(tView.contentCheckHooks || (tView.contentCheckHooks = [])).push(i, ngAfterContentChecked);
97+
(tView.contentHooks ??= []).push(i, ngAfterContentChecked);
98+
(tView.contentCheckHooks ??= []).push(i, ngAfterContentChecked);
10099
}
101100

102101
if (ngAfterViewInit) {
103-
(tView.viewHooks || (tView.viewHooks = [])).push(-i, ngAfterViewInit);
102+
(tView.viewHooks ??= []).push(-i, ngAfterViewInit);
104103
}
105104

106105
if (ngAfterViewChecked) {
107-
(tView.viewHooks || (tView.viewHooks = [])).push(i, ngAfterViewChecked);
108-
(tView.viewCheckHooks || (tView.viewCheckHooks = [])).push(i, ngAfterViewChecked);
106+
(tView.viewHooks ??= []).push(i, ngAfterViewChecked);
107+
(tView.viewCheckHooks ??= []).push(i, ngAfterViewChecked);
109108
}
110109

111110
if (ngOnDestroy != null) {
112-
(tView.destroyHooks || (tView.destroyHooks = [])).push(i, ngOnDestroy);
111+
(tView.destroyHooks ??= []).push(i, ngOnDestroy);
113112
}
114113
}
115114
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,12 +1144,12 @@ export function initializeDirectives(
11441144
// We will push the actual hook function into this array later during dir instantiation.
11451145
// We cannot do it now because we must ensure hooks are registered in the same
11461146
// order that directives are created (i.e. injection order).
1147-
(tView.preOrderHooks || (tView.preOrderHooks = [])).push(tNode.index);
1147+
(tView.preOrderHooks ??= []).push(tNode.index);
11481148
preOrderHooksFound = true;
11491149
}
11501150

11511151
if (!preOrderCheckHooksFound && (lifeCycleHooks.ngOnChanges || lifeCycleHooks.ngDoCheck)) {
1152-
(tView.preOrderCheckHooks || (tView.preOrderCheckHooks = [])).push(tNode.index);
1152+
(tView.preOrderCheckHooks ??= []).push(tNode.index);
11531153
preOrderCheckHooksFound = true;
11541154
}
11551155

@@ -1362,7 +1362,7 @@ export function markAsComponentHost(tView: TView, hostTNode: TNode, componentOff
13621362
ngDevMode && assertFirstCreatePass(tView);
13631363
ngDevMode && assertGreaterThan(componentOffset, -1, 'componentOffset must be great than -1');
13641364
hostTNode.componentOffset = componentOffset;
1365-
(tView.components || (tView.components = [])).push(hostTNode.index);
1365+
(tView.components ??= []).push(hostTNode.index);
13661366
}
13671367

13681368
/** Caches local names and their matching directive indices for query and template lookups. */

packages/core/src/render3/pipe.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export function ɵɵpipe(index: number, pipeName: string): any {
4343
pipeDef = getPipeDef(pipeName, tView.pipeRegistry)!;
4444
tView.data[adjustedIndex] = pipeDef;
4545
if (pipeDef.onDestroy) {
46-
(tView.destroyHooks || (tView.destroyHooks = [])).push(adjustedIndex, pipeDef.onDestroy);
46+
(tView.destroyHooks ??= []).push(adjustedIndex, pipeDef.onDestroy);
4747
}
4848
} else {
4949
pipeDef = tView.data[adjustedIndex] as PipeDef<any>;

0 commit comments

Comments
 (0)