Skip to content

fix: allow to access private fields after this reassignment #11487

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

Merged
merged 2 commits into from
May 6, 2024
Merged

fix: allow to access private fields after this reassignment #11487

merged 2 commits into from
May 6, 2024

Conversation

paoloricciuti
Copy link
Member

@paoloricciuti paoloricciuti commented May 6, 2024

Svelte 5 rewrite

Closes #11480 and #11476

Please note that the Svelte codebase is currently being rewritten for Svelte 5. Changes should target Svelte 5, which lives on the default branch (main).

If your PR concerns Svelte 4 (including updates to svelte.dev.docs), please ensure the base branch is svelte-4 and not main.

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.

Tests and linting

  • Run the tests with pnpm test and lint the project with pnpm lint

Copy link

changeset-bot bot commented May 6, 2024

🦋 Changeset detected

Latest commit: 24ab911

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

Copy link
Member

@dummdidumm dummdidumm left a comment

Choose a reason for hiding this comment

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

Thank you!

@Rich-Harris
Copy link
Member

Do we need to add getters here? Can't we just rewrite instance.#count = 1 as instance.#count.v = 1 (if it's during construction) or set(instance.#count, 1)?

Copy link
Member

@Rich-Harris Rich-Harris left a comment

Choose a reason for hiding this comment

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

see comment

@dummdidumm
Copy link
Member

dummdidumm commented May 6, 2024

No we can't, because after this is assigned to a variable, that one can be passed around etc etc, so it's impossible to track all usages of it, which means it's more robust and consistent to use private getters.

const instance = this;
someFunctionSomewhereElse(instance)

@Rich-Harris
Copy link
Member

Are you sure? There are syntactical restrictions on where you can use private fields that should make this a non-issue. Can you show me a counterexample?

@Rich-Harris
Copy link
Member

(What I mean is that someFunctionSomewhereElse cannot refer to instance.#foo. It's a SyntaxError.)

@dummdidumm
Copy link
Member

dummdidumm commented May 6, 2024

Counter example. It's not possible for us to know what is what here (I mean, maybe we can with much more elaborate analysis, but I don't think it's worth it).

Mhhhhm or maybe we can just assume that when it's there that it works? Mhm, maybe it's enough to tweak the MemberExpression visitor in global.js to always check for a private identifier, instead of only when it's a this context?

@paoloricciuti
Copy link
Member Author

Mhhhhm or maybe we can just assume that when it's there that it works? Mhm, maybe it's enough to tweak the MemberExpression visitor in global.js to always check for a private identifier, instead of only when it's a this context?

That wouldn't even need the check on needs_private_identifiers right? Just remove the if and it should be gold?

@Rich-Harris
Copy link
Member

Ignore Svelte transformations for a moment and consider this case:

class A {
  #blah = 1;

  x() {
    this.y(this);
  }

  y(instance) {
    console.group('conditional access');
    if (#blah in instance) {
      console.log('#blah', instance.#blah);
    }
    console.groupEnd();

    console.group('unconditional access');
    console.log('#blah', instance.#blah);
    console.groupEnd();
  }
}

class B {
  #blah = 2;
}

If you do new A().x() it will log #blah 1 twice. If you do new A().y(new B()) the conditional access is skipped, and the unconditional access results in an error.

Any time you see foo.#bar it's 100% safe to assume that #bar refers to the current class. There's absolutely no need to mess around with getters here, it's needless indirection

@dummdidumm
Copy link
Member

I stand corrected, it is indeed much simpler to just always check the private property access 👍 adjusted the PR

@dummdidumm dummdidumm merged commit fa3e98e into sveltejs:main May 6, 2024
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.

Using this alias breaks reactivity
3 participants