ESLint && Prettier

使用ESLint && prettier对代码进行规范

实现功能:

  1. 在提交的时候对代码进行检测
  2. 实时对代码进行检测
  3. 自定义代码风格

首先安装ESLintprettier

yarn add ESLint
yarn add prettier

踩坑:如果你全局安装了ESLint,并且项目中用的ESLint也是全局的那你装插件也要安装到全局,不然会导致找不到插件。

安装ESLint 插件

这里为了同时使用ESLintPrettier,推荐使用两个插件

eslint-plugin-prettier

eslint-config-prettier

可以解决eslintprettier冲突

eslintrc.js

module.exports = {
  extends: [
    'plugin:vue/strongly-recommended',
    'plugin:prettier/recommended'
  ],
  // add your custom rules here
  rules: {
    // ...other codes
    "prettier/prettier": "error"
  }
}
// 在 rules 里面 添加    
// "prettier/prettier": "error"
// 规则,这样在编辑器里会给prettier错误进行报错

附上prettier配置

prettier.config.js

//prettier.config.js
module.exports = { 
  "printWidth": 80, // 每行代码长度(默认80)
  "tabWidth": 2, // 每个tab相当于多少个空格(默认2)
  "useTabs": false, // 是否使用tab进行缩进(默认false)
  "singleQuote": true, // 使用单引号(默认false)
  "semi": true, // 声明结尾使用分号(默认true)
  "trailingComma": "all", // 多行使用拖尾逗号(默认none)
  "bracketSpacing": true, // 对象字面量的大括号间使用空格(默认true)
  "jsxBracketSameLine": false, // 多行JSX中的>放置在最后一行的结尾,而不是另起一行(默认false)
  "arrowParens": "avoid" // 只有一个参数的箭头函数的参数是否带圆括号(默认avoid)
}; 

.prettierrc 在vscode中生效

{
  "printWidth": 80,
  "tabWidth": 2,
  "useTabs": false,
  "singleQuote": true,
  "semi": true,
  "trailingComma": "all",
  "bracketSpacing": true,
  "jsxBracketSameLine": false,
  "arrowParens": "avoid"
}

如何让编辑器进行报错?

webstrom 里面安装 ESLint Restart Service Action 插件

在设置中搜索 ESLint

参考配置如下

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JXpotXME-1627889501300)(C:\Users\74051\AppData\Roaming\Typora\typora-user-images\image-20210728115134093.png)]

Vscode 参考链接 : https://blog.csdn.net/guang_s/article/details/90293644

在提交的时候对代码进行检测

推荐插件 lint-staged husky

踩坑:注意版本问题,husky: ^0.14.4

// 配置设置如下 
"scripts": {
    "format": "prettier --write \"src/**/*.js\" \"src/**/*.vue\"",
    "precommit": "lint-staged"
  },
  "lint-staged": {
    "src/**/*.{js,json,css,vue}": [
      "prettier --write",
      "eslint --fix",
      "git add"
    ]
  },

husky: ^4.2.5

// 配置如下
"husky": {
    "hooks": {
      "pre-commit": "lint-staged"
    }
  },
  "lint-staged": {
    "src/**/*.{js,json,css,jsx}": [
      "prettier --write",
      "eslint --fix"
    ]
  }

最后附上 VueReact 常用 eslintrc配置

Vue

