commitlint icon indicating copy to clipboard operation
commitlint copied to clipboard

Distinguish null and empty-string for header fields

Open ikatyang opened this issue 5 years ago • 1 comments

Expected Behavior

Superfluous scope should be reported by the scope-empty rule, i.e., feat(): something should be reported.

Current Behavior

The scope-empty rule does not report on superfluous scope, though the test case said that it should, but the test assertion does not seem to match the behavior:

https://github.com/conventional-changelog/commitlint/blob/59849dc8df7fa645c2528970b933edfd27f9c31e/%40commitlint/rules/src/scope-empty.test.ts#L46-L50

The root cause is that the conventional-commits-parser always converts falsy values to null, which causes null and "" not distinguishable.

Affected packages

  • [x] @commitlint/rules
  • [x] conventional-commits-parser

Possible Solution

Somehow disable the falsy conversion.

Steps to Reproduce

mkdir commitlint-superfluous-scope
cd commitlint-superfluous-scope
npm init -y
npm install @commitlint/cli
echo '
module.exports = {
  rules: {
    "scope-empty": [2, "always"],
  },
}
' > .commitlintrc.js
echo "feat(): something" | node_modules/.bin/commitlint # passed but it should report error

Context

I want to write some rules to avoid the usage of the superfluous scope and some custom header fields, but I found that it is impossible because of the falsy conversion, and I don't want to fix it on the regex-level:

-headerPattern: /^(\w*)(?:\(([\w$.\-*/ ]*)\))?: (.*)$/,
+headerPattern: /^(\w*)(?:\(([\w$.\-*/ ]+)\))?: (.*)$/,

since the lint message will be super confusing.

Your Environment

Executable Version
commitlint --version 9.1.2

ikatyang avatar Aug 27 '20 02:08 ikatyang

Thanks for your input! Pity the test doesn't work as it should.

escapedcat avatar Aug 31 '20 07:08 escapedcat