]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-core): properly parse await expressions in edge cases
authorEvan You <yyx990803@gmail.com>
Mon, 22 Apr 2024 15:21:18 +0000 (23:21 +0800)
committerEvan You <yyx990803@gmail.com>
Mon, 22 Apr 2024 15:21:18 +0000 (23:21 +0800)
close #10754

packages/compiler-core/__tests__/transforms/transformExpressions.spec.ts
packages/compiler-core/src/transforms/transformExpression.ts

index b8207e7d42f16522b3dae545cad36e8171fd2880..4f08aeb9b226871adcea05c644c9367a9843b8e0 100644 (file)
@@ -598,5 +598,33 @@ describe('compiler: expression transform', () => {
         `${PatchFlags.TEXT} /* ${PatchFlagNames[PatchFlags.TEXT]} */`,
       )
     })
+
+    // #10754
+    test('await expression in right hand of assignment, inline mode', () => {
+      const node = parseWithExpressionTransform(
+        `{{ (async () => { x = await bar })() }}`,
+        {
+          inline: true,
+          bindingMetadata: {
+            x: BindingTypes.SETUP_LET,
+            bar: BindingTypes.SETUP_CONST,
+          },
+        },
+      ) as InterpolationNode
+      expect(node.content).toMatchObject({
+        type: NodeTypes.COMPOUND_EXPRESSION,
+        children: [
+          `(async () => { `,
+          {
+            content: `_isRef(x) ? x.value = await bar : x`,
+          },
+          ` = await `,
+          {
+            content: `bar`,
+          },
+          ` })()`,
+        ],
+      })
+    })
   })
 })
index c7cd1b63d56537e619fcad6e37e121687c5c605b..53a4bc5925f13bebd97d75f45bad369537e0c39b 100644 (file)
@@ -40,7 +40,7 @@ import type {
   UpdateExpression,
 } from '@babel/types'
 import { validateBrowserExpression } from '../validateExpression'
-import { parse } from '@babel/parser'
+import { parseExpression } from '@babel/parser'
 import { IS_REF, UNREF } from '../runtimeHelpers'
 import { BindingTypes } from '../options'
 
@@ -272,9 +272,10 @@ export function processExpression(
       ? ` ${rawExp} `
       : `(${rawExp})${asParams ? `=>{}` : ``}`
     try {
-      ast = parse(source, {
+      ast = parseExpression(source, {
+        sourceType: 'module',
         plugins: context.expressionPlugins,
-      }).program
+      })
     } catch (e: any) {
       context.onError(
         createCompilerError(