question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support private class properties

See original GitHub issue

Prettier 1.14.3 Playground link

--parser babylon

Input:

// @flow

class Test {
  #privateField: number;

  constructor() {
  this.#privateField = 1;
  }
}

Output:

SyntaxError: Unexpected token, expected ";" (4:16)
  2 | 
  3 | class Test {
> 4 |   #privateField: number;
    |                ^
  5 | 
  6 |   constructor() {
  7 |   this.#privateField = 1;

Expected behavior: It should format the code without any error.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
duailibecommented, Oct 24, 2018

The second is because Flow AST is

ClassProperty {
  key: Identifier "privateField"
}

whereas the Babylon is

ClassProperty {
  key: PrivateName {
    id: Identifier "privateField"
  }
}
1reaction
lydellcommented, Oct 23, 2018

Interestingly, when using the Flow parser instead of Babylon the code parses and prints but generates invalid code:

Prettier 1.14.3 Playground link

--parser flow

Input:

// @flow

class Test {
  #privateField: number;

  constructor() {
  this.#privateField = 1;
  }
}

Output:

// @flow

class Test {
  privateField: number;

  constructor() {
    this.#privateField = 1;
  }
}

Second Output:

SyntaxError: Private fields must be declared before they can be referenced. `#privateField` has not been declared. (7:10)
   5 | 
   6 |   constructor() {
>  7 |     this.#privateField = 1;
     |          ^^^^^^^^^^^^^
   8 |   }
   9 | }
  10 | 
Read more comments on GitHub >

github_iconTop Results From Across the Web

Private class features - JavaScript - MDN Web Docs - Mozilla
These features are collectively called private properties. However, constructors cannot be private in JavaScript. To prevent classes from ...
Read more >
JavaScript classes: Private class fields | Can I use... Support ...
"Can I use" provides up-to-date browser support tables for support of front-end web technologies on desktop and ... JavaScript classes: Private class fields....
Read more >
Private properties in JavaScript ES6 classes - Stack Overflow
Short answer, no, there is no native support for private properties with ES6 classes. But you could mimic that behaviour by not attaching...
Read more >
Doing it Right: Private Class Properties in JavaScript
Class fields are designed to simplify the process of creating private class properties. The syntax is very simple: add a # before the...
Read more >
JavaScript's New Private Class Fields, and How to Use Them
Class fields (also referred to as class properties) aim to deliver simpler constructors with private and static members.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found