]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-core): v-for expression missing source with spaces should emit error...
authorhuangcheng <huangcheng.lq@bytedance.com>
Thu, 30 May 2024 09:24:37 +0000 (17:24 +0800)
committerGitHub <noreply@github.com>
Thu, 30 May 2024 09:24:37 +0000 (17:24 +0800)
close #5819

packages/compiler-core/__tests__/transforms/vFor.spec.ts
packages/compiler-core/src/utils.ts

index 7fabcbb579cdd231078fdf2ecc29b8d09b46d1c3..e434b8888a698c559ab2330b7f1649b4d027a806 100644 (file)
@@ -202,6 +202,18 @@ describe('compiler: v-for', () => {
       expect(forNode.valueAlias).toBeUndefined()
       expect((forNode.source as SimpleExpressionNode).content).toBe('items')
     })
+
+    test('source containing string expression with spaces', () => {
+      const { node: forNode } = parseWithForTransform(
+        `<span v-for="item in state ['my items']" />`,
+      )
+      expect(forNode.keyAlias).toBeUndefined()
+      expect(forNode.objectIndexAlias).toBeUndefined()
+      expect((forNode.valueAlias as SimpleExpressionNode).content).toBe('item')
+      expect((forNode.source as SimpleExpressionNode).content).toBe(
+        "state ['my items']",
+      )
+    })
   })
 
   describe('errors', () => {
@@ -253,6 +265,18 @@ describe('compiler: v-for', () => {
       )
     })
 
+    test('missing source and have multiple spaces with', () => {
+      const onError = vi.fn()
+      parseWithForTransform('<span v-for="item in  " />', { onError })
+
+      expect(onError).toHaveBeenCalledTimes(1)
+      expect(onError).toHaveBeenCalledWith(
+        expect.objectContaining({
+          code: ErrorCodes.X_V_FOR_MALFORMED_EXPRESSION,
+        }),
+      )
+    })
+
     test('missing value', () => {
       const onError = vi.fn()
       parseWithForTransform('<span v-for="in items" />', { onError })
index 99a2c5b61a89974b730a90922483b3f382d608f6..aa59602844003e3b3cfae80f14e9893ed25d5687 100644 (file)
@@ -499,4 +499,4 @@ export function getMemoedVNodeCall(node: BlockCodegenNode | MemoExpression) {
   }
 }
 
-export const forAliasRE = /([\s\S]*?)\s+(?:in|of)\s+([\s\S]*)/
+export const forAliasRE = /([\s\S]*?)\s+(?:in|of)\s+(\S[\s\S]*)/