Skip to content

Commit aa61745

Browse files
committed
refactor(core): unify line splitting
1 parent cefeb74 commit aa61745

File tree

5 files changed

+36
-13
lines changed

5 files changed

+36
-13
lines changed

Diff for: @commitlint/core/src/library/to-lines.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default function toLines(input) {
2+
if (typeof input === 'undefined') {
3+
return [];
4+
}
5+
6+
return input.split(/(?:\r?\n)/);
7+
}

Diff for: @commitlint/core/src/library/to-lines.test.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import test from 'ava';
2+
import toLines from './to-lines';
3+
4+
test('should return an array for empty input', t => {
5+
t.deepEqual(toLines(), []);
6+
});
7+
8+
test('should return an array for empty string input', t => {
9+
t.deepEqual(toLines(''), ['']);
10+
});
11+
12+
test('should split LF newlines', t => {
13+
t.deepEqual(toLines('some\nweird\ntext'), ['some', 'weird', 'text']);
14+
});
15+
16+
test('should split CR+LF newlines', t => {
17+
t.deepEqual(toLines('some\r\nweird\r\ntext'), ['some', 'weird', 'text']);
18+
});

Diff for: @commitlint/core/src/rules/body-leading-blank.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1+
import toLines from '../library/to-lines';
2+
13
export default (parsed, when) => {
24
// Flunk if no body is found
35
if (!parsed.body) {
46
return [true];
57
}
68

79
const negated = when === 'never';
8-
9-
// Get complete body split into lines
10-
const lines = (parsed.raw || '').split(/\r|\n/).slice(1);
11-
const [leading] = lines;
10+
const [leading] = toLines(parsed.raw).slice(1);
1211

1312
// Check if the first line of body is empty
1413
const succeeds = leading === '';

Diff for: @commitlint/core/src/rules/footer-leading-blank.js

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
1+
import toLines from '../library/to-lines';
2+
13
export default (parsed, when) => {
24
// Flunk if no footer is found
35
if (!parsed.footer) {
46
return [true];
57
}
68

79
const negated = when === 'never';
8-
9-
const count = (parsed.body || '').split(/\r|\n/).length;
10-
11-
// Get complete message split into lines
12-
const lines = (parsed.raw || '').split(/\r|\n/).slice(count + 1);
13-
14-
const [leading] = lines;
10+
const [leading] = toLines(parsed.raw).slice(toLines(parsed.body).length + 1);
1511

1612
// Check if the first line of footer is empty
1713
const succeeds = leading === '';

Diff for: @commitlint/core/src/rules/signed-off-by.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import toLines from '../library/to-lines';
2+
13
export default (parsed, when, value) => {
2-
const input = /.*\n(Signed-off-by:).*\n+/g.exec(parsed.raw);
4+
const lines = toLines(parsed.raw).filter(Boolean);
5+
const last = lines[lines.length - 1];
36

47
const negated = when === 'never';
5-
const hasSignedOffBy = Boolean(input && input[1] === value);
8+
const hasSignedOffBy = last.startsWith(value);
69

710
return [
811
negated ? !hasSignedOffBy : hasSignedOffBy,

0 commit comments

Comments
 (0)