MCPcopy Create free account
hub / github.com/angular/angular / createForLoop

Function createForLoop

packages/compiler/src/render3/r3_control_flow.ts:139–209  ·  view source on GitHub ↗
(
  ast: html.Block,
  connectedBlocks: html.Block[],
  visitor: html.Visitor,
  bindingParser: BindingParser,
)

Source from the content-addressed store, hash-verified

137
138/** Creates a `for` loop block from an HTML AST node. */
139export function createForLoop(
140 ast: html.Block,
141 connectedBlocks: html.Block[],
142 visitor: html.Visitor,
143 bindingParser: BindingParser,
144): {node: t.ForLoopBlock | null; errors: ParseError[]} {
145 const errors: ParseError[] = [];
146 const params = parseForLoopParameters(ast, errors, bindingParser);
147 let node: t.ForLoopBlock | null = null;
148 let empty: t.ForLoopBlockEmpty | null = null;
149
150 for (const block of connectedBlocks) {
151 if (block.name === 'empty') {
152 if (empty !== null) {
153 errors.push(new ParseError(block.sourceSpan, '@for loop can only have one @empty block'));
154 } else if (block.parameters.length > 0) {
155 errors.push(new ParseError(block.sourceSpan, '@empty block cannot have parameters'));
156 } else {
157 empty = new t.ForLoopBlockEmpty(
158 html.visitAll(visitor, block.children, block.children),
159 block.sourceSpan,
160 block.startSourceSpan,
161 block.endSourceSpan,
162 block.nameSpan,
163 block.i18n,
164 );
165 }
166 } else {
167 errors.push(new ParseError(block.sourceSpan, `Unrecognized @for loop block "${block.name}"`));
168 }
169 }
170
171 if (params !== null) {
172 // The `for` block has a main span that includes the `empty` branch. For only the span of the
173 // main `for` body, use `mainSourceSpan`.
174 const endSpan = empty?.endSourceSpan ?? ast.endSourceSpan;
175 const sourceSpan = new ParseSourceSpan(
176 ast.sourceSpan.start,
177 endSpan?.end ?? ast.sourceSpan.end,
178 );
179 let trackExpression: ASTWithSource | null;
180 let trackKeywordSpan: ParseSourceSpan | null;
181
182 if (params.trackBy === null) {
183 trackExpression = trackKeywordSpan = null;
184 errors.push(new ParseError(ast.startSourceSpan, '@for loop must have a "track" expression'));
185 } else {
186 trackExpression = params.trackBy.expression;
187 trackKeywordSpan = params.trackBy.keywordSpan;
188 validateTrackByExpression(params.trackBy.expression, params.trackBy.keywordSpan, errors);
189 }
190
191 node = new t.ForLoopBlock(
192 params.itemName,
193 params.expression,
194 trackExpression,
195 trackKeywordSpan,
196 params.context,

Callers 1

visitBlockMethod · 0.90

Calls 4

parseForLoopParametersFunction · 0.85
pushMethod · 0.45
visitAllMethod · 0.45

Tested by

no test coverage detected