Skip to content

Commit

Permalink
commitlint: add new rule
Browse files Browse the repository at this point in the history
Add a rule to reject obvious words in the commit title.
  • Loading branch information
tehraninasab committed Feb 14, 2023
1 parent 1cf8fc7 commit 62e1a2d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
16 changes: 16 additions & 0 deletions commitlint.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ module.exports = {
"too-many-spaces": [RuleConfigSeverity.Error, "always"],
"commit-hash-alone": [RuleConfigSeverity.Error, "always"],
"title-uppercase": [RuleConfigSeverity.Error, "always"],
"reject-obvious-words": [RuleConfigSeverity.Error, "always"],
},
plugins: [
// TODO (ideas for more rules):
Expand Down Expand Up @@ -81,6 +82,21 @@ module.exports = {
return Plugins.commitHashAlone(rawStr);
},

"reject-obvious-words": ({
header,
body,
}: {
header: any;
body: any;
}) => {
let headerStr = Helpers.convertAnyToString(
header,
"header"
);
let bodyStr = Helpers.convertAnyToString(body, "header");
return Plugins.rejectObviousWords(headerStr, bodyStr);
},

"empty-wip": ({ header }: { header: any }) => {
let headerUncastedStr = Helpers.convertAnyToString(
header,
Expand Down
30 changes: 30 additions & 0 deletions commitlint/plugins.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { abbr } from "./abbreviations";
import { Helpers } from "./helpers";
const obviousWords = ["change", "update", "modify"];

export abstract class Plugins {
public static bodyProse(rawStr: string) {
Expand Down Expand Up @@ -260,6 +261,35 @@ export abstract class Plugins {
];
}

public static rejectObviousWords(
headerStr: string | null,
bodyStr: string | null
) {
let offence = false;
let firstWordInTitle = "";

if (headerStr !== null) {
let colonFirstIndex = headerStr.indexOf(":");
let titleStartIndex = Math.max(0, colonFirstIndex + 1);
let title = headerStr
.substring(titleStartIndex, headerStr.length)
.trim();
let titleWords = title.split(" ");
firstWordInTitle = titleWords[0];

if (firstWordInTitle === "update") {
offence = titleWords.length > 1 && bodyStr === null;
} else {
offence = obviousWords.includes(firstWordInTitle);
}
}

return [
!offence,
`Please don't use obvious words such as ${firstWordInTitle} in the commit title`,
];
}

public static titleUppercase(headerStr: string) {
let firstWord = headerStr.split(" ")[0];
let offence =
Expand Down

0 comments on commit 62e1a2d

Please sign in to comment.