( node: ElementNode, dir: DirectiveNode, context: TransformContext, processCodegen?: (forNode: ForNode) => (() => void) | undefined, )
| 251 | |
| 252 | // target-agnostic transform used for both Client and SSR |
| 253 | export function processFor( |
| 254 | node: ElementNode, |
| 255 | dir: DirectiveNode, |
| 256 | context: TransformContext, |
| 257 | processCodegen?: (forNode: ForNode) => (() => void) | undefined, |
| 258 | ): (() => void) | undefined { |
| 259 | if (!dir.exp) { |
| 260 | context.onError( |
| 261 | createCompilerError(ErrorCodes.X_V_FOR_NO_EXPRESSION, dir.loc), |
| 262 | ) |
| 263 | return |
| 264 | } |
| 265 | |
| 266 | const parseResult = dir.forParseResult |
| 267 | |
| 268 | if (!parseResult) { |
| 269 | context.onError( |
| 270 | createCompilerError(ErrorCodes.X_V_FOR_MALFORMED_EXPRESSION, dir.loc), |
| 271 | ) |
| 272 | return |
| 273 | } |
| 274 | |
| 275 | finalizeForParseResult(parseResult, context) |
| 276 | |
| 277 | const { addIdentifiers, removeIdentifiers, scopes } = context |
| 278 | const { source, value, key, index } = parseResult |
| 279 | |
| 280 | const forNode: ForNode = { |
| 281 | type: NodeTypes.FOR, |
| 282 | loc: dir.loc, |
| 283 | source, |
| 284 | valueAlias: value, |
| 285 | keyAlias: key, |
| 286 | objectIndexAlias: index, |
| 287 | parseResult, |
| 288 | children: isTemplateNode(node) ? node.children : [node], |
| 289 | } |
| 290 | |
| 291 | context.replaceNode(forNode) |
| 292 | |
| 293 | // bookkeeping |
| 294 | scopes.vFor++ |
| 295 | if (!__BROWSER__ && context.prefixIdentifiers) { |
| 296 | // scope management |
| 297 | // inject identifiers to context |
| 298 | value && addIdentifiers(value) |
| 299 | key && addIdentifiers(key) |
| 300 | index && addIdentifiers(index) |
| 301 | } |
| 302 | |
| 303 | const onExit = processCodegen && processCodegen(forNode) |
| 304 | |
| 305 | return (): void => { |
| 306 | scopes.vFor-- |
| 307 | if (!__BROWSER__ && context.prefixIdentifiers) { |
| 308 | value && removeIdentifiers(value) |
| 309 | key && removeIdentifiers(key) |
| 310 | index && removeIdentifiers(index) |
no test coverage detected