From 7aa0ea06c822d84a1d43b40cf5643b983aae6d36 Mon Sep 17 00:00:00 2001 From: Herrington Darkholme <2883231+HerringtonDarkholme@users.noreply.github.com> Date: Sun, 26 Sep 2021 02:21:09 +0800 Subject: [PATCH] fix(compiler-core): should treat attribute key as expression (#4658) --- .../__tests__/transforms/vFor.spec.ts | 20 +++++++++++++++++++ packages/compiler-core/src/transforms/vFor.ts | 7 ++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/packages/compiler-core/__tests__/transforms/vFor.spec.ts b/packages/compiler-core/__tests__/transforms/vFor.spec.ts index b677ca58a36..c22b364aa61 100644 --- a/packages/compiler-core/__tests__/transforms/vFor.spec.ts +++ b/packages/compiler-core/__tests__/transforms/vFor.spec.ts @@ -638,6 +638,26 @@ describe('compiler: v-for', () => { }) }) }) + + test('template v-for key no prefixing on attribute key', () => { + const { + node: { codegenNode } + } = parseWithForTransform( + '', + { 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', () => { diff --git a/packages/compiler-core/src/transforms/vFor.ts b/packages/compiler-core/src/transforms/vFor.ts index c6d444ed8dc..f1d739dc3f3 100644 --- a/packages/compiler-core/src/transforms/vFor.ts +++ b/packages/compiler-core/src/transforms/vFor.ts @@ -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