Skip to content

Commit 4cd2208

Browse files
authored
refactor(rules): port rules to typescript (#785)
* refactor(ensure): expose target case type for rules * refactor(rules): rewrite all rules and tests to typescript * refactor(rules): refactor indentation and readability for case rules * test(rules): ignore types file and use ts extension * test(rules): import non-typed preset with require * chore: remove javascript rules pattern from jest * fix(rules): export rule types for implementing packages
1 parent 10556ec commit 4cd2208

Some content is hidden

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

74 files changed

+431
-252
lines changed

@commitlint/ensure/src/case.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as _ from 'lodash';
22

33
export default ensureCase;
44

5-
type TargetCaseType =
5+
export type TargetCaseType =
66
| 'camel-case'
77
| 'kebab-case'
88
| 'snake-case'

@commitlint/ensure/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ import notEmpty from './not-empty';
88
export {ensureCase as case};
99
export {ensureEnum as enum};
1010
export {maxLength, maxLineLength, minLength, notEmpty};
11+
export {TargetCaseType} from './case';

@commitlint/rules/package.json

+2-9
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,13 @@
33
"version": "8.3.4",
44
"description": "Lint your commit messages",
55
"main": "lib/index.js",
6+
"types": "lib/index.d.ts",
67
"files": [
78
"lib/"
89
],
910
"scripts": {
10-
"build": "cross-env NODE_ENV=production babel src --out-dir lib --source-maps",
1111
"deps": "dep-check",
12-
"pkg": "pkg-check --skip-import",
13-
"start": "concurrently \"ava -c 4 --verbose --watch\" \"yarn run watch\"",
14-
"watch": "babel src --out-dir lib --watch --source-maps"
15-
},
16-
"babel": {
17-
"presets": [
18-
"babel-preset-commitlint"
19-
]
12+
"pkg": "pkg-check"
2013
},
2114
"engines": {
2215
"node": ">=4"

@commitlint/rules/src/body-case.test.js renamed to @commitlint/rules/src/body-case.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import parse from '@commitlint/parse';
2-
import bodyCase from './body-case';
2+
import {bodyCase} from './body-case';
33

44
const messages = {
55
empty: 'test: subject',

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import * as ensure from '@commitlint/ensure';
22
import message from '@commitlint/message';
3+
import {Rule} from './types';
34

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

712
if (!body) {

@commitlint/rules/src/body-empty.test.js renamed to @commitlint/rules/src/body-empty.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import parse from '@commitlint/parse';
2-
import bodyEmpty from './body-empty';
2+
import {bodyEmpty} from './body-empty';
33

44
const messages = {
55
empty: 'test: subject',

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
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+
export const bodyEmpty: Rule = (parsed, when = 'always') => {
56
const negated = when === 'never';
6-
const notEmpty = ensure.notEmpty(parsed.body);
7+
const notEmpty = ensure.notEmpty(parsed.body || '');
78

89
return [
910
negated ? notEmpty : !notEmpty,

@commitlint/rules/src/body-leading-blank.test.js renamed to @commitlint/rules/src/body-leading-blank.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import parse from '@commitlint/parse';
2-
import bodyLeadingBlank from './body-leading-blank';
2+
import {bodyLeadingBlank} from './body-leading-blank';
33

44
const messages = {
55
simple: 'test: subject',

@commitlint/rules/src/body-leading-blank.js renamed to @commitlint/rules/src/body-leading-blank.ts

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

4-
export default (parsed, when) => {
5+
export const bodyLeadingBlank: Rule = (parsed, when) => {
56
// Flunk if no body is found
67
if (!parsed.body) {
78
return [true];

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import parse from '@commitlint/parse';
2-
import check from './body-max-length';
2+
import {bodyMaxLength} from './body-max-length';
33

44
const short = 'a';
55
const long = 'ab';
@@ -19,19 +19,19 @@ const parsed = {
1919
};
2020

2121
test('with empty should succeed', async () => {
22-
const [actual] = check(await parsed.empty, '', value);
22+
const [actual] = bodyMaxLength(await parsed.empty, undefined, value);
2323
const expected = true;
2424
expect(actual).toEqual(expected);
2525
});
2626

2727
test('with short should succeed', async () => {
28-
const [actual] = check(await parsed.short, '', value);
28+
const [actual] = bodyMaxLength(await parsed.short, undefined, value);
2929
const expected = true;
3030
expect(actual).toEqual(expected);
3131
});
3232

3333
test('with long should fail', async () => {
34-
const [actual] = check(await parsed.long, '', value);
34+
const [actual] = bodyMaxLength(await parsed.long, undefined, value);
3535
const expected = false;
3636
expect(actual).toEqual(expected);
3737
});

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

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

3-
export default (parsed, when, value) => {
4+
export const bodyMaxLength: Rule<number> = (
5+
parsed,
6+
when = undefined,
7+
value = 0
8+
) => {
49
const input = parsed.body;
510

611
if (!input) {

@commitlint/rules/src/body-max-line-length.test.js renamed to @commitlint/rules/src/body-max-line-length.test.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import parse from '@commitlint/parse';
2-
import check from './body-max-line-length';
2+
import {bodyMaxLineLength} from './body-max-line-length';
33

44
const short = 'a';
55
const long = 'ab';
@@ -21,31 +21,31 @@ const parsed = {
2121
};
2222

2323
test('with empty should succeed', async () => {
24-
const [actual] = check(await parsed.empty, '', value);
24+
const [actual] = bodyMaxLineLength(await parsed.empty, undefined, value);
2525
const expected = true;
2626
expect(actual).toEqual(expected);
2727
});
2828

2929
test('with short should succeed', async () => {
30-
const [actual] = check(await parsed.short, '', value);
30+
const [actual] = bodyMaxLineLength(await parsed.short, undefined, value);
3131
const expected = true;
3232
expect(actual).toEqual(expected);
3333
});
3434

3535
test('with long should fail', async () => {
36-
const [actual] = check(await parsed.long, '', value);
36+
const [actual] = bodyMaxLineLength(await parsed.long, undefined, value);
3737
const expected = false;
3838
expect(actual).toEqual(expected);
3939
});
4040

4141
test('with short with multiple lines should succeed', async () => {
42-
const [actual] = check(await parsed.short, '', value);
42+
const [actual] = bodyMaxLineLength(await parsed.short, undefined, value);
4343
const expected = true;
4444
expect(actual).toEqual(expected);
4545
});
4646

4747
test('with long with multiple lines should fail', async () => {
48-
const [actual] = check(await parsed.long, '', value);
48+
const [actual] = bodyMaxLineLength(await parsed.long, undefined, value);
4949
const expected = false;
5050
expect(actual).toEqual(expected);
5151
});

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

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

3-
export default (parsed, when, value) => {
4+
export const bodyMaxLineLength: Rule<number> = (
5+
parsed,
6+
when = undefined,
7+
value = 0
8+
) => {
49
const input = parsed.body;
510

611
if (!input) {

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import parse from '@commitlint/parse';
2-
import check from './body-min-length';
2+
import {bodyMinLength} from './body-min-length';
33

44
const short = 'a';
55
const long = 'ab';
@@ -19,19 +19,19 @@ const parsed = {
1919
};
2020

2121
test('with simple should succeed', async () => {
22-
const [actual] = check(await parsed.simple, '', value);
22+
const [actual] = bodyMinLength(await parsed.simple, undefined, value);
2323
const expected = true;
2424
expect(actual).toEqual(expected);
2525
});
2626

2727
test('with short should fail', async () => {
28-
const [actual] = check(await parsed.short, '', value);
28+
const [actual] = bodyMinLength(await parsed.short, undefined, value);
2929
const expected = false;
3030
expect(actual).toEqual(expected);
3131
});
3232

3333
test('with long should succeed', async () => {
34-
const [actual] = check(await parsed.long, '', value);
34+
const [actual] = bodyMinLength(await parsed.long, undefined, value);
3535
const expected = true;
3636
expect(actual).toEqual(expected);
3737
});

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

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

3-
export default (parsed, when, value) => {
4+
export const bodyMinLength: Rule<number> = (
5+
parsed,
6+
when = undefined,
7+
value = 0
8+
) => {
49
if (!parsed.body) {
510
return [true];
611
}

@commitlint/rules/src/footer-empty.test.js renamed to @commitlint/rules/src/footer-empty.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import parse from '@commitlint/parse';
2-
import footerEmpty from './footer-empty';
2+
import {footerEmpty} from './footer-empty';
33

44
const messages = {
55
simple: 'test: subject',

@commitlint/rules/src/footer-empty.js renamed to @commitlint/rules/src/footer-empty.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
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+
export 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,

@commitlint/rules/src/footer-leading-blank.test.js renamed to @commitlint/rules/src/footer-leading-blank.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import parse from '@commitlint/parse';
2-
import footerLeadingBlank from './footer-leading-blank';
2+
import {footerLeadingBlank} from './footer-leading-blank';
33

44
const messages = {
55
simple: 'test: subject',

@commitlint/rules/src/footer-leading-blank.js renamed to @commitlint/rules/src/footer-leading-blank.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import toLines from '@commitlint/to-lines';
22
import message from '@commitlint/message';
3+
import {Rule} from './types';
34

4-
export default (parsed, when) => {
5+
export const footerLeadingBlank: Rule = (parsed, when = 'always') => {
56
// Flunk if no footer is found
67
if (!parsed.footer) {
78
return [true];
89
}
910

1011
const negated = when === 'never';
1112
const rawLines = toLines(parsed.raw);
12-
const bodyLines = toLines(parsed.body);
13+
const bodyLines = parsed.body ? toLines(parsed.body) : [];
1314
const bodyOffset = bodyLines.length > 0 ? rawLines.indexOf(bodyLines[0]) : 1;
1415
const [leading] = rawLines.slice(bodyLines.length + bodyOffset);
1516

@commitlint/rules/src/footer-max-length.test.js renamed to @commitlint/rules/src/footer-max-length.test.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import parse from '@commitlint/parse';
2-
import check from './footer-max-length';
2+
import {footerMaxLength} from './footer-max-length';
33

44
const short = 'BREAKING CHANGE: a';
55
const long = 'BREAKING CHANGE: ab';
@@ -21,25 +21,25 @@ const parsed = {
2121
};
2222

2323
test('with simple should succeed', async () => {
24-
const [actual] = check(await parsed.simple, '', value);
24+
const [actual] = footerMaxLength(await parsed.simple, undefined, value);
2525
const expected = true;
2626
expect(actual).toEqual(expected);
2727
});
2828

2929
test('with empty should succeed', async () => {
30-
const [actual] = check(await parsed.empty, '', value);
30+
const [actual] = footerMaxLength(await parsed.empty, undefined, value);
3131
const expected = true;
3232
expect(actual).toEqual(expected);
3333
});
3434

3535
test('with short should succeed', async () => {
36-
const [actual] = check(await parsed.short, '', value);
36+
const [actual] = footerMaxLength(await parsed.short, undefined, value);
3737
const expected = true;
3838
expect(actual).toEqual(expected);
3939
});
4040

4141
test('with long should fail', async () => {
42-
const [actual] = check(await parsed.long, '', value);
42+
const [actual] = footerMaxLength(await parsed.long, undefined, value);
4343
const expected = false;
4444
expect(actual).toEqual(expected);
4545
});

@commitlint/rules/src/footer-max-length.js renamed to @commitlint/rules/src/footer-max-length.ts

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

3-
export default (parsed, when, value) => {
4+
export const footerMaxLength: Rule<number> = (
5+
parsed,
6+
when = undefined,
7+
value = 0
8+
) => {
49
const input = parsed.footer;
510

611
if (!input) {

@commitlint/rules/src/footer-max-line-length.test.js renamed to @commitlint/rules/src/footer-max-line-length.test.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import parse from '@commitlint/parse';
2-
import check from './footer-max-line-length';
2+
import {footerMaxLineLength} from './footer-max-line-length';
33

44
const short = 'BREAKING CHANGE: a';
55
const long = 'BREAKING CHANGE: ab';
@@ -23,37 +23,37 @@ const parsed = {
2323
};
2424

2525
test('with simple should succeed', async () => {
26-
const [actual] = check(await parsed.simple, '', value);
26+
const [actual] = footerMaxLineLength(await parsed.simple, undefined, value);
2727
const expected = true;
2828
expect(actual).toEqual(expected);
2929
});
3030

3131
test('with empty should succeed', async () => {
32-
const [actual] = check(await parsed.empty, '', value);
32+
const [actual] = footerMaxLineLength(await parsed.empty, undefined, value);
3333
const expected = true;
3434
expect(actual).toEqual(expected);
3535
});
3636

3737
test('with short should succeed', async () => {
38-
const [actual] = check(await parsed.short, '', value);
38+
const [actual] = footerMaxLineLength(await parsed.short, undefined, value);
3939
const expected = true;
4040
expect(actual).toEqual(expected);
4141
});
4242

4343
test('with long should fail', async () => {
44-
const [actual] = check(await parsed.long, '', value);
44+
const [actual] = footerMaxLineLength(await parsed.long, undefined, value);
4545
const expected = false;
4646
expect(actual).toEqual(expected);
4747
});
4848

4949
test('with short with multiple lines should succeed', async () => {
50-
const [actual] = check(await parsed.short, '', value);
50+
const [actual] = footerMaxLineLength(await parsed.short, undefined, value);
5151
const expected = true;
5252
expect(actual).toEqual(expected);
5353
});
5454

5555
test('with long with multiple lines should fail', async () => {
56-
const [actual] = check(await parsed.long, '', value);
56+
const [actual] = footerMaxLineLength(await parsed.long, undefined, value);
5757
const expected = false;
5858
expect(actual).toEqual(expected);
5959
});

@commitlint/rules/src/footer-max-line-length.js renamed to @commitlint/rules/src/footer-max-line-length.ts

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

3-
export default (parsed, when, value) => {
4+
export const footerMaxLineLength: Rule<number> = (
5+
parsed,
6+
when = undefined,
7+
value = 0
8+
) => {
49
const input = parsed.footer;
510

611
if (!input) {

0 commit comments

Comments
 (0)