Skip to content

Commit b1229ba

Browse files
authored
Merge pull request #50 from arvinxx/feat/support-unicode
Feature: support emoji unicode parsing close #13
2 parents 838d411 + 02b0477 commit b1229ba

File tree

11 files changed

+122
-21
lines changed

11 files changed

+122
-21
lines changed

packages/config/test/index.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ describe('invalid commit', () => {
3232
expect(valid).toBeFalsy();
3333
expect(errors).toHaveLength(3);
3434
});
35+
36+
it('$ 😂 test: test -> 3 error', async () => {
37+
const { valid, errors } = await lint('😂 test: test');
38+
39+
expect(valid).toBeFalsy();
40+
expect(errors).toHaveLength(1);
41+
});
3542
});
3643

3744
describe('valid commit', () => {
@@ -70,4 +77,12 @@ describe('valid commit', () => {
7077

7178
expect(valid).toBeTruthy();
7279
});
80+
81+
it('$ 💄 style(typography): 优化信息块和内联代码样式 -> passed', async () => {
82+
const { valid } = await lint(
83+
'💄 style(typography): 优化信息块和内联代码样式',
84+
);
85+
86+
expect(valid).toBeTruthy();
87+
});
7388
});

packages/parser-opts/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ this package is used in both [conventional-changelog-gitmoji-config](../changelo
88

99
## Sources
1010

11-
Header regex pattern test here : [Regex101](https://regex101.com/r/YxXWi5/10)
11+
Header regex pattern test here : [Regex101](https://regex101.com/r/YxXWi5/11)
1212

1313
```js
1414
module.exports = {
15-
headerPattern: /^(?::\w*:\s)?(?<type>\w*)(?:\((?<scope>.*)\))?!?:\s(?<subject>(?:(?!#).)*(?:(?!\s).))\s?(?<ticket>#\d*)?$/,
15+
headerPattern: /^(?::\w*:|(?:\ud83c[\udf00-\udfff])|(?:\ud83d[\udc00-\ude4f\ude80-\udeff])|[\u2600-\u2B55])\s(?<type>\w*)(?:\((?<scope>.*)\))?!?:\s(?<subject>(?:(?!#).)*(?:(?!\s).))\s?(?<ticket>#\d*)?$/,
1616
headerCorrespondence: ['type', 'scope', 'subject', 'ticket'],
1717
};
1818
```

packages/parser-opts/src/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
export default {
2-
// Test URL: https://regex101.com/r/YxXWi5/10
3-
// 相关 issue: 是否应该允许用中文? #https://github.com/arvinxx/gitmoji-commit-workflow/issues/8
4-
headerPattern: /^(?::\w*:\s)(?<type>\w*)(?:\((?<scope>.*)\))?!?:\s(?<subject>(?:(?!#).)*(?:(?!\s).))\s?(?<ticket>#\d*)?$/,
2+
// Test URL: https://regex101.com/r/YxXWi5/11
3+
headerPattern: /^(?::\w*:|(?:\ud83c[\udf00-\udfff])|(?:\ud83d[\udc00-\ude4f\ude80-\udeff])|[\u2600-\u2B55])\s(?<type>\w*)(?:\((?<scope>.*)\))?!?:\s(?<subject>(?:(?!#).)*(?:(?!\s).))\s?(?<ticket>#\d*)?$/,
54
headerCorrespondence: ['type', 'scope', 'subject', 'ticket'],
65
};

packages/parser-opts/tests/index.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ describe('@gitmoji/parser-opts', () => {
8282
expect(subject).toBe('extract parser-opts packages');
8383
expect(ticket).toBeUndefined();
8484
});
85+
8586
it(':sparkles: feat(changelog): 添加中文标题', () => {
8687
const result = regex.exec(':sparkles: feat(changelog): 添加中文标题');
8788

@@ -92,5 +93,16 @@ describe('@gitmoji/parser-opts', () => {
9293
expect(subject).toBe('添加中文标题');
9394
expect(ticket).toBeUndefined();
9495
});
96+
97+
it('💥 feat(unicode): support unicode', () => {
98+
const result = regex.exec('💥 feat(unicode): support unicode');
99+
100+
expect(result).toHaveLength(5);
101+
const { type, scope, subject, ticket } = result.groups;
102+
expect(type).toBe('feat');
103+
expect(scope).toBe('unicode');
104+
expect(subject).toBe('support unicode');
105+
expect(ticket).toBeUndefined();
106+
});
95107
});
96108
});

packages/plugin/.changelogrc.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@ const base = require('../../.changelogrc');
22

33
module.exports = {
44
...base,
5-
displayScopes: ['plugin'],
65
};

packages/plugin/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,8 @@
3636
"license": "MIT",
3737
"peerDependencies": {
3838
"commitlint": "^11.0.0"
39+
},
40+
"dependencies": {
41+
"emoji-name-map": "^1.2.9"
3942
}
4043
}

packages/plugin/src/gitmojiCode.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { join } from 'path';
22
import { existsSync, writeFileSync } from 'fs';
3+
import toEmoji from 'emoji-name-map';
34

45
const filePath = join(__dirname, 'gitmojis.json');
56

@@ -27,6 +28,8 @@ if (!existsSync(filePath)) {
2728
}
2829
// eslint-disable-next-line import/no-dynamic-require
2930
const { gitmojis } = require(filePath);
30-
const gitmojiCodes: string[] = gitmojis.map((gitmoji) => gitmoji.code);
31+
export const gitmojiCodes: string[] = gitmojis.map((gitmoji) => gitmoji.code);
3132

32-
export default gitmojiCodes;
33+
export const gitmojiUnicode: string[] = gitmojis.map((gitmoji) =>
34+
toEmoji.get(gitmoji.code),
35+
);

packages/plugin/src/rule.ts

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,40 @@
11
import type { Rule } from '@commitlint/types';
2-
import gitmojiCodes from './gitmojiCode';
2+
import { gitmojiCodes, gitmojiUnicode } from './gitmojiCode';
33

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

7-
const regex = /^(:\w*:)\s.*/gm; // regex test url: https://regex101.com/r/fSdOvB/1
7+
// 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 = /(\ud83c[\udf00-\udfff]|\ud83d[\udc00-\ude4f\ude80-\udeff]|[\u2600-\u2B55])\s.*/gm;
11+
812
const result = regex.exec(raw);
13+
const unicodeResult = unicodeRegex.exec(raw);
914

1015
let pass;
1116
let errorMsg = 'passed';
1217

13-
// if don't has emoji
14-
if (!result) {
15-
pass = false;
16-
errorMsg =
17-
'Your commit should start with gitmoji code,please check the emoji code on https://gitmoji.dev/.';
18-
} else {
18+
// if gitmoji code is valid
19+
if (result) {
1920
const emojiCode = result[1];
20-
2121
pass = gitmojiCodes.includes(emojiCode);
2222
if (!pass) {
2323
errorMsg = `${emojiCode} is not in the correct gitmoji list, please check the emoji code on https://gitmoji.dev/.`;
2424
}
25+
} else if (unicodeResult) {
26+
const unicode = unicodeResult[1];
27+
28+
pass = gitmojiUnicode.includes(unicode);
29+
30+
if (!pass) {
31+
errorMsg = `${unicode} is not in the correct gitmoji list, please check the emoji code on https://gitmoji.dev/.`;
32+
}
33+
} else {
34+
// if don't has gitmoji code or emoji unicode
35+
pass = false;
36+
errorMsg =
37+
'Your commit should start with gitmoji code,please check the emoji code on https://gitmoji.dev/.';
2538
}
2639

2740
return [pass, errorMsg];

packages/plugin/test/gitmojiCode.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ describe('gitmojiCodes work well', () => {
1010

1111
const gitmojiCodes = await import('../src/gitmojiCode');
1212

13-
expect(gitmojiCodes.default).toBeInstanceOf(Array);
13+
expect(gitmojiCodes.gitmojiCodes).toBeInstanceOf(Array);
1414
});
1515

1616
it('will download gitmoji json and write to file without gitmoji json', async () => {
1717
// 如果存在 gitmoji 直接删除
1818
if (existsSync(targetPath)) unlinkSync(targetPath);
1919
const gitmojiCodes = await import('../src/gitmojiCode');
20-
expect(gitmojiCodes.default).toBeInstanceOf(Array);
20+
expect(gitmojiCodes.gitmojiCodes).toBeInstanceOf(Array);
2121
});
2222
});
2323

@@ -36,7 +36,7 @@ describe('gitmojiCodes throw Error', () => {
3636
if (existsSync(targetPath)) unlinkSync(targetPath);
3737
try {
3838
const gitmojiCodes = await import('../src/gitmojiCode');
39-
expect(gitmojiCodes.default).toBeInstanceOf(Array);
39+
expect(gitmojiCodes.gitmojiCodes).toBeInstanceOf(Array);
4040
} catch (e) {
4141
expect(e).toEqual(
4242
Error(

packages/plugin/test/rule.test.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,39 @@ describe('commit start with gitmoji code', () => {
2121
]);
2222
});
2323

24-
it('should pass when return right commit message code', () => {
24+
it('🤔 should failed if commit start with unrecognized gitmoji unicode', () => {
25+
const value = emojiRule({ raw: '🤔 chore(scope): test' } as Commit, when);
26+
expect(value).toEqual([
27+
false,
28+
'Your commit should start with gitmoji code,please check the emoji code on https://gitmoji.dev/.',
29+
]);
30+
});
31+
32+
it('🌙 should failed if commit start with wrong gitmoji unicode', () => {
33+
const value = emojiRule({ raw: '🌙 chore(scope): test' } as Commit, when);
34+
expect(value).toEqual([
35+
false,
36+
'🌙 is not in the correct gitmoji list, please check the emoji code on https://gitmoji.dev/.',
37+
]);
38+
});
39+
40+
it('should pass when return correct commit message code', () => {
2541
const value = emojiRule({ raw: ':tada: test' } as Commit, when);
2642
expect(value).toEqual([true, 'passed']);
2743
});
44+
45+
it('🎉 should pass', () => {
46+
const value = emojiRule({ raw: '🎉 test' } as Commit, when);
47+
expect(value).toEqual([true, 'passed']);
48+
});
49+
50+
it('✅ should pass', () => {
51+
const value = emojiRule({ raw: '✅ test' } as Commit, when);
52+
expect(value).toEqual([true, 'passed']);
53+
});
54+
55+
it('💄 should pass', () => {
56+
const value = emojiRule({ raw: '💄 test' } as Commit, when);
57+
expect(value).toEqual([true, 'passed']);
58+
});
2859
});

0 commit comments

Comments
 (0)