Skip to content

Commit 1975174

Browse files
authored
Merge PR #55 from realmarv/upgradeCommitlint-squashed
Upgrade commitlint to the latest version.
2 parents 9a22527 + 9e74c4f commit 1975174

File tree

7 files changed

+172
-2048
lines changed

7 files changed

+172
-2048
lines changed

.github/workflows/CI.yml

+12-8
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@ jobs:
1414
apt update
1515
apt install -y sudo
1616
sudo apt install -y git
17-
sudo DEBIAN_FRONTEND=noninteractive apt install --yes npm
18-
- name: Install commitlint
19-
run: npm install
17+
18+
sudo apt install -y curl
19+
# can't install ubuntu's default nodejs version because we would get this error:
20+
# 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"
21+
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
22+
sudo DEBIAN_FRONTEND=noninteractive apt install -y nodejs
2023
- name: Print versions
2124
run: |
2225
git --version
@@ -25,7 +28,12 @@ jobs:
2528
npx commitlint --version
2629
- name: Install yarn
2730
run: |
28-
sudo npm install -g yarn
31+
npm install -g yarn
32+
yarn add --dev jest typescript ts-jest @types/jest
33+
- name: Install commitlint
34+
run: |
35+
npm install conventional-changelog-conventionalcommits
36+
npm install commitlint@latest
2937
- name: Print versions
3038
run: |
3139
git --version
@@ -80,8 +88,4 @@ jobs:
8088
# Since we changed file modes in the previous step we need the following command to
8189
# make git ignore mode changes in files and doesn't include them in the git diff command.
8290
git config core.fileMode false
83-
# Since after installing npm packages, package.json and package-lock.json change,
84-
# `git diff --exit-code` will always throw an error, so we need to restore those
85-
# files before running `git diff --exit-code` command.
86-
git restore package.json package-lock.json
8791
git diff --exit-code

commitlint.config.ts

+100-23
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ let bodyMaxLineLength = 64;
66
let headerMaxLineLength = 50;
77
let footerMaxLineLength = 150;
88

