From fbbab03fe47715797d75b9fec5a71322677f9837 Mon Sep 17 00:00:00 2001 From: realmarv Date: Mon, 24 Oct 2022 12:59:49 +0330 Subject: [PATCH] commitlint.config: add new rule Add a rule to reject obvious words in the commit title. --- commitlint.config.ts | 16 ++++++++++++++++ commitlint/helpers.ts | 10 ++-------- commitlint/plugins.ts | 24 ++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 8 deletions(-) diff --git a/commitlint.config.ts b/commitlint.config.ts index e9109582..ebe342d1 100644 --- a/commitlint.config.ts +++ b/commitlint.config.ts @@ -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): @@ -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, diff --git a/commitlint/helpers.ts b/commitlint/helpers.ts index 248a59eb..1b1b0b64 100644 --- a/commitlint/helpers.ts +++ b/commitlint/helpers.ts @@ -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); } diff --git a/commitlint/plugins.ts b/commitlint/plugins.ts index d1cbe403..4d238686 100644 --- a/commitlint/plugins.ts +++ b/commitlint/plugins.ts @@ -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) { @@ -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 =