From 1353dd54c83ea2eb1f1287df3eab9cc60172a1d5 Mon Sep 17 00:00:00 2001 From: Mario Nebl Date: Tue, 26 Dec 2017 18:51:26 +0100 Subject: [PATCH] fix: correctly resolve parserOpts #115 #95 --- .../issue-prefixes/commitlint.config.js | 10 ++++++++ .../parser-preset/commitlint.config.js | 4 +--- @commitlint/cli/src/cli.js | 24 +++++++++---------- @commitlint/cli/src/cli.test.js | 6 +++++ @commitlint/core/src/load.js | 9 +++---- @commitlint/core/src/load.test.js | 21 +++++----------- @commitlint/resolve-extends/src/index.js | 3 +-- 7 files changed, 40 insertions(+), 37 deletions(-) create mode 100644 @commitlint/cli/fixtures/issue-prefixes/commitlint.config.js diff --git a/@commitlint/cli/fixtures/issue-prefixes/commitlint.config.js b/@commitlint/cli/fixtures/issue-prefixes/commitlint.config.js new file mode 100644 index 0000000000..c07ee41eb6 --- /dev/null +++ b/@commitlint/cli/fixtures/issue-prefixes/commitlint.config.js @@ -0,0 +1,10 @@ +module.exports = { + rules: { + 'references-empty': [2, 'never'] + }, + parserPreset: { + parserOpts: { + issuePrefixes: ['REF-'] + } + } +}; diff --git a/@commitlint/cli/fixtures/parser-preset/commitlint.config.js b/@commitlint/cli/fixtures/parser-preset/commitlint.config.js index eebcf63055..c37964c6f5 100644 --- a/@commitlint/cli/fixtures/parser-preset/commitlint.config.js +++ b/@commitlint/cli/fixtures/parser-preset/commitlint.config.js @@ -1,7 +1,5 @@ module.exports = { - parserOpts: { - parserPreset: './parser-preset' - }, + parserPreset: './parser-preset', rules: { 'type-enum': [2, 'always', ['type']], 'scope-enum': [2, 'always', ['scope']], diff --git a/@commitlint/cli/src/cli.js b/@commitlint/cli/src/cli.js index ece9b3cbd9..2b0b9d8c10 100755 --- a/@commitlint/cli/src/cli.js +++ b/@commitlint/cli/src/cli.js @@ -96,17 +96,17 @@ async function main(options) { throw err; } - return Promise.all( - messages.map(async message => { - const loaded = await core.load(getSeed(flags), {cwd: flags.cwd}); - const parserOpts = selectParserOpts(loaded.parserPreset); - const opts = parserOpts ? {parserOpts} : {parserOpts: {}}; + const loaded = await core.load(getSeed(flags), {cwd: flags.cwd}); + const parserOpts = selectParserOpts(loaded.parserPreset); + const opts = parserOpts ? {parserOpts} : {parserOpts: {}}; - // Strip comments if reading from `.git/COMMIT_EDIT_MSG` - if (range.edit) { - opts.parserOpts.commentChar = '#'; - } + // Strip comments if reading from `.git/COMMIT_EDIT_MSG` + if (range.edit) { + opts.parserOpts.commentChar = '#'; + } + return Promise.all( + messages.map(async message => { const report = await core.lint(message, loaded.rules, opts); const formatted = core.format(report, {color: flags.color}); @@ -182,13 +182,11 @@ function selectParserOpts(parserPreset) { return undefined; } - const opts = parserPreset.opts; - - if (typeof opts !== 'object') { + if (typeof parserPreset.parserOpts !== 'object') { return undefined; } - return opts.parserOpts; + return parserPreset.parserOpts; } // Catch unhandled rejections globally diff --git a/@commitlint/cli/src/cli.test.js b/@commitlint/cli/src/cli.test.js index 4567afdf13..9ca1b98ae2 100644 --- a/@commitlint/cli/src/cli.test.js +++ b/@commitlint/cli/src/cli.test.js @@ -182,6 +182,12 @@ test('should handle --amend with signoff', async () => { await execa('git', ['commit', '--amend', '--no-edit'], {cwd}); }); +test('should handle linting with issue prefixes', async t => { + const cwd = await git.bootstrap('fixtures/issue-prefixes'); + const actual = await cli([], {cwd})('foobar REF-1'); + t.is(actual.code, 0); +}); + async function writePkg(payload, options) { const pkgPath = path.join(options.cwd, 'package.json'); const pkg = JSON.parse(await sander.readFile(pkgPath)); diff --git a/@commitlint/core/src/load.js b/@commitlint/core/src/load.js index 59cee9c6b1..0750b2e8f3 100644 --- a/@commitlint/core/src/load.js +++ b/@commitlint/core/src/load.js @@ -26,7 +26,7 @@ export default async (seed = {}, options = {cwd: process.cwd()}) => { config.parserPreset = { name: config.parserPreset, path: resolvedParserPreset, - opts: require(resolvedParserPreset) + parserOpts: (await require(resolvedParserPreset)).parserOpts }; } @@ -38,13 +38,14 @@ export default async (seed = {}, options = {cwd: process.cwd()}) => { }); const preset = valid(mergeWith(extended, config, w)); - // Await parser-preset if applicable if ( typeof preset.parserPreset === 'object' && - typeof preset.parserPreset.opts === 'object' + typeof preset.parserPreset.parserOpts === 'object' && + typeof preset.parserPreset.parserOpts.then === 'function' ) { - preset.parserPreset.opts = await preset.parserPreset.opts; + preset.parserPreset.parserOpts = (await preset.parserPreset + .parserOpts).parserOpts; } // Execute rule config functions if needed diff --git a/@commitlint/core/src/load.test.js b/@commitlint/core/src/load.test.js index 3fa9da29bc..3d9569aa09 100644 --- a/@commitlint/core/src/load.test.js +++ b/@commitlint/core/src/load.test.js @@ -24,12 +24,9 @@ test('uses seed with parserPreset', async t => { }, {cwd} ); - t.is(actual.name, './conventional-changelog-custom'); - t.deepEqual(actual.opts, { - parserOpts: { - headerPattern: /^(\w*)(?:\((.*)\))?-(.*)$/ - } + t.deepEqual(actual.parserOpts, { + headerPattern: /^(\w*)(?:\((.*)\))?-(.*)$/ }); }); @@ -134,25 +131,19 @@ test('recursive extends with package.json file', async t => { test('parser preset overwrites completely instead of merging', async t => { const cwd = await git.bootstrap('fixtures/parser-preset-override'); const actual = await load({}, {cwd}); - t.is(actual.parserPreset.name, './custom'); - t.is(typeof actual.parserPreset.opts, 'object'); - t.deepEqual(actual.parserPreset.opts, { - b: 'b', - parserOpts: { - headerPattern: /.*/ - } + t.deepEqual(actual.parserPreset.parserOpts, { + headerPattern: /.*/ }); }); test('recursive extends with parserPreset', async t => { const cwd = await git.bootstrap('fixtures/recursive-parser-preset'); const actual = await load({}, {cwd}); - t.is(actual.parserPreset.name, './conventional-changelog-custom'); - t.is(typeof actual.parserPreset.opts, 'object'); + t.is(typeof actual.parserPreset.parserOpts, 'object'); t.deepEqual( - actual.parserPreset.opts.parserOpts.headerPattern, + actual.parserPreset.parserOpts.headerPattern, /^(\w*)(?:\((.*)\))?-(.*)$/ ); }); diff --git a/@commitlint/resolve-extends/src/index.js b/@commitlint/resolve-extends/src/index.js index 891f134ebb..40bb74bd8f 100644 --- a/@commitlint/resolve-extends/src/index.js +++ b/@commitlint/resolve-extends/src/index.js @@ -48,13 +48,12 @@ function loadExtends(config = {}, context = {}) { typeof c.parserPreset === 'string' ) { const resolvedParserPreset = resolveFrom(cwd, c.parserPreset); - const parserPreset = { name: c.parserPreset, path: `./${path.relative(process.cwd(), resolvedParserPreset)}` .split(path.sep) .join('/'), - opts: require(resolvedParserPreset) + parserOpts: require(resolvedParserPreset) }; ctx.parserPreset = parserPreset;