Skip to content

Commit 75ec466

Browse files
authored
Fix await block related bugs (#39)
* Fix await block related bugs * fix
1 parent d751fda commit 75ec466

12 files changed

+3099
-76
lines changed

src/ast.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,14 +335,14 @@ export interface SvelteAwaitPendingBlock extends BaseNode {
335335
/** Node of await then block. e.g. `{:then}` */
336336
export interface SvelteAwaitThenBlock extends BaseNode {
337337
type: "SvelteAwaitThenBlock"
338-
value: ESTree.Pattern
338+
value: ESTree.Pattern | null
339339
children: Child[]
340340
parent: SvelteAwaitBlock
341341
}
342342
/** Node of await catch block. e.g. `{:catch}` */
343343
export interface SvelteAwaitCatchBlock extends BaseNode {
344344
type: "SvelteAwaitCatchBlock"
345-
error: ESTree.Pattern
345+
error: ESTree.Pattern | null
346346
children: Child[]
347347
parent: SvelteAwaitBlock
348348
}

src/context/script-let.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,14 @@ export class ScriptLetContext {
378378
const p = fn.params[index]
379379
;(p as any).parent = parents![index]
380380
if (this.ctx.isTypeScript()) {
381+
const typeAnnotation = (p as any).typeAnnotation
381382
delete (p as any).typeAnnotation
383+
384+
p.range![1] = typeAnnotation.range[0]
385+
p.loc!.end = {
386+
line: typeAnnotation.loc.start.line,
387+
column: typeAnnotation.loc.start.column,
388+
}
382389
}
383390
}
384391

src/parser/converts/block.ts

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -213,23 +213,31 @@ export function convertAwaitBlock(
213213
const thenStart = awaitBlock.pending ? node.then.start : node.start
214214
const thenBlock: SvelteAwaitThenBlock = {
215215
type: "SvelteAwaitThenBlock",
216-
value: null as any,
216+
value: null,
217217
children: [],
218218
parent: awaitBlock,
219219
...ctx.getConvertLocation({
220220
start: thenStart,
221221
end: node.then.end,
222222
}),
223223
}
224-
ctx.scriptLet.nestBlock(
225-
thenBlock,
226-
[node.value],
227-
[thenBlock],
228-
([value]) => {
229-
thenBlock.value = value
230-
},
231-
[`typeof ${ctx.getText(node.expression)}`],
232-
)
224+
if (node.value) {
225+
ctx.scriptLet.nestBlock(
226+
thenBlock,
227+
[node.value],
228+
[thenBlock],
229+
([value]) => {
230+
thenBlock.value = value
231+
},
232+
[
233+
`Parameters<Parameters<(typeof ${ctx.getText(
234+
node.expression,
235+
)})["then"]>[0]>[0]`,
236+
],
237+
)
238+
} else {
239+
ctx.scriptLet.nestBlock(thenBlock)
240+
}
233241
thenBlock.children.push(...convertChildren(node.then, thenBlock, ctx))
234242
if (awaitBlock.pending) {
235243
extractMustacheBlockTokens(thenBlock, ctx, { startOnly: true })
@@ -253,7 +261,7 @@ export function convertAwaitBlock(
253261
: node.start
254262
const catchBlock: SvelteAwaitCatchBlock = {
255263
type: "SvelteAwaitCatchBlock",
256-
error: null as any,
264+
error: null,
257265
children: [],
258266
parent: awaitBlock,
259267
...ctx.getConvertLocation({
@@ -262,15 +270,19 @@ export function convertAwaitBlock(
262270
}),
263271
}
264272

265-
ctx.scriptLet.nestBlock(
266-
catchBlock,
267-
[node.error],
268-
[catchBlock],
269-
([error]) => {
270-
catchBlock.error = error
271-
},
272-
["Error"],
273-
)
273+
if (node.error) {
274+
ctx.scriptLet.nestBlock(
275+
catchBlock,
276+
[node.error],
277+
[catchBlock],
278+
([error]) => {
279+
catchBlock.error = error
280+
},
281+
["Error"],
282+
)
283+
} else {
284+
ctx.scriptLet.nestBlock(catchBlock)
285+
}
274286
catchBlock.children.push(
275287
...convertChildren(node.catch, catchBlock, ctx),
276288
)

src/parser/svelte-ast-types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ export interface AwaitBlock extends BaseNode {
7474
type: "AwaitBlock"
7575
expression: ESTree.Expression
7676
pending: PendingBlock
77-
value: ESTree.Pattern
77+
value: ESTree.Pattern | null
7878
then: ThenBlock
79-
error: ESTree.Pattern
79+
error: ESTree.Pattern | null
8080
catch: CatchBlock
8181
}
8282
export interface PendingBlock extends BaseNode {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<script>
2+
let expression = new Promise()
3+
</script>
4+
5+
{#await expression}
6+
{:then}
7+
{:catch}
8+
{/await}
9+
10+
{#await expression}
11+
{:then}
12+
{/await}
13+
14+
{#await expression then name}
15+
{/await}
16+
17+
{#await expression catch name}
18+
{/await}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[
2+
{
3+
"ruleId": "no-unused-vars",
4+
"code": "name",
5+
"line": 14,
6+
"column": 25
7+
},
8+
{
9+
"ruleId": "no-unused-vars",
10+
"code": "name",
11+
"line": 17,
12+
"column": 26
13+
}
14+
]

0 commit comments

Comments
 (0)