9+
function notNullStringErrorMessage(stringType: string): string {
10+
return `This is unexpected because ${stringType} should never be null`;
11+
}
12+
913
module.exports = {
1014
parserPreset: "conventional-changelog-conventionalcommits",
1115
rules: {
@@ -58,20 +62,35 @@ module.exports = {
5862
{
5963
rules: {
6064
"body-prose": ({ raw }: { raw: any }) => {
61-
let rawStr = Helpers.convertAnyToString(raw, "raw").trim();
65+
let rawUncastedStr = Helpers.convertAnyToString(raw, "raw");
66+
let rawStr = Helpers.assertNotNull(
67+
rawUncastedStr,
68+
notNullStringErrorMessage("raw")
69+
).trim();
70+
6271
return Plugins.bodyProse(rawStr);
6372
},
6473

6574
"commit-hash-alone": ({ raw }: { raw: any }) => {
66-
let rawStr = Helpers.convertAnyToString(raw, "raw");
75+
let rawUncastedStr = Helpers.convertAnyToString(raw, "raw");
76+
let rawStr = Helpers.assertNotNull(
77+
rawUncastedStr,
78+
notNullStringErrorMessage("raw")
79+
);
80+
6781
return Plugins.commitHashAlone(rawStr);
6882
},
6983

7084
"empty-wip": ({ header }: { header: any }) => {
71-
let headerStr = Helpers.convertAnyToString(
85+
let headerUncastedStr = Helpers.convertAnyToString(
7286
header,
7387
"header"
7488
);
89+
let headerStr = Helpers.assertNotNull(
90+
headerUncastedStr,
91+
notNullStringErrorMessage("header")
92+
);
93+
7594
return Plugins.emptyWip(headerStr);
7695
},
7796

@@ -80,108 +99,166 @@ module.exports = {
8099
_: any,
81100
maxLineLength: number
82101
) => {
83-
let headerStr = Helpers.convertAnyToString(
102+
let headerUncastedStr = Helpers.convertAnyToString(
84103
header,
85104
"header"
86105
);
106+
let headerStr = Helpers.assertNotNull(
107+
headerUncastedStr,
108+
notNullStringErrorMessage("header")
109+
);
110+
87111
return Plugins.headerMaxLengthWithSuggestions(
88112
headerStr,
89113
maxLineLength
90114
);
91115
},
92116

93-
"footer-notes-misplacement": ({ raw }: { raw: any }) => {
94-
let rawStr = Helpers.convertAnyToString(raw, "raw").trim();
95-
return Plugins.footerNotesMisplacement(rawStr);
117+
"footer-notes-misplacement": ({ body }: { body: any }) => {
118+
let bodyStr = Helpers.convertAnyToString(body, "body");
119+
return Plugins.footerNotesMisplacement(bodyStr);
96120
},
97121

98-
"footer-references-existence": ({ raw }: { raw: any }) => {
99-
let rawStr = Helpers.convertAnyToString(raw, "raw").trim();
100-
return Plugins.footerReferencesExistence(rawStr);
122+
"footer-references-existence": ({ body }: { body: any }) => {
123+
let bodyStr = Helpers.convertAnyToString(body, "body");
124+
125+
return Plugins.footerReferencesExistence(bodyStr);
101126
},
102127

103128
"prefer-slash-over-backslash": ({
104129
header,
105130
}: {
106131
header: any;
107132
}) => {
108-
let headerStr = Helpers.convertAnyToString(
133+
let headerUncastedStr = Helpers.convertAnyToString(
109134
header,
110135
"header"
111136
);
137+
let headerStr = Helpers.assertNotNull(
138+
headerUncastedStr,
139+
notNullStringErrorMessage("header")
140+
);
141+
112142
return Plugins.preferSlashOverBackslash(headerStr);
113143
},
114144

115145
"proper-issue-refs": ({ raw }: { raw: any }) => {
116-
let rawStr = Helpers.convertAnyToString(raw, "raw").trim();
146+
let rawUncastedStr = Helpers.convertAnyToString(raw, "raw");
147+
let rawStr = Helpers.assertNotNull(
148+
rawUncastedStr,
149+
notNullStringErrorMessage("raw")
150+
).trim();
151+
117152
return Plugins.properIssueRefs(rawStr);
118153
},
119154

120155
"title-uppercase": ({ header }: { header: any }) => {
121-
let headerStr = Helpers.convertAnyToString(
156+
let headerUncastedStr = Helpers.convertAnyToString(
122157
header,
123158
"header"
124159
);
160+
let headerStr = Helpers.assertNotNull(
161+
headerUncastedStr,
162+
notNullStringErrorMessage("header")
163+
);
164+
125165
return Plugins.titleUppercase(headerStr);
126166
},
127167

128168
"too-many-spaces": ({ raw }: { raw: any }) => {
129-
let rawStr = Helpers.convertAnyToString(raw, "raw");
169+
let rawUncastedStr = Helpers.convertAnyToString(raw, "raw");
170+
let rawStr = Helpers.assertNotNull(
171+
rawUncastedStr,
172+
notNullStringErrorMessage("raw")
173+
);
174+
130175
return Plugins.tooManySpaces(rawStr);
131176
},
132177

133178
"type-space-after-colon": ({ header }: { header: any }) => {
134-
let headerStr = Helpers.convertAnyToString(
179+
let headerUncastedStr = Helpers.convertAnyToString(
135180
header,
136181
"header"
137182
);
183+
let headerStr = Helpers.assertNotNull(
184+
headerUncastedStr,
185+
notNullStringErrorMessage("header")
186+
);
187+
138188
return Plugins.typeSpaceAfterColon(headerStr);
139189
},
140190

141191
"type-with-square-brackets": ({ header }: { header: any }) => {
142-
let headerStr = Helpers.convertAnyToString(
192+
let headerUncastedStr = Helpers.convertAnyToString(
143193
header,
144194
"header"
145195
);
196+
let headerStr = Helpers.assertNotNull(
197+
headerUncastedStr,
198+
notNullStringErrorMessage("header")
199+
);
200+
146201
return Plugins.typeWithSquareBrackets(headerStr);
147202
},
148203

149204
// NOTE: we use 'header' instead of 'subject' as a workaround to this bug: https://github.com/conventional-changelog/commitlint/issues/3404
150205
"subject-lowercase": ({ header }: { header: any }) => {
151-
let headerStr = Helpers.convertAnyToString(
206+
let headerUncastedStr = Helpers.convertAnyToString(
152207
header,
153208
"header"
154209
);
210+
let headerStr = Helpers.assertNotNull(
211+
headerUncastedStr,
212+
notNullStringErrorMessage("header")
213+
);
155214
return Plugins.subjectLowercase(headerStr);
156215
},
157216

158217
"type-space-after-comma": ({ header }: { header: any }) => {
159-
let headerStr = Helpers.convertAnyToString(
218+
let headerUncastedStr = Helpers.convertAnyToString(
160219
header,
161220
"header"
162221
);
222+
let headerStr = Helpers.assertNotNull(
223+
headerUncastedStr,
224+
notNullStringErrorMessage("header")
225+
);
226+
163227
return Plugins.typeSpaceAfterComma(headerStr);
164228
},
165229

166230
"body-soft-max-line-length": (
167-
{ raw }: { raw: any },
231+
{ body }: { body: any },
168232
_: any,
169233
maxLineLength: number
170234
) => {
171-
let rawStr = Helpers.convertAnyToString(raw, "raw").trim();
172-
return Plugins.bodySoftMaxLineLength(rawStr, maxLineLength);
235+
let bodyStr = Helpers.convertAnyToString(body, "body");
236+
return Plugins.bodySoftMaxLineLength(
237+
bodyStr,
238+
maxLineLength
239+
);
173240
},
174241

175242
"trailing-whitespace": ({ raw }: { raw: any }) => {
176-
let rawStr = Helpers.convertAnyToString(raw, "raw");
243+
let rawUncastedStr = Helpers.convertAnyToString(raw, "raw");
244+
let rawStr = Helpers.assertNotNull(
245+
rawUncastedStr,
246+
notNullStringErrorMessage("raw")
247+
);
248+
177249
return Plugins.trailingWhitespace(rawStr);
178250
},
179251

180252
"type-space-before-paren": ({ header }: { header: any }) => {
181-
let headerStr = Helpers.convertAnyToString(
253+
let headerUncastedStr = Helpers.convertAnyToString(
182254
header,
183255
"header"
184256
);
257+
let headerStr = Helpers.assertNotNull(
258+
headerUncastedStr,
259+
notNullStringErrorMessage("header")
260+
);
261+
185262
return Plugins.typeSpaceBeforeParen(headerStr);
186263
},
187264
},

commitlint.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ set -euxo pipefail
33

44
# cd to directory of this script
55
cd "$(dirname "$0")"
6-
npm install
6+
npm install conventional-changelog-conventionalcommits
7+
npm install commitlint@latest
78
npx commitlint --version
89
npx commitlint $@
910
cd ..

commitlint/helpers.ts

+12-8
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,24 @@ export abstract class Helpers {
1919
public static convertAnyToString(
2020
potentialString: any,
2121
paramName: string
22-
): string {
22+
): string | null {
2323
if (potentialString === null || potentialString === undefined) {
2424
// otherwise, String(null) might give us the stupid string "null"
25-
throw new Error(
26-
"Unexpected " +
27-
paramName +
28-
"===null or " +
29-
paramName +
30-
"===undefined happened"
31-
);
25+
return null;
3226
}
3327
return String(potentialString);
3428
}
3529

30+
public static assertNotNull(
31+
text: string | null,
32+
errorMessage: string
33+
): string {
34+
if (text === null) {
35+
throw new Error(errorMessage);
36+
}
37+
return text as string;
38+
}
39+
3640
public static assertCharacter(letter: string) {
3741
if (letter.length !== 1) {
3842
throw Error("This function expects a character as input");

0 commit comments

Comments
 (0)