From 81f50e35be96c797b48619b5d6fc35585e63d333 Mon Sep 17 00:00:00 2001 From: realmarv Date: Sat, 25 Feb 2023 12:53:25 +0330 Subject: [PATCH] commitlint: add When enum --- commitlint.config.ts | 57 ++++++++++++++++++++++--------------------- commitlint/helpers.ts | 8 +++++- commitlint/plugins.ts | 4 +-- 3 files changed, 38 insertions(+), 31 deletions(-) diff --git a/commitlint.config.ts b/commitlint.config.ts index 008997e3..44cc3693 100644 --- a/commitlint.config.ts +++ b/commitlint.config.ts @@ -1,4 +1,4 @@ -import { Helpers } from "./commitlint/helpers"; +import { Helpers, When } from "./commitlint/helpers"; import { Plugins } from "./commitlint/plugins"; import { RuleConfigSeverity } from "@commitlint/types"; @@ -13,45 +13,48 @@ function notNullStringErrorMessage(stringType: string): string { module.exports = { parserPreset: "conventional-changelog-conventionalcommits", rules: { - "body-leading-blank": [RuleConfigSeverity.Error, "always"], + "body-leading-blank": [RuleConfigSeverity.Error, When.Always], "body-soft-max-line-length": [ RuleConfigSeverity.Error, - "always", + When.Always, bodyMaxLineLength, ], - "body-paragraph-line-min-length": [RuleConfigSeverity.Error, "always"], - "empty-wip": [RuleConfigSeverity.Error, "always"], - "footer-leading-blank": [RuleConfigSeverity.Warning, "always"], + "body-paragraph-line-min-length": [ + RuleConfigSeverity.Error, + When.Always, + ], + "empty-wip": [RuleConfigSeverity.Error, When.Always], + "footer-leading-blank": [RuleConfigSeverity.Warning, When.Always], "footer-max-line-length": [ RuleConfigSeverity.Error, - "always", + When.Always, footerMaxLineLength, ], - "footer-notes-misplacement": [RuleConfigSeverity.Error, "always"], - "footer-refs-validity": [RuleConfigSeverity.Error, "always"], + "footer-notes-misplacement": [RuleConfigSeverity.Error, When.Always], + "footer-refs-validity": [RuleConfigSeverity.Error, When.Always], "header-max-length-with-suggestions": [ RuleConfigSeverity.Error, - "always", + When.Always, headerMaxLineLength, ], - "subject-full-stop": [RuleConfigSeverity.Error, "never", "."], - "type-space-after-colon": [RuleConfigSeverity.Error, "always"], - "subject-lowercase": [RuleConfigSeverity.Error, "always"], - "body-prose": [RuleConfigSeverity.Error, "always"], - "type-space-after-comma": [RuleConfigSeverity.Error, "always"], - "trailing-whitespace": [RuleConfigSeverity.Error, "always"], - "prefer-slash-over-backslash": [RuleConfigSeverity.Error, "always"], - "type-space-before-paren": [RuleConfigSeverity.Error, "always"], - "type-with-square-brackets": [RuleConfigSeverity.Error, "always"], - "proper-issue-refs": [RuleConfigSeverity.Error, "always"], - "too-many-spaces": [RuleConfigSeverity.Error, "always"], - "commit-hash-alone": [RuleConfigSeverity.Error, "always"], - "title-uppercase": [RuleConfigSeverity.Error, "always"], + "subject-full-stop": [RuleConfigSeverity.Error, When.Never, "."], + "type-space-after-colon": [RuleConfigSeverity.Error, When.Always], + "subject-lowercase": [RuleConfigSeverity.Error, When.Always], + "body-prose": [RuleConfigSeverity.Error, When.Always], + "type-space-after-comma": [RuleConfigSeverity.Error, When.Always], + "trailing-whitespace": [RuleConfigSeverity.Error, When.Always], + "prefer-slash-over-backslash": [RuleConfigSeverity.Error, When.Always], + "type-space-before-paren": [RuleConfigSeverity.Error, When.Always], + "type-with-square-brackets": [RuleConfigSeverity.Error, When.Always], + "proper-issue-refs": [RuleConfigSeverity.Error, When.Always], + "too-many-spaces": [RuleConfigSeverity.Error, When.Always], + "commit-hash-alone": [RuleConfigSeverity.Error, When.Always], + "title-uppercase": [RuleConfigSeverity.Error, When.Always], // 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"], - "default-revert-message": [RuleConfigSeverity.Error, "never"], + "type-empty": [RuleConfigSeverity.Disabled, When.Never], + "default-revert-message": [RuleConfigSeverity.Error, When.Never], }, // Commitlint automatically ignores some kinds of commits like Revert commit messages. @@ -156,8 +159,6 @@ module.exports = { }, when: string ) => { - Helpers.assertWhen(when); - let bodyStr = Helpers.convertAnyToString(body, "body"); let headerStr = Helpers.assertNotNull( Helpers.convertAnyToString(header, "header"), @@ -166,7 +167,7 @@ module.exports = { return Plugins.defaultRevertMessage( headerStr, bodyStr, - when + Helpers.assertWhen(when) ); }, diff --git a/commitlint/helpers.ts b/commitlint/helpers.ts index cff26ec5..7f71b220 100644 --- a/commitlint/helpers.ts +++ b/commitlint/helpers.ts @@ -1,3 +1,8 @@ +export enum When { + Never = "never", + Always = "always", +} + export abstract class Helpers { public static errMessageSuffix = "\nFor reference, these are the guidelines that include our commit message conventions: https://github.com/nblockchain/conventions/blob/master/WorkflowGuidelines.md"; @@ -38,11 +43,12 @@ export abstract class Helpers { } public static assertWhen(when: string) { - if (when !== "never" && when !== "always") { + if (when !== When.Never && when !== When.Always) { throw new Error( 'Variable "when" should be either "never" or "always"' ); } + return when as When; } public static assertCharacter(letter: string) { diff --git a/commitlint/plugins.ts b/commitlint/plugins.ts index e2c3b61d..51652fd3 100644 --- a/commitlint/plugins.ts +++ b/commitlint/plugins.ts @@ -1,5 +1,5 @@ import { abbr } from "./abbreviations"; -import { Helpers } from "./helpers"; +import { Helpers, When } from "./helpers"; export abstract class Plugins { public static bodyProse(rawStr: string) { @@ -281,7 +281,7 @@ export abstract class Plugins { public static defaultRevertMessage( headerStr: string, bodyStr: string | null, - when: string + when: When ) { let offence = false; let isRevertCommitMessage = headerStr.toLowerCase().includes("revert");