eslint prettier editrorconfig - 写出干净的前端代码

VSCode 插件安装

  1. Prettier - Code Formatter

  2. ESLint

  3. VsCode 针对配置

     // 如果保存的时候使用eslint --fix自动修复当前文件的话, 将其设置为true
     "eslint.autoFixOnSave": false,
     // 如果保存的时候使用prettier自动修复的话, 将其设置为true
     "editor.formatOnSave": true,
     "[javascript]": {
       "editor.tabSize": 2
     },
    

    主要不用同时将 eslint.autoFixOnSave 和 editor.formatOnSave 设置为 true, 会产生冲突

  4. 全局安装

    npm install eslint -g
    npm install prettier -g
    

开发工具介绍

  1. babel-eslint:这个包让你可以轻松在 Babel 上使用 lint。如果你不使用 ESLint 尚不支持的 Flow 或实验性功能,则不一定需要这个插件。

  2. eslint:这是 lint 代码所需的主要工具。

  3. eslint-config-airbnb:这个包提供了所有 Airbnb 的 ESLint 配置,你可以修改它们。

  4. eslint-plugin-babel:babel-eslint 的插件伴侣。

  5. eslint-plugin-import:这个插件旨在支持 ES2015+(ES6+)的导入 / 导出语法,并防止出现拼写错误的文件路径和导入名称。

  6. eslint-plugin-jsx-a11y:适用于 JSX 元素可访问性规则的 linting 规则。

  7. eslint-plugin-prettier:让 ESLint 与 Prettier 的使用更顺畅。

  8. eslint-plugin-react:特定于 React 的 linting 规则。

  9. eslint-config-jest-enzyme:用于特定于 React 和 Enzyme 的全局变量。这个 lint 配置让 ESLint 知道有哪些全局变量,并且不会针对它们发出警告——有点像断言 it 和 describe。

  10. eslint-plugin-jest:Jest 的 ESLint 插件。

  11. husky:在自动化部分会进行更多介绍。

  12. lint-staged:在自动化部分会进行更多介绍。

yarn add eslint prettier --dev
yarn add eslint-config-airbnb eslint-config-jest-enzyme --dev
yarn add eslint-plugin-import eslint-plugin-babel eslint-plugin-jsx-a11y eslint-plugin-prettier eslint-plugin-react eslint-plugin-jest --dev

husky

husky是一个 Git 钩子,你可以在提交代码前或在将代码推送到分支时执行某些特定的操作。

  1. 安装 husky

    yarn add --dev husky
    
  2. package.json 中添加配置

     "husky": {
       "hooks": {
         "pre-commit": "lint-staged",
         "pre-push": "YOUR_COMMAND_HERE"
       }
     },
    

    每次在提交或推送代码时,它都会执行某个脚本或命令——比如运行测试用例或格式化代码。

lint-staged

lint-staged 可以在暂存(Git staged)文件上运行 linter,这样就不会将错误的代码推送到分支上。

  1. 安装 lint-staged

     yarn add --dev lint-staged
    
  2. package.json 配置

     "lint-staged": {
       "*.(js|jsx)": ["npm run lint:write", "git add"]
     },
    

    这段配置的意思是先运行 lint:write 命令,然后将文件添加到暂存区域。它仅针对.js 和.jsx 文件运行这个命令,但你也可以根据需要针对其他文件运行这个命令。

  3. 每当你提交代码时:

     $ git add .
     $ git commit -m "代码被commit之前都会执行lint-staged命令"
    

    它将根据.eslintrc.js 文件的所有规则对代码进行 lint 和格式化。有了这个,你就可以确保没有坏代码被推到生产环境中。

  4. package.json 的最终配置

    {
      "name": "eslint-prettier-editorconfig-learn",
      "version": "1.0.0",
      "description": "eslint prettier editorconfig",
      "main": "index.js",
      "scripts": {
        "lint": "eslint --debug src/",
        "lint:write": "eslint --debug src/ --fix",
        "prettier": "prettier --write src/**/*.js"
      },
      "husky": {
        "hooks": {
          "pre-commit": "lint-staged"
        }
      },
      "lint-staged": {
        "*.(js|jsx)": ["npm run lint:write", "git add"]
      },
      "author": "VonConfidence",
      "license": "ISC",
      "devDependencies": {
        "babel-eslint": "^8.2.3",
        "eslint": "^4.19.1",
        "eslint-config-airbnb": "^17.0.0",
        "eslint-config-jest-enzyme": "^6.0.2",
        "eslint-plugin-babel": "^5.1.0",
        "eslint-plugin-import": "^2.12.0",
        "eslint-plugin-jest": "^21.18.0",
        "eslint-plugin-jsx-a11y": "^6.0.3",
        "eslint-plugin-prettier": "^2.6.0",
        "eslint-plugin-react": "^7.9.1",
        "husky": "1.3.1",
        "lint-staged": "8.1.0",
        "prettier": "1.15.3"
      }
    }
    

