安装

  1. npm install eslint --save-dev
  2. npm install eslint --global #全局安装
  3. # 设置配置文件
  4. ./node_modules/.bin/eslint --init
  5. # 可以单独运行在一个指定文件 或者目录上面
  6. ./node_modules/.bin/eslint yourfile.js
  7. # 初始化

vue

  1. // eslint-disable-next-line no-undef
  2. module.exports = {
  3. 'env': {
  4. 'browser': true,
  5. 'es2021': true
  6. },
  7. 'extends': [
  8. 'eslint:recommended',
  9. 'plugin:vue/essential'
  10. ],
  11. 'parserOptions': {
  12. 'ecmaVersion': 12,
  13. 'sourceType': 'module'
  14. },
  15. 'plugins': ['vue'],
  16. 'rules': {
  17. 'for-direction': 'error',
  18. 'no-cond-assign': 'error',
  19. 'array-bracket-newline': 0,
  20. 'array-bracket-spacing': 'error',
  21. 'array-callback-return': 'error',
  22. 'callback-return': 'error',
  23. 'array-element-newline': 0,
  24. 'arrow-spacing': 'error',
  25. 'block-scoped-var': 'error',
  26. 'block-spacing': 'error',
  27. 'brace-style': 'error',
  28. 'capitalized-comments': 0,
  29. 'comma-dangle': 0,
  30. 'comma-style': 2,
  31. 'comma-spacing': 'error',
  32. 'complexity': 'error',
  33. 'computed-property-spacing': 'error',
  34. 'consistent-return': 'error',
  35. 'consistent-this': 'error',
  36. 'curly': 'error',
  37. 'default-case': 'error',
  38. 'default-case-last': 'error',
  39. 'default-param-last': 'error',
  40. 'dot-location': 'error',
  41. 'dot-notation': 'error',
  42. 'eol-last': 'error',
  43. 'eqeqeq': 'error',
  44. 'func-call-spacing': 'error',
  45. 'func-name-matching': 'error',
  46. 'func-names': 'error',
  47. 'function-paren-newline': 'error',
  48. 'guard-for-in': 'error',
  49. 'implicit-arrow-linebreak': 'error',
  50. 'indent': ['error', 2, { 'ignoreComments': true }],
  51. 'init-declarations': 'error',
  52. 'no-delete-var': 'error',
  53. 'jsx-quotes': ['error', 'prefer-double'],
  54. 'key-spacing': 'error',
  55. 'keyword-spacing': ['error', { 'after': true, 'before': true}],
  56. 'line-comment-position': ['error', { 'before': true }],
  57. 'linebreak-style': [ 'error','unix'],
  58. 'lines-around-comment': 'error',
  59. 'lines-between-class-members': 'error',
  60. 'no-class-assign': 'error',
  61. 'max-classes-per-file': 'error',
  62. 'max-depth': 'error',
  63. 'max-len': 'error',
  64. 'multiline-comment-style': 'error',
  65. 'new-cap': 'error',
  66. 'new-parens': 'error',
  67. 'newline-per-chained-call': 'error',
  68. 'no-mixed-requires': 0,
  69. 'no-restricted-modules': 0,
  70. 'no-compare-neg-zero': 'error',
  71. 'no-confusing-arrow': 'error',
  72. 'no-case-declarations': 'error',
  73. 'no-dupe-args': 'error',
  74. 'no-dupe-keys': 'error',
  75. 'no-duplicate-case': 'error',
  76. 'no-empty': 'error',
  77. 'no-empty-character-class': 'error',
  78. 'no-duplicate-imports': 'error',
  79. 'no-new-symbol': 'error',
  80. 'no-else-return': 'error',
  81. 'no-empty-function': 'error',
  82. 'no-empty-pattern': 'error',
  83. 'no-eq-null': 'error',
  84. 'no-eval': 'error',
  85. 'no-extra-label': 'error',
  86. 'no-fallthrough': 'error',
  87. 'no-extra-semi': 'error',
  88. 'no-func-assign': 'error',
  89. 'no-inner-declarations': 'error',
  90. 'no-invalid-regexp': 'error',
  91. 'no-irregular-whitespace': 'error',
  92. 'no-obj-calls': 'error',
  93. 'no-regex-spaces': 'error',
  94. 'no-floating-decimal': 'error',
  95. 'no-global-assign': 'error',
  96. 'no-lonely-if': 'error',
  97. 'no-loop-func': 'error',
  98. 'no-loss-of-precision': 'error',
  99. 'no-magic-numbers': 'error',
  100. 'no-multiple-empty-lines': 'error',
  101. 'no-redeclare': 'error',
  102. 'no-return-assign': 'error',
  103. 'no-self-assign': 'error',
  104. 'no-self-compare': 'error',
  105. 'no-sequences': 'error',
  106. 'no-shadow': 'error',
  107. 'no-tabs': 'off',
  108. 'no-shadow-restricted-names': 'error',
  109. 'no-unexpected-multiline': 'error',
  110. 'no-unreachable': 'error',
  111. 'no-unsafe-finally': 'error',
  112. 'no-unsafe-negation': 'error',
  113. 'no-undef': 'error',
  114. 'no-undef-init': 'error',
  115. 'no-undefined': 'error',
  116. 'no-unused-vars': 'error',
  117. 'no-underscore-dangle': 'error',
  118. 'no-unmodified-loop-condition': 'error',
  119. 'no-unneeded-ternary': 'error',
  120. 'no-unreachable-loop': 'error',
  121. 'no-unsafe-optional-chaining': 'error',
  122. 'no-unused-expressions': 'error',
  123. 'no-unused-labels': 'error',
  124. 'no-use-before-define': 'error',
  125. 'no-useless-catch': 'error',
  126. 'no-useless-computed-key': 'error',
  127. 'no-useless-escape': 'error',
  128. 'no-useless-constructor': 'error',
  129. 'no-useless-rename': 'error',
  130. 'no-useless-return': 'error',
  131. 'no-var': 'error',
  132. 'no-whitespace-before-property': 'error',
  133. 'nonblock-statement-body-position': 'error',
  134. 'object-curly-newline': 'error',
  135. 'object-property-newline': 0,
  136. 'object-shorthand': 'error',
  137. 'one-var': 'off',
  138. 'one-var-declaration-per-line': 'error',
  139. 'operator-assignment': 'error',
  140. 'operator-linebreak': 'error',
  141. 'padded-blocks': 'error',
  142. 'padding-line-between-statements': 'error',
  143. 'prefer-arrow-callback': 'error',
  144. 'prefer-const': 'error',
  145. 'no-const-assign': 'error',
  146. 'no-dupe-class-members': 'error',
  147. 'prefer-destructuring': 'error',
  148. 'prefer-object-spread': 'error',
  149. 'prefer-template': 'error',
  150. 'quotes': [
  151. 'error',
  152. 'single'
  153. ],
  154. 'use-isnan': 'error',
  155. 'valid-typeof': 'error',
  156. 'require-await': 'error',
  157. 'semi': 'off',
  158. 'sort-imports': 'error',
  159. 'semi-spacing': 'error',
  160. 'semi-style': 'error',
  161. 'sort-vars': 'error',
  162. 'space-before-blocks': 'error',
  163. 'space-before-function-paren': 'error',
  164. 'space-in-parens': [
  165. 'error',
  166. 'never'
  167. ],
  168. 'space-infix-ops': 'error',
  169. 'space-unary-ops': 'error',
  170. 'spaced-comment': 'error',
  171. 'strict': 'off',
  172. 'unicode-bom': 'error',
  173. 'switch-colon-spacing': 'error'
  174. }
  175. };

