Skip to content

fix(compiler-core): add isTemplateFor to ast of For nodes #8991

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions packages/compiler-core/__tests__/transforms/vFor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,30 @@ describe('compiler: v-for', () => {
expect(forNode.valueAlias).toBeUndefined()
expect((forNode.source as SimpleExpressionNode).content).toBe('items')
})

test('is template property', () => {
const { node: spanForNode } = parseWithForTransform(
'<span v-for="index in 5" />'
)
const { node: templateForNode } = parseWithForTransform(
'<template v-for="index in 5" />'
)
expect(spanForNode.isTemplateFor).toBe(false)
expect(templateForNode.isTemplateFor).toBe(true)
})

test('key props in template', () => {
const { node: spanForNode } = parseWithForTransform(
'<span v-for="index in 5" :key="index" />'
)
const { node: templateForNode } = parseWithForTransform(
'<template v-for="index in 5" :key="index" />'
)

expect(spanForNode.templateProps).toBeUndefined()
expect(templateForNode.templateProps).toHaveLength(1)
expect(templateForNode.templateProps![0].type).toBe(NodeTypes.DIRECTIVE)
})
})

describe('errors', () => {
Expand Down
2 changes: 2 additions & 0 deletions packages/compiler-core/src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ export interface ForNode extends Node {
parseResult: ForParseResult
children: TemplateChildNode[]
codegenNode?: ForCodegenNode
isTemplateFor?: boolean
templateProps?: Array<AttributeNode | DirectiveNode>
}

export interface TextCallNode extends Node {
Expand Down
5 changes: 4 additions & 1 deletion packages/compiler-core/src/transforms/vFor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ export function processFor(

const { addIdentifiers, removeIdentifiers, scopes } = context
const { source, value, key, index } = parseResult
const isTemplate = isTemplateNode(node)

const forNode: ForNode = {
type: NodeTypes.FOR,
Expand All @@ -280,7 +281,9 @@ export function processFor(
keyAlias: key,
objectIndexAlias: index,
parseResult,
children: isTemplateNode(node) ? node.children : [node]
children: isTemplate ? node.children : [node],
isTemplateFor: isTemplate,
templateProps: isTemplate ? node.props : undefined
}

context.replaceNode(forNode)
Expand Down