Skip to content

Commit

Permalink
commitlint.config: remove workaround for body
Browse files Browse the repository at this point in the history
The workaround is not needed anymore because we're now using
a recent-enough version of commitlint that includes the fix
for the bug [1].

[1] conventional-changelog/commitlint#3428
  • Loading branch information
tehraninasab committed Feb 14, 2023
1 parent 60929f3 commit b1a4790
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 94 deletions.
124 changes: 101 additions & 23 deletions commitlint.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ let bodyMaxLineLength = 64;
let headerMaxLineLength = 50;
let footerMaxLineLength = 150;

function notNullStringErrorMessage(stringType: string): string {
return `This is unexpected because ${stringType} should never be null`;
}

module.exports = {
parserPreset: "conventional-changelog-conventionalcommits",
rules: {
Expand Down Expand Up @@ -59,20 +63,35 @@ module.exports = {
{
rules: {
"body-prose": ({ raw }: { raw: any }) => {
let rawStr = Helpers.convertAnyToString(raw, "raw").trim();
let rawUncastedStr = Helpers.convertAnyToString(raw, "raw");
let rawStr = Helpers.assertNotNull(
rawUncastedStr,
notNullStringErrorMessage("raw")
).trim();

return Plugins.bodyProse(rawStr);
},

"commit-hash-alone": ({ raw }: { raw: any }) => {
let rawStr = Helpers.convertAnyToString(raw, "raw");
let rawUncastedStr = Helpers.convertAnyToString(raw, "raw");
let rawStr = Helpers.assertNotNull(
rawUncastedStr,
notNullStringErrorMessage("raw")
).trim();

return Plugins.commitHashAlone(rawStr);
},

"empty-wip": ({ header }: { header: any }) => {
let headerStr = Helpers.convertAnyToString(
let headerUncastedStr = Helpers.convertAnyToString(
header,
"header"
);
let headerStr = Helpers.assertNotNull(
headerUncastedStr,
notNullStringErrorMessage("header")
).trim();

return Plugins.emptyWip(headerStr);
},

Expand All @@ -81,108 +100,167 @@ module.exports = {
_: any,
maxLineLength: number
) => {
let headerStr = Helpers.convertAnyToString(
let headerUncastedStr = Helpers.convertAnyToString(
header,
"header"
);
let headerStr = Helpers.assertNotNull(
headerUncastedStr,
notNullStringErrorMessage("header")
).trim();

return Plugins.headerMaxLengthWithSuggestions(
headerStr,
maxLineLength
);
},

"footer-notes-misplacement": ({ raw }: { raw: any }) => {
let rawStr = Helpers.convertAnyToString(raw, "raw").trim();
return Plugins.footerNotesMisplacement(rawStr);
"footer-notes-misplacement": ({ body }: { body: any }) => {
let bodyStr = Helpers.convertAnyToString(body, "body");
return Plugins.footerNotesMisplacement(bodyStr);
},

"footer-references-existence": ({ raw }: { raw: any }) => {
let rawStr = Helpers.convertAnyToString(raw, "raw").trim();
return Plugins.footerReferencesExistence(rawStr);
"footer-references-existence": ({ body }: { body: any }) => {
let bodyStr = Helpers.convertAnyToString(body, "body");

return Plugins.footerReferencesExistence(bodyStr);
},

"prefer-slash-over-backslash": ({
header,
}: {
header: any;
}) => {
let headerStr = Helpers.convertAnyToString(
let headerUncastedStr = Helpers.convertAnyToString(
header,
"header"
);
let headerStr = Helpers.assertNotNull(
headerUncastedStr,
notNullStringErrorMessage("header")
).trim();

return Plugins.preferSlashOverBackslash(headerStr);
},

"proper-issue-refs": ({ raw }: { raw: any }) => {
let rawStr = Helpers.convertAnyToString(raw, "raw").trim();
let rawUncastedStr = Helpers.convertAnyToString(raw, "raw");
let rawStr = Helpers.assertNotNull(
rawUncastedStr,
notNullStringErrorMessage("raw")
).trim();

return Plugins.properIssueRefs(rawStr);
},

"title-uppercase": ({ header }: { header: any }) => {
let headerStr = Helpers.convertAnyToString(
let headerUncastedStr = Helpers.convertAnyToString(
header,
"header"
);
let headerStr = Helpers.assertNotNull(
headerUncastedStr,
notNullStringErrorMessage("header")
).trim();

return Plugins.titleUppercase(headerStr);
},

"too-many-spaces": ({ raw }: { raw: any }) => {
let rawStr = Helpers.convertAnyToString(raw, "raw");
let rawUncastedStr = Helpers.convertAnyToString(raw, "raw");
let rawStr = Helpers.assertNotNull(
rawUncastedStr,
notNullStringErrorMessage("raw")
).trim();

return Plugins.tooManySpaces(rawStr);
},

"type-space-after-colon": ({ header }: { header: any }) => {
let headerStr = Helpers.convertAnyToString(
let headerUncastedStr = Helpers.convertAnyToString(
header,
"header"
);
let headerStr = Helpers.assertNotNull(
headerUncastedStr,
notNullStringErrorMessage("header")
).trim();

return Plugins.typeSpaceAfterColon(headerStr);
},

"type-with-square-brackets": ({ header }: { header: any }) => {
let headerStr = Helpers.convertAnyToString(
let headerUncastedStr = Helpers.convertAnyToString(
header,
"header"
);
let headerStr = Helpers.assertNotNull(
headerUncastedStr,
notNullStringErrorMessage("header")
).trim();

return Plugins.typeWithSquareBrackets(headerStr);
},

// NOTE: we use 'header' instead of 'subject' as a workaround to this bug: https://github.com/conventional-changelog/commitlint/issues/3404
"subject-lowercase": ({ header }: { header: any }) => {
let headerStr = Helpers.convertAnyToString(
let headerUncastedStr = Helpers.convertAnyToString(
header,
"header"
);
let headerStr = Helpers.assertNotNull(
headerUncastedStr,
notNullStringErrorMessage("header")
).trim();

return Plugins.subjectLowercase(headerStr);
},

"type-space-after-comma": ({ header }: { header: any }) => {
let headerStr = Helpers.convertAnyToString(
let headerUncastedStr = Helpers.convertAnyToString(
header,
"header"
);
let headerStr = Helpers.assertNotNull(
headerUncastedStr,
notNullStringErrorMessage("header")
).trim();

return Plugins.typeSpaceAfterComma(headerStr);
},

"body-soft-max-line-length": (
{ raw }: { raw: any },
{ body }: { body: any },
_: any,
maxLineLength: number
) => {
let rawStr = Helpers.convertAnyToString(raw, "raw").trim();
return Plugins.bodySoftMaxLineLength(rawStr, maxLineLength);
let bodyStr = Helpers.convertAnyToString(body, "body");
return Plugins.bodySoftMaxLineLength(
bodyStr,
maxLineLength
);
},

"trailing-whitespace": ({ raw }: { raw: any }) => {
let rawStr = Helpers.convertAnyToString(raw, "raw");
let rawUncastedStr = Helpers.convertAnyToString(raw, "raw");
let rawStr = Helpers.assertNotNull(
rawUncastedStr,
notNullStringErrorMessage("raw")
);

return Plugins.trailingWhitespace(rawStr);
},

"type-space-before-paren": ({ header }: { header: any }) => {
let headerStr = Helpers.convertAnyToString(
let headerUncastedStr = Helpers.convertAnyToString(
header,
"header"
);
let headerStr = Helpers.assertNotNull(
headerUncastedStr,
notNullStringErrorMessage("header")
).trim();

return Plugins.typeSpaceBeforeParen(headerStr);
},
},
Expand Down
20 changes: 12 additions & 8 deletions commitlint/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,24 @@ export abstract class Helpers {
public static convertAnyToString(
potentialString: any,
paramName: string
): string {
): string | null {
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);
}

public static assertNotNull(
text: string | null,
errorMessage: string
): string {
if (text === null) {
throw new Error(errorMessage);
}
return text as string;
}

public static assertCharacter(letter: string) {
if (letter.length !== 1) {
throw Error("This function expects a character as input");
Expand Down
Loading

0 comments on commit b1a4790

Please sign in to comment.