Skip to content

Commit bf15a8a

Browse files
committed
refactor!: change to ES module, export is still an CommonJS module as that's required by commitlint
1 parent 13d2a4f commit bf15a8a

14 files changed

+9107
-6015
lines changed

.eslintrc.js

-57
This file was deleted.

.github/lint-staged.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
module.exports = {
1+
const config = {
22
'*.{ts,tsx,js,jsx}': [
3-
'prettier --write',
4-
'npm run lint-es:file:fix',
5-
() => 'npm run test',
3+
'xo --fix',
4+
() => 'ava'
65
],
76
'*.{vue,css,less,scss,html,htm,json,md,markdown,yml,yaml}':
87
'prettier --write',
98
};
9+
10+
export default config;

README.md

+13-22
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
# commitlint plugin function rules
1+
# commitlint plugin function rules <!-- omit in toc -->
22

3-
[commitlint][commitlint] plugin to use functions as rule value.
3+
[**commitlint**][commitlint] plugin to use functions as rule value.
44

55
- Create custom function, `sync` or `async`, as rule value.
66
- Create rules that are based on the commit message.
77
- Write rules with regular expressions.
8-
- Use the same rules that are [available](https://commitlint.js.org/#/reference-rules) in [commitlint][commitlint].
8+
- Use the same rules that are [available in **commitlint**][commitlint].
99

1010
---
1111

@@ -17,28 +17,23 @@
1717
[![Code coverage](https://img.shields.io/codecov/c/github/vidavidorra/commitlint-plugin-function-rules?logo=codecov&style=flat-square)](https://codecov.io/gh/vidavidorra/commitlint-plugin-function-rules)
1818
[![License](https://img.shields.io/github/license/vidavidorra/commitlint-plugin-function-rules?style=flat-square)](LICENSE.md)
1919

20-
<a name="toc"></a>
21-
22-
## Table of contents
23-
2420
- [Install](#install)
2521
- [Usage](#usage)
26-
- [Documentation](#documentation)
2722
- [Contributing](#contributing)
2823
- [Security policy](#security-policy)
2924
- [License](#license)
3025

3126
## Install
3227

3328
```shell
34-
npm install --save-dev commitlint-plugin-function-rules @commitlint/cli
29+
npm install --save-dev commitlint-plugin-function-rules @commitlint/cli @commitlint/config-conventional
3530
```
3631

3732
## Usage
3833

39-
Use this plugin in your project's commitlint configuration by specifying it as item in the `plugins` array. All rules have same name as the [commitlint][commitlint] rules, but with the `function-rules` prefix.
34+
Use this plugin in your project's [**commitlint**][commitlint] configuration by specifying it as item in the `plugins` array. All rules have same name as rules that are [available in **commitlint**][rules], but with the `function-rules` prefix. The example `commitlint.config.js`, or `commitlint.config.cjs` if the package is an ES module, shows the usage of a function rule.
4035

41-
> **_Note:_** The available rules are the same as in [commitlint][commitlint], so it is recommended to disable the [commitlint][commitlint] rule when specifying a function rule to avoid undefined behaviour.
36+
> **Note** The available rules are the same as in [**commitlint**][commitlint], so it is recommended to disable the [**commitlint**][commitlint] rule when specifying a function rule to avoid undefined behaviour.
4237
4338
```js
4439
module.exports = {
@@ -50,31 +45,26 @@ module.exports = {
5045
2, // level: error
5146
'always',
5247
(parsed) => {
53-
if (parsed.type === 'chore' && parsed.header.length < 20) {
48+
// Allow longer headers for commits with "deps" scope.
49+
if (parsed.scope === 'deps' && parsed.header.length <= 200) {
5450
return [true];
5551
}
56-
return [false, 'chore header must not be longer than 120 characters'];
52+
return [false, 'deps header must not be longer than 200 characters'];
5753
},
5854
],
5955
},
6056
};
6157
```
6258

63-
## Documentation
64-
6559
## Contributing
6660

67-
Please [create an issue](https://github.com/vidavidorra/commitlint-plugin-function-rules/issues/new/choose) if you have a bug report, feature proposal or question that does not yet exist.
68-
69-
Please give this project a star ⭐ if you like it and consider becoming a [sponsor](https://github.com/sponsors/jdbruijn) to support this project.
61+
Please [create an issue](https://github.com/vidavidorra/commitlint-plugin-function-rules/issues/new/choose) if you have a bug report or feature proposal, or [create a discussion](https://github.com/vidavidorra/commitlint-plugin-function-rules/discussions) if you have a question. If you like this project, please consider giving it a star ⭐ and/or become a [sponsor](https://github.com/sponsors/jdbruijn) to support my work.
7062

7163
Refer to the [contributing guide](https://github.com/vidavidorra/.github/blob/main/CONTRIBUTING.md) detailed information about other contributions, like pull requests.
7264

7365
[![Conventional Commits: 1.0.0](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow?style=flat-square)](https://conventionalcommits.org)
66+
[![XO code style](https://shields.io/badge/code_style-5ed9c7?logo=xo&labelColor=gray&style=flat-square)](https://github.com/xojs/xo)
7467
[![Code style](https://img.shields.io/badge/code_style-Prettier-ff69b4?logo=prettier&style=flat-square)](https://github.com/prettier/prettier)
75-
[![Linting](https://img.shields.io/badge/linting-ESLint-lightgrey?logo=eslint&style=flat-square)](https://eslint.org)
76-
[![Lint commit messages](https://img.shields.io/github/workflow/status/vidavidorra/commitlint-plugin-function-rules/Lint%20commit%20messages?logo=github&label=Lint%20commit%20messages&style=flat-square)](https://github.com/vidavidorra/commitlint-plugin-function-rules/actions)
77-
[![Build](https://img.shields.io/github/workflow/status/vidavidorra/commitlint-plugin-function-rules/Build?logo=github&label=Build&style=flat-square)](https://github.com/vidavidorra/commitlint-plugin-function-rules/actions)
7868

7969
## Security policy
8070

@@ -86,7 +76,7 @@ This project is licensed under the [GPLv3 license](https://www.gnu.org/licenses/
8676

8777
Copyright © 2020-2023 Jeroen de Bruijn
8878

89-
<details><summary>License details.</summary>
79+
<details><summary>License notice</summary>
9080
<p>
9181

9282
This program is free software: you can redistribute it and/or modify
@@ -109,3 +99,4 @@ The full text of the license is available in the [LICENSE](LICENSE.md) file in t
10999
<!-- References -->
110100

111101
[commitlint]: https://commitlint.js.org/
102+
[rules]: https://commitlint.js.org/#/reference-rules

commitlint.config.js

+16-33
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,29 @@
1-
module.exports = {
1+
const config = {
22
extends: ['@commitlint/config-conventional'],
33
plugins: [
44
{
55
rules: {
6-
'header-max-length-deps': (parsed) => {
7-
const config = {
8-
maxLength: 100,
9-
dependencyCommit: {
10-
type: /^(chore|fix)/,
11-
scope: /(peer-)?deps/,
12-
maxLength: 200,
13-
},
14-
};
15-
16-
const length = parsed.header.length;
6+
'function-rules/header-max-length'(parsed) {
7+
const {length} = parsed.header;
8+
const maxLength = 100;
9+
const maxDepsLength = 200;
1710
const isDepsCommit =
18-
config.dependencyCommit.type.test(parsed.type) &&
19-
config.dependencyCommit.scope.test(parsed.scope);
20-
21-
if (length <= config.maxLength) {
22-
return [true];
23-
}
24-
25-
if (!isDepsCommit && length > config.maxLength) {
11+
/^(chore|fix)/.test(parsed.type) && parsed.scope === 'deps';
12+
if (
13+
(isDepsCommit && length > maxDepsLength) ||
14+
(!isDepsCommit && length > maxLength)
15+
) {
16+
const type = isDepsCommit ? 'for dependency commits ' : '';
17+
const maxCharacters = isDepsCommit ? maxDepsLength : maxLength;
2618
return [
2719
false,
2820
[
29-
`header must not be longer than ${config.maxLength}`,
21+
`header ${type}must not be longer than ${maxCharacters}`,
3022
`characters, current length is ${length}`,
3123
].join(' '),
3224
];
3325
}
3426

35-
if (isDepsCommit && length > config.dependencyCommit.maxLength) {
36-
return [
37-
false,
38-
[
39-
`header for dependency commits must not be longer than`,
40-
`${config.dependencyCommit.maxLength} characters, current`,
41-
`length is ${length}`,
42-
].join(' '),
43-
];
44-
}
45-
4627
return [true];
4728
},
4829
},
@@ -52,6 +33,8 @@ module.exports = {
5233
'body-max-line-length': [0],
5334
'footer-max-line-length': [0],
5435
'header-max-length': [0],
55-
'header-max-length-deps': [2, 'always'],
36+
'function-rules/header-max-length': [2, 'always'],
5637
},
5738
};
39+
40+
export default config;

0 commit comments

Comments
 (0)