Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/__tests__/isValidCommitMessage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ test("should be able to correctly validate the commit message", () => {
expect(isValidCommitMessage("fix: menu must open on shortcut press")).toBe(true);
expect(isValidCommitMessage("something: should not work")).toBe(false);
expect(isValidCommitMessage("fixes something")).toBe(false);
expect(isValidCommitMessage("🚧 fix: menu must open on shortcut press")).toBe(true);
expect(isValidCommitMessage("🚧 fix: menu must open on shortcut press")).toBe(false);
expect(isValidCommitMessage("fix(menus): menu must open on shortcut press")).toBe(true);
expect(isValidCommitMessage("🚧 fix(menus): menu must open on shortcut press")).toBe(true);
expect(isValidCommitMessage("🚧 fix(menus): menu must open on shortcut press")).toBe(false);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel that adding emojis to commit messages should be fine as long as the rest of the commit messages adheres to the conventional commits semantics.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this plugin should match the spec as close as possible and I'm not sure emojis are supported: https://www.conventionalcommits.org/en/v1.0.0/#specification

expect(isValidCommitMessage("🚧 fixing something")).toBe(false);
expect(isValidCommitMessage("🚧 something: should not work")).toBe(false);
expect(isValidCommitMessage("chorz: 123")).toBe(false);
expect(isValidCommitMessage("(chorz:) 123")).toBe(false);
expect(isValidCommitMessage("fix: test 🐛 with icon")).toBe(true);
expect(isValidCommitMessage("fix: test 🐛 with icon")).toBe(false);
expect(isValidCommitMessage("fix: 🐛 test with icon")).toBe(true);
});
36 changes: 3 additions & 33 deletions src/isValidCommitMesage.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,12 @@
const DEFAULT_COMMIT_TYPES = [
"feat",
"fix",
"docs",
"style",
"refactor",
"test",
"build",
"perf",
"ci",
"chore",
"revert",
"merge",
"wip",
];

const isValidCommitMessage = (message, availableTypes = DEFAULT_COMMIT_TYPES): boolean => {
const isValidCommitMessage = (message): boolean => {
// Exceptions.
// This is a message that's auto-generated by git. Can't do much about it unfortunately. Let's allow it.
if (message.startsWith("Merge ") || message.startsWith("Revert ")) {
return true;
}

// Commit message doesn't fall into the exceptions group. Let's do the validation.
let [possiblyValidCommitType] = message.split(":");
possiblyValidCommitType = possiblyValidCommitType.toLowerCase();

// Let's remove scope if present.
if (possiblyValidCommitType.match(/\(\S*?\)/)) {
possiblyValidCommitType = possiblyValidCommitType.replace(/\(\S*?\)/, "");
}

possiblyValidCommitType = possiblyValidCommitType
.replace(/\s/g, "") // Remove all whitespace
.replace(/\!/g, "") // Remove bang for notify breaking change
.replace(/()/g, "") // Remove all whitespace
.replace(/[^a-z]/g, ""); // Only leave [a-z] characters.

return availableTypes.includes(possiblyValidCommitType);
let pattern = /^(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test){1}(\([\w\-\.]+\))?(!)?: \S+([\s\S]*)/

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather the DEFAULT_COMMIT_TYPES be as it is and use

commitPrefixes = DEFAULT_COMMIT_TYPES.join("|");

to build the pattern. This makes it easier to extend the list of valid commit prefixes and improves readability.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's worth noting this change is also not backwards compatible as the allowed types are no longer supported. I don't think this is a desirable change and if it were to proceed the PR should updated to feat! as it's a breaking change.

I believe it's possible to update the regex without removing the existing capabilities.

return pattern.test(message)
};

export default isValidCommitMessage;