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 ff53126
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 94 deletions.
84 changes: 61 additions & 23 deletions commitlint.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,28 @@ module.exports = {
{
rules: {
"body-prose": ({ raw }: { raw: any }) => {
let rawStr = Helpers.convertAnyToString(raw, "raw").trim();
let rawUncastedStr = Helpers.convertAnyToString(raw, "raw");
Helpers.assertRaw(rawUncastedStr);
let rawStr = (rawUncastedStr as string).trim();

return Plugins.bodyProse(rawStr);
},

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

return Plugins.commitHashAlone(rawStr);
},

"empty-wip": ({ header }: { header: any }) => {
let headerStr = Helpers.convertAnyToString(
let headerUncastedStr = Helpers.convertAnyToString(
header,
"header"
);
Helpers.assertHeader(headerUncastedStr);
let headerStr = headerUncastedStr as string;
return Plugins.emptyWip(headerStr);
},

Expand All @@ -81,108 +89,138 @@ module.exports = {
_: any,
maxLineLength: number
) => {
let headerStr = Helpers.convertAnyToString(
let headerUncastedStr = Helpers.convertAnyToString(
header,
"header"
);
Helpers.assertHeader(headerUncastedStr);
let headerStr = headerUncastedStr as string;
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"
);
Helpers.assertHeader(headerUncastedStr);
let headerStr = headerUncastedStr as string;
return Plugins.preferSlashOverBackslash(headerStr);
},

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

return Plugins.properIssueRefs(rawStr);
},

"title-uppercase": ({ header }: { header: any }) => {
let headerStr = Helpers.convertAnyToString(
let headerUncastedStr = Helpers.convertAnyToString(
header,
"header"
);
Helpers.assertHeader(headerUncastedStr);
let headerStr = headerUncastedStr as string;

return Plugins.titleUppercase(headerStr);
},

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

return Plugins.tooManySpaces(rawStr);
},

"type-space-after-colon": ({ header }: { header: any }) => {
let headerStr = Helpers.convertAnyToString(
let headerUncastedStr = Helpers.convertAnyToString(
header,
"header"
);
Helpers.assertHeader(headerUncastedStr);
let headerStr = headerUncastedStr as string;
return Plugins.typeSpaceAfterColon(headerStr);
},

"type-with-square-brackets": ({ header }: { header: any }) => {
let headerStr = Helpers.convertAnyToString(
let headerUncastedStr = Helpers.convertAnyToString(
header,
"header"
);
Helpers.assertHeader(headerUncastedStr);
let headerStr = headerUncastedStr as string;
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"
);
Helpers.assertHeader(headerUncastedStr);
let headerStr = headerUncastedStr as string;
return Plugins.subjectLowercase(headerStr);
},

"type-space-after-comma": ({ header }: { header: any }) => {
let headerStr = Helpers.convertAnyToString(
let headerUncastedStr = Helpers.convertAnyToString(
header,
"header"
);
Helpers.assertHeader(headerUncastedStr);
let headerStr = headerUncastedStr as string;
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");
Helpers.assertRaw(rawUncastedStr);
let rawStr = rawUncastedStr as string;

return Plugins.trailingWhitespace(rawStr);
},

"type-space-before-paren": ({ header }: { header: any }) => {
let headerStr = Helpers.convertAnyToString(
let headerUncastedStr = Helpers.convertAnyToString(
header,
"header"
);
Helpers.assertHeader(headerUncastedStr);
let headerStr = headerUncastedStr as string;
return Plugins.typeSpaceBeforeParen(headerStr);
},
},
Expand Down
24 changes: 17 additions & 7 deletions commitlint/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,28 @@ 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"
return null;
}
return String(potentialString);
}

public static assertRaw(rawStr: string | null) {
if (rawStr === null) {
throw new Error(
"Unexpected " +
paramName +
"===null or " +
paramName +
"===undefined happened"
"This is unexpected because raw should never be null"
);
}
}

public static assertHeader(headerStr: string | null) {
if (headerStr === null) {
throw new Error(
"This is unexpected because header should never be null"
);
}
return String(potentialString);
}

public static assertCharacter(letter: string) {
Expand Down
1 change: 1 addition & 0 deletions commitlint/plugins.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,7 @@ test("trailing-whitespace5", () => {
let trailingWhitespace5 = runCommitLintOnMsg(
commitMsgWithTrailingWhiteSpaceInBodyEnd
);

expect(trailingWhitespace5.status).not.toBe(0);
});

Expand Down
Loading

0 comments on commit ff53126

Please sign in to comment.