Skip to content

Commit

Permalink
fix(load): resolve nested parser preset factories (#831)
Browse files Browse the repository at this point in the history
* test: add failing testcase for nested parser preset factories

* fix(load): resolve nested parser preset factories

* refactor(load): remove old console log statement

* refactor(load): remove extraneous white line
  • Loading branch information
byCedric authored Oct 16, 2019
1 parent e2edc43 commit 73a7df7
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
extends: ['./first-extended']
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
extends: ['./second-extended']
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = Promise.resolve().then(
() =>
function factory() {
return {
parserOpts: {
headerPattern: /^(\w*)(?:\((.*)\))?-(.*)$/
}
};
}
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
parserPreset: './conventional-changelog-factory'
};
14 changes: 10 additions & 4 deletions @commitlint/load/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ export default async (seed = {}, options = {cwd: process.cwd()}) => {
pick(config, 'extends', 'plugins', 'ignores', 'defaultIgnores')
);

// Resolve parserPreset key
// Resolve parserPreset key from flat-non-extended config
if (typeof config.parserPreset === 'string') {
const resolvedParserPreset = resolveFrom(base, config.parserPreset);
let resolvedParserConfig = await require(resolvedParserPreset);

// Resolve loaded parser preset if its a factory
// Resolve loaded parser preset factory
if (typeof resolvedParserConfig === 'function') {
resolvedParserConfig = await resolvedParserConfig();
}
Expand All @@ -61,8 +61,14 @@ export default async (seed = {}, options = {cwd: process.cwd()}) => {
typeof preset.parserPreset.parserOpts === 'object' &&
typeof preset.parserPreset.parserOpts.then === 'function'
) {
preset.parserPreset.parserOpts = (await preset.parserPreset
.parserOpts).parserOpts;
let parserPreset = await preset.parserPreset.parserOpts;

// Resolve loaded parser preset factory from extended config
if (typeof parserPreset === 'function') {
parserPreset = await parserPreset();
}

preset.parserPreset.parserOpts = parserPreset.parserOpts;
}

// Resolve config-relative formatter module
Expand Down
10 changes: 10 additions & 0 deletions @commitlint/load/src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,16 @@ test('recursive extends with parserPreset', async t => {
);
});

test('recursive extends with parserPreset factory', async t => {
const cwd = await git.bootstrap('fixtures/recursive-parser-preset-factory');
const actual = await load({}, {cwd});

t.is(actual.parserPreset.name, './conventional-changelog-factory');
t.deepEqual(actual.parserPreset.parserOpts, {
headerPattern: /^(\w*)(?:\((.*)\))?-(.*)$/
});
});

test('ignores unknow keys', async t => {
const cwd = await git.bootstrap('fixtures/trash-file');
const actual = await load({}, {cwd});
Expand Down

0 comments on commit 73a7df7

Please sign in to comment.