@@ -14,6 +14,17 @@ import {
1414 TmplAstElement ,
1515 TmplAstTemplate ,
1616 TmplAstTextAttribute ,
17+ TmplAstNode ,
18+ TmplAstDeferredBlock ,
19+ TmplAstDeferredBlockError ,
20+ TmplAstDeferredBlockLoading ,
21+ TmplAstDeferredBlockPlaceholder ,
22+ TmplAstForLoopBlock ,
23+ TmplAstForLoopBlockEmpty ,
24+ TmplAstIfBlockBranch ,
25+ TmplAstSwitchBlockCase ,
26+ TmplAstIfBlock ,
27+ TmplAstSwitchBlock ,
1728} from '@angular/compiler' ;
1829
1930import { readFile } from '../../utils/file.utils' ;
@@ -98,3 +109,85 @@ export function isSupportedNode<Predicates extends any[]>(
98109) : node is GuardedType < Predicates [ number ] > {
99110 return predicates . some ( ( predicate ) => predicate ( node ) ) ;
100111}
112+
113+ type BlockNode =
114+ | TmplAstDeferredBlockError
115+ | TmplAstDeferredBlockLoading
116+ | TmplAstDeferredBlockPlaceholder
117+ | TmplAstForLoopBlockEmpty
118+ | TmplAstIfBlockBranch
119+ | TmplAstSwitchBlockCase
120+ | TmplAstForLoopBlock
121+ | TmplAstDeferredBlock
122+ | TmplAstIfBlock
123+ | TmplAstSwitchBlock ;
124+
125+ export function isBlockWithChildren (
126+ node : unknown ,
127+ ) : node is { children : TmplAstNode [ ] } {
128+ return (
129+ node instanceof TmplAstDeferredBlockError ||
130+ node instanceof TmplAstDeferredBlockLoading ||
131+ node instanceof TmplAstDeferredBlockPlaceholder ||
132+ node instanceof TmplAstForLoopBlockEmpty ||
133+ node instanceof TmplAstIfBlockBranch ||
134+ node instanceof TmplAstSwitchBlockCase
135+ ) ;
136+ }
137+
138+ export function isTmplAstForLoopBlock (
139+ node : unknown ,
140+ ) : node is TmplAstForLoopBlock {
141+ return node instanceof TmplAstForLoopBlock ;
142+ }
143+
144+ export function isTmplAstDeferredBlock (
145+ node : unknown ,
146+ ) : node is TmplAstDeferredBlock {
147+ return node instanceof TmplAstDeferredBlock ;
148+ }
149+
150+ export function isTmplAstIfBlock ( node : unknown ) : node is TmplAstIfBlock {
151+ return node instanceof TmplAstIfBlock ;
152+ }
153+
154+ export function isTmplAstSwitchBlock (
155+ node : unknown ,
156+ ) : node is TmplAstSwitchBlock {
157+ return node instanceof TmplAstSwitchBlock ;
158+ }
159+
160+ export function isBlockNode ( node : TmplAstNode ) : node is BlockNode {
161+ return (
162+ isTmplAstIfBlock ( node ) ||
163+ isTmplAstForLoopBlock ( node ) ||
164+ isTmplAstDeferredBlock ( node ) ||
165+ isTmplAstSwitchBlock ( node ) ||
166+ isBlockWithChildren ( node )
167+ ) ;
168+ }
169+
170+ export function resolveBlockChildNodes ( node : BlockNode ) : TmplAstNode [ ] {
171+ if ( isTmplAstIfBlock ( node ) ) {
172+ return node . branches ;
173+ }
174+
175+ if ( isTmplAstForLoopBlock ( node ) ) {
176+ return node . empty ? [ ...node . children , node . empty ] : node . children ;
177+ }
178+
179+ if ( isTmplAstDeferredBlock ( node ) ) {
180+ return [
181+ ...node . children ,
182+ ...( [ node . loading , node . error , node . placeholder ] . filter (
183+ Boolean ,
184+ ) as TmplAstNode [ ] ) ,
185+ ] ;
186+ }
187+
188+ if ( isTmplAstSwitchBlock ( node ) ) {
189+ return node . cases ;
190+ }
191+
192+ return node . children ;
193+ }
0 commit comments