Skip to content

Commit cab9541

Browse files
committed
refactor(compiler-sfc): improve TLA codegen
1 parent 39cebf5 commit cab9541

File tree

3 files changed

+70
-86
lines changed

3 files changed

+70
-86
lines changed

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

+40-77
Original file line numberDiff line numberDiff line change
@@ -96,37 +96,26 @@ export default /*#__PURE__*/ Object.assign(__default__, {
9696
})"
9797
`;
9898
99-
exports[`SFC compile <script setup> async/await detection await in expression statement 1`] = `
99+
exports[`SFC compile <script setup> async/await detection expression statement 1`] = `
100100
"import { withAsyncContext as _withAsyncContext } from 'vue'
101101
102102
export default {
103103
async setup(__props, { expose }) {
104104
expose()
105105
106106
let __temp, __restore
107-
foo()
108107
;(
109-
([__temp,__restore] = _withAsyncContext(() => {
110-
return 1
111-
})),
112-
__temp = await __temp,
113-
__restore(),
114-
__temp
115-
) + (
116-
([__temp,__restore] = _withAsyncContext(() => {
117-
return 2
118-
})),
119-
__temp = await __temp,
120-
__restore(),
121-
__temp
108+
([__temp,__restore] = _withAsyncContext(() => foo)),
109+
await __temp,
110+
__restore()
122111
)
123112
return { }
124113
}
125114
126115
}"
127116
`;
128117
129-
exports[`SFC compile <script setup> async/await detection expression statement 1`] = `
118+
exports[`SFC compile <script setup> async/await detection nested await 1`] = `
130119
"import { withAsyncContext as _withAsyncContext } from 'vue'
131120
132121
export default {
@@ -135,20 +124,22 @@ export default {
135124
136125
let __temp, __restore
137126
;(
138-
([__temp,__restore] = _withAsyncContext(() => {
139-
return foo
140-
})),
127+
([__temp,__restore] = _withAsyncContext(async () => ((
128+
([__temp,__restore] = _withAsyncContext(() => foo)),
141129
__temp = await __temp,
142130
__restore(),
143131
__temp
132+
)))),
133+
await __temp,
134+
__restore()
144135
)
145136
return { }
146137
}
147138
148139
}"
149140
`;
150141
151-
exports[`SFC compile <script setup> async/await detection nested await 1`] = `
142+
exports[`SFC compile <script setup> async/await detection nested await 2`] = `
152143
"import { withAsyncContext as _withAsyncContext } from 'vue'
153144
154145
export default {
@@ -157,27 +148,22 @@ export default {
157148
158149
let __temp, __restore
159150
;(
160-
([__temp,__restore] = _withAsyncContext(async () => {
161-
return ((
162-
([__temp,__restore] = _withAsyncContext(() => {
163-
return foo
164-
})),
165-
__temp = await __temp,
166-
__restore(),
167-
__temp
168-
))
169-
})),
151+
([__temp,__restore] = _withAsyncContext(async () => (((
152+
([__temp,__restore] = _withAsyncContext(() => foo)),
170153
__temp = await __temp,
171154
__restore(),
172155
__temp
156+
))))),
157+
await __temp,
158+
__restore()
173159
)
174160
return { }
175161
}
176162
177163
}"
178164
`;
179165
180-
exports[`SFC compile <script setup> async/await detection nested await 2`] = `
166+
exports[`SFC compile <script setup> async/await detection nested await 3`] = `
181167
"import { withAsyncContext as _withAsyncContext } from 'vue'
182168
183169
export default {
@@ -186,52 +172,42 @@ export default {
186172
187173
let __temp, __restore
188174
;(
189-
([__temp,__restore] = _withAsyncContext(async () => {
190-
return (((
191-
([__temp,__restore] = _withAsyncContext(() => {
192-
return foo
193-
})),
175+
([__temp,__restore] = _withAsyncContext(async () => ((
176+
([__temp,__restore] = _withAsyncContext(async () => ((
177+
([__temp,__restore] = _withAsyncContext(() => foo)),
194178
__temp = await __temp,
195179
__restore(),
196180
__temp
197-
)))
198-
})),
181+
)))),
199182
__temp = await __temp,
200183
__restore(),
201184
__temp
185+
)))),
186+
await __temp,
187+
__restore()
202188
)
203189
return { }
204190
}
205191
206192
}"
207193
`;
208194
209-
exports[`SFC compile <script setup> async/await detection nested await 3`] = `
195+
exports[`SFC compile <script setup> async/await detection nested leading await in expression statement 1`] = `
210196
"import { withAsyncContext as _withAsyncContext } from 'vue'
211197
212198
export default {
213199
async setup(__props, { expose }) {
214200
expose()
215201
216202
let __temp, __restore
203+
foo()
217204
;(
218-
([__temp,__restore] = _withAsyncContext(async () => {
219-
return ((
220-
([__temp,__restore] = _withAsyncContext(async () => {
221-
return ((
222-
([__temp,__restore] = _withAsyncContext(() => {
223-
return foo
224-
})),
205+
([__temp,__restore] = _withAsyncContext(() => 1)),
225206
__temp = await __temp,
226207
__restore(),
227208
__temp
228-
))
229-
})),
230-
__temp = await __temp,
231-
__restore(),
232-
__temp
233-
))
234-
})),
209+
) + (
210+
([__temp,__restore] = _withAsyncContext(() => 2)),
235211
__temp = await __temp,
236212
__restore(),
237213
__temp
@@ -251,19 +227,13 @@ export default {
251227
252228
let __temp, __restore
253229
if (ok) { (
254-
([__temp,__restore] = _withAsyncContext(() => {
255-
return foo
256-
})),
257-
__temp = await __temp,
258-
__restore(),
259-
__temp
230+
([__temp,__restore] = _withAsyncContext(() => foo)),
231+
await __temp,
232+
__restore()
260233
) } else { (
261-
([__temp,__restore] = _withAsyncContext(() => {
262-
return bar
263-
})),
264-
__temp = await __temp,
265-
__restore(),
266-
__temp
234+
([__temp,__restore] = _withAsyncContext(() => bar)),
235+
await __temp,
236+
__restore()
267237
) }
268238
return { }
269239
}
@@ -280,9 +250,7 @@ export default {
280250
281251
let __temp, __restore
282252
let a = _ref(1 + ((
283-
([__temp,__restore] = _withAsyncContext(() => {
284-
return foo
285-
})),
253+
([__temp,__restore] = _withAsyncContext(() => foo)),
286254
__temp = await __temp,
287255
__restore(),
288256
__temp
@@ -346,12 +314,9 @@ export default {
346314
347315
let __temp, __restore
348316
if (false) (
349-
([__temp,__restore] = _withAsyncContext(() => {
350-
return foo()
351-
})),
352-
__temp = await __temp,
353-
__restore(),
354-
__temp
317+
([__temp,__restore] = _withAsyncContext(() => foo())),
318+
await __temp,
319+
__restore()
355320
)
356321
return { }
357322
}
@@ -368,9 +333,7 @@ export default {
368333
369334
let __temp, __restore
370335
const a = 1 + ((
371-
([__temp,__restore] = _withAsyncContext(() => {
372-
return foo
373-
})),
336+
([__temp,__restore] = _withAsyncContext(() => foo)),
374337
__temp = await __temp,
375338
__restore(),
376339
__temp

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1089,7 +1089,7 @@ const emit = defineEmits(['a', 'b'])
10891089
})
10901090

10911091
// should prepend semicolon
1092-
test('await in expression statement', () => {
1092+
test('nested leading await in expression statement', () => {
10931093
const code = assertAwaitDetection(`foo()\nawait 1 + await 2`)
10941094
expect(code).toMatch(`foo()\n;(`)
10951095
})

packages/compiler-sfc/src/compileScript.ts

+29-8
Original file line numberDiff line numberDiff line change
@@ -507,13 +507,28 @@ export function compileScript(
507507
}
508508

509509
/**
510-
* await foo() -->
510+
* await foo()
511+
* -->
512+
* ;(
513+
* ([__temp,__restore] = withAsyncContext(() => foo())),
514+
* await __temp,
515+
* __restore()
516+
* )
511517
*
512-
* (([__temp, __restore] = withAsyncContext(async () => {
513-
* return foo()
514-
* })),__temp=await __temp,__restore(),__temp)
518+
* const a = await foo()
519+
* -->
520+
* const a = (
521+
* ([__temp, __restore] = withAsyncContext(() => foo())),
522+
* __temp = await __temp,
523+
* __restore(),
524+
* __temp
525+
* )
515526
*/
516-
function processAwait(node: AwaitExpression, needSemi: boolean) {
527+
function processAwait(
528+
node: AwaitExpression,
529+
needSemi: boolean,
530+
isStatement: boolean
531+
) {
517532
const argumentStart =
518533
node.argument.extra && node.argument.extra.parenthesized
519534
? (node.argument.extra.parenStart as number)
@@ -531,11 +546,13 @@ export function compileScript(
531546
argumentStart + startOffset,
532547
`${needSemi ? `;` : ``}(\n ([__temp,__restore] = ${helper(
533548
`withAsyncContext`
534-
)}(${containsNestedAwait ? `async ` : ``}() => {\n return `
549+
)}(${containsNestedAwait ? `async ` : ``}() => `
535550
)
536551
s.appendLeft(
537552
node.end! + startOffset,
538-
`\n })),\n __temp = await __temp,\n __restore(),\n __temp\n)`
553+
`)),\n ${isStatement ? `` : `__temp = `}await __temp,\n __restore()${
554+
isStatement ? `` : `,\n __temp`
555+
}\n)`
539556
)
540557
}
541558

@@ -937,7 +954,11 @@ export function compileScript(
937954
const needsSemi = scriptSetupAst.body.some(n => {
938955
return n.type === 'ExpressionStatement' && n.start === child.start
939956
})
940-
processAwait(child, needsSemi)
957+
processAwait(
958+
child,
959+
needsSemi,
960+
parent.type === 'ExpressionStatement'
961+
)
941962
}
942963
}
943964
})

0 commit comments

Comments
 (0)