Skip to content

Conversation

@Teiem
Copy link

@Teiem Teiem commented Oct 22, 2025

This PR implements support for switch statements via {#switch ...} ... {/switch} and {:case ...}. There is a relevant RFC, though I only found it after already implementing the feature.

Example of valid syntax:

{#switch "foo"}
  <span>this is the default if nothing matches</span>
{:case "foo")}
  <span>this will be displayed</span>
{:case "bar"}
  <span>this will not be displayed</span>
{/switch}

Like with {#await ...} you can also place a case in the switch block if you do not need a default case, e.g.

{#switch "foo" case "foo"}
  <span>this will be displayed</span>
{:case "bar"}
  <span>this will not be displayed</span>
{/switch}

Tests are mostly stolen from the {#if ...} implementation.

The svelte language-tools would also have to be updated to work with this, I have started on that but am struggling to get the extension to correctly work in vscode even without any changes.

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
  • 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.
  • 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

@changeset-bot
Copy link

changeset-bot bot commented Oct 22, 2025

⚠️ No Changeset found

Latest commit: 33b7ab4

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

@svelte-docs-bot
Copy link

@github-actions
Copy link
Contributor

Playground

pnpm add https://pkg.pr.new/svelte@17010

@Conduitry
Copy link
Member

Conduitry commented Oct 22, 2025

Besides that RFC, see also #13507.

We'd previously rejected this because we couldn't settle on a nice block syntax that matched the semantics of a switch block, and I don't think that's changed. In this implementation, having the first {#switch} also include its own case seems ugly to me. And having the default/fallback be in the first sub-block seems confusing.

@Teiem
Copy link
Author

Teiem commented Oct 22, 2025

I assume changing the supported syntax here would not help then.

My main goal was to see if I could contribute to a big open source project, which I guess I can, though it would ofc have been nicer if the code would have ended up being used.

If there are any changes I should make lmk, otherwise feel free to close this.

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.

2 participants