eslint

依賴 babel-eslint

版本一:.eslintrc

{
  "extends": [
    "eslint:recommended",
    "plugin:react/recommended"
  ],
  "parser": "babel-eslint",
  "rules": {
    "react/prop-types": 0,
    "react/display-name": 0
  },
  "globals": {
    "window": true,
    "document": true,
    "Image": true,
    "Event": true,
    "Promise": true,
    "sessionStorage": true,
    "setTimeout": true,
    "process": true
  }
}

版本二:.eslintrc.js

module.exports = {
  parser: 'babel-eslint',
  extends: 'airbnb',
  plugins: ['react'],
  settings: {
    'import/resolver': {
      node: {
        moduleDirectory: ['node_modules', 'src']
      }
    }
  },
  env: {
    browser: true,
    mocha: true,
    es6: true
  },
  rules: {
    'spaced-comment': [0],
    'class-methods-use-this': 0,
    'jsx-a11y/click-events-have-key-events': 0,
    'no-restricted-syntax': 0,
    'react/jsx-closing-tag-location': 0,
    'arrow-parens': 0,
    'react/prefer-stateless-function': 0,
    'react/require-default-props': 0,
    'jsx-a11y/img-redundant-alt': 0,
    'jsx-a11y/interactive-supports-focus': 0,
    'jsx-a11y/anchor-is-valid': 0,
    'jsx-a11y/mouse-events-have-key-events': 0,
    'no-mixed-operators': 0,
    'react/prop-types': [1, { ignore: ['children'], customValidators: [] }],
    'react/no-array-index-key': 1,
    'no-underscore-dangle': 1,
    'no-unused-vars': 1,
    'space-before-blocks': 1,
    'jsx-a11y/no-static-element-interactions': 0,
    'no-return-assign': 1,
    'no-case-declarations': 1,
    'comma-dangle': [
      'error',
      {
        arrays: 'never',
        objects: 'never',
        imports: 'never',
        exports: 'never',
        functions: 'ignore'
      }
    ],
    "function-paren-newline": 0,
    "no-plusplus": ["error", { "allowForLoopAfterthoughts": true }]
  }
};

.editorconfig(vscode 需要安裝插件配合eslint一起使用解決‘LF’)

root = true
[*]
end_of_line = lf
insert_final_newline = true
insert_final_newline = true

webpack:配置eslint規(guī)則

{
        test: /\.(js|jsx|mjs)$/,
        enforce: 'pre',
        use: [
          {
            options: {
              formatter: eslintFormatter,
              eslintPath: require.resolve('eslint'),

            },
            loader: require.resolve('eslint-loader'),
          },
        ],
        include: paths.appSrc,
      }, 

React項目增加eslint

  • 安裝安裝ESLint、ESLint loader
npm install --save-dev eslint
npm install --save-dev eslint-loader
  • 逐個配置規(guī)則有點麻煩,ESLint有很多第三方配置好的格式插件,Airbnb開發(fā)配置合集就比較常用:
npm install --save-dev eslint-config-airbnb
  • Airbnb包括了以下三個插件需要安裝:
npm install --save-dev eslint-plugin-import
npm install --save-dev eslint-plugin-react 
npm install --save-dev eslint-plugin-jsx-a11y
  • 項目根目錄下創(chuàng)建并ESLint配置文件,.eslintrc.js:
module.exports = {
  parser: 'babel-eslint',
  extends: 'airbnb',
  plugins: ['react'],
  settings: {
    'import/resolver': {
      node: {
        moduleDirectory: ['node_modules', 'src']
      }
    }
  },
  env: {
    browser: true,
    mocha: true,
    es6: true
  },
  rules: {
    'spaced-comment': [0],
    'class-methods-use-this': 0,
    'jsx-a11y/click-events-have-key-events': 0,
    'no-restricted-syntax': 0,
    'react/jsx-closing-tag-location': 0,
    'arrow-parens': 0,
    'react/prefer-stateless-function': 0,
    'react/require-default-props': 0,
    'jsx-a11y/img-redundant-alt': 0,
    'jsx-a11y/interactive-supports-focus': 0,
    'jsx-a11y/anchor-is-valid': 0,
    'jsx-a11y/mouse-events-have-key-events': 0,
    'no-mixed-operators': 0,
    'react/prop-types': [1, { ignore: ['children'], customValidators: [] }],
    'react/no-array-index-key': 1,
    'no-underscore-dangle': 1,
    'no-unused-vars': 1,
    'space-before-blocks': 1,
    'jsx-a11y/no-static-element-interactions': 0,
    'no-return-assign': 1,
    'no-case-declarations': 1,
    'comma-dangle': [
      'error',
      {
        arrays: 'never',
        objects: 'never',
        imports: 'never',
        exports: 'never',
        functions: 'ignore'
      }
    ],
    "function-paren-newline": 0,
    "no-plusplus": ["error", { "allowForLoopAfterthoughts": true }],
    "object-curly-newline": 0
  }
};
  • 如果使用vscode,再添加.editorconfig
root = true
[*]
end_of_line = lf
insert_final_newline = true
insert_final_newline = true

注意:解決項目‘LF’的問題?。。?/h4>

1:拉代碼之前,要先設置git 規(guī)則和vscode的規(guī)則

git需要設置:git config --global core.autocrlf input
vscode設置:"files.eol": "\n"


最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容