简体   繁体   中英

React ESLint Config “Unexpected file extension JSX”

Running Airbnb's Eslint Config and running into an issue using the .jsx extension.

index.jsx

import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter } from 'react-router-dom';
import App from './components/App.jsx'; <<<<<<<<< Unexpected use of file extension "jsx"...

require('./index.scss');

ReactDOM.render(
  <BrowserRouter>
    <App />
  </BrowserRouter>,
document.getElementById('root'),
);

So looked this up and found another SO which is in conjunction with Restrict file extensions that may contain JSX

Ok, so updated my .eslintrc to reflect this in my rules

.eslintrc

{
  "extends": "airbnb",
  "env":{
    "browser": true
  },
  "plugins": [
    "react",
    "jsx-a11y",
    "import"
  ],
  "rules": {
    "react/jsx-filename-extension": [1, { "extensions": [".js", ".jsx"] }],
    "indent": [2, "tab", { "SwitchCase": 1, "VariableDeclarator": 1 }],
    "no-tabs": 0,
    "react/prop-types": 0,
    "react/jsx-indent": [2, "tab"],
    "react/jsx-indent-props": [2, "tab"]
  }
}

However still getting the same error. Is there something else that I'm missing?

Dependencies

  • "eslint": "^4.10.0",
  • "eslint-config-airbnb": "^16.1.0",
  • "eslint-plugin-import": "^2.8.0",
  • "eslint-plugin-jsx-a11y": "^6.0.2",
  • "eslint-plugin-react": "^7.4.0",

Removed extension on import statement:

import App from './components/App';

Which lead to unable to resolve error. That then brought me to another SO: Webpack Can't find module if file named jsx and writing a resolve object in my webpack.config

resolve: {
    extensions: ['.js', '.jsx'],
},

So far this is working as expected, trying to find confirmation that this is the "best practice"

Update to .eslintrc

As pointed out by an answer below, removed "react/jsx-filename-extension": [1, { "extensions": [".js", ".jsx"] }],

{
  "extends": "airbnb",
  "env":{
    "browser": true,
  },
  "plugins": [
    "react",
    "jsx-a11y",
    "import"
  ],
  "rules": {
    "no-tabs": 0,
  }
}

I am pretty sure you're blaming the wrong rule. You're actually hitting this one from eslint-plugin-import : https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/extensions.md

Here's the line in the code with the error you get: https://github.com/benmosher/eslint-plugin-import/blob/master/src/rules/extensions.js#L152

message: `Unexpected use of file extension "${extension}" for "${importPath}"`,

The rule is a good one, why would you want to type the extensions in all the time?

Regarding adding '.jsx' to resolve extensions, definitely the way to go if you're going to write JSX.

PS The jsx-filename-extension configuration you copied let's you keep JSX in files with either '.jsx' or '.js' extensions, which I would avoid. Better keep JSX in .jsx files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM