8000 fix: lift unsafe_state_mutation constraints for SvelteSet and SvelteMap created inside the derived by gyzerok · Pull Request #16221 · sveltejs/svelte · GitHub
[go: up one dir, main page]

Skip to content

fix: lift unsafe_state_mutation constraints for SvelteSet and SvelteMap created inside the derived #16221

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

Conversation

gyzerok
Copy link
Contributor
@gyzerok gyzerok commented Jun 24, 2025

Closes #16211. Changes are mostly tests.

Currently mutating signals local to derived is allowed for primitives, arrays and objects, but not for sets and maps. Since the goal of derived is to be pure, mutating signals created inside it does not violate purity anyhow. This PR makes behavior consistent for sets and maps. Also covering all the other cases with tests to ensure future consistency.

const works = $derived.by(() => {
  let ret = $state([])
  ret.push(1)
  return ret
})

const throws = $derived.by(() => {
  let ret = new SvelteSet()
  ret.add(1)
  return ret
})

Update: also applied the change to SvelteDate, SvelteURL and SvelteURLSearchParams

Tests are passing, but pnpm lint is crashing with Error: Cannot find module ' /Code/svelte/node_modules/.pnpm/svelte-eslint-parser@0.43.0_svelte@packages+svelte/node_modules/svelte/compiler/index.js'. Not sure what to do about it.

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 Jun 24, 2025

🦋 Changeset detected

Latest commit: a784607

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

Fedor Nezhivoi added 2 commits June 24, 2025 10:42
@gyzerok gyzerok changed the title fix: lift unsafe_state_mutation constrained for SvelteSet and SvelteMap created inside the derived fix: lift unsafe_state_mutation constraints for SvelteSet and SvelteMap created inside the derived Jun 24, 2025
Copy link
Contributor

Playground

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

Copy link
Member
@paoloricciuti paoloricciuti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes sense...thanks for exploring and fix this! 🧡

@paoloricciuti paoloricciuti merged commit 2af7ba2 into sveltejs:main Jun 24, 2025
10 checks passed
@github-actions github-actions bot mentioned this pull request Jun 24, 2025
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.

state_unsafe_mutation where it shouldn't be?
2 participants
0