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.

Private Name in the left ForOfStatement is not transformed

See original GitHub issue

Bug Report

  • I would like to work on a fix!

Current Behavior The following snippet, when transformed via preset-env and stage-3 preset, will throw

Property left of ForOfStatement expected node to be of a type ["VariableDeclaration","LVal"] but instead got "CallExpression"

Input Code

class C {
  #p;
  m() {
    for (this.#p of []);
  }
}

Expected behavior/code It should be transformed into something like

// helpers is omissioned
var C = /*#__PURE__*/function () {
  function C() {
    _classCallCheck(this, C);

    _p.set(this, {
      writable: true,
      value: void 0
    });
  }

  _createClass(C, [{
    key: "m",
    value: function m() {
      for (var _i = 0, _arr = []; _i < _arr.length; _i++) {
        i = _arr[_i];
        _classPrivateFieldSet(this, _p, i);
      };
    }
  }]);

  return C;
}();

Possible Solution

As Justin noted, we can fix it by detecting such patterns in @babel/helper-member-expression-to-functions, so babel will apply destructureSet private name handler, which will return a member expression – a valid LHS.

https://github.com/babel/babel/blob/1ba41f208430bdf382b75b56e9aad7436ca522ad/packages/babel-helper-member-expression-to-functions/src/index.js#L110

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
  7. Add a test (only input.js; output.js will be automatically generated)
  8. Update the code!
  9. yarn jest babel-plugin-proposal-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!

Additional context/Screenshots This issue is likely due to plugin ordering: the private-class-properties runs before for-of-transforms after this.#p is transformed to helper calls which is not a valid LHS, the for-of transforms throws on unsupported for-of left.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:13 (12 by maintainers)

github_iconTop GitHub Comments

8reactions
jridgewellcommented, Mar 23, 2020

No problem! This isn’t to make you feel guilty, I just want to avoid situation where you’re stuck and haven’t asked for help. Take the time you need, especially with all circumstances going on.

3reactions
jridgewellcommented, Mar 16, 2020

Looks like we just need to use the destructureSet transform when inside of the init. This could be a GFI.

Read more comments on GitHub >

github_iconTop Results From Across the Web

es6 babelify changes variable name, variable can't be found ...
I created a Babel 6 plugin called babel-plugin-transform-es2015-modules-commonjs-simple that will import modules without changing the symbol names, ...
Read more >
babel/types
assertForOfStatement(node, opts) . AST Node ForOfStatement shape: left : VariableDeclaration | LVal (required); right : Expression ...
Read more >
for...of - JavaScript | MDN - MDN Web Docs
The for...of statement executes a loop that operates on a sequence of values sourced from an iterable object. Iterable objects include ...
Read more >
ES6 Array Iterators. no-restricted-syntax Using…
The reduce() method applies a function against an accumulator and each value of the array (from left-to-right) to reduce it to a single...
Read more >
Open Source Used In Intersight Mobile App 1.0.233 - Cisco
1.796 @babel/plugin-syntax-private-property-in-object 7.14.5 ... 1.816 @babel/plugin-transform-function-name 7.16.7.
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