Skip to content
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

Parse destructuring private fields #13931

Conversation

JLHwung
Copy link
Contributor

@JLHwung JLHwung commented Nov 4, 2021

Q                       A
Fixed Issues? Implement parsing of destructuring private proposal
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature? Y
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

Todo:

  • typings update
  • syntax plugin
  • generator support
  • standalone

This PR implements parsing support for the proposal.

When parsing a binding property, it is unambiguous that { #x: x } must be a pattern so we parse the private name and push it to the object pattern.

When parsing an assignment property, { #x: x } is ambiguous because it can be an object expression or an object pattern. We register the first private key position as refExpressionErrors.privateKey and then decide to throw or discard when the ambiguity is resolved. We add expectPlugin check when the private key is consumed in the object pattern, so users won't see the error "expect experimental destructuringPrivate plugin" when the private key is used in an object literal, which can be quite misleading.

This PR includes commits from #13929, I will rebase after it gets merged.

Preview REPL (Currently it throws Unknown private name #x because the private field transform can't handle private name in property key yet.)

@JLHwung JLHwung added PR: New Feature 🚀 A type of pull request used for our changelog categories Spec: Destructuring Private Fields labels Nov 4, 2021
@codesandbox-ci
Copy link

codesandbox-ci bot commented Nov 4, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit ce9dba3:

Sandbox Source
babel-repl-custom-plugin Configuration
babel-plugin-multi-config Configuration

@babel-bot
Copy link
Collaborator

babel-bot commented Nov 4, 2021

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/49930/

@JLHwung JLHwung marked this pull request as ready for review November 5, 2021 19:37
@JLHwung JLHwung force-pushed the parse-destructuring-private branch 3 times, most recently from cb6eadf to 1c1b938 Compare November 12, 2021 19:03
@@ -1,9 +1,6 @@
{
"type": "File",
"start":0,"end":46,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":1}},
"errors": [
"SyntaxError: Private names can only be used as the name of a class element (i.e. class C { #p = 42; #m() {} } )\n or a property of member expression (i.e. this.#p). (3:11)"
],
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This test has been moved to packages/babel-parser/test/fixtures/experimental/_no-plugin/destructuring-private-arrow-params/input.js and throws expected plugins instead.

This test is also duplicated to packages/babel-parser/test/fixtures/experimental/destructuring-private/valid-arrow-params/input.js as a test case for destructuring private. Here git confusingly marks that output.json is moved to destructuring-private.

packages/babel-parser/src/parser/lval.js Show resolved Hide resolved
"type": "File",
"start":0,"end":46,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":1}},
"errors": [
"SyntaxError: Private names can only be used as the name of a class element (i.e. class C { #p = 42; #m() {} } )\n or a property of member expression (i.e. this.#p). (3:16)"
Copy link
Member

Choose a reason for hiding this comment

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

When the plugin is enabled, we should update the error message to also mention destructuring.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The current error message does not cover #p in o, either. At this point I feel like listing all the valid cases in error messages is a bit verbose. Should we just say unexpected private field?

Copy link
Member

Choose a reason for hiding this comment

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

Uh yes, if adding every case it's too much lets stay vague to cover all of them.

@JLHwung JLHwung changed the base branch from feat-7.17.0/destructuring-private to main November 15, 2021 16:50
@JLHwung JLHwung force-pushed the parse-destructuring-private branch 2 times, most recently from 8e2fb74 to ce9dba3 Compare November 15, 2021 20:43
Copy link
Member

@nicolo-ribaudo nicolo-ribaudo left a comment

Choose a reason for hiding this comment

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

Approving, but I think the error message should be updated either to include all possible cases or none of them (half of them doesn't really make sense).

@JLHwung
Copy link
Contributor Author

JLHwung commented Nov 17, 2021

I agree. I will update the error message to the vague "Unexpected private field".

@nicolo-ribaudo
Copy link
Member

Maybe "Private name", which is the name of the syntactic element (private field is the "thing" installed on an object that you access with its private name).

@JLHwung JLHwung force-pushed the parse-destructuring-private branch 2 times, most recently from 4481524 to b1aa55a Compare November 17, 2021 19:36
@@ -2197,8 +2206,12 @@ export default class ExpressionParser extends LValParser {
return node;
}

// https://tc39.es/ecma262/#prod-PropertyName
// when refExpressionErrors presents, it will parse private name
Copy link
Member

Choose a reason for hiding this comment

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

Nit:

Suggested change
// when refExpressionErrors presents, it will parse private name
// when refExpressionErrors is present, it will parse private name

Copy link
Member

@existentialism existentialism left a comment

Choose a reason for hiding this comment

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

Nice work!

@nicolo-ribaudo nicolo-ribaudo added this to the v7.17.0 milestone Nov 19, 2021
@nicolo-ribaudo
Copy link
Member

@JLHwung If you want to work on the transform, make sure to merge this to a feat-7.17.0 branch and not to main (it currently targets main).

@JLHwung JLHwung changed the base branch from main to feat-7.17.0/destructuring-private November 24, 2021 19:34
@JLHwung
Copy link
Contributor Author

JLHwung commented Nov 25, 2021

Merged to feat-7.17.0/destructuring-private

@JLHwung JLHwung merged commit c87850c into babel:feat-7.17.0/destructuring-private Nov 25, 2021
@JLHwung JLHwung deleted the parse-destructuring-private branch November 25, 2021 20:25
JLHwung added a commit that referenced this pull request Nov 26, 2021
* feat: parse destructuring private

* add test cases

* fix: register private name on destructuring

* add typings

* add syntax plugin

* add generator test case

* add syntax plugin to preset-stage-2

* fix flow errors

* address review comments

* fix: use private name in toAssignable

* test: add case with undefined refExpressionErrors
JLHwung added a commit that referenced this pull request Feb 1, 2022
* feat: parse destructuring private

* add test cases

* fix: register private name on destructuring

* add typings

* add syntax plugin

* add generator test case

* add syntax plugin to preset-stage-2

* fix flow errors

* address review comments

* fix: use private name in toAssignable

* test: add case with undefined refExpressionErrors
JLHwung added a commit that referenced this pull request Feb 1, 2022
* feat: parse destructuring private

* add test cases

* fix: register private name on destructuring

* add typings

* add syntax plugin

* add generator test case

* add syntax plugin to preset-stage-2

* fix flow errors

* address review comments

* fix: use private name in toAssignable

* test: add case with undefined refExpressionErrors
nicolo-ribaudo pushed a commit that referenced this pull request Feb 1, 2022
* feat: parse destructuring private

* add test cases

* fix: register private name on destructuring

* add typings

* add syntax plugin

* add generator test case

* add syntax plugin to preset-stage-2

* fix flow errors

* address review comments

* fix: use private name in toAssignable

* test: add case with undefined refExpressionErrors
@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Feb 25, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 25, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: New Feature 🚀 A type of pull request used for our changelog categories Spec: Destructuring Private Fields
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants