Skip to content

[Question]How to write rules with regex #2051

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
4 tasks
HikawaRin opened this issue Aug 5, 2020 · 3 comments
Closed
4 tasks

[Question]How to write rules with regex #2051

HikawaRin opened this issue Aug 5, 2020 · 3 comments
Labels

Comments

@HikawaRin
Copy link

HikawaRin commented Aug 5, 2020

How to write rules with regex

Expected Behavior

Hi, I want to write my own rules based on Angular rules but has strict rules for scope, which may looks like below:

feat(core/module): add new feature ...

I want rule the scope message with a fixed head, like core, ui, etc...
then give more message descrip affected part(could be empty).
I think the rules I want to add can be expressed with regex like

'^core.*'

I tried

const scopes = [
    '^core.*',
    '^ui.*',
];

module.exports = {
    "rules":{
        'scope-enum': [2, 'always', scopes],
    },
};

but it doesn`t work.
So is there anyway can I apply regex to commitlint rules?

Current Behavior

Affected packages

  • cli
  • core
  • prompt
  • config-angular

Possible Solution

Steps to Reproduce (for bugs)

  1. First step
  2. Second step
commitlint.config.js ```js ```

Context

Your Environment

Executable Version
commitlint --version VERSION
git --version VERSION
node --version VERSION
@jdbruijn
Copy link
Contributor

jdbruijn commented Oct 20, 2020

@HikawaRin I think this issue can be resolved using my plugin commitlint-plugin-function-rules.

Using the plugin, you can write something like this as commitlint config. Note that I've changed from regex to simply do startsWith and give a nice error message with allowed scope, but you could also use a regex!

Hope this helps!

module.exports = {
  extends: ['@commitlint/config-conventional'],
  plugins: ['commitlint-plugin-function-rules'],
  rules: {
    'scope-enum': [0], // level: disabled
    'function-rules/scope-enum': [
      2, // level: error
      'always',
      (parsed) => {
        const scopes = ['core', 'ui'];

        if (
          !parsed.scope ||
          scopes.some((scope) => parsed.scope.startsWith(scope))
        ) {
          return [true];
        }

        return [false, `scope must be one of ${scopes.join(', ')}`];
      },
    ],
  },
};

@xianshenglu
Copy link

Thanks very much. @jdbruijn You saved me. Here is my demo commit to customize commit lint rules.

@escapedcat
Copy link
Member

We consider this question answered. Please reopen if you think you need more input.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

4 participants