diff --git a/packages/compiler-core/src/transforms/transformExpression.ts b/packages/compiler-core/src/transforms/transformExpression.ts index 1e223067..6256569f 100644 --- a/packages/compiler-core/src/transforms/transformExpression.ts +++ b/packages/compiler-core/src/transforms/transformExpression.ts @@ -263,8 +263,9 @@ export function processExpression( return ret } -const isFunction = (node: Node): node is Function => - /Function(Expression|Declaration)$/.test(node.type) +const isFunction = (node: Node): node is Function => { + return /Function(?:Expression|Declaration)$|Method$/.test(node.type) +} const isStaticProperty = (node: Node): node is ObjectProperty => node && diff --git a/packages/compiler-sfc/__tests__/compileScript.spec.ts b/packages/compiler-sfc/__tests__/compileScript.spec.ts index 4be8f5e1..4015683f 100644 --- a/packages/compiler-sfc/__tests__/compileScript.spec.ts +++ b/packages/compiler-sfc/__tests__/compileScript.spec.ts @@ -49,10 +49,6 @@ describe('SFC compile `) + expect(content).toMatch( + `export ${shouldAsync ? `async ` : ``}function setup` + ) + } + + test('expression statement', () => { + assertAwaitDetection(`await foo`) + }) + + test('variable', () => { + assertAwaitDetection(`const a = 1 + (await foo)`) + }) + + test('export', () => { + assertAwaitDetection(`export const a = 1 + (await foo)`) + }) + + test('nested statements', () => { + assertAwaitDetection(`if (ok) { await foo } else { await bar }`) + }) + + test('should ignore await inside functions', () => { + // function declaration + assertAwaitDetection(`export 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('