.editorconfig 文件

  1. 并不是每个人都会使用 VS Code,所以为了让每个人保持统一(例如在制表符空格或换行方面),我们使用.editorconfig,这样有助于强制执行某些规则。

  2. 支持 EditorConfig的编辑器包括 Web Storm、App Code、Atom、Eclipse、Emacs、bbedit,等等。

  3. .editorconfig 基本配置

     # EditorConfig is awesome: http://EditorConfig.org
    
     # top-most EditorConfig file
     root = true
    
     [*.md]
     trim_trailing_whitespace = false
    
     [*.js]
     trim_trailing_whitespace = true
    
     # Unix-style newlines with a newline ending every file
     [*]
     indent_style = space # 将缩进样式设置为空格而不是制表符;
     indent_size = 2 # 缩进大小为 2;
     end_of_line = lf # 行尾是 lf,这样不管使用的是哪种操作系统,都会有相同的行尾;
     charset = utf-8
     insert_final_newline = true # 文件末尾应该有一个新行;
     max_line_length = 120 # 单行的最大度应为 120 个字符。
    
    

.eslintrc.js 配置详解

  1. 规则控制

    "off"或者0     //关闭规则关闭
    "warn"或者1    //在打开的规则作为警告(不影响退出代码)
    "error"或者2   //把规则作为一个错误(退出代码触发时为1)
    
  2. eslint 规则配置 off=0 warn=1 error=2

    "rules": {
        "indent": [2, 2], // 控制缩进为2
        "eqeqeq": 1,// 警告使用全等
        "quotes": [2, "single"], //单引号
        "no-console": 0, //不禁用console
        "no-debugger": 1, //禁用debugger
        "no-var": 2, //对var警告
        "semi": 2, //强制使用分号
        "semi-spacing": [2, {"before": false, "after": true}], // 强制分号前后不允许空格
        "no-irregular-whitespace": 0, //不规则的空白不允许
        "no-trailing-spaces": 1, //一行结束后面有空格就发出警告
        "eol-last": 0, //文件以单一的换行符结束
        "no-unused-vars": [2, {"vars": "all", "args": "after-used"}], //不能有声明后未被使用的变量或参数
        "no-underscore-dangle": 0, //标识符不能以_开头或结尾
        "no-alert": 2, //禁止使用alert confirm prompt
        "no-lone-blocks": 0, //禁止不必要的嵌套块
        "no-class-assign": 2, //禁止给类赋值
        "no-cond-assign": 2, //禁止在条件表达式中使用赋值语句
        "no-const-assign": 2, //禁止修改const声明的变量
        "no-delete-var": 2, //不能对var声明的变量使用delete操作符
        "no-dupe-keys": 2, //在创建对象字面量时不允许键重复
        "no-duplicate-case": 2, //switch中的case标签不能重复
        "no-dupe-args": 2, //函数参数不能重复
        "no-empty": 2, //块语句中的内容不能为空
        "no-func-assign": 2, //禁止重复的函数声明
        "no-invalid-this": 0, //禁止无效的this,只能用在构造器,类,对象字面量
        "no-redeclare": 2, //禁止重复声明变量
        "no-spaced-func": 2, //函数调用时 函数名与()之间不能有空格
        "no-this-before-super": 0, //在调用super()之前不能使用this或super
        "no-undef": 2, //不能有未定义的变量
        "no-use-before-define": 2, //未定义前不能使用
        "camelcase": 0, //强制驼峰法命名
        "jsx-quotes": [2, "prefer-double"], //强制在JSX属性(jsx-quotes)中一致使用双引号
        "react/display-name": 0, //防止在React组件定义中丢失displayName
        "react/forbid-prop-types": [2, {"forbid": ["any"]}], //禁止某些propTypes
        "react/jsx-boolean-value": 2, //在JSX中强制布尔属性符号
        "react/jsx-closing-bracket-location": 1, //在JSX中验证右括号位置
        "react/jsx-curly-spacing": [2, {"when": "never", "children": true}], //在JSX属性和表达式中加强或禁止大括号内的空格。
        "react/jsx-indent": [2,2], // 语法缩进控制
        "react/jsx-indent-props": [2, 2], //验证JSX中的props缩进是否为2个
        "react/jsx-key": 2, //在数组或迭代器中验证JSX具有key属性
        "react/jsx-max-props-per-line": [1, {"maximum": 1}], // 限制JSX中单行上的props的最大数量
        "react/jsx-no-bind": 0, //JSX中不允许使用箭头函数和bind
        "react/jsx-no-duplicate-props": 2, //防止在JSX中重复的props
        "react/jsx-no-literals": 0, //防止使用未包装的JSX字符串
        "react/jsx-no-undef": 1, //在JSX中禁止未声明的变量
        "react/jsx-pascal-case": 0, //为用户定义的JSX组件强制使用PascalCase
        "react/jsx-sort-props": 2, //强化props按字母排序
        "react/jsx-uses-react": 1, //防止反应被错误地标记为未使用
        "react/jsx-uses-vars": 2, //防止在JSX中使用的变量被错误地标记为未使用
        "react/no-danger": 0, //防止使用危险的JSX属性
        "react/no-did-mount-set-state": 0, //防止在componentDidMount中使用setState
        "react/no-did-update-set-state": 1, //防止在componentDidUpdate中使用setState
        "react/no-direct-mutation-state": 2, //防止this.state的直接变异
        "react/no-multi-comp": 2, //防止每个文件有多个组件定义
        "react/no-set-state": 0, //防止使用setState
        "react/no-unknown-property": 2, //防止使用未知的DOM属性
        "react/prefer-es6-class": 2, //为React组件强制执行ES5或ES6类
        "react/prop-types": 0, //防止在React组件定义中丢失props验证
        "react/react-in-jsx-scope": 2, //使用JSX时防止丢失React
        "react/self-closing-comp": 0, //防止没有children的组件的额外结束标签
        "react/sort-comp": 2, //强制组件方法顺序
        "no-extra-boolean-cast": 0, //禁止不必要的bool转换
        "react/no-array-index-key": 0, //防止在数组中遍历中使用数组key做索引
        "react/no-deprecated": 1, //不使用弃用的方法
        "react/jsx-equals-spacing": 2, //在JSX属性中强制或禁止等号周围的空格
        "no-unreachable": 1, //不能有无法执行的代码
        "comma-dangle": ["error", "always"], //对象字面量项尾必须有逗号
        "no-mixed-spaces-and-tabs": 0, //禁止混用tab和空格
        "prefer-arrow-callback": 0, //比较喜欢箭头回调
        "arrow-parens": 0, //箭头函数用小括号括起来
        "arrow-spacing": 0, //=>的前/后括号
        "prefer-const": ["error", {
          "destructuring": "all"
        }],
        "prefer-destructuring": ["error", {
          "VariableDeclarator": {
            "array": false,
            "object": true
          },
          "AssignmentExpression": {
            "array": false,
            "object": false
          }
        }, {
          "enforceForRenamedProperties": false
        }],
        "use-isnan": 2,//禁止比较时使用NaN,只能用isNaN()
      },
      "settings": {
        "import/ignore": [
          "node_modules"
        ]
      }
    
  3. .eslintrc.js 最终形式

     module.exports = {
       // 用于预定义全局变量
       env: {
         es6: true, // 启动es6全局变量
         browser: true, // 浏览器全局变量window, document等
         node: true, // 启用node全局变量global等
       },
       // 扩展了之面配置的额外配置选项。
       // 现在我们正在使用 airbnb 的 linting 规则,这些规则被扩展到 jest,然后是 jest-enzyme。
       extends: ['airbnb', 'plugin:jest/recommended', 'jest-enzyme'],
       // 插件基本上就是我们想要使用的 linting 规则
       plugins: ['babel', 'import', 'jsx-a11y', 'react', 'prettier'],
       // 默认情况下,ESLint 使用 Espree,但因为我们使用了 babel,我们还需要使用 Babel-ESLint
       parser: 'babel-eslint',
       // 如果我们将 Espree 的默认解析器更改为 babel-eslint,需要指定 parserOptions
       /*
       告诉 ESLint,ecmaVersion 是 6。
       因为我们在 EcmaScript 模块(而不是 script)中编写代码,所以我们将 sourceType 指定为 module。
       由于我们使用了 React,引入了 JSX,所以在 ecmaFeatures 中加了 jsx 选项,并将其设置为 true。
       */
       parserOptions: {
         ecmaVersion: 6,
         sourceType: 'module',
         ecmaFeatures: {
           jsx: true,
         },
       },
       // 更改或覆盖添加的规则 off warn error
       rules: {
         'linebreak-style': 'off', // Don't play nicely with Windows.
         'arrow-parens': 'off', // Incompatible with prettier
         'object-curly-newline': 'off', // Incompatible with prettier
         'no-mixed-operators': 'off', // Incompatible with prettier
         'arrow-body-style': 'off', // Not our taste?
         'function-paren-newline': 'off', // Incompatible with prettier
         'no-plusplus': 'off',
         'space-before-function-paren': 0, // Incompatible with prettier
         'max-len': ['error', 100, 2, { ignoreUrls: true }], // airbnb is allowing some edge cases
         'no-console': 'warn', // airbnb is using warn
         'no-alert': 'warn', // airbnb is using warn
         'no-param-reassign': 'off', // Not our taste?
         radix: 'off', // parseInt, parseFloat radix turned off. Not my taste.
         'react/require-default-props': 'off', // airbnb use error
         'react/forbid-prop-types': 'off', // airbnb use error
         'react/jsx-filename-extension': ['error', { extensions: ['.js'] }], // airbnb is using .jsx
         'prefer-destructuring': 'off',
         'react/no-find-dom-node': 'off', // I don't know
         'react/no-did-mount-set-state': 'off',
         'react/no-unused-prop-types': 'off', // Is still buggy
         'react/jsx-one-expression-per-line': 'off',
    
         'jsx-a11y/anchor-is-valid': ['error', { components: ['Link'], specialLink: ['to'] }],
         'jsx-a11y/label-has-for': [
           2,
           {
             required: {
               every: ['id'],
             },
           },
         ], // for nested label htmlFor error
    
         'prettier/prettier': ['error'],
       },
     };
    
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,233评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,013评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,030评论 0 241
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,827评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,221评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,542评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,814评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,513评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,225评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,497评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,998评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,342评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,986评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,055评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,812评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,560评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,461评论 2 266

推荐阅读更多精彩内容