From c8a9391b3d388d081ddfe4249bca2ffef1262c7a Mon Sep 17 00:00:00 2001 From: realmarv Date: Tue, 14 Feb 2023 13:17:49 +0330 Subject: [PATCH] commitlint: add new rule Add a rule to reject obvious words in the commit title. --- commitlint.config.ts | 16 ++++++++++++++++ commitlint/plugins.ts | 30 ++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/commitlint.config.ts b/commitlint.config.ts index a708f127..7e413fb4 100644 --- a/commitlint.config.ts +++ b/commitlint.config.ts @@ -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): @@ -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"), diff --git a/commitlint/plugins.ts b/commitlint/plugins.ts index 03a47233..46610e27 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) { @@ -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 =