Skip to content
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

fix(compiler-core): handle v-if userKey not resolved when use :key shorthand #12298

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions packages/compiler-core/__tests__/transforms/vIf.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,15 @@ describe('compiler: v-if', () => {
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', () => {
Expand Down
16 changes: 13 additions & 3 deletions packages/compiler-core/src/transforms/vIf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { cloneLoc } from '../parser'
import { CREATE_COMMENT, FRAGMENT } from '../runtimeHelpers'
import { findDir, findProp, getMemoedVNodeCall, injectProp } from '../utils'
import { PatchFlags } from '@vue/shared'
import { transformBindShorthand } from './vBind'

export const transformIf: NodeTransform = createStructuralDirectiveTransform(
/^(if|else|else-if)$/,
Expand Down Expand Up @@ -108,7 +109,7 @@ export function processIf(
}

if (dir.name === 'if') {
const branch = createIfBranch(node, dir)
const branch = createIfBranch(node, dir, context)
const ifNode: IfNode = {
type: NodeTypes.IF,
loc: cloneLoc(node.loc),
Expand Down Expand Up @@ -153,7 +154,7 @@ export function processIf(

// move the node to the if node's branches
context.removeNode()
const branch = createIfBranch(node, dir)
const branch = createIfBranch(node, dir, context)
if (
__DEV__ &&
comments.length &&
Expand Down Expand Up @@ -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 keyProp = findProp(node, `key`, false, true)
// resolve :key shorthand #11321
if (keyProp && keyProp.type === NodeTypes.DIRECTIVE && !keyProp.exp) {
transformBindShorthand(keyProp, context)
}
return {
type: NodeTypes.IF_BRANCH,
loc: node.loc,
Expand Down