Skip to content

Commit cfe2d56

Browse files
committed
commitlint.config: add proper-revert-message rule
1 parent 6e95392 commit cfe2d56

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

commitlint.config.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,12 @@ module.exports = {
4343
"too-many-spaces": [RuleConfigSeverity.Error, "always"],
4444
"commit-hash-alone": [RuleConfigSeverity.Error, "always"],
4545
"title-uppercase": [RuleConfigSeverity.Error, "always"],
46+
"proper-revert-message": [RuleConfigSeverity.Error, "always"],
4647
},
48+
49+
// Commitlint automatically ignores some kinds of commits like Revert commit messages.
50+
// We need to set this value to false to apply our rules on these messages.
51+
defaultIgnores: false,
4752
plugins: [
4853
// TODO (ideas for more rules):
4954
// * Detect if paragraphs in body have been cropped too shortly (less than 64 chars), and suggest same auto-wrap command that body-soft-max-line-length suggests, since it unwraps and wraps (both).
@@ -118,6 +123,24 @@ module.exports = {
118123
return Plugins.properIssueRefs(rawStr);
119124
},
120125

126+
"proper-revert-message": ({
127+
header,
128+
body,
129+
}: {
130+
header: any;
131+
body: any;
132+
}) => {
133+
let bodyStr = Helpers.convertAnyToString(
134+
body,
135+
"body"
136+
).trim();
137+
let headerStr = Helpers.convertAnyToString(
138+
header,
139+
"header"
140+
).trim();
141+
return Plugins.properRevertMessage(headerStr, bodyStr);
142+
},
143+
121144
"title-uppercase": ({ header }: { header: any }) => {
122145
let headerStr = Helpers.convertAnyToString(
123146
header,

commitlint/helpers.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,7 @@ export abstract class Helpers {
2222
): string {
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 "";
3226
}
3327
return String(potentialString);
3428
}

commitlint/plugins.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,24 @@ export abstract class Plugins {
272272
];
273273
}
274274

275+
public static properRevertMessage(headerStr: string, bodyStr: string) {
276+
let offence = false;
277+
278+
if (headerStr.match(/^[Rr]evert ".+"$/) !== null) {
279+
// does msg have a body?
280+
if (bodyStr !== "") {
281+
let lines = bodyStr.split("\n");
282+
offence =
283+
lines[0].match(/^This reverts commit [^ ]{40}\.$/) !==
284+
null && lines.length == 1;
285+
} else {
286+
offence = true;
287+
}
288+
}
289+
290+
return [!offence, `Please explain why you're reverting`];
291+
}
292+
275293
public static titleUppercase(headerStr: string) {
276294
let firstWord = headerStr.split(" ")[0];
277295
let offence =

0 commit comments

Comments
 (0)