Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ESLint: Parsing error: This experimental syntax requires enabling one of the following parser plugin(s): 'jsx, flow, typescript' (2:9)

I am trying to use ESLint in WebStrom, but it doesn't work and displays an error:

ESLint: Parsing error: This experimental syntax requires enabling one of the following parser plugin(s): 'jsx, flow, typescript' (2:9).

Here is my .eslintrc and package.json settings. What should I do for fixing the error?

package.json

{
  "name": "raonair-frontend",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.11.4",
    "@testing-library/react": "^11.1.0",
    "@testing-library/user-event": "^12.1.10",
    "eslint-config-react-app": "^6.0.0",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-router-dom": "^5.2.0",
    "react-scripts": "4.0.3",
    "web-vitals": "^1.0.1"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "prepare": "husky install"
  },
  "parser": "babel-eslint",
  "eslintConfig": {
    "extends": [
      "react-app",
      "airbnb",
      "plugin:flowtype/recommended"
    ],
    "plugins": [
      "flowtype"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "@babel/eslint-parser": "^7.15.0",
    "@babel/eslint-plugin": "^7.14.5",
    "@babel/plugin-syntax-jsx": "^7.14.5",
    "eslint": "^7.32.0",
    "eslint-config-airbnb": "^18.2.1",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-flowtype": "^5.9.0",
    "eslint-plugin-import": "^2.23.4",
    "eslint-plugin-jsx-a11y": "^6.4.1",
    "eslint-plugin-react": "^7.24.0",
    "eslint-plugin-react-hooks": "^4.2.0",
    "husky": "^7.0.1",
    "prettier": "^2.3.2"
  }
}

.eslintrc

{
  "env": {
    "browser": true,
    "node": true
  },
  "extends": [
    "airbnb",
    "airbnb/hooks",
    "prettier"
  ],
  "parser": "@babel/eslint-parser",
  "parserOptions": {
    "sourceType": "module",
    "allowImportExportEveryWhere": false,
    "ecmaFeatures": {
      "globalReturn": false,
      "jsx": true
    },
    "ecmaVersion": 2020,
    "babelOptions": {
      "configFile": "./babel.config.js"
    }
  },
  "plugins": [
    "jsx-a11y",
    "react-hooks",
    "@babel/",
    "flowtype",
    "import"
  ],
  "rules": {
    "import/no-anonymous-default-export": "off",
    "import/no-extraneous-dependencies": "off",
    "import/order": [
      "error",
      {
        "groups": [
          "builtin",
          "external",
          "internal",
          "parent",
          "sibling"
        ],
        "newlines-between": "always",
        "alphabetize": {
          "order": "asc",
          "caseInsensitive": false
        }
      }
    ],
    "import/prefer-default-export": "off",
    "indent": [
      "error",
      2,
      {
        "SwitchCase": 1
      }
    ],
    "jsx-a11y/anchor-is-valid": "off",
    "no-console": "error",
    "no-unused-vars": "error",
    "react/jsx-props-no-spreading": "off",
    "react/react-in-jsx-scope": "off"
  },
  "settings": {
    "import/resolver": {
      "typescript": {}
    },
    "insert_final_newline": true
  }
}
like image 525
skull Kim Avatar asked Aug 06 '21 07:08

skull Kim


2 Answers

To expand on Ceroy's answer (source):

  1. Install dependencies needed to enable babel to properly parse jsx

yarn add @babel/preset-react -D

or

npm install --save-dev @babel/preset-react

  1. Configure Babel

.babelrc file in the project root:

{
    "presets": [
        "@babel/preset-react"
    ]
}
  1. Specify parser in .eslintrc.json (so your console and VSCode will use the same one) - full eslintrc shown here:
{
    "parser": "@babel/eslint-parser",
    "settings": {
        "react": {
            "version": "detect"
        }
    },
    "env": {
        "browser": true,
        "es2021": true
    },
    "extends": [
        "plugin:react/recommended",
        "standard",
        "plugin:react/jsx-runtime",
        "plugin:testing-library/react",
        "plugin:jest/all"
    ],
    "parserOptions": {
        "sourceType": "module"
    },
    "rules": {}
}

Note: .eslintrc.json can also be created via npm init @eslint/config or by using the ESLint extension in VSCode. The init will give you more options to choose from (such as TypeScript support)

  1. Try it out in the console: npx eslint --fix .

  2. Check VSCode (with the ESLint extension)

Bonus:

ESLint Plugin Settings for VSCode (autofix on save, Jest aware, babel parser etc. - in global VSCode settings.json):

    ...
    "eslint.alwaysShowStatus": true,
    "eslint.format.enable": true,
    "eslint.lintTask.enable": true,
    "eslint.options": {
        "parser": "@babel/eslint-parser"
    },
    "editor.codeActionsOnSave": {
        "source.fixAll.eslint": true
    },
    "eslint.validate": [
        "javascript"
    ]
    ...

Full package.json:

{
  "name": "okta-test",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.11.4",
    "@testing-library/react": "^12.1.0",
    "@testing-library/user-event": "^13.2.1",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-router": "^5.2.1",
    "react-router-dom": "^5.3.0",
    "react-scripts": "4.0.3",
    "web-vitals": "^2.1.0"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "@babel/eslint-parser": "^7.15.7",
    "@babel/preset-react": "^7.14.5",
    "eslint": "^7.32.0",
    "eslint-config-standard": "^16.0.3",
    "eslint-plugin-babel": "^5.3.1",
    "eslint-plugin-import": "^2.24.2",
    "eslint-plugin-jsx": "^0.1.0",
    "eslint-plugin-node": "^11.1.0",
    "eslint-plugin-promise": "^5.1.0",
    "eslint-plugin-react": "^7.26.0"
  }
}
like image 179
Andrei Avatar answered Oct 23 '22 11:10

Andrei


  1. Install @babel/preset-react in dev dependencies.

  2. Add this in .eslintrc file

     ...
     "parser": "@babel/eslint-parser",
     "parserOptions": {
        ...
        "babelOptions": {
           "presets": ["@babel/preset-react"]
        },
       }
     ...
    

Source: https://ffan0811.medium.com/error-debugging-this-experimental-syntax-requires-enabling-one-of-the-following-parser-plugin-s-22946599a0a4

like image 44
Enrique Briones Arcos Avatar answered Oct 23 '22 10:10

Enrique Briones Arcos