fix(compiler-core): properly parse await expressions in edge cases

close #10754
This commit is contained in:
Evan You 2024-04-22 23:21:18 +08:00
parent 173ec65cd4
commit b92c25f53d
No known key found for this signature in database
GPG Key ID: B9D421896CA450FB
2 changed files with 32 additions and 3 deletions

View 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`,
},
` })()`,
],
})
})
})
})

View 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(