'commitlint not able to make scope optional

I am trying to use issue prefix to have my commit format as mentioned below

JIRA-ID: type(scope): Subject

scope should be optional. which means following are valid message

  • AAAA-12: fix(test): fixed the failing test

  • AAAA-12: fix: fixed the failing test

Following is how my commitlint.config.js looks like

module.exports = {
    extends: ['@commitlint/config-conventional'],
    parserPreset: {
           parserOpts: {
              headerPattern: /^[A-Z]{1,4}-[0-9]{1,4}:\s(\w*)\((\w*)\):\s(.*)$/,
              headerCorrespondence: ["type", "scope", "subject"],
              issuePrefixes: ["^[A-Z]{1,4}-[0-9]{1,4}"],
              referenceActions: ["xxx-"] // (!!)
            }
      },
 rules: {
        'references-empty': [2, 'never'],
       'scope-empty': [1, 'never'],
      ...
     ... 
}
}

the following message appears to be invalid.

AAAA-12: fix: fixed the failing test

it forces me to use empty parenthesis as mentioned below.

AAAA-12(): fix: fixed the failing test


Solution 1:[1]

Your headerPattern is wrong. Try below regex:

/^[A-Z]{1,4}-[0-9]{1,4}:\s(\w*)(\((\w+)\))?:\s(.*)$/

You can also test this regex online: https://regex101.com/r/MtG7sy/2

Solution 2:[2]

As @ridvanaltun mentioned in his answer, the problem is not in the rules, but in regex.

The rule 'scope-empty': [1, 'never'], is means display warning if scope is not empty.

If you want to disable this rule completely, just remove this line from your config, or if you want to be explicit, then change it to:

'scope-empty': [0, 'never'],.

If you want to make this rule mandatory change it to:

'scope-empty': [2, 'never'],.

More info about the rules see documentation:

Rules are made up by a name and a configuration array. The configuration array contains:
Level [0..2]: 0 disables the rule. For 1 it will be considered a warning for 2 an error.
Applicable always|never: never inverts the rule. Value: value to use for this rule.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ridvanaltun
Solution 2 David Navrkal