new feature: Private Fields (T7508) #4408

Open
babel-bot opened this Issue Jul 25, 2016 · 13 comments

Projects

None yet

5 participants

@babel-bot
Collaborator

Issue originally made by @JounQin

Bug information

  • Babel version: 6.11.4
  • Node version: v4.4.7
  • npm version: 3.10.5

Description

[[ https://github.com/tc39/proposal-private-fields | Private Fields ]]

class Point {

    #x;
    #y;

    constructor(x = 0, y = 0) {
        this.#x = +x;
        this.#y = +y;
    }

    get x() { return this.#x }
    set x(value) { this.#x = +value }

    get y() { return this.#y }
    set y(value) { this.#y = +value }

    equals(p) { return this.#x === p.#x && this.#y === p.#y }

    toString() { return `Point<${ this.#x },${ this.#y }>` }

}
@babel-bot
Collaborator

Comment originally made by @isiahmeadows

If this is of any help, I explained a way this could be fully and correctly transpiled to ES6 using WeakMaps here, [1] and most notably, the spec uses. ES5 is a different story, as it doesn't have a weak map, nor anything that could resemble one. The closest you could get would be an almost-correct polyfill using Symbols + the core-js polyfill if necessary.

Oh, and it's an early error (i.e. syntax error) if a private reference can't be statically resolved, so the error should be thrown from Babylon, not any transformer.

[1] The current proposal lives here, but it hasn't changed much.

@hzoo hzoo added the es-proposal label Sep 10, 2016
@chicoxyzzy
Contributor

Current proposal has changed recently. Now Private Fields are based on Internal Slots rather than WeakMaps. However WeakMaps still could be used to transpile feature to ES6 (tc39/proposal-private-fields#42 (comment), https://github.com/tc39/proposal-private-fields#private-field-identiers)

@tibdex tibdex referenced this issue in tibdex/babel-plugin-transform-private-underscore Dec 10, 2016
Open

[Proposal] Transform # access modifier due to tc39/proposal-private-fields #2

@diervo
diervo commented Dec 10, 2016

FYI, I'm working with @littledan on this.
@chicoxyzzy let me know if you want to help, maybe we can share some of the work.

@hzoo
Member
hzoo commented Dec 10, 2016

@diervo please share the PR whenever you feel comfortable! Can also test stuff out on http://astexplorer.net/ and share with us if you want feedback

@diervo
diervo commented Dec 10, 2016 edited

Thanks @hzoo I will start with babylon grammar changes first, so bear with me :)

@hzoo
Member
hzoo commented Dec 10, 2016

Cool! Plenty of over stuff that needs to be worked on 😄 (you are on slack right)?

@chicoxyzzy
Contributor
chicoxyzzy commented Dec 13, 2016 edited

@diervo feel free to ping me (here or in slack) if you need any help! I will have some free time in the end of this week and I can and want to help!

@diervo
diervo commented Dec 14, 2016

I have some pieces on the grammar done in babylon, but trying to cleanup some stuff so will be easy to merge both public and private fields in the next mayor version.
Will try to do the first pass by EOD Thursday, and push a branch.

@chicoxyzzy
Contributor

Hmm... Why should private and public fields be merged?

@diervo
diervo commented Dec 15, 2016

Will be almost certainly a prerequisite for stage3 that they get presented "together or as a package" probably aligning very closely.

I said merge in the sense that the code might end up differing just on a flag private on the classField type, so I'm just making sure I clean a little bit the classBody code, so its easy to refactor in the future.

@diervo
diervo commented Dec 18, 2016 edited

I just pushed a PR with the grammar changes on babylon, feedback is welcome. @chicoxyzzy @littledan @hzoo

@krnlde
krnlde commented Jan 2, 2017 edited

The proposal changed a bit, it's just #x instead of this.#x:

class Point {

    #x;
    #y;

    constructor(x = 0, y = 0) {
        #x = +x;
        #y = +y;
    }
   // ...
}
@diervo
diervo commented Jan 3, 2017

@krnlde If you re-read the spec again, both are valid. One is just syntactic sugar.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment