.eslintrc.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. console.log(
  2. '\x1B[0;37;44m INFO \x1B[0m',
  3. '\x1B[0;;34m ' +
  4. `${new Date().toLocaleString()}读取了: ${__filename.slice(
  5. __dirname.length + 1
  6. )}` +
  7. ' \x1B[0m'
  8. );
  9. module.exports = {
  10. root: true,
  11. settings: {
  12. 'import/parsers': {
  13. '@typescript-eslint/parser': ['.ts', '.tsx'],
  14. },
  15. 'import/resolver': {
  16. typescript: {
  17. alwaysTryTypes: true, // always try to resolve types under `<root>@types` directory even it doesn't contain any source code, like `@types/unist`
  18. project: './tsconfig.json',
  19. },
  20. },
  21. },
  22. env: {
  23. browser: true,
  24. node: true,
  25. },
  26. extends: [
  27. // 'airbnb-base', // airbnb的eslint规范,它会对import和require进行排序,挺好的。如果不用它的话,需要在env添加node:true
  28. 'eslint:recommended',
  29. 'plugin:import/recommended',
  30. 'plugin:vue/vue3-essential', // plugin:vue/vue3-essential或plugin:vue/vue3-strongly-recommended或plugin:vue/vue3-recommended'
  31. '@vue/eslint-config-typescript', // 启用这个规则后,vscode保存文件时格式化很慢
  32. // '@vue/eslint-config-typescript/recommended', // 启用这个规则后,vscode保存文件时格式化很慢
  33. '@vue/eslint-config-prettier',
  34. ],
  35. parser: 'vue-eslint-parser',
  36. parserOptions: {
  37. parser: '@typescript-eslint/parser',
  38. ecmaVersion: 2022,
  39. tsconfigRootDir: __dirname, // https://typescript-eslint.io/docs/linting/typed-linting
  40. project: ['./tsconfig.json'], // https://typescript-eslint.io/docs/linting/typed-linting
  41. },
  42. plugins: ['import'],
  43. // overrides: [],
  44. // rules会覆盖extends里面的规则(https://eslint.org/docs/latest/user-guide/migrating-to-6.0.0#-overrides-in-an-extended-config-file-can-now-be-overridden-by-a-parent-config-file)
  45. // rules里面的规则不会对overrides里面的文件生效
  46. rules: {
  47. /**
  48. * 0 => off
  49. * 1 => warn
  50. * 2 => error
  51. */
  52. 'no-shadow': 0, // 禁止变量声明与外层作用域的变量同名
  53. 'class-methods-use-this': 0, // 类方法如果不使用this的话会报错
  54. 'no-console': 0, // 此规则不允许调用console对象的方法。
  55. 'spaced-comment': ['error', 'always', { exceptions: ['-', '+'] }], // 该规则强制注释中 // 或 /* 后空格的一致性
  56. 'no-var': 2, // 要求let或const代替var
  57. camelcase: [
  58. 'error',
  59. { properties: 'never' }, // properties默认always,即检查属性名;可以设置为never,即不检查属性名
  60. ], // 强制执行驼峰命名约定
  61. 'no-underscore-dangle': 2, // 此规则不允许在标识符中使用悬空下划线。
  62. 'no-param-reassign': 2, // 禁止对 function 的参数进行重新赋值
  63. 'no-nested-ternary': 2, // 禁止嵌套三元
  64. 'no-plusplus': 2, // 禁用一元操作符 ++ 和 --
  65. 'no-unused-vars': 0, // 禁止出现未使用过的变量
  66. 'vars-on-top': 2, // 要求所有的 var 声明出现在它们所在的作用域顶部
  67. 'prefer-const': 2, // 要求使用 const 声明那些声明后不再被修改的变量
  68. 'prefer-template': 2, // 要求使用模板字符串代替字符串连接
  69. 'new-cap': 2, // 要求构造函数名称以大写字母开头
  70. 'no-restricted-syntax': [
  71. // 禁用一些语法
  72. 'error',
  73. // 'ForInStatement',
  74. // 'ForOfStatement',
  75. {
  76. selector: 'ForInStatement',
  77. /**
  78. * 用 map() / every() / filter() / find() / findIndex() / reduce() / some() / ... 遍历数组,
  79. * 和使用 Object.keys() / Object.values() / Object.entries() 迭代你的对象生成数组。
  80. * 拥有返回值得纯函数比这个更容易解释
  81. */
  82. message:
  83. 'for in会迭代遍历原型链(__proto__),建议使用map/every/filter等遍历数组,使用Object.{keys,values,entries}等遍历对象',
  84. },
  85. {
  86. selector: 'ForOfStatement',
  87. message:
  88. '建议使用map/every/filter等遍历数组,使用Object.{keys,values,entries}等遍历对象',
  89. },
  90. ], // https://github.com/BingKui/javascript-zh#%E8%BF%AD%E4%BB%A3%E5%99%A8%E5%92%8C%E5%8F%91%E7%94%9F%E5%99%A8
  91. 'no-iterator': 2, // 禁止使用__iterator__迭代器
  92. 'require-await': 2, // 禁止使用不带 await 表达式的 async 函数
  93. 'no-empty': 2, // 禁止空块语句
  94. 'guard-for-in': 2, // 要求for-in循环包含if语句
  95. 'global-require': 2, // 此规则要求所有调用require()都在模块的顶层,此规则在 ESLint v7.0.0中已弃用。请使用 中的相应规则eslint-plugin-node:https://github.com/mysticatea/eslint-plugin-node
  96. 'no-unused-expressions': [
  97. 2,
  98. {
  99. allowShortCircuit: true, // 允许短路
  100. allowTernary: true, // 允许三元
  101. },
  102. ], // 禁止未使用的表达式,即let a = true && console.log(1)允许,但是true && console.log(1)不行
  103. 'object-shorthand': ['error', 'always'], // (默认)希望尽可能使用速记。var foo = {x:x};替换为var foo = {x};
  104. 'no-useless-escape': 2, // 禁止不必要的转义字符
  105. // eslint-plugin-import插件
  106. // https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/order.md
  107. 'import/order': [
  108. 'error',
  109. {
  110. groups: [
  111. 'builtin', // 如:import fs from 'fs';
  112. 'external', // 如:import _ from 'lodash';
  113. 'internal', // 如:import foo from 'src/foo';
  114. 'parent', // 如:import foo from '../foo';
  115. 'sibling', // 如:import bar from './bar';
  116. // ['sibling', 'parent'],
  117. // ['parent', 'sibling'],
  118. 'index', // 如:import main from './';
  119. 'object', // 如:import log = console.log;
  120. 'type', // 如:import type { Foo } from 'foo';
  121. ],
  122. pathGroups: [
  123. {
  124. pattern: '@/**',
  125. group: 'internal',
  126. },
  127. ],
  128. 'newlines-between': 'always', // 强制或禁止导入组之间的新行
  129. // 根据导入路径以字母顺序排列每个组中的顺序
  130. alphabetize: {
  131. order: 'asc', // 使用asc按升序排序,使用desc按降序排序(默认值:ignore)。
  132. caseInsensitive: true, // 使用true忽略大小写,而false考虑大小写(默认值:false)。
  133. orderImportKind: 'asc', // 使用asc以升序对各种导入类型进行排序,例如以type或typeof为前缀的导入,具有相同的导入路径。使用desc按降序排序(默认值:忽略)
  134. },
  135. },
  136. ],
  137. 'import/newline-after-import': 2, // 强制在最后一个顶级导入语句或 require 调用之后有一个或多个空行
  138. 'import/no-extraneous-dependencies': 2, // 禁止导入未在package.json中声明的外部模块。
  139. /**
  140. * a.js
  141. * export const version = '1.0.0';
  142. * export const bar = { name: 'bar', version };
  143. * export default bar;
  144. * b.js
  145. * import bar from './a';
  146. * console.log(bar.version); // 检测到你使用的version有具名导出,import/no-named-as-default-member就会提示`import {version} from './a'`
  147. */
  148. 'import/no-named-as-default-member': 1, // https://github.com/import-js/eslint-plugin-import/blob/v2.26.0/docs/rules/no-named-as-default-member.md
  149. /**
  150. * import/named
  151. * 在import { version } from 'vuex';的时候会验证vuex有没有具名导出version,
  152. * 但是在vue3的时候,import { defineComponent } from 'vue';会报错defineComponent not found in 'vue'
  153. * 因此vue3项目关闭该规则
  154. */
  155. // 'import/named': 0,
  156. // 'import/prefer-default-export': 0, // 当模块只有一个导出时,更喜欢使用默认导出而不是命名导出。
  157. // 'import/extensions': 0, // 确保在导入路径中一致使用文件扩展名。在js/ts等文件里引其他文件都不能带后缀(比如.css和.jpg),因此关掉
  158. // 'import/no-unresolved': 0, // 不能解析带别名的路径的模块,但实际上是不影响代码运行的。找不到解决办法,暂时关掉。
  159. /**
  160. * a.js
  161. * export const bar = 'bar';
  162. * export const foo = 'foo';
  163. * export default foo;
  164. * b.js
  165. * import bar from './a'; // import/no-named-as-default规则会报错,因为import/no-named-as-default规则误以为你将具名导出的bar作为了默认导出来使用,但是实际上可能我就是想用默认导出的foo
  166. * // import barr from './a'; // 改个名字import/no-named-as-default规则就不会报错了。
  167. * // 不幸的是,React + Redux 是最常见的场景。但是,还有很多其他情况,HOC 会迫使开发人员关闭此规则。https://github.com/import-js/eslint-plugin-import/issues/544#issuecomment-245082471
  168. */
  169. 'import/no-named-as-default': 0, // https://github.com/import-js/eslint-plugin-import/blob/v2.26.0/docs/rules/no-named-as-default.md
  170. // @typescript-eslint插件
  171. '@typescript-eslint/no-unused-vars': 2,
  172. '@typescript-eslint/restrict-template-expressions': [
  173. 'error',
  174. {
  175. allowBoolean: true,
  176. allowNumber: true,
  177. },
  178. ], // 强制模板文字表达式为string类型。即const a = {};console.log(`${a}`);会报错
  179. '@typescript-eslint/no-floating-promises': 0, // 要求适当处理类似 Promise 的语句。即将await或者return Promise,或者对promise进行.then或者.catch
  180. '@typescript-eslint/no-explicit-any': 0, // 不允许定义any类型。即let a: any;会报错
  181. '@typescript-eslint/no-non-null-assertion': 0, // 禁止使用非空断言(后缀运算符!)。即const el = document.querySelector('.app');console.log(el!.tagName);会报错
  182. '@typescript-eslint/ban-ts-comment': 0, // 禁止使用@ts-<directive>注释
  183. '@typescript-eslint/no-unsafe-assignment': 0, // 不允许将具有类型的值分配any给变量和属性。即const a: any = {};const b = a;会报错
  184. '@typescript-eslint/no-unsafe-argument': 0, // 不允许用any类型的值调用一个函数。即let a: any;Object.keys(a);会报错
  185. '@typescript-eslint/no-unsafe-member-access': 0, // 不允许对类型为any的值进行成员访问。即const a: any = [];console.log(a[0]);会报错
  186. '@typescript-eslint/no-unsafe-return': 0, // 不允许从一个函数中返回一个类型为any的值
  187. '@typescript-eslint/no-unsafe-call': 0, // 不允许调用any类型的值
  188. '@typescript-eslint/no-var-requires': 0, // 即不允许var foo = require('foo');。但是允许import foo = require('foo');
  189. '@typescript-eslint/restrict-plus-operands': 0, // 要求加法的两个操作数是相同的类型并且是bigint, number, 或string。即const a = '1';console.log(a + 1);会报错
  190. // vueeslint插件
  191. 'vue/multi-word-component-names': 0,
  192. },
  193. };