Skip to content

Commit 62e1a2d

Browse files
committed
commitlint: add new rule
Add a rule to reject obvious words in the commit title.
1 parent 1cf8fc7 commit 62e1a2d

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

commitlint.config.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ module.exports = {
4747
"too-many-spaces": [RuleConfigSeverity.Error, "always"],
4848
"commit-hash-alone": [RuleConfigSeverity.Error, "always"],
4949
"title-uppercase": [RuleConfigSeverity.Error, "always"],
50+
"reject-obvious-words": [RuleConfigSeverity.Error, "always"],
5051
},
5152
plugins: [
5253
// TODO (ideas for more rules):
@@ -81,6 +82,21 @@ module.exports = {
8182
return Plugins.commitHashAlone(rawStr);
8283
},
8384

85+
"reject-obvious-words": ({
86+
header,
87+
body,
88+
}: {
89+
header: any;
90+
body: any;
91+
}) => {
92+
let headerStr = Helpers.convertAnyToString(
93+
header,
94+
"header"
95+
);
96+
let bodyStr = Helpers.convertAnyToString(body, "header");
97+
return Plugins.rejectObviousWords(headerStr, bodyStr);
98+
},
99+
84100
"empty-wip": ({ header }: { header: any }) => {
85101
let headerUncastedStr = Helpers.convertAnyToString(
86102
header,

commitlint/plugins.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { abbr } from "./abbreviations";
22
import { Helpers } from "./helpers";
3+
const obviousWords = ["change", "update", "modify"];
34

45
export abstract class Plugins {
56
public static bodyProse(rawStr: string) {
@@ -260,6 +261,35 @@ export abstract class Plugins {
260261
];
261262
}
262263

264+
public static rejectObviousWords(
265+
headerStr: string | null,
266+
bodyStr: string | null
267+
) {
268+
let offence = false;
269+
let firstWordInTitle = "";
270+
271+
if (headerStr !== null) {
272+
let colonFirstIndex = headerStr.indexOf(":");
273+
let titleStartIndex = Math.max(0, colonFirstIndex + 1);
274+
let title = headerStr
275+
.substring(titleStartIndex, headerStr.length)
276+
.trim();
277+
let titleWords = title.split(" ");
278+
firstWordInTitle = titleWords[0];
279+
280+
if (firstWordInTitle === "update") {
281+
offence = titleWords.length > 1 && bodyStr === null;
282+
} else {
283+
offence = obviousWords.includes(firstWordInTitle);
284+
}
285+
}
286+
287+
return [
288+
!offence,
289+
`Please don't use obvious words such as ${firstWordInTitle} in the commit title`,
290+
];
291+
}
292+
263293
public static titleUppercase(headerStr: string) {
264294
let firstWord = headerStr.split(" ")[0];
265295
let offence =

0 commit comments

Comments
 (0)