Skip to content

Commit

Permalink
commitlint: add When enum
Browse files Browse the repository at this point in the history
  • Loading branch information
tehraninasab committed Aug 9, 2023
1 parent 09d1e26 commit a5cb5c6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 31 deletions.
54 changes: 26 additions & 28 deletions commitlint.config.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -13,45 +13,45 @@ 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.
Expand Down Expand Up @@ -156,8 +156,6 @@ module.exports = {
},
when: string
) => {
Helpers.assertWhen(when);

let bodyStr = Helpers.convertAnyToString(body, "body");
let headerStr = Helpers.assertNotNull(
Helpers.convertAnyToString(header, "header"),
Expand All @@ -166,7 +164,7 @@ module.exports = {
return Plugins.defaultRevertMessage(
headerStr,
bodyStr,
when
Helpers.assertWhen(when)
);
},

Expand Down
8 changes: 7 additions & 1 deletion commitlint/helpers.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions commitlint/plugins.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit a5cb5c6

Please sign in to comment.