Skip to content

Commit

Permalink
Support destructuring private proposal (prettier#12276)
Browse files Browse the repository at this point in the history
* Enable `destructuringPrivate` plugiin

* Add tests

* Add changelog

* Add more tests
  • Loading branch information
sosukesuzuki authored and nevilm-lt committed Apr 22, 2022
1 parent f6dd3b5 commit 93df59c
Show file tree
Hide file tree
Showing 14 changed files with 538 additions and 0 deletions.
15 changes: 15 additions & 0 deletions changelog_unreleased/javascript/12276.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#### Support destructuring private proposal (#12276 by @sosukesuzuki)

Support [Stage 2 TC39 proposal destructuring private fields](https://github.com/tc39/proposal-destructuring-private) via [Babel 7.17](https://babeljs.io/blog/2022/02/02/7.17.0).

<!-- prettier-ignore -->
```jsx
// Example
class Foo {
constructor() {
console.log(this.#x); // => 1
const { #x: x } = this;
console.log(x); // => 1
}
}
```
1 change: 1 addition & 0 deletions src/language-js/parse/babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const parseOptions = {
"moduleBlocks",
"asyncDoExpressions",
"regexpUnicodeSets",
"destructuringPrivate",
],
tokens: true,
ranges: true,
Expand Down
69 changes: 69 additions & 0 deletions tests/format/js/babel-plugins/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,75 @@ function enumerable(value) {
================================================================================
`;
exports[`destructuring-private.js [acorn] format 1`] = `
"Unexpected token (5:13)
3 | constructor() {
4 | console.log(this.#x); // => 1
> 5 | const { #x: x } = this;
| ^
6 | console.log(x); // => 1
7 | }
8 | equals({ #x: otherX }) {"
`;
exports[`destructuring-private.js [espree] format 1`] = `
"Unexpected token #x (5:13)
3 | constructor() {
4 | console.log(this.#x); // => 1
> 5 | const { #x: x } = this;
| ^
6 | console.log(x); // => 1
7 | }
8 | equals({ #x: otherX }) {"
`;
exports[`destructuring-private.js [meriyah] format 1`] = `
"[5:13]: Unexpected token: 'PrivateField' (5:13)
3 | constructor() {
4 | console.log(this.#x); // => 1
> 5 | const { #x: x } = this;
| ^
6 | console.log(x); // => 1
7 | }
8 | equals({ #x: otherX }) {"
`;
exports[`destructuring-private.js format 1`] = `
====================================options=====================================
parsers: ["babel", "babel-ts", "babel-flow"]
printWidth: 80
| printWidth
=====================================input======================================
class Foo {
#x = 1;
constructor() {
console.log(this.#x); // => 1
const { #x: x } = this;
console.log(x); // => 1
}
equals({ #x: otherX }) {
const { #x: currentX } = this;
return currentX === otherX;
}
}
=====================================output=====================================
class Foo {
#x = 1;
constructor() {
console.log(this.#x); // => 1
const { #x: x } = this;
console.log(x); // => 1
}
equals({ #x: otherX }) {
const { #x: currentX } = this;
return currentX === otherX;
}
}
================================================================================
`;
exports[`do-expressions.js [acorn] format 1`] = `
"Unexpected token (3:9)
1 | // https://babeljs.io/docs/en/babel-plugin-proposal-do-expressions
Expand Down
12 changes: 12 additions & 0 deletions tests/format/js/babel-plugins/destructuring-private.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Foo {
#x = 1;
constructor() {
console.log(this.#x); // => 1
const { #x: x } = this;
console.log(x); // => 1
}
equals({ #x: otherX }) {
const { #x: currentX } = this;
return currentX === otherX;
}
}
3 changes: 3 additions & 0 deletions tests/format/js/babel-plugins/jsfmt.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ run_spec(__dirname, ["babel", "babel-ts", "babel-flow"], {
acorn: [
"decimal.js",
"decorators.js",
"destructuring-private.js",
"do-expressions.js",
"export-default-from.js",
"flow.js",
Expand All @@ -29,6 +30,7 @@ run_spec(__dirname, ["babel", "babel-ts", "babel-flow"], {
espree: [
"decimal.js",
"decorators.js",
"destructuring-private.js",
"do-expressions.js",
"export-default-from.js",
"flow.js",
Expand All @@ -52,6 +54,7 @@ run_spec(__dirname, ["babel", "babel-ts", "babel-flow"], {
meriyah: [
"decimal.js",
"do-expressions.js",
"destructuring-private.js",
"export-default-from.js",
"flow.js",
"function-bind.js",
Expand Down

0 comments on commit 93df59c

Please sign in to comment.