Skip to content

Commit

Permalink
🐛 fix: try to fix gitmoji unicode regex
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Feb 10, 2023
1 parent bcd87e6 commit acd12b1
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 21 deletions.
4 changes: 2 additions & 2 deletions packages/commitlint-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
},
"dependencies": {
"@commitlint/types": "^17",
"emoji-name-map": "^1.2.9",
"gitmojis": "3.13.2"
"emoji-regex": "^10",
"gitmojis": "^3"
},
"publishConfig": {
"access": "public",
Expand Down
6 changes: 2 additions & 4 deletions packages/commitlint-plugin/src/gitmojiCode.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import toEmoji from 'emoji-name-map';

const { gitmojis } = require('gitmojis');
import { gitmojis } from 'gitmojis';

export const gitmojiCodes: string[] = gitmojis.map((gitmoji) => gitmoji.code);

export const gitmojiUnicode: string[] = gitmojis.map((gitmoji) => toEmoji.get(gitmoji.code));
export const gitmojiUnicode: string[] = gitmojis.map((gitmoji) => gitmoji.emoji);
36 changes: 22 additions & 14 deletions packages/commitlint-plugin/src/rule.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,44 @@
import type { Rule } from '@commitlint/types';
import emojiRegex from 'emoji-regex';

import { gitmojiCodes, gitmojiUnicode } from './gitmojiCode';

const gitjimojiCodeStr = `:\\w*:`;
const gitmojiUnicodeStr = gitmojiUnicode.filter((i) => i).join('|');
const emojiStr = emojiRegex().source;

const emoji: Rule = (parsed) => {
const { raw } = parsed;

// code regex test url: https://regex101.com/r/fSdOvB/1
const regex = /^(:\w*:)\s.*/gm;
// unicode regex test url: https://regex101.com/r/OTMgWL/2
const unicodeRegex =
/(\ud83c[\udf00-\udfff]|\ud83d[\udc00-\ude4f\ude80-\udeff]|[\u2600-\u2B55])\s.*/gm;

const result = regex.exec(raw);
const unicodeResult = unicodeRegex.exec(raw);
const gitmojiCodeResult = new RegExp(`(${gitjimojiCodeStr})\\s.*`, 'gm').exec(raw);
// unicode regex test url: https://regex101.com/r/shBTBg/2
const gitmojiUnicodeResult = new RegExp(`(${gitmojiUnicodeStr})\\s.*`, 'gm').exec(raw);
const emojiResult = new RegExp(`(${emojiStr})\\s.*`, 'gm').exec(raw);

let pass;
let errorMsg = 'passed';

// if gitmoji code is valid
if (result) {
const emojiCode = result[1];
if (gitmojiCodeResult) {
const emojiCode = gitmojiCodeResult[1];
pass = gitmojiCodes.includes(emojiCode);
if (!pass) {
errorMsg = `${emojiCode} is not in the correct gitmoji list, please check the emoji code on https://gitmoji.dev/.`;
}
} else if (unicodeResult) {
const unicode = unicodeResult[1];
}
// if gitmoji unicode is valid
else if (gitmojiUnicodeResult) {
const unicode = gitmojiUnicodeResult[1];

pass = gitmojiUnicode.includes(unicode);
}
// is emoji,but isn't included in gitmoji list
else if (emojiResult) {
const unicode = emojiResult[1];

if (!pass) {
errorMsg = `${unicode} is not in the correct gitmoji list, please check the emoji code on https://gitmoji.dev/.`;
}
pass = false;
errorMsg = `${unicode} is not in the correct gitmoji list, please check the emoji code on https://gitmoji.dev/.`;
} else {
// if don't has gitmoji code or emoji unicode
pass = false;
Expand Down
24 changes: 23 additions & 1 deletion packages/commitlint-plugin/test/rule.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { Commit, RuleConfigCondition } from '@commitlint/types';
import { gitmojis } from 'gitmojis';

import emojiRule from '../src/rule';

const when: RuleConfigCondition = 'always';
Expand All @@ -25,7 +27,7 @@ describe('commit start with gitmoji code', () => {
const value = emojiRule({ raw: '🤔 chore(scope): test' } as Commit, when);
expect(value).toEqual([
false,
'Your commit should start with gitmoji code. Please check the emoji code on https://gitmoji.dev/.',
'🤔 is not in the correct gitmoji list, please check the emoji code on https://gitmoji.dev/.',
]);
});

Expand All @@ -42,6 +44,11 @@ describe('commit start with gitmoji code', () => {
expect(value).toEqual([true, 'passed']);
});

it(':construction_worker: should pass', () => {
const value = emojiRule({ raw: ':construction_worker: test' } as Commit, when);
expect(value).toEqual([true, 'passed']);
});

it('🎉 should pass', () => {
const value = emojiRule({ raw: '🎉 test' } as Commit, when);
expect(value).toEqual([true, 'passed']);
Expand All @@ -56,4 +63,19 @@ describe('commit start with gitmoji code', () => {
const value = emojiRule({ raw: '💄 test' } as Commit, when);
expect(value).toEqual([true, 'passed']);
});

it('⚡️should pass', () => {
const value = emojiRule({ raw: '⚡️ test' } as Commit, when);
expect(value).toEqual([true, 'passed']);
});

it('every emoji in list past', () => {
const gitmojiUnicode: string[] = gitmojis.map((gitmoji) => gitmoji.emoji);

gitmojiUnicode.forEach((unicode) => {
const value = emojiRule({ raw: `${unicode} test` } as Commit, when);
console.log(`testing ${unicode}...`);
expect(value).toEqual([true, 'passed']);
});
});
});

0 comments on commit acd12b1

Please sign in to comment.