@coderrob/eslint-plugin-零容忍
无魔法数 允许在命名常数中进行算术,例如 const SOMETHING = 10 * 100;。 。 。 .
行为安全合同 记录正负输入、参数边界、状态隔离和需要人工迁移的情况.
规则套房需要非空有效而无效的固定装置,命名为 should 病例、明确的诊断和混凝土自动固定输出。 所有83个规则也都与共享的TypeScript,JSX,可选访问,以及捷克链剖析固定装置相对抗. 见 贡献指南 用于验证要求.
严格,有意见的ESLint插件用于TypeScript,执行类型安全,代码质量,测试标准,以及可维护的模式——除零例外.
特征
- 83个定制ESLint规则 包括8个类别:类型安全、代码质量、测试、进口、错误处理、防虫、命名公约和文件
- 两个预设 —— 说
recommended通常采用警告严重性来逐步收养,同时strict一般使用出错严重性;以下列出严格例外和选入例外 - ESLint 8.57+, 9.x,和 10.x ——平面布局和遗产
.eslintrc两者均获得支持 - 99xx测试覆盖率 ——每条规则都通过有效案件和无效案件进行彻底检验
- 与
@typescript-eslint/utils用于完整类型脚本 AST 支持
所需资源
- 节点.js
- 18.1.8.0+ 当使用ESLint 8.5.7.0+或9.x时
- 20.19.0+, 22.13.0+,或 24.0.0+ 当使用ESLint 10.x时
- 经测试的LTS释放:18.20.8、20.20.2、22.23.4、24.22.2和26.13.0
- ESLint 8.57.0+, 9.x, 或 10.x
- CI测试的主要释放:8.57.1、9.39.5和10.14.0
@typescript-eslint/parser8.x 电话- 类型脚本 5.x
安装
npm install --save-dev @coderrob/eslint-plugin-zero-tolerance @typescript-eslint/parser
快速启动
ESLint 9+ (Flat Config) (中文(简体) )
// eslint.config.js
import zeroTolerance from '@coderrob/eslint-plugin-zero-tolerance';
export default [zeroTolerance.configs.recommended];
ESLint 8.x (法语:Legacy Config)
// .eslintrc.js
module.exports = {
plugins: ['@coderrob/zero-tolerance'],
extends: ['plugin:@coderrob/zero-tolerance/legacy-recommended'],
};
遗留 ESLint 通过 @coderrob/zero-tolerance 简称,所以
手动选择遗留规则 ID 使用 @coderrob/zero-tolerance/<rule-name>。平面配置继续
用于使用 zero-tolerance 以下显示名称空间 .
自定义规则选择
// eslint.config.js
import zeroTolerance from '@coderrob/eslint-plugin-zero-tolerance';
export default [
{
plugins: {
'zero-tolerance': zeroTolerance,
},
rules: {
'zero-tolerance/require-interface-prefix': 'error',
'zero-tolerance/no-throw-literal': 'error',
'zero-tolerance/max-function-lines': ['warn', { max: 40 }],
},
},
];
预设
| 预设 | 严重性 | 配置密钥 |
|---|---|---|
| 建议 | warn | configs.recommended |
| 严格 | error | configs.strict |
| 建议的遗产 | warn | legacy-recommended |
| 严格遗产 | error | legacy-strict |
预设断裂描述启用规则的默认值 . prefer-result-return 严格,而 require-bdd-spec, (中文(简体) ), require-jsdoc-anonymous-functions, (中文(简体) ), no-fetch-in-tests, (中文(简体) ), no-parent-internal-access,以及 no-restricted-imports-in-tests 继续选择两个预设。 见 完整规则文档 每个规则的预设状态.
规则
命名公约
| 规则 | 说明 |
|---|---|
require-interface-prefix | 执行 TypeScript 界面名称的起始 I 然后是大写字母 |
文档
| 规则 | 说明 |
|---|---|
require-bdd-spec | 强制执行每个 TypeScript 源文件都有有效的兄弟姐妹.ts.bdd.json BDD spec |
require-jsdoc-anonymous-functions | 要求 JSDoc 除了测试文件和已知的测试回调之外, 对匿名函数类构造的评论 |
require-jsdoc-functions | 要求 JSDoc 对命名函数的评论(测试文件除外) |
require-optional-chaining | 报告重复的逻辑保护访问,供手工审查 |
require-readonly-props | 要求 JSX 组件道具作为只读键输入 |
测试
| 规则 | 说明 |
|---|---|
require-test-description-style | 执行测试描述从 should |
no-chai-imprecise-spy-assertions | 需要精确的柴式间谍呼叫数和引用论据 |
no-conditional-test-assertion | 在测试分支中否定无法执行的断言 |
no-jest-have-been-called | 在 Jest 断言中要求精确的呼叫数和引用指数;允许否定的零呼叫检查 |
no-mock-implementation | 禁止持久性模拟方法;使用 Once 防止测试出血的变体 |
no-sinon-imprecise-spy-assertions | 需要准确的Sinon间谍通话数和引用论据 |
no-set-timeout-in-tests | 否定 setTimeout 测试文件中的使用 |
no-set-interval-in-tests | 否定 setInterval 测试文件中的使用 |
no-fetch-in-tests | 否定 fetch 测试文件中的使用 |
no-restricted-imports-in-tests | 取消测试文件中配置的依赖性导入 |
no-test-interface-declaration | 在测试文件中取消接口声明; 改为导入生产类型 |
no-unawaited-async-assertion | 要求同步测试断言等待或返回 |
no-vitest-imprecise-spy-assertions | 需要精确的Vitest间谍电话数和引用指数 |
类型安全
| 规则 | 说明 |
|---|---|
no-type-assertion | 防止使用类型脚本 as 和角-括号断言 |
no-non-null-assertion | 使用 ! 后缀运算符 |
no-explicit-any | 明确否定 any;需要精确的建模或明确缩小范围 |
no-indexed-access-types | 删除类型Script索引访问类型 |
no-literal-unions | 报告人工迁移到矿井的文字结合 |
no-literal-property-unions | 要求属性字面结合使用命名域类型 |
require-union-type-alias | 需要包含多个类型引用的内置联盟类型以使用命名类型别名 |
no-inline-type-import | 淡出内线 import("...").Type 说明 |
no-return-type | 丢弃类型脚本 ReturnType 公用事业使用情况 |
no-destructured-parameter-type-literal | 解结构参数上的内线对象类型 |
require-exported-object-type | 要求导出对象常数声明明确类型的注释 |
no-unsafe-json-parse | 拒绝将 JSON.parse 结果作为未验证而输入的数据 |
代码质量
| 规则 | 说明 |
|---|---|
max-function-lines | 每个函数体执行最大行数 |
max-params | 执行最大函数参数 |
no-array-mutation | 取消变异数组方法 |
no-date-now | 否定 Date.now() 和无鼠标 new Date() 使用量 |
no-placeholder-implementation | 取消占位符、分贝、TODO和未执行的生产代码 |
no-magic-numbers | 取消魔法数; 使用命名常数代替 |
no-magic-strings | 在比较和切换时取消魔法字符串 |
no-map-set-mutation | 拒绝直接 Map 财务报告和审定财务报表 Set 变异方法 |
no-object-mutation | 直接对象属性变异 |
sort-imports | 在保存运行时依赖命令时报告导入命令 |
sort-functions | 需要按字母排序顶级函数和康斯特函数表达式 |
prefer-nullish-coalescing | 偏好为本地绑定的标识标记三角形进行无效组合 |
prefer-object-spread | 报告空目标对象。 在使用对象扩展前, 指定人工审查的调用 |
prefer-readonly-parameters | 对象和类似数组参数优先只读打入 |
prefer-string-raw | 首选本土 String.raw 当建造者没有被阴影的时候 |
prefer-structured-clone | 在考虑结构化Clone(.)之前审查 JSON.parse(JSON.stringify (.)) |
处理错误
| 规则 | 说明 |
|---|---|
no-empty-catch | 取消空捕捉块, 静静地吞下错误 |
no-throw-literal | 拒绝丢弃文字、对象或模板; 总是丢出新的 Error 实例 |
prefer-result-return | 偏好返回结果样式的值而不是抛出 |
进口
no-parent-internal-access 仍然选择使用,并且只匹配在 .. 横行,例如 ../src/foo。 。 。 .
| 规则 | 说明 |
|---|---|
require-clean-barrel | 需要枪管文件( E)index.*)只包含模块再导出 |
require-barrel-relative-exports | 要求再出口桶使用 ./ 后人路径 |
no-barrel-parent-imports | 在枪管文件导入路径中取消父目录转录 |
no-parent-internal-access | 不允许父目录和亲目录连接到受保护的内部目录, 如 src |
no-dynamic-import | 禁用动态 import() 财务报告和审定财务报表 require() 外部测试文件 |
no-export-alias | 防止在出口报表中使用别名 |
no-hardcoded-secrets | 否认硬码密件、证书、信使和密件默认 |
no-raw-sql-interpolation | 拒绝插入原始 SQL 和不安全的原始查询助手 |
no-re-export | 从父母/祖父母模块取消直接出口或通过再出口 |
no-shell-command-construction | 通过子进程 API 取消 shell 命令构建 |
no-unsafe-code-generation | Disallow eval, 函数构造器, 字符串定时器, 以及 vm 执行 APIs |
require-node-protocol | 需要节点.js内置模块导入以使用 node: 协议前缀 |
防止错误
| 规则 | 说明 |
|---|---|
no-identical-expressions | 在二进制或逻辑运算符的两侧取消相同的表达式 |
no-identical-branches | 取消相同的条件分支 |
no-boolean-return-trap | 取消上游命名以外的模糊布尔返回 API |
no-redundant-boolean | 仅在已知操作为布尔时简化比较 |
no-for-in | 在不重写继承的密钥行为的循环中, 拒绝.. |
no-labels | 取消标签的语句 |
no-with | 否定 with 声明 |
no-await-in-loop | 否定 await 内环; 使用 Promise.all() 换成 |
no-floating-promises | 请检查url=值 (帮助) |
no-math-random | 否定 Math.random(); 转而插入随机源 |
no-eslint-disable | 防止使用 eslint-disable 评论 |
no-parameter-reassign | 取消重新指定函数参数 |
no-process-env-outside-config | 否定 process.env 读取外部配置模块 |
no-flag-argument | 函数签名中的布尔旗参数 |
prefer-guard-clauses | 如果分支终止,则删除其他块,从而优先适用保护条款 |
prefer-shortcut-return | 在保留注释的同时简化布尔返回 |
no-query-side-effects | 在查询样式函数中消除副作用 |
require-timeout-for-io | 外部 IO 呼叫需要超时或取消选项 |
require-exhaustive-switch | 需要详尽 switch 限定性歧视类型的声明 |
文档
附有实例的完整规则文件,见:
https://coderrob.github.io/eslint-config-0-容忍主义/
选择检查
使用 规则参考 以选择检查和审查实例、选项和限制。 启用 需要 - 错误 - 因为 单独保留原有例外.
关于承诺处理、不安全类型、回调和分支限制,参见 额外编码质量检查。 。 。 .
许可证
Apache 2.0 (英语) 版权:罗伯特·林德利