Skip to content

Commit

Permalink
fix: don't merge array properties with custom opts (#616)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ingmarh authored and marionebl committed Apr 26, 2019
1 parent 6ad687f commit f321647
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
9 changes: 7 additions & 2 deletions @commitlint/parse/src/index.js
Original file line number Diff line number Diff line change
@@ -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;
}
22 changes: 22 additions & 0 deletions @commitlint/parse/src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit f321647

Please sign in to comment.