This commit is contained in:
edison 2025-07-01 11:08:38 +02:00 committed by GitHub
commit 267d3038fc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 22 additions and 3 deletions

View File

@ -209,6 +209,15 @@ describe('compiler: v-if', () => {
content: `_ctx.ok`, content: `_ctx.ok`,
}) })
}) })
test('v-if + :key shorthand', () => {
const { node } = parseWithIfTransform(`<div v-if="ok" :key></div>`)
expect(node.type).toBe(NodeTypes.IF)
expect(node.branches[0].userKey).toMatchObject({
arg: { content: 'key' },
exp: { content: 'key' },
})
})
}) })
describe('errors', () => { describe('errors', () => {

View File

@ -34,6 +34,7 @@ import { cloneLoc } from '../parser'
import { CREATE_COMMENT, FRAGMENT } from '../runtimeHelpers' import { CREATE_COMMENT, FRAGMENT } from '../runtimeHelpers'
import { findDir, findProp, getMemoedVNodeCall, injectProp } from '../utils' import { findDir, findProp, getMemoedVNodeCall, injectProp } from '../utils'
import { PatchFlags } from '@vue/shared' import { PatchFlags } from '@vue/shared'
import { transformBindShorthand } from './vBind'
export const transformIf: NodeTransform = createStructuralDirectiveTransform( export const transformIf: NodeTransform = createStructuralDirectiveTransform(
/^(if|else|else-if)$/, /^(if|else|else-if)$/,
@ -108,7 +109,7 @@ export function processIf(
} }
if (dir.name === 'if') { if (dir.name === 'if') {
const branch = createIfBranch(node, dir) const branch = createIfBranch(node, dir, context)
const ifNode: IfNode = { const ifNode: IfNode = {
type: NodeTypes.IF, type: NodeTypes.IF,
loc: cloneLoc(node.loc), loc: cloneLoc(node.loc),
@ -153,7 +154,7 @@ export function processIf(
// move the node to the if node's branches // move the node to the if node's branches
context.removeNode() context.removeNode()
const branch = createIfBranch(node, dir) const branch = createIfBranch(node, dir, context)
if ( if (
__DEV__ && __DEV__ &&
comments.length && comments.length &&
@ -205,8 +206,17 @@ export function processIf(
} }
} }
function createIfBranch(node: ElementNode, dir: DirectiveNode): IfBranchNode { function createIfBranch(
node: ElementNode,
dir: DirectiveNode,
context: TransformContext,
): IfBranchNode {
const isTemplateIf = node.tagType === ElementTypes.TEMPLATE const isTemplateIf = node.tagType === ElementTypes.TEMPLATE
const keyProp = findProp(node, `key`, false, true)
// resolve :key shorthand #11321
if (keyProp && keyProp.type === NodeTypes.DIRECTIVE && !keyProp.exp) {
transformBindShorthand(keyProp, context)
}
return { return {
type: NodeTypes.IF_BRANCH, type: NodeTypes.IF_BRANCH,
loc: node.loc, loc: node.loc,