Skip to content

Commit 75b67b8

Browse files
authored
fix(load): use Rule | AsyncRule | SyncRule as rule value type in Plugin (#2146)
Use `Rule | AsyncRule | SyncRule` instead of `Rule<unknown>` so plugin developers don't need to cast every non-default rule's value to `Rule<unknown>`, but can define and assign their rules as `Rule | AsyncRule | SyncRule` instead.
1 parent ef8bdad commit 75b67b8

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

@commitlint/load/src/utils/load-plugin.test.ts

+49
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import loadPlugin from './load-plugin';
2+
import {AsyncRule, Plugin, Rule, SyncRule} from '@commitlint/types';
23

34
jest.mock('commitlint-plugin-example', () => ({example: true}), {
45
virtual: true,
@@ -8,6 +9,39 @@ jest.mock('@scope/commitlint-plugin-example', () => ({scope: true}), {
89
virtual: true,
910
});
1011

12+
jest.mock(
13+
'commitlint-plugin-rule',
14+
(): Plugin => {
15+
const rule: Rule<number> = (_parsed, when, _value) => {
16+
return [when === 'never'];
17+
};
18+
return {rules: {rule}};
19+
},
20+
{virtual: true}
21+
);
22+
23+
jest.mock(
24+
'commitlint-plugin-sync-rule',
25+
(): Plugin => {
26+
const syncRule: SyncRule<number> = (_parsed, when, _value) => {
27+
return [when === 'never'];
28+
};
29+
return {rules: {syncRule}};
30+
},
31+
{virtual: true}
32+
);
33+
34+
jest.mock(
35+
'commitlint-plugin-async-rule',
36+
(): Plugin => {
37+
const asyncRule: AsyncRule<number> = (_parsed, when, _value) => {
38+
return new Promise(() => [when === 'never']);
39+
};
40+
return {rules: {asyncRule}};
41+
},
42+
{virtual: true}
43+
);
44+
1145
test('should load a plugin when referenced by short name', () => {
1246
const plugins = loadPlugin({}, 'example');
1347
expect(plugins['example']).toBe(require('commitlint-plugin-example'));
@@ -18,6 +52,21 @@ test('should load a plugin when referenced by long name', () => {
1852
expect(plugins['example']).toBe(require('commitlint-plugin-example'));
1953
});
2054

55+
test('should load a plugin with a rule', () => {
56+
const plugins = loadPlugin({}, 'commitlint-plugin-rule');
57+
expect(plugins['rule']).toBe(require('commitlint-plugin-rule'));
58+
});
59+
60+
test('should load a plugin with a sync rule', () => {
61+
const plugins = loadPlugin({}, 'commitlint-plugin-sync-rule');
62+
expect(plugins['sync-rule']).toBe(require('commitlint-plugin-sync-rule'));
63+
});
64+
65+
test('should load a plugin with an async rule', () => {
66+
const plugins = loadPlugin({}, 'commitlint-plugin-async-rule');
67+
expect(plugins['async-rule']).toBe(require('commitlint-plugin-async-rule'));
68+
});
69+
2170
test('should throw an error when a plugin has whitespace', () => {
2271
expect(() => loadPlugin({}, 'whitespace ')).toThrow(
2372
"Whitespace found in plugin name 'whitespace '"

@commitlint/types/src/load.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1-
import {Rule, RulesConfig, RuleConfigQuality} from './rules';
1+
import {
2+
Rule,
3+
RulesConfig,
4+
RuleConfigQuality,
5+
AsyncRule,
6+
SyncRule,
7+
} from './rules';
28

39
export type PluginRecords = Record<string, Plugin>;
410

511
export interface Plugin {
612
rules: {
7-
[ruleName: string]: Rule<unknown>;
13+
[ruleName: string]: Rule | AsyncRule | SyncRule;
814
};
915
}
1016

0 commit comments

Comments
 (0)