Skip to content

Commit

Permalink
commitlint: introduce an Option type
Browse files Browse the repository at this point in the history
And make use of it in one of the plugins already.

See my SO post about this:
https://stackoverflow.com/a/78937127/544947
  • Loading branch information
knocte committed Sep 5, 2024
1 parent 6e4cc33 commit f007103
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
9 changes: 9 additions & 0 deletions commitlint/fpHelpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export class None {}
export class Some<T> {
value: T;

constructor(val: T) {
this.value = val;
}
}
export type Option<T> = None | Some<T>;
14 changes: 8 additions & 6 deletions commitlint/plugins.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Option, Some, None } from "./fpHelpers.js"
import { abbr } from "./abbreviations.js";
import { Helpers } from "./helpers.js";

Expand Down Expand Up @@ -437,7 +438,7 @@ export abstract class Plugins {
paragraphLineMinLength: number,
paragraphLineMaxLength: number
) {
let offence: string | null = null;
let offence: Option<string> = None;

if (bodyStr !== null) {
bodyStr = Helpers.removeAllCodeBlocks(bodyStr).trim();
Expand Down Expand Up @@ -502,19 +503,20 @@ export abstract class Plugins {
!isLastLineBeforeNextBullet &&
!isLastCharAColonBreak
) {
offence = line;
offence = new Some(line);
break;
}
}
}
}
}

let isValid = offence === null;

if (offence instanceof None) {
return [true, "bodyParagraphLineMinLength's bug, this text should not be shown if offence was false"];
}
return [
isValid,
`Please do not subceed ${paragraphLineMinLength} characters in the lines of the commit message's body paragraphs. Offending line has this text: "${offence}"; we recommend this script (for editing the last commit message): \n` +
false,
`Please do not subceed ${paragraphLineMinLength} characters in the lines of the commit message's body paragraphs. Offending line has this text: "${offence.value}"; we recommend this script (for editing the last commit message): \n` +
"https://github.com/nblockchain/conventions/blob/master/scripts/wrapLatestCommitMsg.fsx" +
Helpers.errMessageSuffix,
];
Expand Down
18 changes: 18 additions & 0 deletions commitlint/tests/testHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
import { test, expect } from "vitest";
import { None, Some, Option } from "../fpHelpers.js";
const { spawnSync } = require("child_process");
const os = require("os");

function typeGuard(option: Option<number>) {
if (option instanceof None) {
return "NAH";
} else {
let val = option.value;
return (val * val).toString();
}
}

test("testing Options", () => {
let foo: Option<number> = new None();
let bar: Option<number> = new Some(2);
expect(typeGuard(foo)).toBe("NAH");
expect(typeGuard(bar)).toBe("4");
});

export function runCommitLintOnMsg(inputMsg: string) {
// FIXME: should we .lowerCase().startsWith("win") in case it starts
// returning Win64 in the future? thing is, our CI doesn't like this
Expand Down

0 comments on commit f007103

Please sign in to comment.