Skip to content

Commit 840640c

Browse files
author
Alan Agius
committed
feat(banned phrases): add banned-phrases rule
1 parent a969174 commit 840640c

4 files changed

Lines changed: 37 additions & 2 deletions

File tree

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ The majority of the rules can be applied in any part of the configuration.
2525
|--------------------|------------------------------------------------------------------|----------|---------|
2626
| `no-unscoped` | Disallows unscoped commit messages | boolean | Message |
2727
| `valid-types` | An array of allowed commit message types ex: `["feat", "chore"]` | string[] | Type |
28+
| `banned-phrases` | An array of disallowed phrases. (Case insensitive) | string[] | All |
2829
| `max-length` | Requires text to be under a certain max length | number | All |
2930
| `no-dash` | Disallows dashes | boolean | All |
3031
| `no-space` | Disallows spaces | boolean | All |
@@ -49,7 +50,15 @@ If the file is not found it will fallback to an internal `speedy-commit-msg.json
4950
{
5051
"rules": {
5152
"message": {
52-
"max-length": 75
53+
"max-length": 75,
54+
"banned-phrases": [
55+
"minor change",
56+
"minor fix",
57+
"minor refactor",
58+
"pr change",
59+
"pr comment",
60+
"following pr"
61+
]
5362
},
5463
"type": {
5564
"valid-types": [

config/speedy-commit-msg.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
{
22
"rules": {
33
"message": {
4-
"max-length": 75
4+
"max-length": 75,
5+
"banned-phrases": [
6+
"minor change",
7+
"minor fix",
8+
"minor refactor",
9+
"pr change",
10+
"pr comment",
11+
"following pr"
12+
]
513
},
614
"type": {
715
"valid-types": [

src/rules/rules.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@ describe("rulesSpec", () => {
55
const MESSAGE_PART: CommitMessagePart = "Type";
66
const COMMIT_MSG = "style(all): formatting";
77

8+
describe(Rules.bannedPhrases.name, () => {
9+
const bannedPhrases = ["pr change"];
10+
it("should fail when commit contain banned phrases", () => {
11+
expect(Rules.bannedPhrases("chore(all): PR changes", MESSAGE_PART, bannedPhrases).failed).toBe(true);
12+
});
13+
14+
it("should not fail when commit doesn't contain banned phrases", () => {
15+
expect(Rules.bannedPhrases(COMMIT_MSG, MESSAGE_PART, bannedPhrases).failed).toBe(false);
16+
});
17+
});
18+
819
describe(Rules.noUnscoped.name, () => {
920
it("should fail when commit message is not scope", () => {
1021
expect(Rules.noUnscoped("unscoped message").failed).toBe(true);

src/rules/rules.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ export namespace Rules {
66

77
export const SCOPED_COMMIT_REGEXP = /[a-z]+[\s]?\(.+\):/;
88

9+
export function bannedPhrases(text: string, part: CommitMessagePart, bannedPhrases: string[]): RulesResult {
10+
return {
11+
failed: new RegExp(`(${bannedPhrases.join("|")})`, "ig").test(text),
12+
message: `Commit '${part}' is not valid. You are using a 'Banned Phrase'. Banned phrases are: ${bannedPhrases.join(", ")}.`
13+
};
14+
}
15+
916
export function noUnscoped(text: string): RulesResult {
1017
return {
1118
failed: !SCOPED_COMMIT_REGEXP.test(text),

0 commit comments

Comments
 (0)