Skip to content

Commit

Permalink
fix: use alternative approach
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Apr 26, 2022
1 parent a42582f commit b4732dc
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,29 @@ return { }
}"
`;

exports[`SFC compile <script setup> async/await detection nested statements with preceding statements 1`] = `
"import { withAsyncContext as _withAsyncContext } from 'vue'

export default {
async setup(__props, { expose }) {
expose();

let __temp, __restore

if (ok) {
const a = 'test'
;(
([__temp,__restore] = _withAsyncContext(() => foo)),
await __temp,
__restore()
)
}
return { }
}

}"
`;

exports[`SFC compile <script setup> async/await detection ref 1`] = `
"import { withAsyncContext as _withAsyncContext, ref as _ref } from 'vue'

Expand Down
8 changes: 8 additions & 0 deletions packages/compiler-sfc/__tests__/compileScript.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1164,6 +1164,14 @@ const emit = defineEmits(['a', 'b'])
assertAwaitDetection(`if (ok) { await foo } else { await bar }`)
})

test('nested statements with preceding statements', () => {
assertAwaitDetection(`
if (ok) {
const a = 'test'
await foo
}`)
})

test('should ignore await inside functions', () => {
// function declaration
assertAwaitDetection(`async function foo() { await bar }`, false)
Expand Down
9 changes: 8 additions & 1 deletion packages/compiler-sfc/src/compileScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,13 @@ export function compileScript(
)

const containsNestedAwait = /\bawait\b/.test(argumentStr)
for (let pos = startOffset + node.start! - 1; pos > 0; pos--) {
if (/\s/.test(source[pos])) {
continue
}
needSemi = needSemi || /['"`A-Za-z0-9_$\]]/.test(source[pos])
break
}

s.overwrite(
node.start! + startOffset,
Expand Down Expand Up @@ -1067,7 +1074,7 @@ export function compileScript(
}
if (child.type === 'AwaitExpression') {
hasAwait = true
const needsSemi = [parent, ...scriptSetupAst.body].some(n => {
const needsSemi = scriptSetupAst.body.some(n => {
return n.type === 'ExpressionStatement' && n.start === child.start
})
processAwait(
Expand Down

0 comments on commit b4732dc

Please sign in to comment.