{
root: true, // 当前配置为根配置,将不再从上级文件夹查找配置
parserOptions: {
  parser: 'babel-eslint',  // 采用 babel-eslint 作为语法解析器
  sourceType: 'module',   // 指定来源的类型,有两种script或module
  ecmaVersion: 6, //指定ECMAScript支持的版本,6为ES6
},
env: {
  browser: true,  // 设置为所需检查的代码是在浏览器环境运行的
  es6: true  // 设置所需检查代码为 es6 语法书写
},
extends: ['plugin:vue/recommended', 'eslint:recommended'],// 扩展使用 vue 检查规则和eslint推荐规则
  rules: {
    'vue/attribute-hyphenation': 0, // 忽略属性连字
    'vue/max-attributes-per-line':[2, { singleline: 10, multiline: { max: 1, allowFirstLine: false } }], // 每行最大属性
    'vue/singleline-html-element-content-newline': 'off', // 单行html元素内容在新的一行
    'vue/multiline-html-element-content-newline': 'off', // 多行html元素内容在新的一行
    'vue/html-closing-bracket-newline': 'off', // html右括号在新的一行
    'vue/no-v-html': 'off', // 不使用v-html
    'vue/html-self-closing': 0, // 忽略html标签自闭合
    'accessor-pairs': 2, // 应同时设置setter和getter
    'arrow-spacing': [2, { before: true, after: true }], // 箭头间距
    'vue/require-default-prop': 0, // 不检查默认属性
    'vue/require-prop-types': 0, // 不检查默认类型
    'block-spacing': [2, 'always'], // 块间距
    'brace-style': [2, '1tbs', { allowSingleLine: true }], // 大括号样式允许单行
    'camelcase': [2, { properties: 'always' }], //为属性强制执行驼峰命名
    'comma-dangle': [2, 'never'], // 逗号不使用悬挂
    'comma-spacing': [2, { before: false, after: true }], // 逗号间距
    'comma-style': [2, 'last'], //(默认)与数组元素,对象属性或变量声明在同一行之后和同一行需要逗号
    'constructor-super': 2,
    'consistent-this': [2, 'that'], //强制this别名为that
    'curly': [2, 'multi-line'], // 当一个块只包含一条语句时,不允许省略花括号。
    'dot-location': [2, 'property'], //成员表达式中的点应与属性部分位于同一行
    'eol-last': 2, // 强制文件以换行符结束(LF)
    'eqeqeq': ['error', 'always', { null: 'ignore' }], // 强制使用全等
    'generator-star-spacing': [2, { before: true, after: true }], // 生成器中'*'两侧都要有间距
    'global-require': 1, // 所有调用require()都位于模块的顶层
    'indent': [2, 2, { SwitchCase: 2 }], //缩进风格
    'key-spacing': [2, { beforeColon: false, afterColon: true }], // 强制在对象字面量属性中的键和值之间保持一致的间距
    'keyword-spacing': [2, { before: true, after: true }], // 关键字如if/function等的间距
    'new-cap': [2, { newIsCap: true, capIsNew: false }],// 允许在没有new操作符的情况下调用大写启动的函数;(默认)要求new使用大写启动函数调用所有操作符
    'new-parens': 2, // JavaScript通过new关键字调用函数时允许省略括号
    'no-array-constructor': 1, // 不允许使用Array构造函数。除非要指定生成数组的长度
    'no-console': process.env.NODE_ENV === 'production' ? 'error' : 'off', // 只有开发环境可以使用console
    'no-class-assign': 2, // 不允许修改类声明的变量
    'no-const-assign': 2, // 不能修改使用const关键字声明的变量
    'no-control-regex': 0, // 不允许正则表达式中的控制字符
    'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off', // 只有开发环境可以使用debugger
    'no-delete-var': 2, // 不允许在变量上使用delete操作符
    'no-dupe-args': 2, // 不允许在函数声明或表达式中使用重复的参数名称
    'no-dupe-class-members': 2, // 不允许在类成员中使用重复的参数名称
    'no-dupe-keys': 2, // 不允许在对象文字中使用重复键
    'no-duplicate-case': 2, // 不允许在switch语句的case子句中使用重复的测试表达式
    'no-empty-character-class': 2, // 不允许在正则表达式中使用空字符类
    'no-empty-pattern': 2, // 不允许空块语句
    'no-eval': 2, // 不允许使用eval
    'no-ex-assign': 2, // 不允许在catch子句中重新分配例外
    'no-extend-native': 2, // 不允许直接修改内建对象的原型
    'no-extra-boolean-cast': 2, // 禁止不必要的布尔转换
    'no-extra-parens': [2, 'functions'], // 仅在函数表达式附近禁止不必要的括号
    'no-fallthrough': 2, //消除一个案件无意中掉到另一个案件
    'no-floating-decimal': 2, //消除浮点小数点,并在数值有小数点但在其之前或之后缺少数字时发出警告
    'no-func-assign': 2, // 允许重新分配function声明
    'no-implied-eval': 2, // 消除隐含eval()
    'no-inner-declarations': [2, 'functions'], // 不允许function嵌套块中的声明
    'no-invalid-regexp': 2, //不允许RegExp构造函数中的无效正则表达式字符串
    'no-irregular-whitespace': 2, //捕获无效的空格
    'no-iterator': 2, //消除阴影变量声明
    'no-label-var': 2, //禁止创建与范围内的变量共享名称的标签
    'no-labels': [2, { allowLoop: false, allowSwitch: false }], // 消除 JavaScript 中使用带标签的语句
    'no-lone-blocks': 2, //消除脚本顶层或其他块中不必要的和可能混淆的块
    'no-mixed-spaces-and-tabs': 2, // 不允许使用混合空格和制表符进行缩进
    'no-multi-spaces': 2, // 禁止在逻辑表达式,条件表达式,声明,数组元素,对象属性,序列和函数参数周围使用多个空格
    'no-multi-str': 2, // 防止使用多行字符串
    'no-multiple-empty-lines': [2, { max: 1 }], // 最多一个空行
    'no-native-reassign': 2, // 不允许修改只读全局变量
    'no-new-object': 2, // 不允许使用Object构造函数
    'no-new-require': 2, // 消除new require表达的使用
    'no-new-symbol': 2, // 防止Symbol与new 同时使用的意外错误
    'no-new-wrappers': 2, // 杜绝使用String,Number以及Boolean与new操作
    'no-obj-calls': 2, // 不允许调用Math,JSON和Reflect对象作为功能
    'no-octal': 2, // 不允许使用八进制文字
    'no-octal-escape': 2, // 不允许字符串文字中的八进制转义序列
    'no-path-concat': 2, // 防止 Node.js 中的目录路径字符串连接无效
    'no-redeclare': 2, // 消除在同一范围内具有多个声明的变量
    'no-regex-spaces': 2, // 在正则表达式文字中不允许有多个空格
    'no-return-assign': [2, 'except-parens'], // 消除return陈述中的任务,除非用括号括起来
    'no-self-assign': 2, // 消除自我分配
    'no-self-compare': 2, // 禁止比较变量与自身
    'no-sequences': 2, // 禁止使用逗号运算符
    'no-sparse-arrays': 2, // 不允许稀疏数组文字
    'no-this-before-super': 2, // 在呼call前标记this/super关键字super()
    'no-throw-literal': 2, // 不允许抛出不可能是Error对象的文字和其他表达式
    'no-trailing-spaces': 2, // 不允许在行尾添加尾随空白
    'no-undef': 2, // 任何对未声明的变量的引用都会导致错误
    'no-undef-init': 2, // 消除初始化为undefined的变量声明
    'no-underscore-dangle': 2, // 标识符不能以_开头或结尾
    'no-unexpected-multiline': 2, // 不允许混淆多行表达式
    'no-unmodified-loop-condition': 2, // 查找循环条件内的引用,然后检查这些引用的变量是否在循环中被修改
    'no-unneeded-ternary': [2, { defaultAssignment: false }], // 不允许将条件表达式作为默认的分配模式
    'no-unreachable': 2, // 不允许可达代码return,throw,continue,和break语句后面还有语句。
    'no-unsafe-finally': 2, // 不允许return,throw,break,和continue里面的语句finally块
    'no-unused-vars': [2, { vars: 'all', args: 'after-used' }],
    // 消除未使用的变量,函数和函数的参数
    // vars: 'all' 检查所有变量的使用情况,包括全局范围内的变量。这是默认设置。 args: 'after-used'  只有最后一个参数必须使用。例如,这允许您为函数使用两个命名参数,并且只要您使用第二个参数,ESLint 就不会警告您第一个参数。这是默认设置。
    'no-useless-call': 2, // 标记使用情况,Function.prototype.call()并且Function.prototype.apply()可以用正常的函数调用来替代
    'no-useless-computed-key': 2, // 禁止不必要地使用计算属性键
    'no-useless-constructor': 2, // 在不改变类的工作方式的情况下安全地移除的类构造函数
    'no-useless-escape': 0, // 禁用不必要的转义字符
    'no-whitespace-before-property': 2, // 如果对象的属性位于同一行上,不允许围绕点或在开头括号之前留出空白
    'no-with': 2, //禁用with
    'no-var': 2, // 禁用var
    'one-var': [2, { initialized: 'never' }], // 强制将变量声明为每个函数(对于var)或块(对于let和const)范围一起声明或单独声明。 initialized: 'never' 每个作用域要求多个变量声明用于初始化变量
    'operator-linebreak': [2, 'after', { overrides: { '?': 'before', ':': 'before' } }], // 实施一致的换行
    'padded-blocks': [2, 'never'], // 在块内强制执行一致的空行填充
    'prefer-destructuring': ['error', { object: false, array: false }], // 此规则强制使用解构而不是通过成员表达式访问属性。
    'quotes': [2, 'single', { avoidEscape: true, allowTemplateLiterals: true }],// avoidEscape: true 允许字符串使用单引号或双引号,只要字符串包含必须以其他方式转义的引号 ;allowTemplateLiterals: true 允许字符串使用反引号
    'radix': 2, //parseInt必须指定第二个参数
    'semi': [2, 'never'], // 不使用分号
    'semi-spacing': [2, { before: false, after: true }], // 强制分号间隔
    'space-before-blocks': [2, 'always'], // 块必须至少有一个先前的空间
    'space-before-function-paren': [2, 'never'], // 在(参数后面不允许任何空格
    'space-in-parens': [2, 'never'], // 禁止或要求(或)左边的一个或多个空格
    'space-infix-ops': 2, // 强制二元运算符左右各有一个空格
    'space-unary-ops': [2, { words: true, nonwords: false }],//  words: true  如:new,delete,typeof,void,yield 左右必须有空格 //  nonwords: false  一元运算符,如:-,+,--,++,!,!!左右不能有空格
    'spaced-comment': [2, 'always', { markers: ['global', 'globals', 'eslint', 'eslint-disable', '*package', '!', ','] }], // 注释开始后,此规则将强制间距的一致性//或/*
    'template-curly-spacing': [2, 'never'], // 不允许大括号内的空格
    'use-isnan': 2, //禁止比较时使用NaN,只能用isNaN()
    'valid-typeof': 2, //必须使用合法的typeof的值
    'wrap-iife': [2, 'any'], //立即执行函数表达式的小括号风格
    'yield-star-spacing': [2, 'both'], // 强制执行*周围 yield*表达式的间距,两侧都必须有空格
    'yoda': [2, 'never'],
    'prefer-const': 2, // 使用let关键字声明的变量,但在初始分配后从未重新分配变量,应改为const声明
    'object-curly-spacing': [2, 'always', { objectsInObjects: false }],// 不允许以对象元素开始和/或以对象元素结尾的对象的大括号内的间距
    'array-bracket-spacing': [2, 'never'] // 不允许数组括号内的空格
  }
}

