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 Aug 9, 2023
1 parent 7c678a2 commit c8a9391
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 @@ -51,6 +51,7 @@ module.exports = {
// disabled because most of the time it doesn't work, due to https://github.com/conventional-changelog/commitlint/issues/3404
// and anyway we were using this rule only as a warning, not an error (because a scope is not required, e.g. when too broad)
"type-empty": [RuleConfigSeverity.Disabled, "never"],
"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 headerStr = Helpers.assertNotNull(
Helpers.convertAnyToString(header, "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 @@ -278,6 +279,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 c8a9391

Please sign in to comment.