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.

[Bug]: Unable to transform arrow inside class property

See original GitHub issue

💻

  • Would you like to work on a fix?

How are you using Babel?

babel-loader (webpack)

Input code

source code:

class A {
  handle = (x = 0) => {
    console.log(x);
  };
}

github repo: https://github.com/Zousdie/babel-bug-unable_to_transform_arrow_inside_class_property

Configuration file name

babel.config.js

Configuration

module.exports = {
  presets: ['@babel/preset-env'],
};

Current and expected behavior

SyntaxError “Unable to transform arrow inside class property” during compilation.

Module build failed (from ./node_modules/babel-loader/lib/index.js):
SyntaxError: Unable to transform arrow inside class property
  1 | class A {
> 2 |   handle = (x = 0) => {
    |            ^
  3 |     console.log(x);
  4 |   };
  5 | }

Environment

System: OS: macOS 10.15.7 Binaries: Node: 14.18.1 - ~/.nvm/versions/node/v14.18.1/bin/node Yarn: 1.22.17 - ~/.nvm/versions/node/v14.18.1/bin/yarn npm: 8.1.3 - ~/.nvm/versions/node/v14.18.1/bin/npm npmPackages: @babel/core: ^7.16.0 => 7.16.0 @babel/preset-env: ^7.16.0 => 7.16.0 babel-loader: ^8.2.3 => 8.2.3 webpack: ^5.62.1 => 5.62.1

Possible solution

No response

Additional context

This error occurs when the browserslist config contains last 1 safari version. It’s strange that if you remove this rule, there will be no problem

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
nicolo-ribaudocommented, Nov 8, 2021

Whops, I didn’t see your comment and I opened a PR to relax the check (which might be useful in general).

0reactions
JLHwungcommented, Nov 8, 2021

The root issue here is that we have transforms (e.g. class transform) which assume certain language feature (arrows) should not be transpiled before. This is mostly true when a language feature is implemented before another universally among major browsers: In this case, the class property is implemented after arrow functions. So we don’t expect transpiling arrows leaving class properties untouched.

However, the introduction of bugfixes breaks this assumption, so the parameters-transform unconditionally kicks in if bugfixes: false (the current Babel 7 default) is applied. From users perspective, upgrading preset-env certainly becomes a regression, which is caused by us downgrading the language feature supports due to how bugfixes works.

Therefore, I propose we enable the bugfixes, at least the newly added (v8-spread/safari-id-destructuring) by default.

An alternative solution would be not downgrading the support matrix: So:

  • bugfixes: false means the compilation results may be affected by certain browser bugs
  • bugfixes: true adds such bugfix plugin to workaround the bug

Neither of them will affect the browser compat data.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to use Arrow functions inside React component class
Using an arrow function with a class property ensures that the method is always invoked with the component as the value for this...
Read more >
Arrow function class fields transform this to undefined #5510
bug report I'm not sure whether this is a parcel or a babel issue but given the following Code class DrawingArea extends React.Component<....
Read more >
Arrow Functions in Class Properties Might Not Be As Great As ...
Conclusion. The initialization of arrow functions in class properties are transpiled into the constructor. Arrow functions in class properties ...
Read more >
babel-plugin-transform-class-property-arrow-functions - npm
This plugin transforms class properties assigned to arrow functions into class methods bound in the class' constructor.
Read more >
Unable to use Arrow functions inside React component class ...
It is not a problem of arrow function but using it inside class declaration, this code will work in constructor body or any...
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