Skip to content

Commit

Permalink
Merge PR #55 from realmarv/upgradeCommitlint-squashed
Browse files Browse the repository at this point in the history
Upgrade commitlint to the latest version.
  • Loading branch information
knocte authored Feb 14, 2023
2 parents 9a22527 + 9e74c4f commit 1975174
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 2,048 deletions.
20 changes: 12 additions & 8 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ jobs:
apt update
apt install -y sudo
sudo apt install -y git
sudo DEBIAN_FRONTEND=noninteractive apt install --yes npm
- name: Install commitlint
run: npm install
sudo apt install -y curl
# can't install ubuntu's default nodejs version because we would get this error:
# error @jest/[email protected]: The engine "node" is incompatible with this module. Expected version "^14.15.0 || ^16.10.0 || >=18.0.0". Got "12.22.9"
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
sudo DEBIAN_FRONTEND=noninteractive apt install -y nodejs
- name: Print versions
run: |
git --version
Expand All @@ -25,7 +28,12 @@ jobs:
npx commitlint --version
- name: Install yarn
run: |
sudo npm install -g yarn
npm install -g yarn
yarn add --dev jest typescript ts-jest @types/jest
- name: Install commitlint
run: |
npm install conventional-changelog-conventionalcommits
npm install commitlint@latest
- name: Print versions
run: |
git --version
Expand Down Expand Up @@ -80,8 +88,4 @@ jobs:
# Since we changed file modes in the previous step we need the following command to
# make git ignore mode changes in files and doesn't include them in the git diff command.
git config core.fileMode false
# Since after installing npm packages, package.json and package-lock.json change,
# `git diff --exit-code` will always throw an error, so we need to restore those
# files before running `git diff --exit-code` command.
git restore package.json package-lock.json
git diff --exit-code
123 changes: 100 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 @@ -58,20 +62,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")
);

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")
);

return Plugins.emptyWip(headerStr);
},

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

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")
);

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")
);

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")
);

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")
);

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")
);

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")
);
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")
);

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")
);

return Plugins.typeSpaceBeforeParen(headerStr);
},
},
Expand Down
3 changes: 2 additions & 1 deletion commitlint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ set -euxo pipefail

# cd to directory of this script
cd "$(dirname "$0")"
npm install
npm install conventional-changelog-conventionalcommits
npm install commitlint@latest
npx commitlint --version
npx commitlint $@
cd ..
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 1975174

Please sign in to comment.