Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add partial evaluation #15494

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open

feat: add partial evaluation #15494

wants to merge 13 commits into from

Conversation

Rich-Harris
Copy link
Member

@Rich-Harris Rich-Harris commented Mar 11, 2025

Noticed while looking into something unrelated that this input...

<script>
  let choice = $state('one');
</script>

<input type="radio" bind:group={choice} value="one" />
<input type="radio" bind:group={choice} value="two" />
<input type="radio" bind:group={choice} value="three" />

<p>{choice}</p>

...yields this output:

input.value = null == (input.__value = 'one') ? '' : 'one';

At minimum, it should be this instead:

input.value = input.__value = 'one' ?? '';

But we can go much further. Something I've been meaning to get round to for a while is to add some simple partial evaluation, which would make it possible to avoid the ?? '' stuff in a lot more cases. @Ocean-OS made some progress on this front in #15374 but I'd like it to be able to accommodate bindings as well (in other words, knowing that message is always a string and count is always a number). I think it makes sense to implement this as an evaluate method on the Scope 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
  • 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

Copy link

changeset-bot bot commented Mar 11, 2025

🦋 Changeset detected

Latest commit: 696bb2e

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

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

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

Copy link
Contributor

Playground

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

@Rich-Harris Rich-Harris marked this pull request as ready for review March 14, 2025 00:34
@Rich-Harris
Copy link
Member Author

Rich-Harris commented Mar 14, 2025

Marking as ready for review, even though there's room for future improvement:

  • marking some bindings as always defined (e.g. each block indices), so they don't need ?? ''
  • tracking reassignments and bind: directives (e.g. if count = 0 and count++ then we know that count is always a number
  • skipping $.escape calls where unnecessary (e.g. because value is a number)
  • baking more stuff into $.template calls
  • maybe simplifying gather_possible_values
  • hoisting stuff
  • (one day) evaluating imports and props

@Ocean-OS
Copy link
Contributor

Is this PR meant to be combined with #15374, or just replace it?

@svelte-docs-bot
Copy link

@Rich-Harris
Copy link
Member Author

I think it's probably a replacement given their overlap. Which isn't a commentary on your PR! Convergent evolution; I started working on this before I understood the degree to which we were addressing a similar problem.

The reason I favour this PR over #15374 is that it feels more future-proof to have an Evaluation class with different properties than a single value which could be DYNAMIC. For example it's useful to know that an each index binding is a) defined and b) always a number, because then we can ditch the ?? '' in the client output and the $.escape(...) in the server output. I suspect it will also be useful to know that a ? 'b' : 'c' will always be either "b" or "c" when doing CSS pruning, for example.

Which isn't to say this analysis is particularly sophisticated — it definitely isn't, compared to what a good general purpose optimiser will give you. But it gives us something to build from. (Maybe one day we'll be able to incorporate type information and so on.)

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