UNPKG

eslint-config-nstarter

Version:
695 lines (694 loc) 22.1 kB
/** * nstarter ESLint 规则 * https://jiandaoyun.github.io/nstarter-eslint-config/ * * 依赖版本: * eslint ^8.45.0 * eslint-plugin-import ^2.29.1 * @babel/core ^7.22.9 * @babel/eslint-parser ^7.22.9 * @typescript-eslint/parser ^7.18.0 * @typescript-eslint/eslint-plugin ^7.18.0 * * 此文件是由脚本 scripts/build.ts 自动生成 */ module.exports = { parser: '@typescript-eslint/parser', plugins: ['@typescript-eslint'], parserOptions: { project: './tsconfig.json', }, rules: { /** * 重载的函数必须写在一起 * @reason 增加可读性 */ '@typescript-eslint/adjacent-overload-signatures': 'error', /** * 限制数组类型必须使用 Array<T> 或 T[] * @reason 允许灵活运用两者 */ '@typescript-eslint/array-type': 'off', /** * 禁止对没有 then 方法的对象使用 await */ '@typescript-eslint/await-thenable': 'off', /** * 禁止使用 // @ts-ignore // @ts-nocheck // @ts-check * @reason 这种注释本身就是对特殊代码的说明 */ '@typescript-eslint/ban-ts-comment': 'off', /** * 是否允许使用 // @ts-ignore 来忽略编译错误 * @reason 既然已经使用注释来忽略编译错误了,说明已经清楚可能带来的后果 */ '@typescript-eslint/ban-ts-ignore': 'off', /** * 禁止使用类似 tslint:disable-next-line 这样的注释 */ '@typescript-eslint/ban-tslint-comment': 'off', /** * 禁止使用指定的类型 * 不使用 Object, String, Number, Boolean 类型,而使用原生的 ts 类型,允许使用 Function 用于装饰器包装 * @reason 统一代码风格 */ '@typescript-eslint/ban-types': [ 'error', { types: { '{}': false, Function: false, Object: { message: "Use '{}' instead", fixWith: '{}', }, String: { message: "Use 'string' instead", fixWith: 'string', }, Number: { message: "Use 'number' instead", fixWith: 'number', }, Boolean: { message: "Use 'boolean' instead", fixWith: 'boolean', }, }, }, ], /** * 变量名必须是 camelcase 风格的 * @reason 很多 api 或文件名都不是 camelcase 风格的 */ '@typescript-eslint/camelcase': 'off', /** * 类的只读属性若是一个字面量,则必须使用只读属性而不是 getter */ '@typescript-eslint/class-literal-property-style': ['error', 'fields'], /** * 在类的非静态方法中,必须存在对 this 的引用 */ 'class-methods-use-this': 'off', '@typescript-eslint/class-methods-use-this': 'off', /** * 使用 Map 或 Set 时,必须在构造函数上用泛型定义类型 */ '@typescript-eslint/consistent-generic-constructors': 'off', /** * 必须使用内置的 Record<K, T> 来描述仅包含可索引成员的接口 */ '@typescript-eslint/consistent-indexed-object-style': 'off', /** * 类型断言必须使用 as Type,禁止使用 <Type>,禁止对对象字面量进行类型断言(断言成 any 是允许的) * @reason <Type> 容易被理解为 jsx */ '@typescript-eslint/consistent-type-assertions': [ 'error', { assertionStyle: 'as', objectLiteralTypeAssertions: 'never', }, ], /** * 优先使用 interface 而不是 type * @reason interface 可以 implement, extend 和 merge */ '@typescript-eslint/consistent-type-definitions': ['error', 'interface'], /** * 一致的类型导出语法 */ '@typescript-eslint/consistent-type-exports': 'off', /** * 必须使用 import type 导入类型 */ '@typescript-eslint/consistent-type-imports': 'error', /** * 有默认值或可选的参数必须放到最后 */ 'default-param-last': 'off', '@typescript-eslint/default-param-last': 'off', /** * 禁止使用 foo['bar'],必须写成 foo.bar * @reason 当需要写一系列属性的时候,可以更统一 */ 'dot-notation': 'off', '@typescript-eslint/dot-notation': 'off', /** * 函数返回值必须与声明的类型一致 * @reason 编译阶段检查就足够了 */ '@typescript-eslint/explicit-function-return-type': 'off', /** * 必须设置类的成员的可访问性 * 特殊规则,对于 constructor,不要求指定其 public 状态。 * @reason 将不需要公开的成员设为私有的,可以增强代码的可理解性,对文档输出也很友好 */ '@typescript-eslint/explicit-member-accessibility': [ 'error', { accessibility: 'explicit', overrides: { constructors: 'no-public', }, }, ], /** * 导出的函数或类中的 public 方法必须定义输入输出参数的类型 */ '@typescript-eslint/explicit-module-boundary-types': 'off', /** * 约束泛型的命名规则 */ '@typescript-eslint/generic-type-naming': 'off', /** * 缩进规定为 4 空格 * @reason 已被 javascript 规则覆盖,无需重复指定。 */ indent: 'off', '@typescript-eslint/indent': 'off', /** * 变量必须在定义的时候赋值 */ 'init-declarations': 'off', '@typescript-eslint/init-declarations': 'off', /** * 接口名称必须以 I 开头 */ '@typescript-eslint/interface-name-prefix': 'off', /** * 类的成员之间是否需要空行 * @reason 有时为了紧凑需要挨在一起,有时为了可读性需要空一行 */ 'lines-between-class-members': 'off', '@typescript-eslint/lines-between-class-members': 'off', /** * 统一成员属性的分隔符形式 * 对于对象或 Interface 声明,使用 ;。对于 type 声明使用 ,。 * @reason 统一代码风格 */ '@typescript-eslint/member-delimiter-style': [ 'error', { multiline: { delimiter: 'semi', requireLast: true, }, singleline: { delimiter: 'semi', requireLast: false, }, overrides: { typeLiteral: { multiline: { delimiter: 'comma', requireLast: false, }, singleline: { delimiter: 'comma', requireLast: false, }, }, }, }, ], /** * 私有成员必须以 _ 开头 * @reason 已有 private 修饰符了,没必要限制变量名 */ '@typescript-eslint/member-naming': 'off', /** * 成员属性定义过程的指导性顺序约束 * 不做强制性要求 * @reason 优先级: * 1. static > instance * 2. field > constructor > method * 3. public > protected > private */ '@typescript-eslint/member-ordering': [ 'warn', { default: [ 'static-field', 'instance-field', 'public-constructor', 'protected-constructor', 'private-constructor', 'private-static-method', 'protected-static-method', 'public-static-method', ], }, ], /** * 接口中的方法必须用属性的方式定义 * @reason 配置了 strictFunctionTypes 之后,用属性的方式定义方法可以获得更严格的检查 */ '@typescript-eslint/method-signature-style': 'error', /** * 限制各种变量或类型的命名规则 */ '@typescript-eslint/naming-convention': [ 'error', { selector: 'class', format: ['PascalCase'], }, ], /** * 禁止使用 Array 构造函数 */ 'no-array-constructor': 'off', '@typescript-eslint/no-array-constructor': 'off', /** * 禁止滥用 toString 方法 */ '@typescript-eslint/no-base-to-string': 'off', /** * 禁止使用容易混淆的非空断言 */ '@typescript-eslint/no-confusing-non-null-assertion': 'off', /** * 禁止使用返回值为 void 的函数的返回值 */ '@typescript-eslint/no-confusing-void-expression': 'off', /** * 禁止重复定义类的成员 * @reason 编译阶段就会报错了 */ 'no-dupe-class-members': 'off', '@typescript-eslint/no-dupe-class-members': 'off', /** * 禁止枚举类型存在两个相同的值 */ '@typescript-eslint/no-duplicate-enum-values': 'error', /** * 不允许枚举同时具有数字和字符串成员 */ '@typescript-eslint/no-duplicate-type-constituents': 'off', /** * 禁止 delete 时传入的 key 是动态的 */ '@typescript-eslint/no-dynamic-delete': 'off', /** * 不允许有空函数 * @reason 有时需要将一个空函数设置为某个项的默认值 */ 'no-empty-function': 'off', '@typescript-eslint/no-empty-function': 'off', /** * 禁止定义空的接口 * @reason 允许定义空的接口作为基础类型声明 */ '@typescript-eslint/no-empty-interface': 'off', /** * 禁止使用 any */ '@typescript-eslint/no-explicit-any': 'off', /** * 禁止多余的 non-null 断言 */ '@typescript-eslint/no-extra-non-null-assertion': 'off', /** * 禁止定义没必要的类,比如只有静态方法的类 */ '@typescript-eslint/no-extraneous-class': 'off', /** * 禁止调用 Promise 时没有处理异常情况 */ '@typescript-eslint/no-floating-promises': 'off', /** * 禁止对 array 使用 for in 循环 */ '@typescript-eslint/no-for-in-array': 'off', /** * 禁止使用 eval */ 'no-implied-eval': 'off', '@typescript-eslint/no-implied-eval': 'off', /** * 强制要求在只有内联类型限定符的情况下使用顶级导入类型限定符 */ '@typescript-eslint/no-import-type-side-effects': 'error', /** * 禁止给一个初始化时直接赋值为 number, string 的变量显式的声明类型 * @reason 可以简化代码 */ '@typescript-eslint/no-inferrable-types': 'error', /** * 禁止在类之外的地方使用 this */ 'no-invalid-this': 'off', '@typescript-eslint/no-invalid-this': 'error', /** * 禁止使用无意义的 void 类型 * @reason void 只能用在函数的返回值中 */ '@typescript-eslint/no-invalid-void-type': 'error', /** * 禁止在循环内的函数内部出现循环体条件语句中定义的变量 * @reason 使用 let 就已经解决了这个问题了 */ 'no-loop-func': 'off', '@typescript-eslint/no-loop-func': 'off', /** * 禁止使用超出 js 精度范围的数字 */ 'no-loss-of-precision': 'off', '@typescript-eslint/no-loss-of-precision': 'error', /** * 禁止使用 magic numbers */ 'no-magic-numbers': 'off', '@typescript-eslint/no-magic-numbers': 'off', /** * 禁止 void 抛出空 */ '@typescript-eslint/no-meaningless-void-operator': 'off', /** * 禁止在接口中定义 constructor,或在类中定义 new */ '@typescript-eslint/no-misused-new': 'off', /** * 避免错误的使用 Promise */ '@typescript-eslint/no-misused-promises': 'off', /** * 不允许枚举同时具有数字和字符串成员 */ '@typescript-eslint/no-mixed-enums': 'off', /** * 禁止使用 namespace 来定义命名空间 * @reason 使用 es6 引入模块,才是更标准的方式。 * 但是允许使用 declare namespace ... {} 来定义外部命名空间 */ '@typescript-eslint/no-namespace': [ 'error', { allowDeclarations: true, allowDefinitionFiles: true, }, ], /** * 禁止非空断言后面跟着双问号 */ '@typescript-eslint/no-non-null-asserted-nullish-coalescing': 'error', /** * 禁止在 optional chaining 之后使用 non-null 断言(感叹号) * @reason optional chaining 后面的属性一定是非空的 */ '@typescript-eslint/no-non-null-asserted-optional-chain': 'error', /** * 禁止使用 non-null 断言(感叹号) * @reason 使用 non-null 断言时就已经清楚了风险 */ '@typescript-eslint/no-non-null-assertion': 'off', /** * 禁止重复定义变量 * @reason 禁用 var 之后,编译阶段就会报错了 */ 'no-redeclare': 'off', '@typescript-eslint/no-redeclare': 'off', /** * 禁止无用的联合类型或交叉类型 */ '@typescript-eslint/no-redundant-type-constituents': 'off', /** * 禁止使用 require * 原则上禁止动态引用,对于循环依赖,通过 IOC 的方式解决。 * @reason 统一使用 import 来引入模块,特殊情况使用单行注释允许 require 引入 */ '@typescript-eslint/no-require-imports': 'error', /** * 禁止导入指定的模块 */ 'no-restricted-imports': 'off', '@typescript-eslint/no-restricted-imports': 'off', /** * 禁止变量名与上层作用域内的已定义的变量重复 * @reason 很多时候函数的形参和传参是同名的 */ 'no-shadow': 'off', '@typescript-eslint/no-shadow': 'off', /** * 禁止将 this 赋值给其他变量,除非是解构赋值 */ '@typescript-eslint/no-this-alias': [ 'error', { allowDestructuring: true, }, ], /** * 禁止 throw 字面量,必须 throw 一个 Error 对象 */ 'no-throw-literal': 'off', '@typescript-eslint/no-throw-literal': 'off', /** * 测试表达式中的布尔类型禁止与 true 或 false 直接比较 */ '@typescript-eslint/no-unnecessary-boolean-literal-compare': 'off', /** * 条件表达式禁止是永远为真(或永远为假)的 */ '@typescript-eslint/no-unnecessary-condition': 'off', /** * 在命名空间中,可以直接使用内部变量,不需要添加命名空间前缀 */ '@typescript-eslint/no-unnecessary-qualifier': 'off', /** * 禁止范型的类型有默认值时,将范型设置为该默认值 */ '@typescript-eslint/no-unnecessary-type-arguments': 'off', /** * 禁止无用的类型断言 */ '@typescript-eslint/no-unnecessary-type-assertion': 'off', /** * 禁止没用的类型限制 */ '@typescript-eslint/no-unnecessary-type-constraint': 'error', /** * 禁止将 any 类型的变量作为函数参数调用 */ '@typescript-eslint/no-unsafe-argument': 'off', /** * 禁止将变量或属性的类型设置为 any */ '@typescript-eslint/no-unsafe-assignment': 'off', /** * 禁止调用 any 类型的变量上的方法 */ '@typescript-eslint/no-unsafe-call': 'off', /** * 禁止 class 和 interface 合并类型 */ '@typescript-eslint/no-unsafe-declaration-merging': 'off', /** * 禁止将枚举值与非枚举值进行比较 */ '@typescript-eslint/no-unsafe-enum-comparison': 'off', /** * 禁止获取 any 类型的变量中的属性 */ '@typescript-eslint/no-unsafe-member-access': 'off', /** * 禁止函数的返回值的类型是 any */ '@typescript-eslint/no-unsafe-return': 'off', /** * 禁止无用的表达式 */ 'no-unused-expressions': 'off', '@typescript-eslint/no-unused-expressions': [ 'error', { allowShortCircuit: true, allowTernary: true, allowTaggedTemplates: true, }, ], /** * 已定义的变量必须使用 * @reason 编译阶段检查就足够了 */ 'no-unused-vars': 'off', '@typescript-eslint/no-unused-vars': 'off', /** * 禁止在定义变量之前就使用它 * @reason 编译阶段检查就足够了 */ 'no-use-before-define': 'off', '@typescript-eslint/no-use-before-define': 'off', /** * 禁止出现没必要的 constructor */ 'no-useless-constructor': 'off', '@typescript-eslint/no-useless-constructor': 'error', /** * 禁止导出空对象 */ '@typescript-eslint/no-useless-empty-export': 'off', /** * 禁止使用 require 来引入模块 * @reason no-require-imports 规则已经约束了 require */ '@typescript-eslint/no-var-requires': 'off', /** * 必须使用 ! 而不是 as */ '@typescript-eslint/non-nullable-type-assertion-style': 'off', /** * 限制语句之间的空行规则,比如变量定义完之后必须要空行 */ 'padding-line-between-statements': 'off', '@typescript-eslint/padding-line-between-statements': 'off', /** * 类的构造函数参数作为类属性时,必须加上可访问性修饰符 */ '@typescript-eslint/parameter-properties': 'off', /** * 使用 as const 替代 as 'bar' * @reason as const 是新语法,不是很常见 */ '@typescript-eslint/prefer-as-const': 'off', /** * 枚举值必须初始化 */ '@typescript-eslint/prefer-enum-initializers': 'off', /** * 使用 for 循环遍历数组时,如果索引仅用于获取成员,则必须使用 for of 循环替代 for 循环 * @reason for of 循环更加易读 */ '@typescript-eslint/prefer-for-of': 'error', /** * 可以简写为函数类型的接口或字面类型的话,则必须简写 * @reason 不要求函数类型接口的简写,因为可读性并不好。 */ '@typescript-eslint/prefer-function-type': 'off', /** * 使用 includes 而不是 indexOf */ '@typescript-eslint/prefer-includes': 'off', /** * 枚举类型的值必须是字面量,禁止是计算值 * @reason 编译阶段检查就足够了 */ '@typescript-eslint/prefer-literal-enum-member': 'off', /** * 禁止使用 module 来定义命名空间 * @reason module 已成为 js 的关键字 */ '@typescript-eslint/prefer-namespace-keyword': 'error', /** * 使用 ?? 替代 || */ '@typescript-eslint/prefer-nullish-coalescing': 'off', /** * 使用 optional chaining 替代 && */ '@typescript-eslint/prefer-optional-chain': 'off', /** * 私有变量如果没有在构造函数外被赋值,则必须设为 readonly */ '@typescript-eslint/prefer-readonly': 'off', /** * 函数的参数必须设置为 readonly */ '@typescript-eslint/prefer-readonly-parameter-types': 'off', /** * 使用 reduce 方法时,必须传入范型,而不是对第二个参数使用 as */ '@typescript-eslint/prefer-reduce-type-parameter': 'off', /** * 使用 RegExp#exec 而不是 String#match */ '@typescript-eslint/prefer-regexp-exec': 'off', /** * 类的方法返回值是 this 时,类型必须设置为 this */ '@typescript-eslint/prefer-return-this-type': 'off', /** * 使用 String#startsWith 而不是其他方式 */ '@typescript-eslint/prefer-string-starts-ends-with': 'off', /** * 当需要忽略下一行的 ts 错误时,必须使用 @ts-expect-error 而不是 @ts-ignore * @reason 使用 @ts-expect-error 可以避免对不会报错的代码设置了 @ts-ignore */ '@typescript-eslint/prefer-ts-expect-error': 'off', /** * async 函数的返回值必须是 Promise */ '@typescript-eslint/promise-function-async': 'off', /** * 使用 sort 时必须传入比较函数 */ '@typescript-eslint/require-array-sort-compare': 'off', /** * async 函数中必须存在 await 语句 */ 'require-await': 'off', '@typescript-eslint/require-await': 'off', /** * 使用加号时,两者必须同为数字或同为字符串 */ '@typescript-eslint/restrict-plus-operands': 'off', /** * 模版字符串中的变量类型必须是字符串 */ '@typescript-eslint/restrict-template-expressions': 'off', /** * 建议使用 return await promise,利于调试与问题排查 */ 'no-return-await': 'off', '@typescript-eslint/return-await': ['warn', 'always'], /** * 联合类型和交叉类型必须排序 */ '@typescript-eslint/sort-type-constituents': 'off', /** * 条件判断必须传入布尔值 */ '@typescript-eslint/strict-boolean-expressions': 'off', /** * 使用联合类型作为 switch 的对象时,必须包含每一个类型的 case */ '@typescript-eslint/switch-exhaustiveness-check': 'off', /** * 禁止使用三斜杠导入文件 * @reason 三斜杠是已废弃的语法,但在类型声明文件中还是可以使用的 */ '@typescript-eslint/triple-slash-reference': [ 'error', { path: 'never', types: 'always', lib: 'always', }, ], /** * interface 和 type 定义时必须声明成员的类型 */ '@typescript-eslint/typedef': [ 'error', { arrayDestructuring: false, arrowParameter: false, memberVariableDeclaration: false, objectDestructuring: false, parameter: false, propertyDeclaration: true, variableDeclaration: false, }, ], /** * 方法调用时需要绑定到正确的 this 上 */ '@typescript-eslint/unbound-method': 'off', /** * 函数重载时,若能通过联合类型将两个函数的类型声明合为一个,则使用联合类型而不是两个函数声明 */ '@typescript-eslint/unified-signatures': 'error', }, };