Skip to content

Commit 542f50e

Browse files
Fetzmarionebl
authored andcommitted
feat: add max line length to body/footer
1 parent e22bd5c commit 542f50e

9 files changed

+214
-1
lines changed

@commitlint/ensure/src/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import ensureCase from './case';
22
import ensureEnum from './enum';
33
import maxLength from './max-length';
4+
import maxLineLength from './max-line-length';
45
import minLength from './min-length';
56
import notEmpty from './not-empty';
67

78
export {ensureCase as case};
89
export {ensureEnum as enum};
9-
export {maxLength, minLength, notEmpty};
10+
export {maxLength, maxLineLength, minLength, notEmpty};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import ensure from './max-length';
2+
3+
export default (value, max) =>
4+
typeof value === 'string' &&
5+
value.split(/\r?\n/).every(line => ensure(line, max));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import test from 'ava';
2+
import ensure from './max-line-length';
3+
4+
test('false for no params', t => {
5+
const actual = ensure();
6+
t.is(actual, false);
7+
});
8+
9+
test('true for a against 1', t => {
10+
const actual = ensure('a', 1);
11+
t.is(actual, true);
12+
});
13+
14+
test('false for ab against 0', t => {
15+
const actual = ensure('a', 0);
16+
t.is(actual, false);
17+
});
18+
19+
test('true for a against 2', t => {
20+
const actual = ensure('a', 2);
21+
t.is(actual, true);
22+
});
23+
24+
test('true for ab against 2', t => {
25+
const actual = ensure('ab', 2);
26+
t.is(actual, true);
27+
});
28+
29+
test('false for ab/\nab/\nab 1', t => {
30+
const actual = ensure(
31+
`ab
32+
ab
33+
ab`,
34+
2
35+
);
36+
37+
t.is(actual, true);
38+
});
39+
40+
test('true for ab/\nab/\nab 2', t => {
41+
const actual = ensure(
42+
`ab
43+
ab
44+
ab`,
45+
2
46+
);
47+
48+
t.is(actual, true);
49+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import {maxLineLength} from '@commitlint/ensure';
2+
3+
export default (parsed, when, value) => {
4+
const input = parsed.body;
5+
6+
if (!input) {
7+
return [true];
8+
}
9+
10+
return [
11+
maxLineLength(input, value),
12+
`body's lines must not be longer than ${value} characters`
13+
];
14+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import test from 'ava';
2+
import parse from '@commitlint/parse';
3+
import check from './body-max-line-length';
4+
5+
const short = 'a';
6+
const long = 'ab';
7+
8+
const value = short.length;
9+
10+
const messages = {
11+
empty: 'test: subject',
12+
short: `test: subject\n${short}`,
13+
long: `test: subject\n${long}`,
14+
shortMultipleLines: `test:subject\n${short}\n${short}\n${short}`,
15+
longMultipleLines: `test:subject\n${short}\n${long}\n${short}`
16+
};
17+
18+
const parsed = {
19+
empty: parse(messages.empty),
20+
short: parse(messages.short),
21+
long: parse(messages.long)
22+
};
23+
24+
test('with empty should succeed', async t => {
25+
const [actual] = check(await parsed.empty, '', value);
26+
const expected = true;
27+
t.is(actual, expected);
28+
});
29+
30+
test('with short should succeed', async t => {
31+
const [actual] = check(await parsed.short, '', value);
32+
const expected = true;
33+
t.is(actual, expected);
34+
});
35+
36+
test('with long should fail', async t => {
37+
const [actual] = check(await parsed.long, '', value);
38+
const expected = false;
39+
t.is(actual, expected);
40+
});
41+
42+
test('with short with multiple lines should succeed', async t => {
43+
const [actual] = check(await parsed.short, '', value);
44+
const expected = true;
45+
t.is(actual, expected);
46+
});
47+
48+
test('with long with multiple lines should fail', async t => {
49+
const [actual] = check(await parsed.long, '', value);
50+
const expected = false;
51+
t.is(actual, expected);
52+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import {maxLineLength} from '@commitlint/ensure';
2+
3+
export default (parsed, when, value) => {
4+
const input = parsed.footer;
5+
6+
if (!input) {
7+
return [true];
8+
}
9+
10+
return [
11+
maxLineLength(input, value),
12+
`footer's lines must not be longer than ${value} characters`
13+
];
14+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import test from 'ava';
2+
import parse from '@commitlint/parse';
3+
import check from './footer-max-line-length';
4+
5+
const short = 'BREAKING CHANGE: a';
6+
const long = 'BREAKING CHANGE: ab';
7+
8+
const value = short.length;
9+
10+
const messages = {
11+
simple: 'test: subject',
12+
empty: 'test: subject\nbody',
13+
short: `test: subject\n${short}`,
14+
long: `test: subject\n${long}`,
15+
shortMultipleLines: `test:subject\n${short}\n${short}\n${short}`,
16+
longMultipleLines: `test:subject\n${short}\n${long}\n${short}`
17+
};
18+
19+
const parsed = {
20+
simple: parse(messages.simple),
21+
empty: parse(messages.empty),
22+
short: parse(messages.short),
23+
long: parse(messages.long)
24+
};
25+
26+
test('with simple should succeed', async t => {
27+
const [actual] = check(await parsed.simple, '', value);
28+
const expected = true;
29+
t.is(actual, expected);
30+
});
31+
32+
test('with empty should succeed', async t => {
33+
const [actual] = check(await parsed.empty, '', value);
34+
const expected = true;
35+
t.is(actual, expected);
36+
});
37+
38+
test('with short should succeed', async t => {
39+
const [actual] = check(await parsed.short, '', value);
40+
const expected = true;
41+
t.is(actual, expected);
42+
});
43+
44+
test('with long should fail', async t => {
45+
const [actual] = check(await parsed.long, '', value);
46+
const expected = false;
47+
t.is(actual, expected);
48+
});
49+
50+
test('with short with multiple lines should succeed', async t => {
51+
const [actual] = check(await parsed.short, '', value);
52+
const expected = true;
53+
t.is(actual, expected);
54+
});
55+
56+
test('with long with multiple lines should fail', async t => {
57+
const [actual] = check(await parsed.long, '', value);
58+
const expected = false;
59+
t.is(actual, expected);
60+
});

@commitlint/rules/src/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ export default {
33
'body-empty': require('./body-empty'),
44
'body-leading-blank': require('./body-leading-blank'),
55
'body-max-length': require('./body-max-length'),
6+
'body-max-line-length': require('./body-max-line-length'),
67
'body-min-length': require('./body-min-length'),
78
'body-tense': require('./body-tense'),
89
'footer-empty': require('./footer-empty'),
910
'footer-leading-blank': require('./footer-leading-blank'),
1011
'footer-max-length': require('./footer-max-length'),
12+
'footer-max-line-length': require('./footer-max-line-length'),
1113
'footer-min-length': require('./footer-min-length'),
1214
'footer-tense': require('./footer-tense'),
1315
'header-max-length': require('./header-max-length'),

docs/reference-rules.md

+16
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ Rule configurations are either of type `array` residing on a key with the rule's
4646
Infinity
4747
```
4848

49+
#### body-max-line-length
50+
* **condition**: `body` lines has `value` or less characters
51+
* **rule**: `always`
52+
* **value**
53+
```js
54+
Infinity
55+
```
56+
4957
#### body-min-length
5058
* **condition**: `body` has `value` or more characters
5159
* **rule**: `always`
@@ -66,6 +74,14 @@ Rule configurations are either of type `array` residing on a key with the rule's
6674
Infinity
6775
```
6876

77+
#### footer-max-line-length
78+
* **condition**: `footer` lines has `value` or less characters
79+
* **rule**: `always`
80+
* **value**
81+
```js
82+
Infinity
83+
```
84+
6985
#### footer-min-length
7086
* **condition**: `footer` has `value` or more characters
7187
* **rule**: `always`

0 commit comments

Comments
 (0)