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

new feature: Private Fields (T7508) #4408

Closed
babel-bot opened this Issue Jul 25, 2016 · 23 comments

Comments

Projects
None yet
@babel-bot
Collaborator

babel-bot commented Jul 25, 2016

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

This comment has been minimized.

Show comment
Hide comment
@babel-bot

babel-bot Jul 26, 2016

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.

Collaborator

babel-bot commented Jul 26, 2016

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

This comment has been minimized.

Show comment
Hide comment
@chicoxyzzy

chicoxyzzy Dec 7, 2016

Member

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)

Member

chicoxyzzy commented Dec 7, 2016

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)

@diervo

This comment has been minimized.

Show comment
Hide comment
@diervo

diervo Dec 10, 2016

Contributor

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.

Contributor

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

This comment has been minimized.

Show comment
Hide comment
@hzoo

hzoo Dec 10, 2016

Member

@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

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

This comment has been minimized.

Show comment
Hide comment
@diervo

diervo Dec 10, 2016

Contributor

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

Contributor

diervo commented Dec 10, 2016

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

@hzoo

This comment has been minimized.

Show comment
Hide comment
@hzoo

hzoo Dec 10, 2016

Member

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

Member

hzoo commented Dec 10, 2016

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

@chicoxyzzy

This comment has been minimized.

Show comment
Hide comment
@chicoxyzzy

chicoxyzzy Dec 13, 2016

Member

@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!

Member

chicoxyzzy commented Dec 13, 2016

@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

This comment has been minimized.

Show comment
Hide comment
@diervo

diervo Dec 14, 2016

Contributor

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.

Contributor

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

This comment has been minimized.

Show comment
Hide comment
@chicoxyzzy

chicoxyzzy Dec 14, 2016

Member

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

Member

chicoxyzzy commented Dec 14, 2016

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

@diervo

This comment has been minimized.

Show comment
Hide comment
@diervo

diervo Dec 15, 2016

Contributor

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.

Contributor

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

This comment has been minimized.

Show comment
Hide comment
@diervo

diervo Dec 18, 2016

Contributor

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

Contributor

diervo commented Dec 18, 2016

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

@krnlde

This comment has been minimized.

Show comment
Hide comment
@krnlde

krnlde Jan 2, 2017

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;
    }
   // ...
}

krnlde commented Jan 2, 2017

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

This comment has been minimized.

Show comment
Hide comment
@diervo

diervo Jan 3, 2017

Contributor

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

Contributor

diervo commented Jan 3, 2017

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

@devblazer

This comment has been minimized.

Show comment
Hide comment
@devblazer

devblazer Feb 17, 2017

I am a bit new on staying up to date with latest features. I would really like to make use of this, how do I get this feature 'enabled' on my babel?

My package versions:
"babel": "^6.5.2",
"babel-core": "^6.23.1",
"babel-loader": "^6.3.2",
"babel-plugin-transform-decorators": "^6.13.0",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-preset-es2015": "^6.22.0",
"babel-preset-react": "^6.16.0",
"babel-preset-stage-2": "^6.22.0",
"webpack": "^2.2.1",
"webpack-dev-server": "^2.3.0"

My .babelrc:
{
"presets": [["es2015",{"modules":false}], "stage-2", "react"],
"plugins": ["transform-decorators-legacy"]
}

devblazer commented Feb 17, 2017

I am a bit new on staying up to date with latest features. I would really like to make use of this, how do I get this feature 'enabled' on my babel?

My package versions:
"babel": "^6.5.2",
"babel-core": "^6.23.1",
"babel-loader": "^6.3.2",
"babel-plugin-transform-decorators": "^6.13.0",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-preset-es2015": "^6.22.0",
"babel-preset-react": "^6.16.0",
"babel-preset-stage-2": "^6.22.0",
"webpack": "^2.2.1",
"webpack-dev-server": "^2.3.0"

My .babelrc:
{
"presets": [["es2015",{"modules":false}], "stage-2", "react"],
"plugins": ["transform-decorators-legacy"]
}

@loganfsmyth

This comment has been minimized.

Show comment
Hide comment
@loganfsmyth

loganfsmyth Feb 17, 2017

Member

@devblazer This issue is about implementing this language proposal, so it does not currently exist yet in Babel as a plugin.

Member

loganfsmyth commented Feb 17, 2017

@devblazer This issue is about implementing this language proposal, so it does not currently exist yet in Babel as a plugin.

@hzoo

This comment has been minimized.

Show comment
Hide comment
@hzoo

hzoo Jun 6, 2017

Member

New parser issue: babel/babylon#540

Join the #proposal-class-fields join our on https://babeljs.slack.com (signup at https://slack.babeljs.io)

Member

hzoo commented Jun 6, 2017

New parser issue: babel/babylon#540

Join the #proposal-class-fields join our on https://babeljs.slack.com (signup at https://slack.babeljs.io)

@montogeek

This comment has been minimized.

Show comment
Hide comment
@montogeek

montogeek Jun 7, 2017

Why # is used? Couldn't find information about that decision

montogeek commented Jun 7, 2017

Why # is used? Couldn't find information about that decision

@hzoo

This comment has been minimized.

Show comment
Hide comment
@hzoo

hzoo Jun 7, 2017

Member

Might just make a new issue since this one has a lot of random stuff in it given the updates.

@montogeek, a long issue: tc39/proposal-private-fields#14

Member

hzoo commented Jun 7, 2017

Might just make a new issue since this one has a lot of random stuff in it given the updates.

@montogeek, a long issue: tc39/proposal-private-fields#14

@gingur

This comment has been minimized.

Show comment
Hide comment
@gingur

gingur Jun 11, 2017

my interpretation of this feature via Proxy https://tinyurl.com/y9vsh85t

gingur commented Jun 11, 2017

my interpretation of this feature via Proxy https://tinyurl.com/y9vsh85t

@diervo

This comment has been minimized.

Show comment
Hide comment
@diervo

diervo Jun 11, 2017

Contributor

@gingur I believe you can't define private fields on the constructor.

Contributor

diervo commented Jun 11, 2017

@gingur I believe you can't define private fields on the constructor.

@nahumzs

This comment has been minimized.

Show comment
Hide comment

nahumzs commented Jun 12, 2017

@hzoo

This comment has been minimized.

Show comment
Hide comment
@hzoo

hzoo Jul 10, 2017

Member

@babel-bot move to babel/proposals

Member

hzoo commented Jul 10, 2017

@babel-bot move to babel/proposals

@hzoo hzoo closed this Jul 10, 2017

@babel-bot babel-bot referenced this issue Jul 10, 2017

Open

Class Fields (Stage 3) #12

2 of 5 tasks complete
@babel-bot

This comment has been minimized.

Show comment
Hide comment
@babel-bot

babel-bot Jul 10, 2017

Collaborator

Hey @babel-bot! I've moved your issue to the correct repository. Please make sure to keep an eye on the new issue for the latest information.

Collaborator

babel-bot commented Jul 10, 2017

Hey @babel-bot! I've moved your issue to the correct repository. Please make sure to keep an eye on the new issue for the latest information.

@lock lock bot added the outdated label May 4, 2018

@lock lock bot locked as resolved and limited conversation to collaborators May 4, 2018

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