conventional-changelog / conventional-changelog/commitlint

Feature Request: Support for custom 'when' values

Open
#1,003 1 comment 0 reactions 0 assignees View on GitHub
feature
Dominant language
TypeScript
Stars
18.7k
Forks
970
Avg merge
7h 33m
Merged PRs (30d)
49

Description

I've been working on a custom plugin with custom rules, and for some of the rules I want to apply to all commit comments, however for other rules I want to only apply for non-WIP commits. I don't believe I can currently do this on a per rule basis.

## Expected Behaviour

What I would like to do is something like this (ignore rule names since they refer to custom rules I created):

```js
{
rules: {
[commitlintGitHubRules.issueNumberMissing]: [2, 'never-in-non-wips'],
[commitlintGitHubRules.issueNumberFormat]: [2, 'always'],
[commitlintGitHubRules.issueNumberDuplicate]: [2, 'never'],

[commitlintGitHubRules.wipAllowed]: [2, 'always'],

[commitlintGitHubRules.subjectEmpty]: [2, 'never-in-non-wips'],
[commitlintGitHubRules.subjectSeparator]: [2, 'always'],
[commitlintGitHubRules.subjectCase]: [2, 'always-in-non-wips', 'sentence-case'],
[commitlintGitHubRules.subjectFullStop]: [1, 'always-in-non-wips'],

[commitlintGitHubRules.typeCase]: [2, 'always', 'lower-case'],
},
};
```

Notice how some rules use some new `when` values to only apply to non-WIP commits, while others apply to all commits.

As you can see, what I would like to do is specify in my configuration `always-in-non-wips` or `never-in-non-wips` since my custom plugin identifies whether a commit is a WIP or not and if it is a WIP and the `when` value is one of these custom values, it just returns `[true]` as it skips validating the WIP commit.

If the commit isn't a WIP though, then it converts the custom `when` to the standard equivalent: `always-in-non-wips` => `always`; `never-in-non-wips` => `never`. It then proceeds to validate accordingly.

In the configuration example above, many of the custom rules delegate to the standard corresponding rules such as `subject-case`, `subject-full-stop`, but the custom rules short-circuit for WIP commits if the `when` value stipulates to only validate non-WIPs. Otherwise it delegates to the standard rules after converting the `when` to the corresponding standard `always` and `never` values as necessary.

## Current Behaviour

This all works great with one problem I didn't anticipate - the overall commitlint linter rejects any custom `when` value in a configuration and so blows everything up, so I can't do this.

Not allowing individual rules to allow for more than two different rule conditions seems quite limiting, and I'm raising this Issue to get some feedback on this.

## Suggestion

My suggestion would be to update the linter to avoid globally policing the allowable `when` values and instead delegate that to individual rules to allow each one to police which values it accepts.

## Affected packages

- [ ] cli
- [x] core
- [ ] prompt
- [ ] config-angular

## Your Environment

| Executable | Version |
| ---------------------: | :------ |
| `commitlint --version` | 8.3.5 |
| `git --version` | 2.25.0 |
| `node --version` | 12.16.1 |

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.