Skip to content

Commit acd12b1

Browse files
committed
🐛 fix: try to fix gitmoji unicode regex
#662 #56 #493
1 parent bcd87e6 commit acd12b1

File tree

4 files changed

+49
-21
lines changed

4 files changed

+49
-21
lines changed

packages/commitlint-plugin/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
},
3333
"dependencies": {
3434
"@commitlint/types": "^17",
35-
"emoji-name-map": "^1.2.9",
36-
"gitmojis": "3.13.2"
35+
"emoji-regex": "^10",
36+
"gitmojis": "^3"
3737
},
3838
"publishConfig": {
3939
"access": "public",
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import toEmoji from 'emoji-name-map';
2-
3-
const { gitmojis } = require('gitmojis');
1+
import { gitmojis } from 'gitmojis';
42

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

7-
export const gitmojiUnicode: string[] = gitmojis.map((gitmoji) => toEmoji.get(gitmoji.code));
5+
export const gitmojiUnicode: string[] = gitmojis.map((gitmoji) => gitmoji.emoji);

packages/commitlint-plugin/src/rule.ts

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,44 @@
11
import type { Rule } from '@commitlint/types';
2+
import emojiRegex from 'emoji-regex';
3+
24
import { gitmojiCodes, gitmojiUnicode } from './gitmojiCode';
35

6+
const gitjimojiCodeStr = `:\\w*:`;
7+
const gitmojiUnicodeStr = gitmojiUnicode.filter((i) => i).join('|');
8+
const emojiStr = emojiRegex().source;
9+
410
const emoji: Rule = (parsed) => {
511
const { raw } = parsed;
612

713
// code regex test url: https://regex101.com/r/fSdOvB/1
8-
const regex = /^(:\w*:)\s.*/gm;
9-
// unicode regex test url: https://regex101.com/r/OTMgWL/2
10-
const unicodeRegex =
11-
/(\ud83c[\udf00-\udfff]|\ud83d[\udc00-\ude4f\ude80-\udeff]|[\u2600-\u2B55])\s.*/gm;
12-
13-
const result = regex.exec(raw);
14-
const unicodeResult = unicodeRegex.exec(raw);
14+
const gitmojiCodeResult = new RegExp(`(${gitjimojiCodeStr})\\s.*`, 'gm').exec(raw);
15+
// unicode regex test url: https://regex101.com/r/shBTBg/2
16+
const gitmojiUnicodeResult = new RegExp(`(${gitmojiUnicodeStr})\\s.*`, 'gm').exec(raw);
17+
const emojiResult = new RegExp(`(${emojiStr})\\s.*`, 'gm').exec(raw);
1518

1619
let pass;
1720
let errorMsg = 'passed';
1821

1922
// if gitmoji code is valid
20-
if (result) {
21-
const emojiCode = result[1];
23+
if (gitmojiCodeResult) {
24+
const emojiCode = gitmojiCodeResult[1];
2225
pass = gitmojiCodes.includes(emojiCode);
2326
if (!pass) {
2427
errorMsg = `${emojiCode} is not in the correct gitmoji list, please check the emoji code on https://gitmoji.dev/.`;
2528
}
26-
} else if (unicodeResult) {
27-
const unicode = unicodeResult[1];
29+
}
30+
// if gitmoji unicode is valid
31+
else if (gitmojiUnicodeResult) {
32+
const unicode = gitmojiUnicodeResult[1];
2833

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

31-
if (!pass) {
32-
errorMsg = `${unicode} is not in the correct gitmoji list, please check the emoji code on https://gitmoji.dev/.`;
33-
}
40+
pass = false;
41+
errorMsg = `${unicode} is not in the correct gitmoji list, please check the emoji code on https://gitmoji.dev/.`;
3442
} else {
3543
// if don't has gitmoji code or emoji unicode
3644
pass = false;

packages/commitlint-plugin/test/rule.test.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import type { Commit, RuleConfigCondition } from '@commitlint/types';
2+
import { gitmojis } from 'gitmojis';
3+
24
import emojiRule from '../src/rule';
35

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

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

47+
it(':construction_worker: should pass', () => {
48+
const value = emojiRule({ raw: ':construction_worker: test' } as Commit, when);
49+
expect(value).toEqual([true, 'passed']);
50+
});
51+
4552
it('🎉 should pass', () => {
4653
const value = emojiRule({ raw: '🎉 test' } as Commit, when);
4754
expect(value).toEqual([true, 'passed']);
@@ -56,4 +63,19 @@ describe('commit start with gitmoji code', () => {
5663
const value = emojiRule({ raw: '💄 test' } as Commit, when);
5764
expect(value).toEqual([true, 'passed']);
5865
});
66+
67+
it('⚡️should pass', () => {
68+
const value = emojiRule({ raw: '⚡️ test' } as Commit, when);
69+
expect(value).toEqual([true, 'passed']);
70+
});
71+
72+
it('every emoji in list past', () => {
73+
const gitmojiUnicode: string[] = gitmojis.map((gitmoji) => gitmoji.emoji);
74+
75+
gitmojiUnicode.forEach((unicode) => {
76+
const value = emojiRule({ raw: `${unicode} test` } as Commit, when);
77+
console.log(`testing ${unicode}...`);
78+
expect(value).toEqual([true, 'passed']);
79+
});
80+
});
5981
});

0 commit comments

Comments
 (0)