]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-core): should treat attribute key as expression (#4658)
authorHerrington Darkholme <2883231+HerringtonDarkholme@users.noreply.github.com>
Sat, 25 Sep 2021 18:21:09 +0000 (02:21 +0800)
committerGitHub <noreply@github.com>
Sat, 25 Sep 2021 18:21:09 +0000 (14:21 -0400)
packages/compiler-core/__tests__/transforms/vFor.spec.ts
packages/compiler-core/src/transforms/vFor.ts

index b677ca58a36e84fb311d631c04f486d1e01f157d..c22b364aa615dc7dc47b441f5627e91d54f48320 100644 (file)
@@ -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', () => {
index c6d444ed8dca7cd4d4ae6b22a716f306a43d9e92..f1d739dc3f3d58af23a0dd2d77f41dfbebc2e4dd 100644 (file)
@@ -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