fix(compiler-sfc): support nested await statements (#4458)

fix #4448
This commit is contained in:
edison
2021-09-17 04:23:46 +08:00
committed by GitHub
parent 524688bc99
commit ae942cdcd9
3 changed files with 261 additions and 53 deletions

View File

@@ -509,19 +509,33 @@ export function compileScript(
/**
* await foo()
* -->
* (([__temp, __restore] = withAsyncContext(() => foo())),__temp=await __temp,__restore(),__temp)
* (([__temp, __restore] = withAsyncContext(async () => foo())),__temp=await __temp,__restore(),__temp)
*/
function processAwait(node: AwaitExpression, isStatement: boolean) {
const argumentStart =
node.argument.extra && node.argument.extra.parenthesized
? (node.argument.extra.parenStart as number)
: node.argument.start!
const argumentStr = source.slice(
argumentStart + startOffset,
node.argument.end! + startOffset
)
const containsNestedAwait = /\bawait\b/.test(argumentStr)
s.overwrite(
node.start! + startOffset,
node.argument.start! + startOffset,
`${isStatement ? `;` : ``}(([__temp,__restore]=${helper(
argumentStart + startOffset,
`${isStatement ? `;` : ``}(\n ([__temp,__restore] = ${helper(
`withAsyncContext`
)}(()=>(`
)}(${containsNestedAwait ? `async ` : ``}() => {\n return `
)
s.appendLeft(
node.end! + startOffset,
`))),__temp=await __temp,__restore()${isStatement ? `` : `,__temp`})`
`\n })),\n __temp = await __temp,\n __restore()${
isStatement ? `` : `,\n __temp`
}\n)`
)
}