Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix(load): use Rule | AsyncRule | SyncRule as rule value type in `P…
…lugin`

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 committed Jan 21, 2021
commit 8b778ccf13378836e56fa6f307856f6eb5d1148b
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