Skip to content

Commit

Permalink
fix(load): use Rule | AsyncRule | SyncRule as rule value type in `P…
Browse files Browse the repository at this point in the history
…lugin` (#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.
  • Loading branch information
jdbruijn authored Jan 22, 2021
1 parent ef8bdad commit 75b67b8
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
49 changes: 49 additions & 0 deletions @commitlint/load/src/utils/load-plugin.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import loadPlugin from './load-plugin';
import {AsyncRule, Plugin, Rule, SyncRule} from '@commitlint/types';

jest.mock('commitlint-plugin-example', () => ({example: true}), {
virtual: true,
Expand All @@ -8,6 +9,39 @@ jest.mock('@scope/commitlint-plugin-example', () => ({scope: true}), {
virtual: true,
});

jest.mock(
'commitlint-plugin-rule',
(): Plugin => {
const rule: Rule<number> = (_parsed, when, _value) => {
return [when === 'never'];
};
return {rules: {rule}};
},
{virtual: true}
);

jest.mock(
'commitlint-plugin-sync-rule',
(): Plugin => {
const syncRule: SyncRule<number> = (_parsed, when, _value) => {
return [when === 'never'];
};
return {rules: {syncRule}};
},
{virtual: true}
);

jest.mock(
'commitlint-plugin-async-rule',
(): Plugin => {
const asyncRule: AsyncRule<number> = (_parsed, when, _value) => {
return new Promise(() => [when === 'never']);
};
return {rules: {asyncRule}};
},
{virtual: true}
);

test('should load a plugin when referenced by short name', () => {
const plugins = loadPlugin({}, 'example');
expect(plugins['example']).toBe(require('commitlint-plugin-example'));
Expand All @@ -18,6 +52,21 @@ test('should load a plugin when referenced by long name', () => {
expect(plugins['example']).toBe(require('commitlint-plugin-example'));
});

test('should load a plugin with a rule', () => {
const plugins = loadPlugin({}, 'commitlint-plugin-rule');
expect(plugins['rule']).toBe(require('commitlint-plugin-rule'));
});

test('should load a plugin with a sync rule', () => {
const plugins = loadPlugin({}, 'commitlint-plugin-sync-rule');
expect(plugins['sync-rule']).toBe(require('commitlint-plugin-sync-rule'));
});

test('should load a plugin with an async rule', () => {
const plugins = loadPlugin({}, 'commitlint-plugin-async-rule');
expect(plugins['async-rule']).toBe(require('commitlint-plugin-async-rule'));
});

test('should throw an error when a plugin has whitespace', () => {
expect(() => loadPlugin({}, 'whitespace ')).toThrow(
"Whitespace found in plugin name 'whitespace '"
Expand Down
10 changes: 8 additions & 2 deletions @commitlint/types/src/load.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import {Rule, RulesConfig, RuleConfigQuality} from './rules';
import {
Rule,
RulesConfig,
RuleConfigQuality,
AsyncRule,
SyncRule,
} from './rules';

export type PluginRecords = Record<string, Plugin>;

export interface Plugin {
rules: {
[ruleName: string]: Rule<unknown>;
[ruleName: string]: Rule | AsyncRule | SyncRule;
};
}

Expand Down

0 comments on commit 75b67b8

Please sign in to comment.