mirror of https://github.com/sveltejs/svelte
fix: preserve logical assignments to private state fields (#18594)
Fixes #18592. Logical assignments to private `$state` fields were compiled as unconditional setter calls. This preserves JavaScript short-circuiting by evaluating the setter only when `||=`, `&&=`, or `??=` should assign. The regression sample covers the no-assignment branch for all three operators, alongside the existing assignment-path coverage. ### Before submitting the PR, please make sure you do the following - [x] 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 - [x] Prefix your PR title with `feat:`, `fix:`, `chore:`, or `docs:`. - [x] This message body should clearly illustrate what problems it solves. - [x] Ideally, include a test that fails without this PR but passes with it. - [x] If this PR changes code within `packages/svelte/src`, add a changeset (`npx changeset`). ### Tests and linting - [x] Run the tests with `pnpm test` and lint the project with `pnpm lint`pull/18673/head
parent
a5ea12ee93
commit
384ee73aa2
@ -0,0 +1,5 @@
|
||||
---
|
||||
'svelte': patch
|
||||
---
|
||||
|
||||
fix: preserve short-circuiting for logical assignments to private state fields
|
||||
@ -0,0 +1,5 @@
|
||||
import { test } from '../../test';
|
||||
|
||||
export default test({
|
||||
html: `<p>truthy||value</p>`
|
||||
});
|
||||
@ -0,0 +1,24 @@
|
||||
<script>
|
||||
class Values {
|
||||
#or = $state.raw('truthy');
|
||||
#and = $state.raw('');
|
||||
#nullish = $state.raw('value');
|
||||
|
||||
get or() {
|
||||
return (this.#or ||= 'assigned');
|
||||
}
|
||||
|
||||
get and() {
|
||||
return (this.#and &&= 'assigned');
|
||||
}
|
||||
|
||||
get nullish() {
|
||||
return (this.#nullish ??= 'assigned');
|
||||
}
|
||||
}
|
||||
|
||||
const values = new Values();
|
||||
const result = $derived([values.or, values.and, values.nullish]);
|
||||
</script>
|
||||
|
||||
<p>{result.join('|')}</p>
|
||||
Loading…
Reference in new issue