.eslintrc.js文件配置

  1. /*
  2. *@author Augo
  3. * 单行注释 推荐的规则
  4. * 多行注释 推荐可选
  5. * global
  6. */
  7. /* @global */
  8. module.exports = {
  9. 'root': true,
  10. // 指定环境
  11. 'env': {
  12. 'browser': true,
  13. // 'es2021': true,
  14. 'es6': true,
  15. 'node': true
  16. },
  17. 'extends': [
  18. 'plugin:vue/essential',
  19. 'standard'
  20. /*
  21. * 'eslint:recommended',
  22. *
  23. */
  24. // 'plugin:vue/vue3-recommended'
  25. /*
  26. *
  27. * 'plugin:vue/essential',
  28. *
  29. */
  30. ],
  31. // 'parser': 'babel-eslint',
  32. // 指定解析器选项
  33. 'parserOptions': {
  34. 'parser': 'babel-eslint',
  35. /*
  36. *
  37. * 设置为 3、5(默认)、6、7、8、9、10、11 或 12 以指定要使用的 ECMAScript 语法版本
  38. */
  39. 'ecmaVersion': 6,
  40. // 设置为"script"(默认)或者"module"如果您的代码在 ECMAScript 模块中
  41. 'sourceType': 'module'
  42. /*
  43. * 一个对象,指示您要使用哪些附加语言功能
  44. * 'ecmaFeatures': {
  45. * 'jsx': true
  46. * // 'experimentalObjectRestSpread': true
  47. * }
  48. */
  49. },
  50. // 使用插件
  51. 'plugins': ['vue'],
  52. // 配置规则
  53. 'rules': {
  54. /*
  55. * Vue3 的校验配置
  56. * allow async-await
  57. */
  58. 'generator-star-spacing': 'off',
  59. // Allow debugger during development
  60. 'vue/no-use-v-if-with-v-for': [
  61. 'error', {
  62. 'allowUsingIterationVar': false
  63. }
  64. ],
  65. 'vue/return-in-computed-property': [
  66. 'error', {
  67. 'treatUndefinedAsUnspecified': false
  68. }
  69. ],
  70. 'vue/no-unused-components': [
  71. 'error', {
  72. 'ignoreWhenBindingPresent': true
  73. }
  74. ],
  75. 'vue/attribute-hyphenation': [
  76. 'error', 'always', {
  77. 'ignore': []
  78. }
  79. ],
  80. 'vue/component-name-in-template-casing': [
  81. 'error', 'kebab-case', {
  82. 'ignores': []
  83. }
  84. ],
  85. 'vue/html-closing-bracket-newline': [
  86. 'error', {
  87. 'singleline': 'never',
  88. 'multiline': 'always'
  89. }
  90. ],
  91. 'vue/html-closing-bracket-spacing': [
  92. 'error', {
  93. 'startTag': 'never',
  94. 'endTag': 'never',
  95. 'selfClosingTag': 'always'
  96. }
  97. ],
  98. 'vue/html-indent': [
  99. 'error', 2, {
  100. 'attribute': 1,
  101. 'baseIndent': 1,
  102. 'closeBracket': 0,
  103. 'alignAttributesVertically': true,
  104. 'ignores': []
  105. }
  106. ],
  107. 'vue/html-quotes': ['error', 'double'],
  108. 'vue/html-self-closing': [
  109. 'error', {
  110. 'html': {
  111. 'void': 'never',
  112. 'normal': 'never',
  113. 'component': 'always'
  114. },
  115. 'svg': 'always',
  116. 'math': 'always'
  117. }
  118. ],
  119. 'vue/max-attributes-per-line': [
  120. 'error', {
  121. 'singleline': {
  122. 'max': 3,
  123. 'allowFirstLine': true
  124. },
  125. 'multiline': {
  126. 'max': 3,
  127. 'allowFirstLine': true
  128. }
  129. }
  130. ],
  131. 'vue/multiline-html-element-content-newline': [
  132. 'error', {
  133. 'ignoreWhenEmpty': true,
  134. 'ignores': ['pre', 'textarea']
  135. }
  136. ],
  137. 'vue/mustache-interpolation-spacing': ['error', 'always'],
  138. 'vue/name-property-casing': ['error', 'kebab-case'],
  139. 'vue/no-multi-spaces': [
  140. 'error', {
  141. 'ignoreProperties': false
  142. }
  143. ],
  144. 'vue/no-spaces-around-equal-signs-in-attribute': ['error'],
  145. 'vue/no-template-shadow': ['error'],
  146. 'vue/prop-name-casing': ['error', 'camelCase'],
  147. 'vue/require-default-prop': ['error'],
  148. 'vue/v-bind-style': ['error', 'shorthand'],
  149. 'vue/v-on-style': ['error', 'shorthand'],
  150. 'vue/attributes-order': [
  151. 'error', {
  152. 'order': [
  153. 'DEFINITION',
  154. 'LIST_RENDERING',
  155. 'CONDITIONALS',
  156. 'RENDER_MODIFIERS',
  157. 'GLOBAL',
  158. 'UNIQUE',
  159. 'TWO_WAY_BINDING',
  160. 'OTHER_DIRECTIVES',
  161. 'OTHER_ATTR',
  162. 'EVENTS',
  163. 'CONTENT'
  164. ]
  165. }
  166. ],
  167. 'vue/order-in-components': [
  168. 'error', {
  169. 'order': [
  170. 'el',
  171. 'name',
  172. 'parent',
  173. 'functional',
  174. ['delimiters', 'comments'],
  175. ['components', 'directives', 'filters'],
  176. 'extends',
  177. 'mixins',
  178. 'inheritAttrs',
  179. 'model',
  180. ['props', 'propsData'],
  181. 'data',
  182. 'computed',
  183. 'watch',
  184. 'LIFECYCLE_HOOKS',
  185. 'methods',
  186. ['template', 'render'],
  187. 'renderError'
  188. ]
  189. }
  190. ],
  191. 'vue/this-in-template': ['error', 'never'],
  192. // * 强制 “for” 循环中更新子句的计数器朝着正确的方向移动
  193. 'for-direction': 'error',
  194. //* * 禁止条件表达式中出现赋值操作符
  195. 'no-cond-assign': 'error',
  196. /*
  197. * 强制 getter 和 setter 在对象中成对出现
  198. * 'accessor-pairs': 'error',
  199. */
  200. // 禁止使用异步函数作为 Promise executorrules
  201. 'no-async-promise-executor': 'error',
  202. // 在数组开括号后和闭括号前强制换行
  203. 'array-bracket-newline': 'error',
  204. // 强制数组方括号中使用一致的空格
  205. 'array-bracket-spacing': 'error',
  206. // 强制数组方法的回调函数中有 return 语句
  207. 'array-callback-return': 'error',
  208. /*
  209. * 强制数组方法的回调函数中有 return 语句
  210. * 'callback-return': 'error',
  211. */
  212. /*
  213. * 要求回调函数中有容错处理
  214. * 'handle-callback-err': 'error',
  215. */
  216. /*
  217. * 强制数组元素间出现换行
  218. * 'array-element-newline': 'error',
  219. */
  220. /*
  221. * 要求箭头函数体使用大括号
  222. * 'arrow-body-style': 'error',
  223. */
  224. // 要求箭头函数的参数使用圆括号
  225. 'arrow-parens': [
  226. 'error',
  227. 'as-needed'
  228. ],
  229. // 强制箭头函数的箭头前后使用一致的空格
  230. 'arrow-spacing': 'error',
  231. // 强制把变量的使用限制在其定义的作用域范围内
  232. 'block-scoped-var': 'error',
  233. // 禁止或强制在代码块中开括号前和闭括号后有空格
  234. 'block-spacing': 'error',
  235. // 强制在代码块中使用一致的大括号风格
  236. 'brace-style': 'error',
  237. /*
  238. * 强制使用骆驼拼写法命名约定
  239. * 'camelcase': 'error',
  240. */
  241. // 强制或禁止对注释的第一个字母大写
  242. 'capitalized-comments': 'error',
  243. // 强制类方法使用 this
  244. 'class-methods-use-this': 'error',
  245. // 要求或禁止末尾逗号
  246. 'comma-dangle': 'error',
  247. /*
  248. * 强制在逗号前后使用一致的空格
  249. * 'comma-spacing': 'error',
  250. */
  251. /*
  252. * 强制使用一致的逗号风格
  253. * 'comma-style': 'error',
  254. */
  255. /*
  256. * 指定程序中允许的最大环路复杂度
  257. * 'complexity': 'error',
  258. */
  259. // 强制在计算的属性的方括号中使用一致的空格
  260. 'computed-property-spacing': 'error',
  261. // 要求 return 语句要么总是指定返回的值,要么不指定
  262. 'consistent-return': 'error',
  263. /*
  264. * 当获取当前执行环境的上下文时,强制使用一致的命名
  265. * 'consistent-this': 'error',
  266. */
  267. /*
  268. * 强制所有控制语句使用一致的括号风格
  269. * 'curly': 'error',
  270. */
  271. // 要求 switch 语句中有 default 分支
  272. 'default-case': 'error',
  273. /*
  274. * 最后一个默认案例
  275. * 'default-case-last': 'error',
  276. */
  277. /*
  278. * 默认参数last
  279. * 'default-param-last': 'error',
  280. */
  281. // 强制在点号之前和之后一致的换行
  282. 'dot-location': 'error',
  283. // 强制尽可能地使用点号
  284. 'dot-notation': 'error',
  285. // 要求或禁止文件末尾存在空行
  286. 'eol-last': 'error',
  287. // 要求使用 === 和 !==
  288. 'eqeqeq': 'error',
  289. // 要求或禁止在函数标识符和其调用之间有空格
  290. 'func-call-spacing': 'error',
  291. /*
  292. * 要求函数名与赋值给它们的变量名或属性名相匹配
  293. * 'func-name-matching': 'error',
  294. */
  295. /*
  296. * 要求或禁止使用命名的 function 表达式
  297. * 'func-names': 'error',
  298. */
  299. /*
  300. * 强制一致使用function声明或表达式
  301. * 'func-style': 'error',
  302. */
  303. /*
  304. * 函数调用参数换行符
  305. * 'function-call-argument-newline': 'error',
  306. */
  307. /*
  308. * 强制在函数括号内使用一致的换行
  309. * 'function-paren-newline': 'error',
  310. */
  311. /*
  312. * 禁用指定的标识符
  313. * 'id-blacklist': 'error',
  314. */
  315. /*
  316. * 强制 generator 函数中 * 号周围使用一致的空格
  317. * 'generator-star-spacing': 'error',
  318. */
  319. /*
  320. * 分组存取器对
  321. * 'grouped-accessor-pairs': 'error',
  322. */
  323. /*
  324. * 要求 for-in 循环中有一个 if 语句
  325. * 'guard-for-in': 'error',
  326. */
  327. // 'id-denylist': 'error',
  328. /*
  329. * 强制标识符的最小和最大长度
  330. * 'id-length': 'error',
  331. */
  332. /*
  333. * 要求标识符匹配一个指定的正则表达式
  334. * 'id-match': 'error',
  335. */
  336. // 强制隐式返回的箭头函数体的位置
  337. 'implicit-arrow-linebreak': 'error',
  338. // 强制使用一致的缩进 缩进 2空格 ignoreComments (默认: false) 当注释不需要与前一行或下一行的注释对齐,可以使用此选项
  339. 'indent': ['error', 2, { 'ignoreComments': true }],
  340. /*
  341. * 要求或禁止 var 声明中的初始化
  342. * 'init-declarations': 'error',
  343. */
  344. // 禁止删除变量
  345. 'no-delete-var': 'error',
  346. /*
  347. * 该规则强制在 JSX 属性中使用一致的单引号或双引号。
  348. * "prefer-double" (默认) 强制所有不包含双引号的 JSX 属性值使用双引号。
  349. * "prefer-single" 强制所有不包含单引号的 JSX 属性值使用单引号。
  350. */
  351. 'jsx-quotes': ['error', 'prefer-double'],
  352. // 强制在对象字面量的属性中键和值之间使用一致的间距
  353. 'key-spacing': 'error',
  354. // 强制在关键字前后使用一致的空格
  355. 'keyword-spacing': [
  356. 'error',
  357. {
  358. 'after': true,
  359. 'before': true
  360. }
  361. ],
  362. /*
  363. *
  364. * 强制行注释的位置
  365. * 'line-comment-position': ['error', { 'before': true }],
  366. */
  367. // 强制使用一致风格
  368. 'linebreak-style': [
  369. 'error',
  370. // "windows"
  371. 'unix'
  372. ],
  373. // 要求注释周围有空格
  374. 'lines-around-comment': 'error',
  375. // 要求或禁止类成员之间出现空行
  376. 'lines-between-class-members': 'error',
  377. // 禁止修改类声明的变量
  378. 'no-class-assign': 'error',
  379. /*
  380. * 强制每个文件中包含的的类的最大数量
  381. * 'max-classes-per-file': 'error',
  382. */
  383. /*
  384. * 强制可嵌套的块的最大深度
  385. * 'max-depth': 'error',
  386. */
  387. /*
  388. * 强制一行的最大长度
  389. * 'max-len': 'error',
  390. */
  391. /*
  392. * 强制最大文件长度
  393. * "code"(默认80)强制最大行长度
  394. * "tabWidth"(默认4)指定制表符的字符宽度
  395. * "comments"强制注释的最大行长;默认值为code
  396. * "ignorePattern"忽略匹配正则表达式的行;只能匹配一行,用YAML或JSON编写时需要双重转义
  397. * "ignoreComments": true 忽略所有尾随注释和它们自己行的注释
  398. * "ignoreTrailingComments": true 仅忽略尾随评论
  399. * "ignoreUrls": true 忽略包含 URL 的行
  400. * "ignoreStrings": true 忽略包含双引号或单引号字符串的行
  401. * "ignoreTemplateLiterals": true 忽略包含模板文字的行
  402. * "ignoreRegExpLiterals": true 忽略包含 RegExp 文字的行
  403. */
  404. /*
  405. * 'max-lines': [
  406. * 'error',
  407. * {
  408. * 'max': 500,
  409. * 'skipComments': true,
  410. * 'skipBlankLines': true
  411. * }
  412. * ],
  413. */
  414. /*
  415. * 强制函数最大代码行数
  416. * 'max-lines-per-function': 'error',
  417. */
  418. /*
  419. * 强制回调函数最大嵌套深度
  420. * 'max-nested-callbacks': 'error',
  421. */
  422. /*
  423. * 强制函数定义中最多允许的参数数量
  424. * 'max-params': 'error',
  425. */
  426. /*
  427. * 强制函数块最多允许的的语句数量
  428. * 'max-statements': 'error',
  429. */
  430. /*
  431. * 强制每一行中所允许的最大语句数量
  432. * 'max-statements-per-line': 'error',
  433. */
  434. // 强制对多行注释使用特定风格
  435. 'multiline-comment-style': 'error',
  436. /*
  437. * 要求或禁止在三元操作数中间换行
  438. * 'multiline-ternary': 'error',
  439. */
  440. // 要求构造函数首字母大写
  441. 'new-cap': 'error',
  442. // 强制或禁止调用无参构造函数时有圆括号
  443. 'new-parens': 'error',
  444. // 要求方法链中每个调用都有一个换行符
  445. 'newline-per-chained-call': 'error',
  446. /*
  447. * 禁用 alert、confirm 和 prompt
  448. * 'no-alert': 'error',
  449. */
  450. /*
  451. * 禁用 Array 构造函数
  452. * 'no-array-constructor': 'error',
  453. */
  454. // 禁用 Buffer() 构造函数
  455. 'no-buffer-constructor': 'error',
  456. /*
  457. * 禁止混合常规变量声明和 require 调用
  458. * 'no-mixed-requires': 'error',
  459. */
  460. /*
  461. * 禁止调用 require 时使用 new 操作符
  462. * 'no-new-require': 'error',
  463. */
  464. /*
  465. * 禁止对 __dirname 和 __filename 进行字符串连接
  466. * 'no-path-concat': 'error',
  467. */
  468. /*
  469. * 禁用 process.env
  470. * 'no-process-env': 'error',
  471. */
  472. /*
  473. * 禁用 process.exit()
  474. * 'no-process-exit': 'error',
  475. */
  476. /*
  477. * 禁用通过 require 加载的指定模块
  478. * 'no-restricted-modules': 'error',
  479. */
  480. /*
  481. * 禁用同步方法
  482. * 'no-sync': 'error',
  483. */
  484. // 禁止在循环中出现 await
  485. 'no-await-in-loop': 'error',
  486. // 禁止与 -0 进行比较
  487. 'no-compare-neg-zero': 'error',
  488. /*
  489. * 禁用按位运算符
  490. * 'no-bitwise': 'error',
  491. */
  492. /*
  493. * 禁用 arguments.caller 或 arguments.callee
  494. * 'no-caller': 'error',
  495. */
  496. // 禁止在可能与比较操作符相混淆的地方使用箭头函数
  497. 'no-confusing-arrow': 'error',
  498. // 不允许在 case 子句中使用词法声明
  499. 'no-case-declarations': 'error',
  500. /*
  501. * 禁用 console
  502. * 'no-console': 'error',
  503. */
  504. // 禁止在条件中使用常量表达式
  505. 'no-constant-condition': 'error',
  506. // 禁止在正则表达式中使用控制字符
  507. 'no-control-regex': 'error',
  508. // 禁用 debugger
  509. 'no-debugger': 'error',
  510. // 禁止 function 定义中出现重名参数
  511. 'no-dupe-args': 'error',
  512. // 禁止对象字面量中出现重复的 key
  513. 'no-dupe-keys': 'error',
  514. // 禁止出现重复的 case 标签
  515. 'no-duplicate-case': 'error',
  516. // 禁止出现空语句块
  517. 'no-empty': 'error',
  518. // /禁止在正则表达式中使用空字符集
  519. 'no-empty-character-class': 'error',
  520. // 禁止对 catch 子句的参数重新赋值
  521. 'no-ex-assign': 'error',
  522. // 禁止不必要的布尔转换
  523. 'no-extra-boolean-cast': 'error',
  524. /*
  525. * 无构造函数返回
  526. * 'no-constructor-return': 'error',
  527. */
  528. /*
  529. * 要求在构造函数中有 super() 的调用
  530. * 'constructor-super': 'error',
  531. */
  532. // 禁用 continue 语句
  533. 'no-continue': 'error',
  534. // 禁止除法操作符显式的出现在正则表达式开始的位置
  535. 'no-div-regex': 'error',
  536. // 禁止重复模块导入
  537. 'no-duplicate-imports': 'error',
  538. // 禁止 Symbolnew 操作符和 new 一起使用
  539. 'no-new-symbol': 'error',
  540. // 禁止 if 语句中 return 语句之后有 else 块
  541. 'no-else-return': 'error',
  542. // 禁止出现空函数
  543. 'no-empty-function': 'error',
  544. // 禁止使用空解构模式
  545. 'no-empty-pattern': 'error',
  546. // 禁止在没有类型检查操作符的情况下与 null 进行比较
  547. 'no-eq-null': 'error',
  548. // 禁用 eval()
  549. 'no-eval': 'error',
  550. // 禁止扩展原生类型
  551. 'no-extend-native': 'error',
  552. // 禁止不必要的 .bind() 调用
  553. 'no-extra-bind': 'error',
  554. // 禁用不必要的标签
  555. 'no-extra-label': 'error',
  556. // 禁止 case 语句落空
  557. 'no-fallthrough': 'error',
  558. /*
  559. * 禁止不必要的括号
  560. * 'no-extra-parens': 'error',
  561. */
  562. // 禁止不必要的分号
  563. 'no-extra-semi': 'error',
  564. // 禁止对 function 声明重新赋值
  565. 'no-func-assign': 'error',
  566. // 禁止在嵌套的块中出现变量声明或 function 声明
  567. 'no-inner-declarations': 'error',
  568. // 禁止 RegExp 构造函数中存在无效的正则表达式字符串
  569. 'no-invalid-regexp': 'error',
  570. // 禁止不规则的空白
  571. 'no-irregular-whitespace': 'error',
  572. // 不允许在字符类语法中出现由多个代码点组成的字符
  573. 'no-misleading-character-class': 'error',
  574. // 禁止把全局对象作为函数调用
  575. 'no-obj-calls': 'error',
  576. // 禁止直接调用 Object.prototypes 的内置属性
  577. 'no-prototype-builtins': 'error',
  578. // 禁止正则表达式字面量中出现多个空格
  579. 'no-regex-spaces': 'error',
  580. // 禁用稀疏数组
  581. 'no-sparse-arrays': 'error',
  582. // 禁止数字字面量中使用前导和末尾小数点 错误的 .5 真确的 0.5
  583. 'no-floating-decimal': 'error',
  584. // 禁止对原生对象或只读的全局对象进行赋值
  585. 'no-global-assign': 'error',
  586. // 禁止使用短符号进行类型转换
  587. 'no-implicit-coercion': 'error',
  588. /*
  589. * 禁止在全局范围内使用变量声明和 function 声明
  590. * 'no-implicitrules-globals': 'error',
  591. */
  592. /*
  593. * 禁止使用类似 eval() 的方法
  594. * 'no-implied-eval': 'error',
  595. */
  596. /*
  597. * 禁止在代码后使用内联注释
  598. * 'no-inline-comments': 'error',
  599. */
  600. // 禁止 this 关键字出现在类和类对象之外
  601. 'no-invalid-this': 'error',
  602. // 禁用 __iterator__ 属性
  603. 'no-iterator': 'error',
  604. /*
  605. * 不允许标签与变量同名
  606. * 'no-label-var': 'error',
  607. */
  608. // 禁用标签语句
  609. 'no-labels': 'error',
  610. // 禁用不必要的嵌套块
  611. 'no-lone-blocks': 'error',
  612. /*
  613. * 禁止 if 作为唯一的语句出现在 else 语句中
  614. * 'no-lonely-if': 'error',
  615. */
  616. // 禁止在循环语句中出现包含不安全引用的函数声明
  617. 'no-loop-func': 'error',
  618. /*
  619. * 无精度损失
  620. * 'no-loss-of-precision': 'error',
  621. */
  622. /*
  623. * 禁用魔术数字
  624. * 'no-magic-numbers': 'error',
  625. */
  626. /*
  627. * 禁止混合使用不同的操作符
  628. * 'no-mixed-operators': 'error',
  629. *
  630. */
  631. /*
  632. * 禁止连续赋值
  633. * 'no-multi-assign': 'error',
  634. */
  635. /*
  636. * 禁止空格和 tab 的混合缩进
  637. * 'no-mixed-spaces-and-tabs': 'error',
  638. */
  639. // 禁止使用多个空格
  640. 'no-multi-spaces': 'error',
  641. // 禁止使用多行字符串
  642. 'no-multi-str': 'error',
  643. // 禁止出现多行空行
  644. 'no-multiple-empty-lines': 'error',
  645. /*
  646. * 禁用否定的表达式
  647. * 'no-negated-condition': 'error',
  648. */
  649. /*
  650. * 禁用嵌套的三元表达式
  651. * 'no-nested-ternary': 'error',
  652. */
  653. // 禁止使用 new 以避免产生副作用
  654. 'no-new': 'error',
  655. // 禁止对 Function 对象使用 new 操作符
  656. 'no-new-func': 'error',
  657. // 禁止对 object 对象使用 new 操作符
  658. 'no-new-object': 'error',
  659. // 禁止对 String,Number 和 Boolean 使用 new 操作符
  660. 'no-new-wrappers': 'error',
  661. /*
  662. * 无非八进制十进制转义
  663. * 'no-nonoctal-decimal-escape': 'error',
  664. */
  665. // 禁用八进制字面量
  666. 'no-octal': 'error',
  667. // 禁止在字符串中使用八进制转义序列
  668. 'no-octal-escape': 'error',
  669. // 禁止对 function 的参数进行重新赋值
  670. 'no-param-reassign': 'error',
  671. // 禁用一元操作符 ++ 和 --
  672. 'no-plusplus': 'error',
  673. /*
  674. * 无承诺执行人返回
  675. * 'no-promise-executor-return': 'error',
  676. */
  677. // 禁用 __proto__ 属性
  678. 'no-proto': 'error',
  679. // 禁止多次声明同一变量
  680. 'no-redeclare': 'error',
  681. /*
  682. * 没有限制出口
  683. * 'no-restricted-exports': 'error',
  684. */
  685. /*
  686. * 禁用特定的全局变量
  687. * 'no-restricted-globals': 'error',
  688. */
  689. /*
  690. * 禁止使用指定的 import 加载的模块
  691. * 'no-restricted-imports': 'error',
  692. */
  693. /*
  694. * 禁止在构造函数中,在调用 super() 之前使用 this 或 super
  695. * 'no-this-before-super': 'error',
  696. */
  697. /*
  698. * 禁止使用对象的某些属性
  699. * 'no-restricted-properties': 'error',
  700. */
  701. /*
  702. * 禁用特定的语法
  703. * 'no-restricted-syntax': 'error',
  704. */
  705. // 禁止在 return 语句中使用赋值语句
  706. 'no-return-assign': 'error',
  707. // 禁用不必要的 return await
  708. 'no-return-await': 'error',
  709. // 禁止使用 javascript: url
  710. 'no-script-url': 'error',
  711. // 禁止自我赋值
  712. 'no-self-assign': 'error',
  713. // 禁止自身比较
  714. 'no-self-compare': 'error',
  715. // 禁用逗号操作符
  716. 'no-sequences': 'error',
  717. /*
  718. * 禁止变量声明与外层作用域的变量同名
  719. * 'no-shadow': 'error',
  720. */
  721. /*
  722. * 禁用 tab
  723. * 'no-tabs': 'off',
  724. */
  725. 'no-tabs': 'off',
  726. // 禁止将标识符定义为受限的名字
  727. 'no-shadow-restricted-names': 'error',
  728. /*
  729. * 禁止在常规字符串中出现模板字面量占位符语法
  730. * 'no-template-curly-in-string': 'error',
  731. */
  732. // 禁止出现令人困惑的多行表达式
  733. 'no-unexpected-multiline': 'error',
  734. // 禁止在 return、throw、continue 和 break 语句之后出现不可达代码
  735. 'no-unreachable': 'error',
  736. // 禁止在 finally 语句块中出现控制流语句
  737. 'no-unsafe-finally': 'error',
  738. // 禁止对关系运算符的左操作数使用否定操作符
  739. 'no-unsafe-negation': 'error',
  740. /*
  741. * 禁用三元操作符
  742. * 'no-ternary': 'error',
  743. */
  744. // 禁止抛出异常字面量
  745. 'no-throw-literal': 'error',
  746. // 禁用行尾空格
  747. 'no-trailing-spaces': 'error',
  748. /*
  749. * 禁用未声明的变量,除非它们在 global 注释中被提到
  750. * 'no-undef': 'error',
  751. *
  752. */
  753. // 禁止将变量初始化为 undefined
  754. 'no-undef-init': 'error',
  755. // 禁止将 undefined 作为标识符
  756. 'no-undefined': 'error',
  757. // 禁止出现未使用过的变量
  758. 'no-unused-vars': 'error',
  759. // 禁止标识符中有悬空下划线
  760. 'no-underscore-dangle': 'error',
  761. // 禁用一成不变的循环条件
  762. 'no-unmodified-loop-condition': 'error',
  763. // 禁止可以在有更简单的可替代的表达式时使用三元操作符
  764. 'no-unneeded-ternary': 'error',
  765. /*
  766. * 没有无法到达的循环
  767. * 'no-unreachable-loop': 'error',
  768. */
  769. /*
  770. * 没有不安全的可选链接
  771. * 'no-unsafe-optional-chaining': 'error',
  772. */
  773. // 禁止出现未使用过的表达式
  774. 'no-unused-expressions': 'error',
  775. // 禁用出现未使用过的标
  776. 'no-unused-labels': 'error',
  777. // 禁止在变量定义之前使用它们
  778. 'no-use-before-define': 'error',
  779. /*
  780. * 沒找到 自动生成的 个人理解 没有无用的反向引用
  781. * 'no-useless-backreference': 'error',
  782. */
  783. /*
  784. * 禁止不必要的 .call() 和 .apply()
  785. * 'no-useless-call': 'error',
  786. */
  787. // 禁止不必要的 catch 子句
  788. 'no-useless-catch': 'error',
  789. // 禁止在对象中使用不必要的计算属性
  790. 'no-useless-computed-key': 'error',
  791. /*
  792. * 禁止不必要的字符串字面量或模板字面量的连接
  793. * 'no-useless-concat': 'error',
  794. */
  795. // 禁用不必要的转义字符
  796. 'no-useless-escape': 'error',
  797. // 禁用不必要的构造函数
  798. 'no-useless-constructor': 'error',
  799. // 禁止在 import 和 export 和解构赋值时将引用重命名为相同的名字
  800. 'no-useless-rename': 'error',
  801. // 禁止多余的 return 语句
  802. 'no-useless-return': 'error',
  803. // 要求使用 let 或 const 而不是 var
  804. 'no-var': 'error',
  805. /*
  806. * 禁用 void 操作符
  807. * 'no-void': 'error',
  808. */
  809. /*
  810. * 禁止在注释中使用特定的警告术语
  811. * 'no-warning-comments': 'error',
  812. */
  813. /*
  814. * 禁用 with 语句
  815. * 'no-with': 'error',
  816. */
  817. // 禁止属性前有空白
  818. 'no-whitespace-before-property': 'error',
  819. // 强制单个语句的位置
  820. 'nonblock-statement-body-position': 'error',
  821. // 强制大括号内换行符的一致性
  822. 'object-curly-newline': 'error',
  823. /*
  824. * 强制在大括号中使用一致的空格
  825. * 'object-curly-spacing': [
  826. * 'error',
  827. * 'error'
  828. * ],
  829. */
  830. // 强制将对象的属性放在不同的行上
  831. 'object-property-newline': 'error',
  832. // 要求或禁止对象字面量中方法和属性使用简写语法
  833. 'object-shorthand': 'error',
  834. // 强制函数中的变量要么一起声明要么分开声明
  835. 'one-var': 'off',
  836. // 要求或禁止在变量声明周围换行
  837. 'one-var-declaration-per-line': 'error',
  838. // 要求或禁止在可能的情况下使用简化的赋值操作符
  839. 'operator-assignment': 'error',
  840. // 强制操作符使用一致的换行符
  841. 'operator-linebreak': 'error',
  842. // 要求或禁止块内填充
  843. 'padded-blocks': 'error',
  844. // 要求或禁止在语句间填充空行
  845. 'padding-line-between-statements': 'error',
  846. // 要求回调函数使用箭头函数
  847. 'prefer-arrow-callback': 'error',
  848. // 要求使用 const 声明那些声明后不再被修改的变量
  849. 'prefer-const': 'error',
  850. // 禁止修改 const 声明的变量
  851. 'no-const-assign': 'error',
  852. // 禁止类成员中出现重复的名称
  853. 'no-dupe-class-members': 'error',
  854. // 优先使用数组和对象解构
  855. 'prefer-destructuring': 'error',
  856. /*
  857. * 求幂运算符
  858. * 'prefer-exponentiation-operator': 'error',
  859. */
  860. /*
  861. * 建议在正则表达式中使用命名捕获组
  862. * 'prefer-named-capture-group': 'error',
  863. */
  864. /*
  865. * 禁用 parseInt() 和 Number.parseInt(),使用二进制,八进制和十六进制字面量
  866. * 'prefer-numeric-literals': 'error',
  867. */
  868. // 禁止使用以对象字面量作为第一个参数的 Object.assign,优先使用对象扩展。
  869. 'prefer-object-spread': 'error',
  870. /*
  871. * 要求使用 Error 对象作为 Promise 拒绝的原因
  872. * 'prefer-promise-reject-errors': 'error',
  873. */
  874. /*
  875. * 正则
  876. * 'prefer-regex-literals': 'error',
  877. */
  878. // 要求 generator 函数内有 yield
  879. 'require-yield': 'error',
  880. /*
  881. * 要求使用剩余参数而不是 arguments
  882. * 'prefer-rest-params': 'error',
  883. */
  884. /*
  885. * 要求使用扩展运算符而非 .apply()
  886. * 'prefer-spread': 'error',
  887. */
  888. // 要求使用模板字面量而非字符串连接
  889. 'prefer-template': 'error',
  890. /*
  891. * 要求对象字面量属性名称用引号括起来
  892. * 'quote-props': 'error',
  893. */
  894. // 强制使用一致的反勾号、双引号或单引号
  895. 'quotes': [
  896. 'error',
  897. 'single'
  898. ],
  899. /*
  900. * 强制在 parseInt() 使用基数参数
  901. * 'radix': 'error',
  902. */
  903. // 禁止由于 await 或 yield的使用而可能导致出现竞态条件的赋值
  904. 'require-atomic-updates': 'error',
  905. /*
  906. * 要求 require() 出现在顶层模块作用域中
  907. * 'global-require': 'error',
  908. */
  909. // 要求使用 isNaN() 检查 NaN
  910. 'use-isnan': 'error',
  911. // 强制 typeof 表达式与有效的字符串进行比较
  912. 'valid-typeof': 'error',
  913. /*
  914. * 禁止使用不带 await 表达式的 async 函数
  915. * 'require-await': 'error',
  916. */
  917. /*
  918. * 强制在 RegExp 上使用 u 标志
  919. * 'require-unicode-regexp': 'error',
  920. */
  921. // 强制剩余和扩展运算符及其表达式之间有空格
  922. 'rest-spread-spacing': 'error',
  923. // 要求或禁止使用分号代替 ASI
  924. 'semi': 'off',
  925. /*
  926. * 强制模块内的 import 字母排序
  927. * 'sort-imports': 'error',
  928. */
  929. // 强制分号之前和之后使用一致的空格
  930. 'semi-spacing': 'error',
  931. // 强制分号的位置
  932. 'semi-style': 'error',
  933. /*
  934. * 对象按照字母排序
  935. * 'sort-keys': 'error',
  936. */
  937. // 要求同一个声明块中的变量按顺序排列
  938. 'sort-vars': 'error',
  939. // 强制在块之前使用一致的空格
  940. 'space-before-blocks': 'error',
  941. // 强制在 function的左括号之前使用一致的空格
  942. 'space-before-function-paren': 'error',
  943. // 强制在圆括号内使用一致的空格
  944. 'space-in-parens': [
  945. 'error',
  946. 'never'
  947. ],
  948. // 要求操作符周围有空格
  949. 'space-infix-ops': 'error',
  950. // 强制在一元操作符前后使用一致的空格
  951. 'space-unary-ops': 'error',
  952. // 强制在注释中 // 或 /* 使用一致的空格
  953. 'spaced-comment': 'error',
  954. //* 要求或禁止使用严格模式指令
  955. 'strict': 'off',
  956. // 要求或禁止在模板标记和它们的字面量之间有空格
  957. 'unicode-bom': 'error',
  958. // 强制在 switch 的冒号左右有空格
  959. 'switch-colon-spacing': 'error',
  960. /*
  961. * 要求 symbol 描述
  962. * 'symbol-description': 'error',
  963. */
  964. // 要求或禁止模板字符串中的嵌入表达式周围空格的使用
  965. 'template-curly-spacing': 'error',
  966. // 要求或禁止在模板标记和它们的字面量之间有空格
  967. 'template-tag-spacing': 'error',
  968. /*
  969. * 要求所有的 var 声明出现在它们所在的作用域顶部
  970. * 'vars-on-top': 'error',
  971. */
  972. // 要求 IIFE 使用括号括起来
  973. 'wrap-iife': 'error',
  974. // 要求正则表达式被括号括起来
  975. 'wrap-regex': 'error',
  976. // 强制在 yield* 表达式中 * 周围使用空格
  977. 'yield-star-spacing': 'error'
  978. /*
  979. * 要求或禁止 “Yoda” 条件
  980. * 'yoda': 'error'
  981. */
  982. /*
  983. * 'singleQuote': 'error'
  984. * 使用单引号, 默认false(在jsx中配置无效, 默认都是双引号)
  985. */
  986. }
  987. };
  • 规则级别

"off" or 0 - 关闭规则
"warn" or 1 - 将规则视为一个警告(不会影响退出码)
"error" or 2 - 将规则视为一个错误 (退出码为1)

具体规则介绍

https://eslint.bootcss.com/docs/user-guide/configuring#extending-configuration-files
https://eslint.bootcss.com/docs/rules/object-curly-spacing

配置vscode 自动格式化

setting.json

  1. "editor.codeActionsOnSave": {
  2. "source.fixAll": true,
  3. "source.fixAll.eslint": true,
  4. },
  5. "eslint.format.enable": true,

忽略文件配置 .eslintignore

  1. # 可以通过.eslintignore在项目的根目录中创建一个文件来告诉 ESLint 忽略特定的文件和目录
  2. # *.js 忽略所有的 js文件