Skip to content

Commit

Permalink
fix(compiler-core): should attach key to single element child of `<te…
Browse files Browse the repository at this point in the history
…mplate v-for>` (#1910)
  • Loading branch information
underfin committed Aug 20, 2020
1 parent 7ffb79c commit 69cfed6
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,23 @@ return function render(_ctx, _cache) {
}"
`;

exports[`compiler: v-for codegen template v-for key injection with single child 1`] = `
"const _Vue = Vue
return function render(_ctx, _cache) {
with (_ctx) {
const { renderList: _renderList, Fragment: _Fragment, openBlock: _openBlock, createBlock: _createBlock, createVNode: _createVNode } = _Vue
return (_openBlock(true), _createBlock(_Fragment, null, _renderList(items, (item) => {
return (_openBlock(), _createBlock(\\"span\\", {
key: item.id,
id: item.id
}, null, 8 /* PROPS */, [\\"id\\"]))
}), 128 /* KEYED_FRAGMENT */))
}
}"
`;

exports[`compiler: v-for codegen template v-for w/ <slot/> 1`] = `
"const _Vue = Vue
Expand Down
23 changes: 23 additions & 0 deletions packages/compiler-core/__tests__/transforms/vFor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,29 @@ describe('compiler: v-for', () => {
expect(generate(root).code).toMatchSnapshot()
})

// #1907
test('template v-for key injection with single child', () => {
const {
root,
node: { codegenNode }
} = parseWithForTransform(
'<template v-for="item in items" :key="item.id"><span :id="item.id" /></template>'
)
expect(assertSharedCodegen(codegenNode, true)).toMatchObject({
source: { content: `items` },
params: [{ content: `item` }],
innerVNodeCall: {
type: NodeTypes.VNODE_CALL,
tag: `"span"`,
props: createObjectMatcher({
key: '[item.id]',
id: '[item.id]'
})
}
})
expect(generate(root).code).toMatchSnapshot()
})

test('v-for on <slot/>', () => {
const {
root,
Expand Down
3 changes: 3 additions & 0 deletions packages/compiler-core/src/transforms/vFor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ export const transformFor = createStructuralDirectiveTransform(
// but mark it as a block.
childBlock = (children[0] as PlainElementNode)
.codegenNode as VNodeCall
if (isTemplate && keyProperty) {
injectProp(childBlock, keyProperty, context)
}
childBlock.isBlock = !isStableFragment
if (childBlock.isBlock) {
helper(OPEN_BLOCK)
Expand Down

0 comments on commit 69cfed6

Please sign in to comment.