Skip to content

Conversation

@Ocean-OS
Copy link
Member

@Ocean-OS Ocean-OS commented Oct 24, 2025

Closes #17012.
As described here, the way we transform computed class fields is using a variable to deduplicate the key (to avoid side effects and the like):

// before
let i = 0;
class Foo {
  [i++] = $state(0);
}
// after
let i = 0;
var Foo = (() => {
  let key;
  return class Foo {
    #_ = $.state(0);
    get [key = i++]() {
      return $.get(this.#_);
    }
    set [key]($$value) {
      return $.set(this.#_, $$value, true);
    }
  }
})();

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

🦋 Changeset detected

Latest commit: 307e42f

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

@svelte-docs-bot
Copy link

@github-actions
Copy link
Contributor

Playground

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

@Ocean-OS Ocean-OS marked this pull request as ready for review October 24, 2025 05:09

const member = b.member(b.this, field.key);

const should_proxy = field.type === '$state' && true; // TODO
Copy link

@dangodai dangodai Oct 25, 2025

Choose a reason for hiding this comment

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

This predates your change, but it looks like the second condition (true) is from a leftover TODO that never came to fruition? There are 2 more copies of it now in this file. (I was bothered by it when reading the source, just not enough to make a PR for it 😅 )

Also looks like at least member and should_proxy can be de-duplicated and shared between computed and non-computed branches.

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.

Cannot use runes with class fields using a Symbol identifier

2 participants