Skip to content

Commit 382c6d3

Browse files
committed
refactor(rules): port next batch of rules to ts
1 parent 36ad0a4 commit 382c6d3

File tree

75 files changed

+1313
-1557
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+1313
-1557
lines changed

@commitlint/rules/src/body-case.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import * as ensure from '@commitlint/ensure';
22
import message from '@commitlint/message';
33
import {Rule} from './types';
44

5-
const bodyCase: Rule<ensure.TargetCaseType> = (parsed, when, value) => {
5+
const bodyCase: Rule<ensure.TargetCaseType> = (
6+
parsed,
7+
when = 'always',
8+
value = undefined
9+
) => {
610
const {body} = parsed;
711

812
if (!body) {

@commitlint/rules/src/body-empty.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as ensure from '@commitlint/ensure';
22
import message from '@commitlint/message';
33
import {Rule} from './types';
44

5-
const bodyEmpty: Rule = (parsed, when) => {
5+
const bodyEmpty: Rule = (parsed, when = 'always') => {
66
const negated = when === 'never';
77
const notEmpty = ensure.notEmpty(parsed.body || '');
88

@commitlint/rules/src/body-leading-blank.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import toLines from '@commitlint/to-lines';
22
import message from '@commitlint/message';
33
import {Rule} from './types';
44

5-
const bodyLeadingBlank: Rule = (parsed, when) => {
5+
const bodyLeadingBlank: Rule = (parsed, when = 'always') => {
66
// Flunk if no body is found
77
if (!parsed.body) {
88
return [true];

@commitlint/rules/src/body-max-length.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import {maxLength} from '@commitlint/ensure';
22
import {Rule} from './types';
33

4-
const bodyMaxLength: Rule<number> = (parsed, when, value) => {
4+
const bodyMaxLength: Rule<number> = (parsed, when = 'always', value = 0) => {
55
const input = parsed.body;
66

7-
if (!input || value == undefined) {
7+
if (!input) {
88
return [true];
99
}
1010

@commitlint/rules/src/body-max-line-length.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import {maxLineLength} from '@commitlint/ensure';
22
import {Rule} from './types';
33

4-
const bodyMaxLineLength: Rule<number> = (parsed, when, value) => {
4+
const bodyMaxLineLength: Rule<number> = (
5+
parsed,
6+
when = 'always',
7+
value = 0
8+
) => {
59
const input = parsed.body;
610

7-
if (!input || value === undefined) {
11+
if (!input) {
812
return [true];
913
}
1014

@commitlint/rules/src/body-min-length.test.js

-38
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import parse from '@commitlint/parse';
2+
import check from './body-min-length';
3+
4+
const short = 'a';
5+
const long = 'ab';
6+
7+
const value = long.length;
8+
9+
const messages = {
10+
simple: 'test: subject',
11+
short: `test: subject\n${short}`,
12+
long: `test: subject\n${long}`
13+
};
14+
15+
const parsed = {
16+
simple: parse(messages.simple),
17+
short: parse(messages.short),
18+
long: parse(messages.long)
19+
};
20+
21+
test('with simple should succeed', async () => {
22+
const [actual] = check(await parsed.simple, 'always', value);
23+
expect(actual).toBeTruthy();
24+
});
25+
26+
test('with short should fail', async () => {
27+
const [actual] = check(await parsed.short, 'always', value);
28+
expect(actual).toBeFalsy();
29+
});
30+
31+
test('with long should succeed', async () => {
32+
const [actual] = check(await parsed.long, 'always', value);
33+
expect(actual).toBeTruthy();
34+
});

@commitlint/rules/src/body-min-length.js renamed to @commitlint/rules/src/body-min-length.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {minLength} from '@commitlint/ensure';
2+
import {Rule} from './types';
23

3-
export default (parsed, when, value) => {
4+
const bodyMinLength: Rule<number> = (parsed, when = 'always', value = 0) => {
45
if (!parsed.body) {
56
return [true];
67
}
@@ -10,3 +11,5 @@ export default (parsed, when, value) => {
1011
`body must not be shorter than ${value} characters`
1112
];
1213
};
14+
15+
export default bodyMinLength;
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import test from 'ava';
21
import parse from '@commitlint/parse';
32
import footerEmpty from './footer-empty';
43

@@ -14,56 +13,47 @@ const parsed = {
1413
filled: parse(messages.filled)
1514
};
1615

17-
test('with simple message should succeed for empty keyword', async t => {
16+
test('with simple message should succeed for empty keyword', async () => {
1817
const [actual] = footerEmpty(await parsed.simple);
19-
const expected = true;
20-
t.is(actual, expected);
18+
expect(actual).toBeTruthy();
2119
});
2220

23-
test('with simple message should fail for "never"', async t => {
21+
test('with simple message should fail for "never"', async () => {
2422
const [actual] = footerEmpty(await parsed.simple, 'never');
25-
const expected = false;
26-
t.is(actual, expected);
23+
expect(actual).toBeFalsy();
2724
});
2825

29-
test('with simple message should succeed for "always"', async t => {
26+
test('with simple message should succeed for "always"', async () => {
3027
const [actual] = footerEmpty(await parsed.simple, 'always');
31-
const expected = true;
32-
t.is(actual, expected);
28+
expect(actual).toBeTruthy();
3329
});
3430

35-
test('with empty footer should succeed for empty keyword', async t => {
31+
test('with empty footer should succeed for empty keyword', async () => {
3632
const [actual] = footerEmpty(await parsed.empty);
37-
const expected = true;
38-
t.is(actual, expected);
33+
expect(actual).toBeTruthy();
3934
});
4035

41-
test('with empty footer should fail for "never"', async t => {
36+
test('with empty footer should fail for "never"', async () => {
4237
const [actual] = footerEmpty(await parsed.empty, 'never');
43-
const expected = false;
44-
t.is(actual, expected);
38+
expect(actual).toBeFalsy();
4539
});
4640

47-
test('with empty footer should succeed for "always"', async t => {
41+
test('with empty footer should succeed for "always"', async () => {
4842
const [actual] = footerEmpty(await parsed.empty, 'always');
49-
const expected = true;
50-
t.is(actual, expected);
43+
expect(actual).toBeTruthy();
5144
});
5245

53-
test('with footer should fail for empty keyword', async t => {
46+
test('with footer should fail for empty keyword', async () => {
5447
const [actual] = footerEmpty(await parsed.filled);
55-
const expected = false;
56-
t.is(actual, expected);
48+
expect(actual).toBeFalsy();
5749
});
5850

59-
test('with footer should succeed for "never"', async t => {
51+
test('with footer should succeed for "never"', async () => {
6052
const [actual] = footerEmpty(await parsed.filled, 'never');
61-
const expected = true;
62-
t.is(actual, expected);
53+
expect(actual).toBeTruthy();
6354
});
6455

65-
test('with footer should fail for "always"', async t => {
56+
test('with footer should fail for "always"', async () => {
6657
const [actual] = footerEmpty(await parsed.filled, 'always');
67-
const expected = false;
68-
t.is(actual, expected);
58+
expect(actual).toBeFalsy();
6959
});
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import * as ensure from '@commitlint/ensure';
22
import message from '@commitlint/message';
3+
import {Rule} from './types';
34

4-
export default (parsed, when) => {
5+
const footerEmpty: Rule = (parsed, when = 'always') => {
56
const negated = when === 'never';
6-
const notEmpty = ensure.notEmpty(parsed.footer);
7+
const notEmpty = ensure.notEmpty(parsed.footer || '');
78

89
return [
910
negated ? notEmpty : !notEmpty,
1011
message(['footer', negated ? 'may not' : 'must', 'be empty'])
1112
];
1213
};
14+
15+
export default footerEmpty;

0 commit comments

Comments
 (0)