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.

Babel should transform class private properties mixed with logical assignment operators

See original GitHub issue

Bug Report

This is a good first issue for first-time Babel contributors.

Current behavior A clear and concise description of the behavior.

throws

unknown: Property operator expected value to be one of ["+","-","/","%","*","**","&","|",">>",">>>","<<","^","==","===","!=","!==","in","instanceof",">","<",">=","<="] but got "??"

Input Code

class C {
  #x;
  m() { this.#x ??= 42 }
};

Expected behavior It should be transpiled successfully

Babel Configuration (babel.config.js, .babelrc, package.json#babel, cli command, .eslintrc)

  • Filename: babel.config.js
{
  "presets": [
    ["@babel/preset-env", {
      "targets": "Edge 16",
      "shippedProposals": true
    }]
  ],
  "plugins": ["@babel/plugin-proposal-logical-assignment-operators"]
}

Environment CodeSandbox

Possible Solution

When transforming private class properties, babel will call packages/babel-helper-member-expression-to-functions/src/index.js which transforms private property usage to helper calls. For example,

this.#x += 42

is approximately transformed to

_privateSet(this, x, _privateGet(this, x) + 42)

As you can see _privateGet(this, x) + 42 is a binary expression. But ?? is not a valid binary expression, so the error is thrown. To fix it, we should construct logical expression for logical assignment operators, namely ??=, ||= and &&=.

If you don’t know how to clone Babel, follow these steps: (you need to have make and yarn available on your machine).

  1. Write a comment there to let other possible contributors know that you are working on this bug.
  2. Fork the repo
  3. Run git clone https://github.com/<YOUR_USERNAME>/babel.git && cd babel
  4. Run yarn && make bootstrap
  5. Wait ⏳
  6. Run make watch (or make build whenever you change a file)
  7. Add a test (only input.js; output.js will be automatically generated) to packages/babel-plugin-proposal-class-properties/test/fixtures/private/logical-assignment (tip: you can copy from contents of fixtures/private/assignment)
  8. Update the code!
  9. yarn jest class-properties to run the tests
    • If some test outputs don’t match but the new results are correct, you can delete the bad output.js files and run the tests again
  10. If it is working, run make test to run all the tests
  11. Run git push and open a PR!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
sag1vcommented, Jul 6, 2020

@sag1v yep, check out the linked PR for the full conversation and current status. I’m planning to make the final requested changes this week 🤞

Thank you 💚

Read more comments on GitHub >

github_iconTop Results From Across the Web

babel/plugin-proposal-logical-assignment-operators
While using the @babel/plugin-proposal-nullish-coalescing-operator plugin (included in @babel/preset-env ). a ??= b; obj.a.b ??= c;.
Read more >
@babel/plugin-proposal-logical-assignment-operators | Yarn
Transforms logical assignment operators into short-circuited assignments. babel-plugin. readme. babel. The compiler for writing next generation JavaScript.
Read more >
Migrating from Babel - SWC
babel -plugin-proposal-logical-assignment-operators, ✔️ ... babel-plugin-syntax-class-properties, ✔️. babel-plugin-syntax-decorators ...
Read more >
Babel 7.14 enables class fields & private methods by default ...
This plugin transforms class properties in such a way that we can define ... initializer syntax (i.e., by using the = assignment operator)....
Read more >
Documentation - Classes - TypeScript
A field declaration creates a public writeable property on a class: ... of your class for you), you can use the definite assignment...
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