Skip to content

Array property merge with custom parserOpts #594

@ingmarh

Description

@ingmarh

Expected Behavior

Custom "parserOpts" key values in commitlint.config.js take precedence over the default "parserOpts" (from conventional-changelog-angular).

E.g. using ['type', 'subject'] for "headerCorrespondence" works.

Current Behavior

When using custom "parserOpts" in commitlint.config.js, array properties are merged with the default. E.g. setting "headerCorrespondence" to ['type', 'subject'] (ESLint convention) becomes ['type', 'subject', 'subject'] after merging the "parserOpts" object with conventional-changelog-angular "parserOpts".

This means that when using the "subject-empty" rule to ensure that the subject is not empty, it would always error ("subject may not be empty") in this case.

Affected packages

  • cli
  • core
  • prompt
  • config-angular

Possible Solution

The cause is the "parserOpts" merge introduced with #496 (commitlint 7.5.0): Lodash merge also merges array and plain object properties recursively. So this can probably be addressed in @commitlint/parse/src/index.js.

One solution could be to skip array objects when merging (if there's no need to merge those). For example, using Lodash mergeWith:

_.mergeWith({}, defaultOpts, parserOpts, (objValue, srcValue) => {
  if (_.isArray(objValue)) return srcValue
})

Steps to Reproduce (for bugs)

  1. Use commitlint >= 7.5.0 and set up commitlint.config.js (see below)
  2. Try echo 'Fix: Any subject' | commitlint
  3. It always fails with subject may not be empty [subject-empty]
commitlint.config.js
// ESLint Commit Message style
module.exports = {
  parserPreset: {
    parserOpts: {
      headerPattern: /^(.*):\s(.*)$/,
      headerCorrespondence: ['type', 'subject'],
    },
  },
  rules: {
    'subject-empty': [2, 'never'],
    'type-empty': [2, 'never'],
    'type-enum': [2, 'always', ['Fix', 'Update', 'New', 'Breaking', 'Docs', 'Build', 'Upgrade', 'Chore']],
  },
}

Context

In order to use ESLint Commit Message convention and own "issuePrefixes", we set up custom "parserOpts" in commitlint.config.js.

(As a workaround, we're currently setting "headerCorrespondence" to ['type', 'subject', 'unused'] to avoid the problem.)

Your Environment

Executable Version
commitlint --version 7.5.2
git --version 2.18.0
node --version 8.15.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions