简体   繁体   English

React ESLint配置“意外的文件扩展名JSX”

[英]React ESLint Config “Unexpected file extension JSX”

Running Airbnb's Eslint Config and running into an issue using the .jsx extension. 使用.jsx扩展程序运行Airbnb的Eslint配置并遇到问题。

index.jsx 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 所以查了一下,发现另一个SORestrict文件扩展名一起使用,可能包含JSX

Ok, so updated my .eslintrc to reflect this in my rules 好的,所以更新了我的.eslintrc以在我的规则中反映这一点

.eslintrc .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”:“^ 4.10.0”,
  • "eslint-config-airbnb": "^16.1.0", “eslint-config-airbnb”:“^ 16.1.0”,
  • "eslint-plugin-import": "^2.8.0", “eslint-plugin-import”:“^ 2.8.0”,
  • "eslint-plugin-jsx-a11y": "^6.0.2", “eslint-plugin-jsx-a11y”:“^ 6.0.2”,
  • "eslint-plugin-react": "^7.4.0", “eslint-plugin-react”:“^ 7.4.0”,

Removed extension on import statement: 删除了import语句的扩展名:

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 那然后把我带到另一个SO:Webpack找不到模块,如果文件名为jsx并在我的webpack.config编写一个resolve对象

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

So far this is working as expected, trying to find confirmation that this is the "best practice" 到目前为止,这是按预期工作,试图找到确认这是“最佳做法”

Update to .eslintrc 更新到.eslintrc

As pointed out by an answer below, removed "react/jsx-filename-extension": [1, { "extensions": [".js", ".jsx"] }], 正如下面的答案所指出的,删除了"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 你实际上是从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 以下是代码中的行,并显示错误: 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. 关于添加'.jsx'来解析扩展,如果你打算编写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. PS你复制的jsx-filename-extension配置允许你将JSX保存在带有'.jsx'或'.js'扩展名的文件中,我会避​​免这种情况。 Better keep JSX in .jsx files. 最好将JSX保存在.jsx文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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