Skip to content

Commit

Permalink
commitlint.config: 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 12, 2023
1 parent 7b336be commit fbbab03
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
16 changes: 16 additions & 0 deletions commitlint.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,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 All @@ -68,6 +69,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.convertAnyToString(
header,
Expand Down
10 changes: 2 additions & 8 deletions commitlint/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,10 @@ export abstract class Helpers {
public static convertAnyToString(
potentialString: any,
paramName: string
): string {
): any {
if (potentialString === null || potentialString === undefined) {
// otherwise, String(null) might give us the stupid string "null"
throw new Error(
"Unexpected " +
paramName +
"===null or " +
paramName +
"===undefined happened"
);
return null;
}
return String(potentialString);
}
Expand Down
24 changes: 24 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 @@ -272,6 +273,29 @@ export abstract class Plugins {
];
}

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

let colonFirstIndex = headerStr.indexOf(":");
let titleStartIndex = Math.max(0, colonFirstIndex + 1);
let title = headerStr
.substring(titleStartIndex, headerStr.length)
.trim();
let titleWords = title.split(" ");
let 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 fbbab03

Please sign in to comment.