Skip to content

Commit a0290fe

Browse files
liulinboyiyyx990803
authored andcommitted
fix(compiler-sfc): add test for #5808
1 parent 6c3b681 commit a0290fe

File tree

2 files changed

+220
-0
lines changed

2 files changed

+220
-0
lines changed

packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap

+167
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,173 @@ return { }
194194
}"
195195
`;
196196

197+
exports[`SFC compile <script setup> async/await detection multiple \`if for\` nested statements 1`] = `
198+
"import { withAsyncContext as _withAsyncContext } from 'vue'
199+
200+
export default {
201+
async setup(__props, { expose }) {
202+
expose();
203+
204+
let __temp, __restore
205+
if (ok) {
206+
for (let a of [1,2,3]) {
207+
(
208+
([__temp,__restore] = _withAsyncContext(() => a)),
209+
await __temp,
210+
__restore()
211+
)
212+
}
213+
for (let a of [1,2,3]) {
214+
(
215+
([__temp,__restore] = _withAsyncContext(() => a)),
216+
await __temp,
217+
__restore()
218+
)
219+
;(
220+
([__temp,__restore] = _withAsyncContext(() => a)),
221+
await __temp,
222+
__restore()
223+
)
224+
}
225+
}
226+
return { }
227+
}
228+
229+
}"
230+
`;
231+
232+
exports[`SFC compile <script setup> async/await detection multiple \`if while\` nested statements 1`] = `
233+
"import { withAsyncContext as _withAsyncContext } from 'vue'
234+
235+
export default {
236+
async setup(__props, { expose }) {
237+
expose();
238+
239+
let __temp, __restore
240+
if (ok) {
241+
while (d) {
242+
(
243+
([__temp,__restore] = _withAsyncContext(() => 5)),
244+
await __temp,
245+
__restore()
246+
)
247+
}
248+
while (d) {
249+
(
250+
([__temp,__restore] = _withAsyncContext(() => 5)),
251+
await __temp,
252+
__restore()
253+
)
254+
;(
255+
([__temp,__restore] = _withAsyncContext(() => 6)),
256+
await __temp,
257+
__restore()
258+
)
259+
if (c) {
260+
let f = 10
261+
10 + (
262+
([__temp,__restore] = _withAsyncContext(() => 7)),
263+
__temp = await __temp,
264+
__restore(),
265+
__temp
266+
)
267+
} else {
268+
(
269+
([__temp,__restore] = _withAsyncContext(() => 8)),
270+
await __temp,
271+
__restore()
272+
)
273+
;(
274+
([__temp,__restore] = _withAsyncContext(() => 9)),
275+
await __temp,
276+
__restore()
277+
)
278+
}
279+
}
280+
}
281+
return { }
282+
}
283+
284+
}"
285+
`;
286+
287+
exports[`SFC compile <script setup> async/await detection multiple \`if\` nested statements 1`] = `
288+
"import { withAsyncContext as _withAsyncContext } from 'vue'
289+
290+
export default {
291+
async setup(__props, { expose }) {
292+
expose();
293+
294+
let __temp, __restore
295+
if (ok) {
296+
let a = 'foo'
297+
;(
298+
([__temp,__restore] = _withAsyncContext(() => 0)),
299+
__temp = await __temp,
300+
__restore(),
301+
__temp
302+
) + (
303+
([__temp,__restore] = _withAsyncContext(() => 1)),
304+
__temp = await __temp,
305+
__restore(),
306+
__temp
307+
)
308+
;(
309+
([__temp,__restore] = _withAsyncContext(() => 2)),
310+
await __temp,
311+
__restore()
312+
)
313+
} else if (a) {
314+
(
315+
([__temp,__restore] = _withAsyncContext(() => 10)),
316+
await __temp,
317+
__restore()
318+
)
319+
if (b) {
320+
(
321+
([__temp,__restore] = _withAsyncContext(() => 0)),
322+
__temp = await __temp,
323+
__restore(),
324+
__temp
325+
) + (
326+
([__temp,__restore] = _withAsyncContext(() => 1)),
327+
__temp = await __temp,
328+
__restore(),
329+
__temp
330+
)
331+
} else {
332+
let a = 'foo'
333+
;(
334+
([__temp,__restore] = _withAsyncContext(() => 2)),
335+
await __temp,
336+
__restore()
337+
)
338+
}
339+
if (b) {
340+
(
341+
([__temp,__restore] = _withAsyncContext(() => 3)),
342+
await __temp,
343+
__restore()
344+
)
345+
;(
346+
([__temp,__restore] = _withAsyncContext(() => 4)),
347+
await __temp,
348+
__restore()
349+
)
350+
}
351+
} else {
352+
(
353+
([__temp,__restore] = _withAsyncContext(() => 5)),
354+
await __temp,
355+
__restore()
356+
)
357+
}
358+
return { }
359+
}
360+
361+
}"
362+
`;
363+
197364
exports[`SFC compile <script setup> async/await detection nested await 1`] = `
198365
"import { withAsyncContext as _withAsyncContext } from 'vue'
199366

packages/compiler-sfc/__tests__/compileScript.spec.ts

+53
Original file line numberDiff line numberDiff line change
@@ -1195,6 +1195,59 @@ const emit = defineEmits(['a', 'b'])
11951195
assertAwaitDetection(`if (ok) { await foo } else { await bar }`)
11961196
})
11971197

1198+
test('multiple `if` nested statements', () => {
1199+
assertAwaitDetection(`if (ok) {
1200+
let a = 'foo'
1201+
await 0 + await 1
1202+
await 2
1203+
} else if (a) {
1204+
await 10
1205+
if (b) {
1206+
await 0 + await 1
1207+
} else {
1208+
let a = 'foo'
1209+
await 2
1210+
}
1211+
if (b) {
1212+
await 3
1213+
await 4
1214+
}
1215+
} else {
1216+
await 5
1217+
}`)
1218+
})
1219+
1220+
test('multiple `if while` nested statements', () => {
1221+
assertAwaitDetection(`if (ok) {
1222+
while (d) {
1223+
await 5
1224+
}
1225+
while (d) {
1226+
await 5
1227+
await 6
1228+
if (c) {
1229+
let f = 10
1230+
10 + await 7
1231+
} else {
1232+
await 8
1233+
await 9
1234+
}
1235+
}
1236+
}`)
1237+
})
1238+
1239+
test('multiple `if for` nested statements', () => {
1240+
assertAwaitDetection(`if (ok) {
1241+
for (let a of [1,2,3]) {
1242+
await a
1243+
}
1244+
for (let a of [1,2,3]) {
1245+
await a
1246+
await a
1247+
}
1248+
}`)
1249+
})
1250+
11981251
test('should ignore await inside functions', () => {
11991252
// function declaration
12001253
assertAwaitDetection(`async function foo() { await bar }`, false)

0 commit comments

Comments
 (0)