Search code examples
javascriptreactjssonarqubesonarqube-scan

Unexpected token = (with espree parser in module mode) in my react project using sonar-scanner


I am facing this error while using sonar-scanner. It is unable to parse all those files in which i have used arrow functions.

import React from "react";
import { Button } from "antd";
import history from "src/components/history";

class BackButton extends React.Component {
  handleClick = () => {
    history.goBack();
    if (this.props.onBack) {
      this.props.onBack();
    }
  };
  render() {
    return <Button icon="arrow-left" onClick={this.handleClick} />;
  }
}

export default BackButton;

The error at line 6. Need a solution to fix this.


Solution

  • in order to use arrow functions inside a class you need to enable this plugin in your babel configuration.

    {
      "plugins": [
        "transform-class-properties"
      ]
    }
    

    or you can do it like this

    class BackButton extends React.Component {
      constructor() {
        super();
        this.handleClick = (val) => {
          ...
        };
        …
      }
    }