From 156789e500bee307135aa6d06f3154ea7041004c Mon Sep 17 00:00:00 2001 From: buqiyuan <1743369777@qq.com> Date: Mon, 22 Apr 2024 23:51:07 +0800 Subject: [PATCH] fix(compiler-core): properly parse await expressions in edge cases close #10754 --- .../transforms/transformExpressions.spec.ts | 28 +++++++++++++++++++ .../src/transforms/transformExpression.ts | 7 +++-- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/packages/compiler-core/__tests__/transforms/transformExpressions.spec.ts b/packages/compiler-core/__tests__/transforms/transformExpressions.spec.ts index b8207e7d42f..4f08aeb9b22 100644 --- a/packages/compiler-core/__tests__/transforms/transformExpressions.spec.ts +++ b/packages/compiler-core/__tests__/transforms/transformExpressions.spec.ts @@ -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`, + }, + ` })()`, + ], + }) + }) }) }) diff --git a/packages/compiler-core/src/transforms/transformExpression.ts b/packages/compiler-core/src/transforms/transformExpression.ts index c7cd1b63d56..53a4bc5925f 100644 --- a/packages/compiler-core/src/transforms/transformExpression.ts +++ b/packages/compiler-core/src/transforms/transformExpression.ts @@ -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(