From f04d0db199563b880697bb22b2a7515763bdda0b Mon Sep 17 00:00:00 2001 From: Ingmar Hergst Date: Mon, 8 Apr 2019 22:55:11 +0200 Subject: [PATCH] fix: don't merge array properties with custom opts With a custom "headerCorrespondence" of two entries, commit message subjects couldn't be parsed due to merge with the default (Angular). For example, the following custom parser opts (ESLint style) wouldn't work because "headerCorrespondence" is `['type', 'subject', 'subject']` after the merge. ```js { headerPattern: /^(.*):\s(.*)$/, headerCorrespondence: ['type', 'subject'], } ``` Fixes #594 --- @commitlint/parse/src/index.js | 9 +++++++-- @commitlint/parse/src/index.test.js | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/@commitlint/parse/src/index.js b/@commitlint/parse/src/index.js index dfb96f1dcb..ee4f7742ce 100644 --- a/@commitlint/parse/src/index.js +++ b/@commitlint/parse/src/index.js @@ -1,12 +1,17 @@ import {sync} from 'conventional-commits-parser'; import defaultChangelogOpts from 'conventional-changelog-angular'; -import {merge} from 'lodash'; +import {isArray, mergeWith} from 'lodash'; export default parse; async function parse(message, parser = sync, parserOpts = undefined) { const defaultOpts = (await defaultChangelogOpts).parserOpts; - const parsed = parser(message, merge({}, defaultOpts, parserOpts)); + const parsed = parser( + message, + mergeWith({}, defaultOpts, parserOpts, (objValue, srcValue) => { + if (isArray(objValue)) return srcValue; + }) + ); parsed.raw = message; return parsed; } diff --git a/@commitlint/parse/src/index.test.js b/@commitlint/parse/src/index.test.js index 1da6464b7f..f44a1182bb 100644 --- a/@commitlint/parse/src/index.test.js +++ b/@commitlint/parse/src/index.test.js @@ -98,6 +98,28 @@ test('uses custom opts parser', async t => { t.deepEqual(actual, expected); }); +test('does not merge array properties with custom opts', async t => { + const message = 'type: subject'; + const actual = await parse(message, undefined, { + headerPattern: /^(.*):\s(.*)$/, + headerCorrespondence: ['type', 'subject'] + }); + const expected = { + body: null, + footer: null, + header: 'type: subject', + mentions: [], + merge: null, + notes: [], + raw: 'type: subject', + references: [], + revert: null, + subject: 'subject', + type: 'type' + }; + t.deepEqual(actual, expected); +}); + test('supports scopes with /', async t => { const message = 'type(some/scope): subject'; const actual = await parse(message);