Giter Site home page Giter Site logo

Comments (6)

paoloricciuti avatar paoloricciuti commented on June 2, 2024 1

I would add metadata.needs_private_getters to the class node and set that to true in the analysis phase when detecting a this expression that is not the first part of a member expression. Then in the code gen phase create those private getters/setters if that field is true

Great I'll try to give it a shot tomorrow if I have time.

from svelte.

paoloricciuti avatar paoloricciuti commented on June 2, 2024

Btw this is only true for private fields REPL.

This is because when a private field is accessed with this the code get's converted by adding a .v at the end which actually set the value of the signal. By doing it on an instance you are tricking the compiler in not doing that.

image

while with public fields the set still goes through the setter which actually does a set on the signal.

from svelte.

paoloricciuti avatar paoloricciuti commented on June 2, 2024

Basically here we should check if node.object.type is ThisExpression but also if the name is any name to which this has been assigned to:

if (node.object.type === 'ThisExpression') {
// rewrite `this.#foo` as `this.#foo.v` inside a constructor
if (node.property.type === 'PrivateIdentifier') {
const field = state.private_state.get(node.property.name);
if (field) {
return state.in_constructor ? b.member(node, b.id('v')) : b.call('$.get', node);
}
}
// rewrite `this.foo` as `this.#foo.v` inside a constructor
if (node.property.type === 'Identifier' && !node.computed) {
const field = state.public_state.get(node.property.name);
if (field && state.in_constructor) {
return b.member(b.member(b.this, field.id), b.id('v'));
}
}
}
next();

from svelte.

dummdidumm avatar dummdidumm commented on June 2, 2024

Is This the same as #11476 ? I think the easiest solution is to have a private getter/setter pair for each private state/derived backed by another private field. Bonus points for only doing that when we need to (i.e. when we detect this is assigned/passed to something)

from svelte.

paoloricciuti avatar paoloricciuti commented on June 2, 2024

Is This the same as #11476 ? I think the easiest solution is to have a private getter/setter pair for each private state/derived backed by another private field. Bonus points for only doing that when we need to (i.e. when we detect this is assigned/passed to something)

Yeah I think is the same. Since the problem is only there with private fields because public gets rewritten to private+get&set even if you return this you'll have no way to access them outside.

I like your idea...we should explore every method of the class to be sure this is not assigned to a class property right?

from svelte.

dummdidumm avatar dummdidumm commented on June 2, 2024

I would add metadata.needs_private_getters to the class node and set that to true in the analysis phase when detecting a this expression that is not the first part of a member expression. Then in the code gen phase create those private getters/setters if that field is true

from svelte.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.