Skip to content

Commit e1d0d00

Browse files
authored
[feat] allow shorthand {#await ... then/catch} (sveltejs#6564)
1 parent 32b376b commit e1d0d00

File tree

5 files changed

+154
-6
lines changed

5 files changed

+154
-6
lines changed

src/compiler/parse/state/mustache.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -290,16 +290,24 @@ export default function mustache(parser: Parser) {
290290

291291
const await_block_shorthand = type === 'AwaitBlock' && parser.eat('then');
292292
if (await_block_shorthand) {
293-
parser.require_whitespace();
294-
block.value = read_context(parser);
295-
parser.allow_whitespace();
293+
if (parser.match_regex(/\s*}/)) {
294+
parser.allow_whitespace();
295+
} else {
296+
parser.require_whitespace();
297+
block.value = read_context(parser);
298+
parser.allow_whitespace();
299+
}
296300
}
297301

298302
const await_block_catch_shorthand = !await_block_shorthand && type === 'AwaitBlock' && parser.eat('catch');
299303
if (await_block_catch_shorthand) {
300-
parser.require_whitespace();
301-
block.error = read_context(parser);
302-
parser.allow_whitespace();
304+
if (parser.match_regex(/\s*}/)) {
305+
parser.allow_whitespace();
306+
} else {
307+
parser.require_whitespace();
308+
block.error = read_context(parser);
309+
parser.allow_whitespace();
310+
}
303311
}
304312

305313
parser.eat('}', true);
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
let fulfil;
2+
3+
let thePromise = new Promise(f => {
4+
fulfil = f;
5+
});
6+
7+
export default {
8+
props: {
9+
thePromise
10+
},
11+
12+
html: `
13+
<br />
14+
<p>the promise is pending</p>
15+
`,
16+
17+
async test({ assert, component, target }) {
18+
fulfil(42);
19+
20+
await thePromise;
21+
22+
assert.htmlEqual(target.innerHTML, '<br />');
23+
24+
let reject;
25+
26+
thePromise = new Promise((f, r) => {
27+
reject = r;
28+
});
29+
30+
component.thePromise = thePromise;
31+
32+
assert.htmlEqual(target.innerHTML, `
33+
<br />
34+
<p>the promise is pending</p>
35+
`);
36+
37+
reject(new Error());
38+
39+
await thePromise.catch(() => {});
40+
41+
assert.htmlEqual(target.innerHTML, `
42+
<p>oh no! Something broke!</p>
43+
<br />
44+
<p>oh no! Something broke!</p>
45+
`);
46+
}
47+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<script>
2+
export let thePromise;
3+
</script>
4+
5+
{#await thePromise catch}
6+
<p>oh no! Something broke!</p>
7+
{/await}
8+
9+
<br />
10+
11+
{#await thePromise}
12+
<p>the promise is pending</p>
13+
{:catch}
14+
<p>oh no! Something broke!</p>
15+
{/await}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
let fulfil;
2+
3+
let thePromise = new Promise(f => {
4+
fulfil = f;
5+
});
6+
7+
export default {
8+
props: {
9+
thePromise
10+
},
11+
12+
html: `
13+
<br>
14+
<br>
15+
<p>the promise is pending</p>
16+
`,
17+
18+
async test({ assert, component, target }) {
19+
fulfil();
20+
21+
await thePromise;
22+
23+
assert.htmlEqual(target.innerHTML, `
24+
<p>the promise is resolved</p>
25+
<br>
26+
<p>the promise is resolved</p>
27+
<br>
28+
<p>the promise is resolved</p>
29+
`);
30+
31+
let reject;
32+
33+
thePromise = new Promise((f, r) => {
34+
reject = r;
35+
});
36+
37+
component.thePromise = thePromise;
38+
39+
assert.htmlEqual(target.innerHTML, `
40+
<br>
41+
<br>
42+
<p>the promise is pending</p>
43+
`);
44+
45+
reject(new Error('something broke'));
46+
47+
await thePromise.catch(() => {});
48+
49+
assert.htmlEqual(target.innerHTML, `
50+
<p>oh no! something broke</p>
51+
<br>
52+
<br>
53+
`);
54+
}
55+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<script>
2+
export let thePromise;
3+
</script>
4+
5+
{#await thePromise then}
6+
<p>the promise is resolved</p>
7+
{:catch theError}
8+
<p>oh no! {theError.message}</p>
9+
{/await}
10+
11+
<br />
12+
13+
{#await thePromise then}
14+
<p>the promise is resolved</p>
15+
{/await}
16+
17+
<br />
18+
19+
{#await thePromise}
20+
<p>the promise is pending</p>
21+
{:then}
22+
<p>the promise is resolved</p>
23+
{/await}

0 commit comments

Comments
 (0)