fix: improve partial evaluation
#15781Minor, but given a component like this...
<script>
let a = $state(1);
let b = $state(2);
</script>
<p>{a} + {b} = {a + b}</p>
...neither a
nor b
is turned into state, because they are never updated. But the contents of the <p>
are put in an effect anyway:
export default function App($$anchor) {
let a = 1;
let b = 2;
var p = root();
var text = $.child(p);
p.textContent = `${a ?? ""} + ${b ?? ""} = ${a + b ?? ""}`;
$.reset(p);
$.template_effect(() => $.set_text(text, `${a ?? ""} + ${b ?? ""} = ${a + b ?? ""}`));
$.append($$anchor, p);
}
Whatever logic is used for determining whether it's a = 1
or a = $.state(1)
should also be used for determining whether to create an effect.
No response
next
annoyance
This further improves the partial evaluation implemented in #15494. It accounts for sources that are declared as $state
, but never reassigned to (and can be treated as constants), and other (somewhat more complex) cases.
A few possible end goals for this:
function get_pi() {
return 3.14159265358979; //constant value
}
String(stuff); //string type
Math.random(); //number
closes #14280
feat:
, fix:
, chore:
, or docs:
.packages/svelte/src
, add a changeset (npx changeset
).pnpm test
and lint the project with pnpm lint
Latest commit: 0ef68b9
The changes in this PR will be included in the next version bump.
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
pnpm add https://pkg.pr.new/svelte@15781
This is great, thank you! Hope you don't mind me jumping in, I started out with some minor comments but got carried away. Am keen to get this in to bring us slightly closer to #15292 (comment) — was there anything else you had planned for this PR or is it ready to go in its current form?
I was thinking of maybe looking into some of the goals I outlined, such as adding more globals and maybe some function analysis, how does that sound?
Adding more globals sounds good. I feel like function analysis could potentially become a bit of a rabbit hole with diminishing returns; maybe if you have something in mind it could be a good follow-up PR though?
I started working on function analysis last night, I'll try pushing it (along with more globals and a couple of other smallish things) now and if there are too many possible issues, I'll remove it (and maybe work on it in a follow-up PR)
That's not too bad, I honestly expected more failing tests. It appears that some of the tests failed just because the evaluation worked (and they were snapshots that didn't expect the change), but there are definitely some actually failing tests.
After looking more into the failing tests, I've decided against trying to work on this in this PR, but since it does seem rather promising, I'll work on it in another PR, probably after #15792 gets merged.
Currently the only failing test has to do with sourcemaps, and I don't quite understand it, could you please fix it?
looking into it
add more globals, template literals, try functions and (some) member expressions
• Apr 18, 2025, 4:29 AMUpdate packages/svelte/src/compiler/phases/3-transform/client/visitors/shared/utils.js
• Apr 18, 2025, 2:35 PM