Skip to content

Commit 065a93f

Browse files
authored
Merge pull request #666 from arvinxx/beta
🐛 fix: Fix gitmoji unicode regex
2 parents bcd87e6 + d3f868d commit 065a93f

File tree

7 files changed

+64
-24
lines changed

7 files changed

+64
-24
lines changed

packages/commitlint-config/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## commitlint-config-gitmoji [2.2.11](https://github.com/arvinxx/gitmoji-commit-workflow/compare/[email protected]@2.2.11) (2023-02-10)
4+
5+
### Dependencies
6+
7+
- **commitlint-plugin-gitmoji:** upgraded to 2.2.5
8+
39
## commitlint-config-gitmoji [2.2.10](https://github.com/arvinxx/gitmoji-commit-workflow/compare/[email protected]@2.2.10) (2023-01-14)
410

511
### Dependencies

packages/commitlint-config/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "commitlint-config-gitmoji",
3-
"version": "2.2.10",
3+
"version": "2.2.11",
44
"description": "shareable commitlint config enforcing gitmoji commit message",
55
"keywords": [
66
"commitlint",
@@ -31,7 +31,7 @@
3131
"@commitlint/types": "^17",
3232
"@gitmoji/commit-types": "1.1.5",
3333
"@gitmoji/parser-opts": "1.3.1",
34-
"commitlint-plugin-gitmoji": "2.2.4"
34+
"commitlint-plugin-gitmoji": "2.2.5"
3535
},
3636
"publishConfig": {
3737
"access": "public",

packages/commitlint-plugin/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## commitlint-plugin-gitmoji [2.2.5](https://github.com/arvinxx/gitmoji-commit-workflow/compare/[email protected]@2.2.5) (2023-02-10)
4+
5+
### 🐛 Bug Fixes
6+
7+
- try to fix gitmoji unicode regex ([acd12b1](https://github.com/arvinxx/gitmoji-commit-workflow/commit/acd12b1)), closes [#662](https://github.com/arvinxx/gitmoji-commit-workflow/issues/662) [#56](https://github.com/arvinxx/gitmoji-commit-workflow/issues/56) [#493](https://github.com/arvinxx/gitmoji-commit-workflow/issues/493)
8+
39
## commitlint-plugin-gitmoji [2.2.4](https://github.com/arvinxx/gitmoji-commit-workflow/compare/[email protected]@2.2.4) (2023-01-14)
410

511
### 🐛 Bug Fixes

packages/commitlint-plugin/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "commitlint-plugin-gitmoji",
3-
"version": "2.2.4",
3+
"version": "2.2.5",
44
"description": "shareable commitlint plugin enforcing gitmoji commit rules",
55
"keywords": [
66
"commitlint",
@@ -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)