Skip to content

Commit

Permalink
fix(compiler-core): should treat attribute key as expression (#4658)
Browse files Browse the repository at this point in the history
  • Loading branch information
HerringtonDarkholme committed Sep 25, 2021
1 parent 901e18b commit 7aa0ea0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
20 changes: 20 additions & 0 deletions packages/compiler-core/__tests__/transforms/vFor.spec.ts
Expand Up @@ -638,6 +638,26 @@ describe('compiler: v-for', () => {
})
})
})

test('template v-for key no prefixing on attribute key', () => {
const {
node: { codegenNode }
} = parseWithForTransform(
'<template v-for="item in items" key="key">test</template>',
{ prefixIdentifiers: true }
)
const innerBlock = codegenNode.children.arguments[1].returns
expect(innerBlock).toMatchObject({
type: NodeTypes.VNODE_CALL,
tag: FRAGMENT,
props: createObjectMatcher({
key: {
type: NodeTypes.SIMPLE_EXPRESSION,
content: 'key'
}
})
})
})
})

describe('codegen', () => {
Expand Down
7 changes: 6 additions & 1 deletion packages/compiler-core/src/transforms/vFor.ts
Expand Up @@ -68,7 +68,12 @@ export const transformFor = createStructuralDirectiveTransform(
: keyProp.exp!)
const keyProperty = keyProp ? createObjectProperty(`key`, keyExp!) : null

if (!__BROWSER__ && context.prefixIdentifiers && keyProperty) {
if (
!__BROWSER__ &&
context.prefixIdentifiers &&
keyProperty &&
keyProp!.type !== NodeTypes.ATTRIBUTE
) {
// #2085 process :key expression needs to be processed in order for it
// to behave consistently for <template v-for> and <div v-for>.
// In the case of `<template v-for>`, the node is discarded and never
Expand Down

0 comments on commit 7aa0ea0

Please sign in to comment.