简体   繁体   English

将 ESLint 与 Airbnb 样式和选项卡 (React.js) 结合使用

[英]Use ESLint with Airbnb style and tab (React.js)

I'm working on a React.js application and I'm trying to lint my code.我正在开发一个 React.js 应用程序,我正在尝试检查我的代码。 I use ESLint with the Airbnb style, but I have these errors:我将 ESLint 与 Airbnb 风格结合使用,但出现以下错误:

../src/Test.jsx
  4:2   error  Unexpected tab character                                no-tabs
  5:2   error  Unexpected tab character                                no-tabs
  5:3   error  Expected indentation of 2 space characters but found 0  react/jsx-indent
  6:2   error  Unexpected tab character                                no-tabs

Here my code:这是我的代码:

Test.jsx:测试.jsx:

import React from 'react';

function Test() {
    return (
        <h1>Test</h1>
    );
}

export default Test;

.eslintrc: .eslintrc:

{
  "env": {
    "browser": true,
    "es6": true,
    "node": true
  },
  "extends": "airbnb",
  "parser": "babel-eslint",
  "rules": {
    "indent": [2, "tab", { "SwitchCase": 1, "VariableDeclarator": 1 }],
    "react/prop-types": 0,
    "react/jsx-indent-props": [2, "tab"],
  }
}

As you can see above, I would like to indent with tab.正如您在上面看到的,我想用制表符缩进。

webpack.config.js: webpack.config.js:

loaders: [{
  test: /\.jsx$|\.js$/,
  loaders: ["babel-loader", "eslint-loader"],
  exclude: /node_modules/
},
...
]

I also tried to indent why 2 spaces, without success.我还尝试缩进为什么 2 个空格,但没有成功。 I really don't understand why I have theses errors.我真的不明白为什么我有这些错误。 Do you have an idea?你有好主意吗?

Thanks!谢谢!

As @mark-willian told me, I added some lines in my .eslintrc and it works: 正如@ mark-willian告诉我的那样,我在.eslintrc中添加了一些行并且它有效:

{
    "env": {
        "browser": true,
        "es6": true,
        "node": true
    },
    "extends": "airbnb",
    "parser": "babel-eslint",
    "rules": {
        "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"],
    }
}

Thank you for all of your answers. 谢谢你的所有答案。

The airbnb rules want you to use spaces instead of tabs for formatting your code. airbnb规则要求您使用空格而不是制表符来格式化代码。 Good editors (sublime is one!) will let you use tabs but translate them to spaces when saving your code. 优秀的编辑器(sublime是一个!)将允许您使用选项卡,但在保存代码时将它们转换为空格。

You need to change the config of your sublime; 你需要改变sublime的配置; go to Preferences - Settings and customize the following settings: 转到首选项 - 设置并自定义以下设置:

"tab_size": 2,
"translate_tabs_to_spaces": true

Sublime will convert your existing code - click on the text in the status bar at the bottom right that says tabs or spaces. Sublime将转换您现有的代码 - 单击右下方状态栏中显示标签或空格的文本。

You can selectively turn off eslint rules if (for example) one of airbnb's rules doesn't match your coding style guide. 如果(例如)某个airbnb规则与您的编码风格指南不匹配,您可以有选择地关闭eslint规则。

Try replace every tab to spaces . 尝试将每个标签替换为空格

It is also a good idea to enable the ESLint autofix feature with this eslint-loader option in your webpack configuration. 在webpack配置中使用eslint-loader选项启用ESLint自动修复功能也是一个好主意。

You can add this comment on page which you get no-tabs error.您可以在出现无制表符错误的页面上添加此评论。

 /* eslint-disable */

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

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