Skip to content

Commit d0f0eb9

Browse files
juliusccJulius CelikbyCedric
authoredJun 29, 2020
fix(load): resolve plugins from extended configs (#1976)
* test(load): adds test for loading plugins from an extended config * fix(load): loads plugins from extended configs * refactor: clean up a loading test * test(load): adds test for including a shareable config with a local plugin * test: adds more realistic test-scenarios * Update @commitlint/load/src/load.ts Co-authored-by: Cedric van Putten <me@bycedric.com> Co-authored-by: Julius Celik <julius.celik@digitalroute.com> Co-authored-by: Cedric van Putten <me@bycedric.com>
1 parent 50ae7c1 commit d0f0eb9

File tree

8 files changed

+64
-0
lines changed

8 files changed

+64
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
extends: ['./config-with-local-plugin'],
3+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module.exports = {
2+
extends: [],
3+
plugins: [{
4+
rules: {
5+
'hello-world-rule': ({ subject }) => {
6+
const HELLO_WORLD = 'Hello World';
7+
return [
8+
subject.includes(HELLO_WORLD),
9+
`Your subject should contain ${HELLO_WORLD} message`
10+
];
11+
},
12+
'is-positive': ({ subject }) => {
13+
const POSITIVE_EMOJI = ':)';
14+
return [
15+
subject.includes(POSITIVE_EMOJI),
16+
`Your subject should contain ${POSITIVE_EMOJI} message`
17+
];
18+
}
19+
}
20+
}]
21+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./commitlint.config.js');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
extends: ['./config-with-plugins'],
3+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
plugins: ['example', '@scope/example']
3+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./commitlint.config.js');

‎@commitlint/load/src/load.test.ts

+26
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,32 @@ test('plugins should be loaded from config', async () => {
9595
});
9696
});
9797

98+
test('plugins should be loaded from shareable config', async () => {
99+
const cwd = await gitBootstrap('fixtures/extends-with-plugins');
100+
const actual = await load({}, {cwd});
101+
102+
expect(actual.plugins).toMatchObject({
103+
example: plugin,
104+
'@scope/example': scopedPlugin
105+
});
106+
});
107+
108+
test('local plugins should be loaded from shareable configs', async () => {
109+
const cwd = await gitBootstrap('fixtures/extends-with-local-plugins');
110+
const actual = await load({}, {cwd});
111+
112+
expect(actual.plugins).toEqual(
113+
expect.objectContaining({
114+
local: {
115+
rules: {
116+
'hello-world-rule': expect.any(Function),
117+
'is-positive': expect.any(Function)
118+
}
119+
}
120+
})
121+
);
122+
});
123+
98124
test('uses seed with parserPreset', async () => {
99125
const cwd = await gitBootstrap('fixtures/parser-preset');
100126
const {parserPreset: actual} = await load(

‎@commitlint/load/src/load.ts

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Path from 'path';
33
import merge from 'lodash/merge';
44
import mergeWith from 'lodash/mergeWith';
55
import pick from 'lodash/pick';
6+
import union from 'lodash/union';
67
import resolveFrom from 'resolve-from';
78

89
import executeRule from '@commitlint/execute-rule';
@@ -84,6 +85,11 @@ export default async function load(
8485
resolveFrom.silent(base, config.formatter) || config.formatter;
8586
}
8687

88+
// Read plugins from extends
89+
if (Array.isArray(extended.plugins)) {
90+
config.plugins = union(config.plugins, extended.plugins || []);
91+
}
92+
8793
// resolve plugins
8894
if (Array.isArray(config.plugins)) {
8995
config.plugins.forEach(plugin => {

0 commit comments

Comments
 (0)
Please sign in to comment.