Skip to content

fix(compiler-vapor): prevent v-for components from being single root #13149

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

Merged
merged 5 commits into from
Jun 20, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,18 @@ export function render(_ctx) {
}"
`;

exports[`compiler: element transform > component > v-for on component should not mark as single root 1`] = `
"import { createComponent as _createComponent, createFor as _createFor } from 'vue';

export function render(_ctx, $props, $emit, $attrs, $slots) {
const n0 = _createFor(() => (_ctx.items), (_for_item0) => {
const n2 = _createComponent(_ctx.Comp)
return n2
}, (item) => (item), 2)
return n0
}"
`;

exports[`compiler: element transform > component > v-on expression is a function call 1`] = `
"import { resolveComponent as _resolveComponent, createComponentWithFallback as _createComponentWithFallback } from 'vue';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
transformElement,
transformText,
transformVBind,
transformVFor,
transformVOn,
} from '../../src'
import {
Expand All @@ -15,7 +16,12 @@ import {
} from '@vue/compiler-core'

const compileWithElementTransform = makeCompile({
nodeTransforms: [transformElement, transformChildren, transformText],
nodeTransforms: [
transformVFor,
transformElement,
transformChildren,
transformText,
],
directiveTransforms: {
bind: transformVBind,
on: transformVOn,
Expand Down Expand Up @@ -170,6 +176,17 @@ describe('compiler: element transform', () => {
expect(code).contains('_createComponent(_ctx.Comp)')
})

test('v-for on component should not mark as single root', () => {
const { code } = compileWithElementTransform(
`<Comp v-for="item in items" :key="item"/>`,
{
bindingMetadata: { Comp: BindingTypes.SETUP_CONST },
},
)
expect(code).toMatchSnapshot()
expect(code).contains('_createComponent(_ctx.Comp)')
})

test('static props', () => {
const { code, ir } = compileWithElementTransform(
`<Foo id="foo" class="bar" />`,
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler-vapor/src/transforms/transformElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ function transformComponentElement(
tag,
props: propsResult[0] ? propsResult[1] : [propsResult[1]],
asset,
root: singleRoot,
root: singleRoot && !context.inVFor,
slots: [...context.slots],
once: context.inVOnce,
dynamic: dynamicComponent,
Expand Down
Loading