未继承@commitlint/config-conventional时commitlint规则未生效问题
你的commitlint配置未达到预期效果,核心问题有三点:
1. 规则目标错误:body-full-stop不作用于subject
body-full-stop仅校验提交消息的body段落,而git commit -m 'hello.'只有header内容(无body),所以这条规则不会触发任何校验。若要禁止subject结尾的句号,应使用subject-full-stop规则。
2. 缺少消息结构识别规则
commitlint的subject-case等规则依赖于对提交消息结构(type、subject、body等)的正确解析。当你不继承@commitlint/config-conventional时,没有默认规则指导commitlint识别消息的各个部分——它无法判断hello.属于subject,因此subject-case规则无法生效。
你需要添加基础规则明确结构,比如subject-exists,强制subject必须存在,确保commitlint将header内容识别为subject。
3. type-empty规则的实际效果与预期偏差
你设置的type-empty: [2, 'always']是强制type部分必须为空,意味着提交消息不能有<type>: 前缀。当前提交hello.符合这个要求,所以不会被拦截。如果需要提交消息带type前缀,需将该规则改为[2, 'never'];若确实不需要type,保持规则即可,但需配合subject-exists让commitlint识别subject。
修正后的配置示例
场景1:不需要type前缀,仅校验整个提交消息(作为subject)
module.exports = { rules: { // 强制subject必须存在,确保commitlint识别header内容为subject 'subject-exists': [2, 'always'], // 禁止subject结尾出现句号 'subject-full-stop': [2, 'never', '.'], // 强制subject采用首字母大写的句子格式 'subject-case': [2, 'always', 'sentence-case'], // 保持type为空(不需要type前缀) 'type-empty': [2, 'always'], }, };
场景2:需要type前缀(符合常规commit格式)
module.exports = { rules: { // 强制type不能为空,必须带<type>: 前缀 'type-empty': [2, 'never'], // 禁止subject结尾出现句号 'subject-full-stop': [2, 'never', '.'], // 强制subject采用首字母大写的句子格式 'subject-case': [2, 'always', 'sentence-case'], // 可选:限制type的可选值 'type-enum': [2, 'always', ['feat', 'fix', 'docs', 'chore']], }, };
此时提交需遵循type: subject格式,例如git commit -m 'feat: Add user login module',若提交git commit -m 'hello.'会因缺少type前缀被拦截,提交git commit -m 'feat: hello.'会因subject结尾有句号、首字母小写被拦截。
内容的提问来源于stack exchange,提问作者iserdmi

