ESLint in React: Parsing error: Unexpected token =

3.6k views Asked by At

I'm getting this error:

error Generating development JavaScript bundle failed 92:7 error

Parsing error: Unexpected token =

I am using gatsby & a custom eslintrc:

{
    "parser": "babel-eslint", // uses babel-eslint transforms
    "parserOptions": {
        "sourceType": "module",
        "ecmaVersion": 2020,
        "ecmaFeatures": {
          "jsx": true,
          "modules": true
        }
      },
    "settings": {
      "react": {
        "version": "detect" // detect react version
      }
    },
    "env": {
      "node": true, // defines things like process.env when generating through node
      "browser": true,
      "es6": true
    },
    "extends": [
      "eslint:recommended", // use recommended configs
      "plugin:react/recommended"
    ]
  }

I found solutions to this problem in several places, but none of it worked. The most common solution I found was "parser": "babel-eslint" to the eslintrc file. But I already have that. Don't know what I can try anymore.

The files this error occurs in are usually files written in an older style React, but I don't know why they shouldn't work anymore?

For example:

class Post extends Component {
  constructor(props) {
    super(props)

    this.state = {
      isVisible: true,
      isOpen: false,
    }

    this.toggleIsVisible = this.toggleIsVisible.bind(this)
    this.toggleModal = this.toggleModal.bind(this)
  }

  toggleIsVisible = () => {    <=== HERE ESLINT COMPLAINS ABOUT THE FIRST, DEFINING EQUALS-SIGN
    this.setState({ isVisible: !this.state.isVisible })
  }

Another piece of code eslint doesn't like is this, in a different file:

      try {
        await Function(values)
      } catch { <==== ESLINT DOESNT LIKE THIS OPEN BRACKET, SIMILAR ERROR MESSAGE: "Unexpected token {"
        setFormState('error')
      }

I have also tried disabling the line in eslint as well as the full file, but didn't work either. Oh, and when I chuck eslint out completely, everything works fine and I can run as well as build my project.

0

There are 0 answers