【问题标题】:Use ESLint with Airbnb style and tab (React.js)将 ESLint 与 Airbnb 样式和选项卡一起使用 (React.js)
【发布时间】:2017-04-11 15:05:17
【问题描述】:

我正在开发一个 React.js 应用程序,并且正在尝试对我的代码进行 lint。我使用 Airbnb 风格的 ESLint,但出现以下错误:

../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

这是我的代码:

Test.jsx:

import React from 'react';

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

export default Test;

.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"],
  }
}

正如你在上面看到的,我想用制表符缩进。

webpack.config.js:

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

我还尝试缩进为什么 2 个空格,但没有成功。我真的不明白为什么我有这些错误。你有什么想法吗?

谢谢!

【问题讨论】:

  • 你用的是什么文本编辑器?
  • @azium 我正在使用 Sublime,但我也尝试使用 vim 再次创建我的文件。

标签: javascript reactjs webpack eslint


【解决方案1】:

正如@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"],
    }
}

感谢您的所有回答。

【讨论】:

    【解决方案2】:

    airbnb 规则希望您使用空格而不是制表符来格式化代码。好的编辑器(sublime 就是其中之一!)将允许您使用制表符,但在保存代码时将它们转换为空格。

    你需要改变你的 sublime 的配置;转到首选项-设置并自定义以下设置:

    "tab_size": 2,
    "translate_tabs_to_spaces": true
    

    Sublime 将转换您现有的代码 - 单击右下角状态栏中显示制表符或空格的文本。

    如果(例如)airbnb 的规则之一与您的编码风格指南不匹配,您可以选择性地关闭 eslint 规则。

    【讨论】:

    • 感谢您的回复,但我不想使用空格。
    • 好的,那么你需要禁用 eslint no-tabs 以及 react/jsx-indent 规则。
    【解决方案3】:

    尝试替换every tab to spaces

    在您的 webpack 配置中启用 ESLint autofix 功能和 this eslint-loader option 也是一个好主意。

    【讨论】:

    • 有空格,我得到这个错误:4:3 错误预期缩进 1 个选项卡,但发现缩进 2 个空格
    • 您可以在此处找到我的代码的简化版本:GitHub [已编辑]
    猜你喜欢
    • 2017-01-17
    • 2019-06-27
    • 2016-10-14
    • 2020-04-30
    • 1970-01-01
    • 2016-12-10
    • 2015-02-19
    • 1970-01-01
    • 2016-11-07
    相关资源
    最近更新 更多