conventional-changelog/commitlint

fix: can't set headerPattern from YAML configuration files

Closed

#3,487 opened on Jan 6, 2023

 (7 comments) (0 reactions) (0 assignees)TypeScript (896 forks)batch import
bughelp wanted

Repository metrics

Stars
 (15,497 stars)
PR merge metrics
 (Avg merge 12h 56m) (52 merged PRs in 30d)

Description

Expected Behavior

I use YAML configuration and try to set headerPattern like this:

extends:
  - '@commitlint/config-conventional'
parserPreset:
  parserOpts:
    headerPattern: '/^([^0-9]\w*)(?:\(([^0-9].*)\))?!?: (.*)$/' 

This should avoid scope starting with a digit:

$ echo 'fix(123): foo' | commitlint --verbose
⧗   input: fix(123): foo
✖   subject may not be empty [subject-empty]
✖   type may not be empty [type-empty]

✖   found 2 problems, 0 warnings
ⓘ   Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint

Current Behavior

This load the headerPattern as a string instead of a regular expression:

$ commitlint --print-config | grep headerPattern
    parserOpts: { headerPattern: '/^([^0-9]\\w*)(?:\\(([^0-9].*)\\))?!?: (.*)$/' }

It looks like the scope starting with a digit is catch:

$ echo 'fix(123): foo' | commitlint --verbose
⧗   input: fix(123): foo
✖   subject may not be empty [subject-empty]
✖   type may not be empty [type-empty]

✖   found 2 problems, 0 warnings
ⓘ   Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint

But it catch everything since headerPattern is not a regex:

echo 'fix(bar): foo' | commitlint --verbose
⧗   input: fix(bar): foo
✖   subject may not be empty [subject-empty]
✖   type may not be empty [type-empty]

✖   found 2 problems, 0 warnings
ⓘ   Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint

Affected packages

  • cli
  • core
  • prompt
  • config-angular

Possible Solution

The pattern options should be converted to regular expression.

Steps to Reproduce

1. define the following configuration `.commitlintrc.yaml`
   
   extends:
     - '@commitlint/config-conventional'
   parserPreset:
     parserOpts:
       headerPattern: '/^([^0-9]\w*)(?:\(([^0-9].*)\))?!?: (.*)$/'
   
2. execute the following command which should fail: `echo 'fix(123): foo' | commitlint --verbose`
3. execute the following command which should pass: `echo 'fix(bar): foo' | commitlint --verbose`

Context

Note that replacing the .commitlintrc.yaml with the following commitlint.config.js is working:

module.exports = {
    extends: ['@commitlint/config-conventional'],
    parserPreset: {
        parserOpts: {
            headerPattern: /^([^0-9]\w*)(?:\(([^0-9].*)\))?!?: (.*)$/,
        }
    }
};

commitlint --version

@commitlint/cli@14.4.0

git --version

2.38.2

node --version

v19.3.0

Contributor guide