React

const path = require('path');

module.exports = {
  parser: '@typescript-eslint/parser',
  parserOptions: {
    ecmaVersion: 2015, // specify the version of ECMAScript syntax you want to use: 2015 => (ES6)
    sourceType: 'module', // Allows for the use of imports
    ecmaFeatures: {
      jsx: true, // enable JSX
      impliedStrict: true, // enable global strict mode
    },
  },
  extends: [
    'airbnb', // Uses airbnb, it including the react rule(eslint-plugin-react/eslint-plugin-jsx-a11y)
    'plugin:promise/recommended',
    // 'prettier', // Use prettier, it can pretty file before executing eslint --fix
    // 'prettier/react' // Use prettier/react to pretty react syntax
    'plugin:prettier/recommended',
  ],
  settings: {
    'import/resolver': {
      // This config is described in article []()
      // webpack: {
      //   config: 'config/webpack.config.js'
      // },
      node: {
        extensions: ['.js', '.jsx', '.ts', '.tsx'],
      },
      alias: {
        map: [
          ['@', path.resolve(__dirname, './src')],
        ],
        extensions: ['.ts', '.js', '.jsx', '.json', '.tsx'],
      },
    },
  },
  env: {
    browser: true, // enable all browser global variables
  },
  plugins: ['react-hooks', 'promise', 'prettier', '@typescript-eslint'], // ['prettier', 'react-hooks']
  rules: {
    // Place to specify ESLint rules. Can be used to overwrite rules specified from the extended configs
    // 0 - off, 1 - warn, 2 - error
    'react-hooks/rules-of-hooks': 'error',
    // 'react-hooks/exhaustive-deps': 'warn',
    'react/jsx-one-expression-per-line': 0,
    'react/jsx-curly-newline': 0,
    'react/prop-types': 0,
    'import/extensions': 0,
    'react/jsx-filename-extension': 0,
    'jsx-a11y/click-events-have-key-events': 0,
    'jsx-a11y/no-noninteractive-element-interactions': 0,
    'promise/always-return': 0,
    'promise/catch-or-return': 0,
    'jsx-a11y/no-static-element-interactions': 0,
    'no-console': 0,
    'no-shadow': 0,
    'react/jsx-props-no-spreading': 0,
    'camelcase': 0,
    'prefer-promise-reject-errors': 0,
    'no-param-reassign': 0,
    'consistent-return': 0,
    'no-nested-ternary': 0,
    /**
     * @description rules of eslint-plugin-prettier
     */
    // https://prettier.io/docs/en/options.html
    'prettier/prettier': 'error',
    'no-useless-escape': 0,
    'no-throw-literal': 0,
    'no-restricted-syntax': 0,
    'guard-for-in': 0,
    'jsx-a11y/control-has-associated-label': 0,
    'react/jsx-wrap-multilines': 1,
    'import/prefer-default-export': 0,
    'react/button-has-type': 0,
    'react/destructuring-assignment': 0,
    'jsx-a11y/media-has-caption': 0,
    'no-return-assign': 0,
    'react/forbid-prop-types': 0,
    'no-unused-vars': 0, // ts中的type/interface
    'no-plusplus': 0,
  },
  globals: {
    eruda: 'readonly',
    wx: 'readonly',
  },
};

promise eslint插件

https://www.npmjs.com/package/eslint-plugin-promise

'promise/always-return': 'error',
'promise/no-return-wrap': 'error',
'promise/param-names': 'error',
'promise/catch-or-return': 'error',
'promise/no-native': 'off',
'promise/no-nesting': 'warn',
'promise/no-promise-in-callback': 'warn',
'promise/no-callback-in-promise': 'warn',
'promise/avoid-new': 'warn',
'promise/no-new-statics': 'error',
'promise/no-return-in-finally': 'warn',
'promise/valid-params': 'warn',

使用装饰器

npm i --save-dev eslint-plugin-use-decorator
 " plugins " : [ " use-decorator " ],
  " rules " : {
    " use-decorator/use-decorator " : [ 1 , {
      " params " : [
        {
          " name " : " assertParameter " ,
          " public " : true
        }

ew-statics’: ‘error’,
‘promise/no-return-in-finally’: ‘warn’,
‘promise/valid-params’: ‘warn’,


### 使用装饰器

```js
npm i --save-dev eslint-plugin-use-decorator
 " plugins " : [ " use-decorator " ],
  " rules " : {
    " use-decorator/use-decorator " : [ 1 , {
      " params " : [
        {
          " name " : " assertParameter " ,
          " public " : true
        }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值