Skip to content

Commit 2381874

Browse files
committed
fix(compiler-core): handle v-if userKey not resolved when use :key shorthand
1 parent 664d2e5 commit 2381874

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

packages/compiler-core/__tests__/transforms/vIf.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,15 @@ describe('compiler: v-if', () => {
209209
content: `_ctx.ok`,
210210
})
211211
})
212+
213+
test('v-if + :key shorthand', () => {
214+
const { node } = parseWithIfTransform(`<div v-if="ok" :key></div>`)
215+
expect(node.type).toBe(NodeTypes.IF)
216+
expect(node.branches[0].userKey).toMatchObject({
217+
arg: { content: 'key' },
218+
exp: { content: 'key' },
219+
})
220+
})
212221
})
213222

214223
describe('errors', () => {

packages/compiler-core/src/transforms/vIf.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import { cloneLoc } from '../parser'
3434
import { CREATE_COMMENT, FRAGMENT } from '../runtimeHelpers'
3535
import { findDir, findProp, getMemoedVNodeCall, injectProp } from '../utils'
3636
import { PatchFlags } from '@vue/shared'
37+
import { transformBindShorthand } from './vBind'
3738

3839
export const transformIf: NodeTransform = createStructuralDirectiveTransform(
3940
/^(if|else|else-if)$/,
@@ -108,7 +109,7 @@ export function processIf(
108109
}
109110

110111
if (dir.name === 'if') {
111-
const branch = createIfBranch(node, dir)
112+
const branch = createIfBranch(node, dir, context)
112113
const ifNode: IfNode = {
113114
type: NodeTypes.IF,
114115
loc: cloneLoc(node.loc),
@@ -153,7 +154,7 @@ export function processIf(
153154

154155
// move the node to the if node's branches
155156
context.removeNode()
156-
const branch = createIfBranch(node, dir)
157+
const branch = createIfBranch(node, dir, context)
157158
if (
158159
__DEV__ &&
159160
comments.length &&
@@ -205,8 +206,17 @@ export function processIf(
205206
}
206207
}
207208

208-
function createIfBranch(node: ElementNode, dir: DirectiveNode): IfBranchNode {
209+
function createIfBranch(
210+
node: ElementNode,
211+
dir: DirectiveNode,
212+
context: TransformContext,
213+
): IfBranchNode {
209214
const isTemplateIf = node.tagType === ElementTypes.TEMPLATE
215+
const keyProp = findProp(node, `key`, false, true)
216+
// resolve :key shorthand #11321
217+
if (keyProp && keyProp.type === NodeTypes.DIRECTIVE && !keyProp.exp) {
218+
transformBindShorthand(keyProp, context)
219+
}
210220
return {
211221
type: NodeTypes.IF_BRANCH,
212222
loc: node.loc,

0 commit comments

Comments
 (0)