diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts index bc078806f54..942345ed2fb 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -30,7 +30,6 @@ import { CallExpression, RestElement, TSInterfaceBody, - AwaitExpression, Program, ObjectMethod, LVal, @@ -187,7 +186,6 @@ export function compileScript( | undefined let emitsTypeDeclRaw: Node | undefined let emitIdentifier: string | undefined - let hasAwait = false let hasInlinedSsrRenderFn = false // props/emits declared via types const typeDeclaredProps: Record = {} @@ -471,56 +469,6 @@ export function compileScript( }) } - /** - * await foo() - * --> - * ;( - * ([__temp,__restore] = withAsyncContext(() => foo())), - * await __temp, - * __restore() - * ) - * - * const a = await foo() - * --> - * const a = ( - * ([__temp, __restore] = withAsyncContext(() => foo())), - * __temp = await __temp, - * __restore(), - * __temp - * ) - */ - function processAwait( - node: AwaitExpression, - needSemi: boolean, - 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, - argumentStart + startOffset, - `${needSemi ? `;` : ``}(\n ([__temp,__restore] = ${helper( - `withAsyncContext` - )}(${containsNestedAwait ? `async ` : ``}() => ` - ) - s.appendLeft( - node.end! + startOffset, - `)),\n ${isStatement ? `` : `__temp = `}await __temp,\n __restore()${ - isStatement ? `` : `,\n __temp` - }\n)` - ) - } - /** * check defaults. If the default object is an object literal with only * static properties, we can directly generate more optimized default @@ -984,23 +932,9 @@ export function compileScript( scope.push(child.body) } if (child.type === 'AwaitExpression') { - hasAwait = true - // if the await expression is an expression statement and - // - is in the root scope - // - or is not the first statement in a nested block scope - // then it needs a semicolon before the generated code. - const currentScope = scope[scope.length - 1] - const needsSemi = currentScope.some((n, i) => { - return ( - (scope.length === 1 || i > 0) && - n.type === 'ExpressionStatement' && - n.start === child.start - ) - }) - processAwait( - child, - needsSemi, - parent.type === 'ExpressionStatement' + error( + `Vue 2 does not support top level await in `) - if (shouldAsync) { - expect(content).toMatch(`let __temp, __restore`) - } - expect(content).toMatch(`${shouldAsync ? `async ` : ``}setup(`) - assertCode(content) - return content - } - - test('expression statement', () => { - assertAwaitDetection(`await foo`) - }) - - test('variable', () => { - assertAwaitDetection(`const a = 1 + (await foo)`) - }) - - test('ref', () => { - assertAwaitDetection(`let a = $ref(1 + (await foo))`) - }) - - // #4448 - test('nested await', () => { - assertAwaitDetection(`await (await foo)`) - assertAwaitDetection(`await ((await foo))`) - assertAwaitDetection(`await (await (await foo))`) - }) - - // should prepend semicolon - test('nested leading await in expression statement', () => { - const code = assertAwaitDetection(`foo()\nawait 1 + await 2`) - expect(code).toMatch(`foo()\n;(`) - }) - - // #4596 should NOT prepend semicolon - test('single line conditions', () => { - const code = assertAwaitDetection(`if (false) await foo()`) - expect(code).not.toMatch(`if (false) ;(`) - }) - - test('nested statements', () => { - assertAwaitDetection(`if (ok) { await foo } else { await bar }`) - }) - - test('multiple `if` nested statements', () => { - assertAwaitDetection(`if (ok) { - let a = 'foo' - await 0 + await 1 - await 2 - } else if (a) { - await 10 - if (b) { - await 0 + await 1 - } else { - let a = 'foo' - await 2 - } - if (b) { - await 3 - await 4 - } - } else { - await 5 - }`) - }) - - test('multiple `if while` nested statements', () => { - assertAwaitDetection(`if (ok) { - while (d) { - await 5 - } - while (d) { - await 5 - await 6 - if (c) { - let f = 10 - 10 + await 7 - } else { - await 8 - await 9 - } - } - }`) - }) - - test('multiple `if for` nested statements', () => { - assertAwaitDetection(`if (ok) { - for (let a of [1,2,3]) { - await a - } - for (let a of [1,2,3]) { - await a - await a - } - }`) - }) - - test('should ignore await inside functions', () => { - // function declaration - assertAwaitDetection(`async function foo() { await bar }`, false) - // function expression - assertAwaitDetection(`const foo = async () => { await bar }`, false) - // object method - assertAwaitDetection(`const obj = { async method() { await bar }}`, false) - // class method - assertAwaitDetection( - `const cls = class Foo { async method() { await bar }}`, - false - ) - }) - }) - describe('errors', () => { test('