commitlint icon indicating copy to clipboard operation
commitlint copied to clipboard

Error when linting a message with the word "fix" in it using custom parser preset

Open bencelang opened this issue 3 years ago • 2 comments

When using a custom parser preset, any form of the word "fix" seems to break parsing of references.

Running echo "refs#111111 Fixed something" | commitlint yields the following error:

{
  references: [],
  subject: 'Fixed something',
  merge: null,
  header: 'refs#111111 Fixed something',
  body: null,
  footer: null,
  notes: [],
  mentions: [],
  revert: null,
  raw: 'refs#111111 Fixed something\r\n'
}
â§—   input: refs#111111 Fixed something
✖   references may not be empty [references-empty]

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

However, linting "refs#111111 This is a correct message" passes as expected.

echo "refs#111111 This is a correct message" | commitlint

{
  references: [
    {
      action: null,
      owner: null,
      repository: null,
      issue: '111111',
      raw: 'refs#111111',
      prefix: 'refs#'
    }
  ],
  subject: 'This is a correct message',
  merge: null,
  header: 'refs#111111 This is a correct message',
  body: null,
  footer: null,
  notes: [],
  mentions: [],
  revert: null,
  raw: 'refs#111111 This is a correct message\r\n'
}

Expected Behavior

The mentioned commit message should pass validation

Current Behavior

It seems as if any form of the word "fix" (e.g. "fixed", "fixes", "Fix..") must be the last word of the message or the linter will mistakenly fail.

Affected packages

  • [ ] cli
  • [x] core (I suppose)
  • [ ] prompt
  • [ ] config-angular

Steps to Reproduce (for bugs)

commitlint.config.js
module.exports = {
  parserPreset: './commitlint.preset',
  rules: {
    'debug': [2, 'always'],
    // Built-in rules
    'header-full-stop': [2, 'never'], // Cannot have a trailing dot character
    'references-empty': [2, 'never'], // Must have at least one referenced ticket
    'subject-empty': [2, 'never'], // Must have a message (not just refs)
    'subject-case': [2, 'always', 'sentence-case'], // Must be sentence-like (e.g. Added xy function)
    // Custom rules
    'subject-start': [2, 'never', ['-', 'refs', ':', '.']], // Message cannot start with these (format error)
    'references-format': [2, 'always', /^,? ?refs#\d{5,}/] // Ban invalid refs format
  },
  plugins: [
    {
      rules: {
        'debug': (parsed) => {
          console.log(parsed);
          return [true];
        },
        'subject-start': (parsed, when, value) => {
          const { subject } = parsed;
          if (!subject) {
            return [true]; // subject-empty should handle missing subject
          }
          const negated = when === 'never';
          const doesMach = negated ?
              value.some(v => subject.trim().startsWith(v)) :
              value.every(v => subject.trim().startsWith(v));
          return [
              negated ? !doesMach : doesMach,
              `Subject ${negated ? 'may not' : 'must'} start with "${value.join('", "')}"`
          ];
        },
        'references-format': (parsed, when, matcher) => {
          const { references } = parsed;
          const negated = when === 'never';
          const doesMach = negated ?
              references.some(ref => ref.raw.trim().match(matcher)) :
              references.every(ref => ref.raw.trim().match(matcher));
          return [
              negated ? !doesMach : doesMach,
              `References ${negated ? 'may not' : 'must'} match format "${matcher}"`
          ];
        }
      }
    }
  ]
};
commitlint.preset.js
module.exports = {
  parserOpts: {
    headerPattern: /^(?:(refs#\d{5,}(?:, ?refs#\d{5,})*) )?(.*)$/,
    //headerPattern: /^((?:refs#\d{5,},?)*) ?(.*)$/,
    headerCorrespondence: ['references', 'subject'],
    issuePrefixes: ['refs#'],
    issuePrefixesCaseSensitive: true
  },
};

Context

I am trying to configure commitlint for our custom message format refs#TICKETNO Sentence case message, where ticketno is a number at least 6 digits wide.

Your Environment

Executable Version
commitlint --version @commitlint/[email protected]
git --version git version 2.35.3.windows.1
node --version v14.16.0

bencelang avatar Jun 07 '22 19:06 bencelang

This is something I ran into as well, and managed to trace the root cause to the conventional-changelog-parser package. Opened an issue there (referenced just above), will try to wrap things up there and update here when there's anything interesting.

Zirak avatar Jun 16 '22 13:06 Zirak

https://github.com/conventional-changelog/commitlint/issues/372

sawin-dennis avatar Jan 02 '24 14:01 sawin-dennis