Skip to content
Closed
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 @@ -12,6 +12,18 @@ export function render(_ctx, _cache) {
}"
`;

exports[`compiler: v-memo transform > on element with :key but without v-for 1`] = `
"import { createTextVNode as _createTextVNode, openBlock as _openBlock, createElementBlock as _createElementBlock, withMemo as _withMemo } from "vue"

export function render(_ctx, _cache) {
return (_openBlock(), _createElementBlock("div", null, [
_withMemo([_ctx.y], () => (_openBlock(), _createElementBlock("div", { key: _ctx.x }, [
_createTextVNode("foo")
])), _cache, 0)
]))
}"
`;

exports[`compiler: v-memo transform > on normal element 1`] = `
"import { openBlock as _openBlock, createElementBlock as _createElementBlock, withMemo as _withMemo } from "vue"

Expand Down
4 changes: 4 additions & 0 deletions packages/compiler-core/__tests__/transforms/vMemo.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ describe('compiler: v-memo transform', () => {
).toMatchSnapshot()
})

test('on element with :key but without v-for', () => {
expect(compile(`<div :key="x" v-memo="[y]">foo</div>`)).toMatchSnapshot()
})

test('on v-for', () => {
expect(
compile(
Expand Down
3 changes: 3 additions & 0 deletions packages/compiler-core/src/transforms/transformExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export const transformExpression: NodeTransform = (node, context) => {
} else if (node.type === NodeTypes.ELEMENT) {
// handle directives on element
const memo = findDir(node, 'memo')
const hasFor = findDir(node, 'for')
for (let i = 0; i < node.props.length; i++) {
const dir = node.props[i]
// do not process for v-on & v-for since they are special handled
Expand All @@ -68,8 +69,10 @@ export const transformExpression: NodeTransform = (node, context) => {
exp.type === NodeTypes.SIMPLE_EXPRESSION &&
!(dir.name === 'on' && arg) &&
// key has been processed in transformFor(vMemo + vFor)
// only skip when both v-memo and v-for are present
!(
memo &&
hasFor &&
arg &&
arg.type === NodeTypes.SIMPLE_EXPRESSION &&
arg.content === 'key'
Expand Down