繁体   English   中英

JSX 的 ESLint 配置

[英]ESLint configuration for JSX

我想配置 ESLint 来检查我的 JSX 文件,但我的配置不起作用。 什么是正确的方法?

.eslintrc.js:

module.exports = {
  extends: 'airbnb',
  parserOptions: {
    ecmaFeatures: {
      jsx: true
    }
  },
  plugins: [
    'react',
  ],
  parser: 'babel-eslint'
};

为了 lint JSX 文件,仅靠配置是不够的。 您的配置看起来不错(尽管您可能不需要babel-eslint ,除非您使用的功能低于第 4 阶段提案)。 默认情况下,ESLint 只会处理.js文件。 您必须通过在命令行上使用--ext标志来告诉它您也想处理.jsx文件: eslint --ext .jsx .

我建议你也使用 eslint https://github.com/yannickcr/eslint-plugin-react的插件

您可以执行此示例配置:

'use strict';

module.exports = {
  'extends': [ 'plugin:react/recommended' ],
  'parserOptions': {
    'ecmaFeatures': {
      'jsx': true
    }
  },
  'plugins': [ 'react' ],
  'rules': {

    // React
    'react/forbid-prop-types': 'error',
    'react/no-multi-comp': [ 'error', { 'ignoreStateless': true }],
    'react/no-set-state': 'error',
    'react/no-string-refs': 'error',
    'react/prefer-es6-class': 'error',
    'react/prefer-stateless-function': 'error',
    'react/require-extension': 'error',
    'react/require-render-return': 'error',
    'react/self-closing-comp': 'error',
    'react/sort-comp': 'error',
    'react/sort-prop-types': 'error',
    'react/wrap-multilines': 'error',

    // JSX
    'react/jsx-boolean-value': 'error',
    'react/jsx-closing-bracket-location': 'error',
    'react/jsx-curly-spacing': [ 'error', 'always' ],
    'react/jsx-equals-spacing': 'error',
    'react/jsx-first-prop-new-line': 'error',
    'react/jsx-handler-names': 'error',
    'react/jsx-indent-props': [ 'error', 2 ],
    'react/jsx-indent': [ 'error', 2 ],
    'react/jsx-key': 'error',
    'react/jsx-max-props-per-line': [ 'error', { 'maximum': 3 }],
    'react/jsx-no-bind': 'error',
    'react/jsx-no-literals': 'off',
    'react/jsx-no-target-blank': 'error',
    'react/jsx-pascal-case': 'error',
    'react/jsx-sort-props': 'error',
    'react/jsx-space-before-closing': 'error'
  }
};

我找到了原始答案的替代方法,这样您就不必在运行eslint命令时指定参数。

选项 1

使用命令eslint . --ext .js --ext .jsx eslint . --ext .js --ext .jsx

选项 2

在 eslint 配置中指定overrides ...

//.eslintrc
{
  ...
  "overrides": [
    {
      "files": ["*.jsx", "*.js"]
    }
  ],
  ...
}

暂无
暂无

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

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