Skip to content

Commit 04a8d99

Browse files
committed
commitlint.config: remove workaround for body
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
1 parent 60929f3 commit 04a8d99

File tree

3 files changed

+158
-94
lines changed

3 files changed

+158
-94
lines changed

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: {
@@ -59,20 +63,35 @@ module.exports = {
5963
{
6064
rules: {
6165
"body-prose": ({ raw }: { raw: any }) => {
62-
let rawStr = Helpers.convertAnyToString(raw, "raw").trim();
66+
let rawUncastedStr = Helpers.convertAnyToString(raw, "raw");
67+
let rawStr = Helpers.assertNotNull(
68+
rawUncastedStr,
69+
notNullStringErrorMessage("raw")
70+
).trim();
71+
6372
return Plugins.bodyProse(rawStr);
6473
},
6574

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

7185
"empty-wip": ({ header }: { header: any }) => {
72-
let headerStr = Helpers.convertAnyToString(
86+
let headerUncastedStr = Helpers.convertAnyToString(
7387
header,
7488
"header"
7589
);
90+
let headerStr = Helpers.assertNotNull(
91+
headerUncastedStr,
92+
notNullStringErrorMessage("header")
93+
);
94+
7695
return Plugins.emptyWip(headerStr);
7796
},
7897

@@ -81,108 +100,166 @@ module.exports = {
81100
_: any,
82101
maxLineLength: number
83102
) => {
84-
let headerStr = Helpers.convertAnyToString(
103+
let headerUncastedStr = Helpers.convertAnyToString(
85104
header,
86105
"header"
87106
);
107+
let headerStr = Helpers.assertNotNull(
108+
headerUncastedStr,
109+
notNullStringErrorMessage("header")
110+
);
111+
88112
return Plugins.headerMaxLengthWithSuggestions(
89113
headerStr,
90114
maxLineLength
91115
);
92116
},
93117

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

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

104129
"prefer-slash-over-backslash": ({
105130
header,
106131
}: {
107132
header: any;
108133
}) => {
109-
let headerStr = Helpers.convertAnyToString(
134+
let headerUncastedStr = Helpers.convertAnyToString(
110135
header,
111136
"header"
112137
);
138+
let headerStr = Helpers.assertNotNull(
139+
headerUncastedStr,
140+
notNullStringErrorMessage("header")
141+
);
142+
113143
return Plugins.preferSlashOverBackslash(headerStr);
114144
},
115145

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

121156
"title-uppercase": ({ header }: { header: any }) => {
122-
let headerStr = Helpers.convertAnyToString(
157+
let headerUncastedStr = Helpers.convertAnyToString(
123158
header,
124159
"header"
125160
);
161+
let headerStr = Helpers.assertNotNull(
162+
headerUncastedStr,
163+
notNullStringErrorMessage("header")
164+
);
165+
126166
return Plugins.titleUppercase(headerStr);
127167
},
128168

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

134179
"type-space-after-colon": ({ header }: { header: any }) => {
135-
let headerStr = Helpers.convertAnyToString(
180+
let headerUncastedStr = Helpers.convertAnyToString(
136181
header,
137182
"header"
138183
);
184+
let headerStr = Helpers.assertNotNull(
185+
headerUncastedStr,
186+
notNullStringErrorMessage("header")
187+
);
188+
139189
return Plugins.typeSpaceAfterColon(headerStr);
140190
},
141191

142192
"type-with-square-brackets": ({ header }: { header: any }) => {
143-
let headerStr = Helpers.convertAnyToString(
193+
let headerUncastedStr = Helpers.convertAnyToString(
144194
header,
145195
"header"
146196
);
197+
let headerStr = Helpers.assertNotNull(
198+
headerUncastedStr,
199+
notNullStringErrorMessage("header")
200+
);
201+
147202
return Plugins.typeWithSquareBrackets(headerStr);
148203
},
149204

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

159218
"type-space-after-comma": ({ header }: { header: any }) => {
160-
let headerStr = Helpers.convertAnyToString(
219+
let headerUncastedStr = Helpers.convertAnyToString(
161220
header,
162221
"header"
163222
);
223+
let headerStr = Helpers.assertNotNull(
224+
headerUncastedStr,
225+
notNullStringErrorMessage("header")
226+
);
227+
164228
return Plugins.typeSpaceAfterComma(headerStr);
165229
},
166230

167231
"body-soft-max-line-length": (
168-
{ raw }: { raw: any },
232+
{ body }: { body: any },
169233
_: any,
170234
maxLineLength: number
171235
) => {
172-
let rawStr = Helpers.convertAnyToString(raw, "raw").trim();
173-
return Plugins.bodySoftMaxLineLength(rawStr, maxLineLength);
236+
let bodyStr = Helpers.convertAnyToString(body, "body");
237+
return Plugins.bodySoftMaxLineLength(
238+
bodyStr,
239+
maxLineLength
240+
);
174241
},
175242

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

181253
"type-space-before-paren": ({ header }: { header: any }) => {
182-
let headerStr = Helpers.convertAnyToString(
254+
let headerUncastedStr = Helpers.convertAnyToString(
183255
header,
184256
"header"
185257
);
258+
let headerStr = Helpers.assertNotNull(
259+
headerUncastedStr,
260+
notNullStringErrorMessage("header")
261+
);
262+
186263
return Plugins.typeSpaceBeforeParen(headerStr);
187264
},
188265
},

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)