Skip to content

Commit

Permalink
feat: add max line length to body/footer
Browse files Browse the repository at this point in the history
  • Loading branch information
Fetz authored and marionebl committed Aug 20, 2018
1 parent e22bd5c commit 542f50e
Show file tree
Hide file tree
Showing 9 changed files with 214 additions and 1 deletion.
3 changes: 2 additions & 1 deletion @commitlint/ensure/src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import ensureCase from './case';
import ensureEnum from './enum';
import maxLength from './max-length';
import maxLineLength from './max-line-length';
import minLength from './min-length';
import notEmpty from './not-empty';

export {ensureCase as case};
export {ensureEnum as enum};
export {maxLength, minLength, notEmpty};
export {maxLength, maxLineLength, minLength, notEmpty};
5 changes: 5 additions & 0 deletions @commitlint/ensure/src/max-line-length.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import ensure from './max-length';

export default (value, max) =>
typeof value === 'string' &&
value.split(/\r?\n/).every(line => ensure(line, max));
49 changes: 49 additions & 0 deletions @commitlint/ensure/src/max-line-length.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import test from 'ava';
import ensure from './max-line-length';

test('false for no params', t => {
const actual = ensure();
t.is(actual, false);
});

test('true for a against 1', t => {
const actual = ensure('a', 1);
t.is(actual, true);
});

test('false for ab against 0', t => {
const actual = ensure('a', 0);
t.is(actual, false);
});

test('true for a against 2', t => {
const actual = ensure('a', 2);
t.is(actual, true);
});

test('true for ab against 2', t => {
const actual = ensure('ab', 2);
t.is(actual, true);
});

test('false for ab/\nab/\nab 1', t => {
const actual = ensure(
`ab
ab
ab`,
2
);

t.is(actual, true);
});

test('true for ab/\nab/\nab 2', t => {
const actual = ensure(
`ab
ab
ab`,
2
);

t.is(actual, true);
});
14 changes: 14 additions & 0 deletions @commitlint/rules/src/body-max-line-length.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {maxLineLength} from '@commitlint/ensure';

export default (parsed, when, value) => {
const input = parsed.body;

if (!input) {
return [true];
}

return [
maxLineLength(input, value),
`body's lines must not be longer than ${value} characters`
];
};
52 changes: 52 additions & 0 deletions @commitlint/rules/src/body-max-line-length.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import test from 'ava';
import parse from '@commitlint/parse';
import check from './body-max-line-length';

const short = 'a';
const long = 'ab';

const value = short.length;

const messages = {
empty: 'test: subject',
short: `test: subject\n${short}`,
long: `test: subject\n${long}`,
shortMultipleLines: `test:subject\n${short}\n${short}\n${short}`,
longMultipleLines: `test:subject\n${short}\n${long}\n${short}`
};

const parsed = {
empty: parse(messages.empty),
short: parse(messages.short),
long: parse(messages.long)
};

test('with empty should succeed', async t => {
const [actual] = check(await parsed.empty, '', value);
const expected = true;
t.is(actual, expected);
});

test('with short should succeed', async t => {
const [actual] = check(await parsed.short, '', value);
const expected = true;
t.is(actual, expected);
});

test('with long should fail', async t => {
const [actual] = check(await parsed.long, '', value);
const expected = false;
t.is(actual, expected);
});

test('with short with multiple lines should succeed', async t => {
const [actual] = check(await parsed.short, '', value);
const expected = true;
t.is(actual, expected);
});

test('with long with multiple lines should fail', async t => {
const [actual] = check(await parsed.long, '', value);
const expected = false;
t.is(actual, expected);
});
14 changes: 14 additions & 0 deletions @commitlint/rules/src/footer-max-line-length.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {maxLineLength} from '@commitlint/ensure';

export default (parsed, when, value) => {
const input = parsed.footer;

if (!input) {
return [true];
}

return [
maxLineLength(input, value),
`footer's lines must not be longer than ${value} characters`
];
};
60 changes: 60 additions & 0 deletions @commitlint/rules/src/footer-max-line-length.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import test from 'ava';
import parse from '@commitlint/parse';
import check from './footer-max-line-length';

const short = 'BREAKING CHANGE: a';
const long = 'BREAKING CHANGE: ab';

const value = short.length;

const messages = {
simple: 'test: subject',
empty: 'test: subject\nbody',
short: `test: subject\n${short}`,
long: `test: subject\n${long}`,
shortMultipleLines: `test:subject\n${short}\n${short}\n${short}`,
longMultipleLines: `test:subject\n${short}\n${long}\n${short}`
};

const parsed = {
simple: parse(messages.simple),
empty: parse(messages.empty),
short: parse(messages.short),
long: parse(messages.long)
};

test('with simple should succeed', async t => {
const [actual] = check(await parsed.simple, '', value);
const expected = true;
t.is(actual, expected);
});

test('with empty should succeed', async t => {
const [actual] = check(await parsed.empty, '', value);
const expected = true;
t.is(actual, expected);
});

test('with short should succeed', async t => {
const [actual] = check(await parsed.short, '', value);
const expected = true;
t.is(actual, expected);
});

test('with long should fail', async t => {
const [actual] = check(await parsed.long, '', value);
const expected = false;
t.is(actual, expected);
});

test('with short with multiple lines should succeed', async t => {
const [actual] = check(await parsed.short, '', value);
const expected = true;
t.is(actual, expected);
});

test('with long with multiple lines should fail', async t => {
const [actual] = check(await parsed.long, '', value);
const expected = false;
t.is(actual, expected);
});
2 changes: 2 additions & 0 deletions @commitlint/rules/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ export default {
'body-empty': require('./body-empty'),
'body-leading-blank': require('./body-leading-blank'),
'body-max-length': require('./body-max-length'),
'body-max-line-length': require('./body-max-line-length'),
'body-min-length': require('./body-min-length'),
'body-tense': require('./body-tense'),
'footer-empty': require('./footer-empty'),
'footer-leading-blank': require('./footer-leading-blank'),
'footer-max-length': require('./footer-max-length'),
'footer-max-line-length': require('./footer-max-line-length'),
'footer-min-length': require('./footer-min-length'),
'footer-tense': require('./footer-tense'),
'header-max-length': require('./header-max-length'),
Expand Down
16 changes: 16 additions & 0 deletions docs/reference-rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ Rule configurations are either of type `array` residing on a key with the rule's
Infinity
```

#### body-max-line-length
* **condition**: `body` lines has `value` or less characters
* **rule**: `always`
* **value**
```js
Infinity
```

#### body-min-length
* **condition**: `body` has `value` or more characters
* **rule**: `always`
Expand All @@ -66,6 +74,14 @@ Rule configurations are either of type `array` residing on a key with the rule's
Infinity
```

#### footer-max-line-length
* **condition**: `footer` lines has `value` or less characters
* **rule**: `always`
* **value**
```js
Infinity
```

#### footer-min-length
* **condition**: `footer` has `value` or more characters
* **rule**: `always`
Expand Down

0 comments on commit 542f50e

Please sign in to comment.