Skip to content

fix: print a space before the catch binding in {#await ... catch x} - #18645

Open
winklemad wants to merge 1 commit into
sveltejs:mainfrom
winklemad:fix-print-await-catch-shorthand
Open

fix: print a space before the catch binding in {#await ... catch x}#18645
winklemad wants to merge 1 commit into
sveltejs:mainfrom
winklemad:fix-print-await-catch-shorthand

Conversation

@winklemad

Copy link
Copy Markdown

print() emits unparseable output for the shorthand {#await ... catch x} form. Round-tripping the example straight out of the await docs:

import { parse, print } from 'svelte/compiler';

const src = '{#await promise catch error}\n\t<p>The error is {error}</p>\n{/await}\n';
print(parse(src, { modern: true })).code;
// '{#await promise catcherror}\n\t<p>The error is {error}</p>\n{/await}'

parse(that, { modern: true });
// throws: Expected whitespace

The catch clause gates its trailing space on node.value — the then binding — instead of node.error:

 if (node.catch) {
-    context.write(node.value ? 'catch ' : 'catch');
+    context.write(node.error ? 'catch ' : 'catch');
     if (node.error) context.visit(node.error);

The then clause immediately above is correct (node.value ? 'then ' : 'then', paired with context.visit(node.value)), so this looks like it was copied down without swapping the property.

It also affects the long form whenever {:then} has no binding, since node.value is null there too:

{#await promise}…{:then}…{:catch error}   ->   {:catcherror}

{#await p}{:then v}{:catch e} round-trips today only incidentally, because node.value happens to be truthy.

node.error appears exactly once in the printer, and there are only two space-gating ternaries of this shape — line 692 (then, correct) and this one — so this is the whole class.

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
    • No issue filed — this is a small self-contained bug fix rather than a feature or large change. Happy to open one if you'd prefer it tracked.
  • Prefix your PR title with feat:, fix:, chore:, or docs:.
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.
    • tests/print/samples/await-block-shorthand. On main it fails with expected '{#await promise catcherror}…' to deeply equal '{#await promise catch error}…'. The sample also covers {:catch} and {#await p then v} with no binding, which must keep emitting a bare catch/then — so the fix can't over-correct.
  • If this PR changes code within packages/svelte/src, add a changeset (npx changeset).

Tests and linting

  • Run the tests with pnpm test and lint the project with pnpm lint
    • pnpm test: 33 files, 7687 passed, 69 skipped, 0 failed. pnpm lint: eslint clean, prettier clean.

The AwaitBlock printer gated the space after `catch` on `node.value`,
which is the then binding, rather than `node.error`. With no then
clause `node.value` is null, so `{#await promise catch error}` printed
as `{#await promise catcherror}`, which no longer parses. The same line
also broke `{:then}` followed by `{:catch error}`, printing `{:catcherror}`.
@pkg-svelte-dev

Copy link
Copy Markdown

Install the latest version of svelte from 831ffb3:

pnpm add https://pkg.svelte.dev/svelte/c/831ffb315b90c0d93611308573c728913e64b88b

Open in pkg.svelte.dev: https://pkg.svelte.dev/repos/svelte/pr/18645

Note

This PR is from a fork. A maintainer must approve approve each commit before it can be built and installed.

@changeset-bot

changeset-bot Bot commented Aug 15, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 831ffb3

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
svelte Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant