ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

@commitlint/config-angular-type-enum:仅约束提交类型(type)的共享式 commitlint 配置

@commitlint/config-angular-type-enum:仅约束提交类型(type)的共享式 commitlint 配置 开发工具Lint代码质量【免费下载链接】commitlint Lint commit messages项目地址https://gitcode.com/gh_mirrors/co/commitlint点击查看免费下载本指南围绕 commitlint 仓库中的commitlint/config-angular-type-enum包展开它是一份只聚焦于 Angular 提交规范中type提交类型枚举的共享式配置Shareable Config。读完本文你将掌握如何安装、接入并复用这份配置理解其type-enum规则的底层判定逻辑severity、when、value 三元组以及如何通过value()方法将类型枚举二次注入到你自己的规则中实现“仅校验类型、其余规则自定”的轻量场景。这个包是什么commitlint/config-angular-type-enum是 commitlint 官方提供的一个极简共享配置它的职责只有一个强制提交信息的 type 必须是 Angular 提交规范所允许的枚举值。与完整版的 commitlint/config-angular还包含scope-case、subject-case、header-max-length等十余条规则不同本包刻意保持单一职责适合那些只希望约束类型、其余规则自行定制的项目。该包适用于 commitlint/cli 命令行校验也可以配合 commitlint/prompt-cli 在交互式提交提示中复用同一份类型清单。核心实现一份极简的配置源码整个包的全部逻辑都浓缩在 index.js 中const types [build, ci, docs, feat, fix, perf, refactor, revert, style, test]; export default { rules: { type-enum: [2, always, types], }, value: () types, };从源码结构可以归纳出三件事types数组定义了 Angular 规范的 10 种提交类型注意其中不包含chore完整版 config-conventional 的类型枚举是 11 项额外多出chore。这是选择本包与 config-conventional 时最直观的差异点。rules[type-enum]以 commitlint 规则标准三元组[severity, condition, value]形式暴露即“严重级别 2error错误时退出码非 0、条件always、允许值types”。value()方法一个返回类型数组的辅助函数方便其他配置或代码直接复用同一份枚举而无需重新维护一份类型列表。快速开始在项目根目录执行以下命令安装依赖并生成配置与原文档一致可直接复制运行npm install --save-dev commitlint/config-angular-type-enum commitlint/cli echo module.exports {extends: [commitlint/config-angular-type-enum]}; commitlint.config.jsextends字段会让 commitlint 在加载配置时递归合并被引用包的rules从而把type-enum规则带入当前项目。整个合并与解析流程由 commitlint/load 负责它支持本地文件、npm 包名等多种形式的extends解析。用法验证配置完成后即可用管道方式把提交信息喂给commitlint校验echo foo: bar | commitlint # fails echo build: bar | commitlint # passesfoo不在允许的 10 种类型之列规则判定失败CLI 以非零退出码结束build是合法类型校验通过。在package.json的 scripts 中也可以这样接入例如结合 husky 的 pre-commit/prepare-commit-msg 钩子{ scripts: { commitmsg: commitlint -E HUSKY_GIT_PARAMS } }更细粒度的用法直接使用 rules 或 value()原文档还提供了两种 JavaScript 层面的用法适用于自定义配置的进阶场景。方式一展开rules按 error 级别约束类型// commitlint.config.js const types require(commitlint/config-angular-type-enum); // Use as rule creating errors for non-allowed types module.exports { rules: { ...types.rules, }, };将配置对象的rules属性展开进自己的rules等价于type-enum: [2, always, [...]]非允许类型会产生 error 级别的失败。方式二借用value()把枚举降级为 warning// Warn for non-allowed types module.exports { rules: { type-enum: [1, always, types.values()], }, };这里将 severity 从2改为1warning非允许类型仅输出警告、不影响退出码适合迁移期或宽松团队。注意types.values()返回的是同一份数组引用如果你只想取出类型列表而不引入完整 rules可以像这样复用const config require(commitlint/config-angular-type-enum); module.exports { rules: { type-enum: [2, always, config.value()], }, };被上游配置复用的真实案例本包在仓库内并非孤立存在——完整版 commitlint/config-angular 正是通过导入本包来获得类型枚举import typeEnum from commitlint/config-angular-type-enum; // ... rules: { // ... type-enum: typeEnum.rules[type-enum], },也就是说config-angular的type-enum规则值与config-angular-type-enum完全一致。这印证了本包的设计意图把“类型枚举”这一单一规则拆成可独立复用、可组合的共享单元避免在多个官方配置间重复维护同一份列表。底层原理type-enum 规则是如何判定的要理解这份配置的行为需要看规则的实现。规则本体位于 type-enum.tsexport const typeEnum: SyncRulestring[] (parsed, when always, value []) { const { type: input } parsed; if (!input) { return [true]; } const negated when never; const result ensure.enum(input, value); return [ negated ? !result : result, message([type must, negated ? not : null, be one of [${value.join(, )}]]), ]; };几个关键点parsed是 commitlint/parse 解析提交信息后得到的结构化对象parsed.type即type(scope): subject中的 type 部分当提交信息没有 type如(): \n这类空类型时规则直接返回[true]放行——空类型本身由type-empty: never规则负责拦截type-enum只关心“有类型时是否合法”when为always时要求 type 必须在枚举内never时则要求不在枚举内本配置固定使用always判定逻辑最终委托给commitlint/ensure中的 enum.tsexport default (value: any, enums: any[] []): boolean { if (value undefined) return false; if (!Array.isArray(enums)) return false; return enums.indexOf(value) -1; };即通过indexOf做严格全等包含性检查type 为undefined或枚举参数不是数组时一律返回false否则判断type是否在允许列表中。对应的测试覆盖了“命中、未命中、never 取反、空类型放行”等分支见 type-enum.test.ts 与 enum.test.ts。类型枚举参考本配置允许的完整类型列表如下可直接作为团队规范文档引用type典型用途build影响构建系统或外部依赖的改动如 gulp、broccoli、npmciCI 配置文件和脚本的改动如 Travis、Circle、BrowserStack、SauceLabsdocs仅文档变更feat新功能fix缺陷修复perf提升性能的改动refactor既不修 bug 也不加功能的代码重构revert回滚之前的提交style不影响代码含义的改动空白、格式、缺失分号等test补充缺失测试或修正现有测试若你的项目还需要chore不修改 src 或测试文件的其他改动可自行在配置中扩展module.exports { extends: [commitlint/config-angular-type-enum], rules: { type-enum: [2, always, [...require(commitlint/config-angular-type-enum).value(), chore]], }, };包发布信息与使用前提包入口为index.js采用 ESMtype: modulemain指向 index.js发布文件仅含该单一文件根据 package.json 的engines字段当前版本要求Node.js 22.12.0通过npm install --save-dev commitlint/config-angular-type-enum commitlint/cli安装后以extends方式接入即可生效。小结commitlint/config-angular-type-enum用最小的表面积解决了“只锁类型”这一具体问题一份 10 元素的类型数组、一条type-enum规则和一个value()辅助方法。它既可以独立使用也可以作为config-angular等完整配置的“类型来源”被组合复用。对团队而言选择它的核心判断点是是否认可这份不含chore的 Angular 类型清单认可则零配置直用不认可则基于value()快速扩展出自己的枚举。赞分享开发工具Lint代码质量【免费下载链接】commitlint Lint commit messages项目地址https://gitcode.com/gh_mirrors/co/commitlint点击查看免费下载相关推荐使用 commitlint/config-angular 以 Angular 提交规范约束 Git 提交信息使用 commitlint/config angular 以 Angular 提交规范约束 Git 提交信息 commitlint/config angul开发工具Lint代码质量commitlint/config-pnpm-scopes为 pnpm workspace 仓库自动生成 scope 枚举的 commitlint 共享配置commitlint/config pnpm scopes为 pnpm workspace 仓库自动生成 scope 枚举的 commitlint 共享配置开发工具Lint代码质量commitlint 与 Lerna 集成指南commitlint/config-lerna-scopes 共享配置的演进、实现与迁移commitlint 与 Lerna 集成指南commitlint/config lerna scopes 共享配置的演进、实现与迁移 导读 commit开发工具Lint代码质量创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表