Skip to content

Commit

Permalink
refactor(rules): port rules to typescript (#785)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
byCedric authored Jan 26, 2020
1 parent 10556ec commit 4cd2208
Show file tree
Hide file tree
Showing 74 changed files with 431 additions and 252 deletions.
2 changes: 1 addition & 1 deletion @commitlint/ensure/src/case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as _ from 'lodash';

export default ensureCase;

type TargetCaseType =
export type TargetCaseType =
| 'camel-case'
| 'kebab-case'
| 'snake-case'
Expand Down
1 change: 1 addition & 0 deletions @commitlint/ensure/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ import notEmpty from './not-empty';
export {ensureCase as case};
export {ensureEnum as enum};
export {maxLength, maxLineLength, minLength, notEmpty};
export {TargetCaseType} from './case';
11 changes: 2 additions & 9 deletions @commitlint/rules/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,13 @@
"version": "8.3.4",
"description": "Lint your commit messages",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"files": [
"lib/"
],
"scripts": {
"build": "cross-env NODE_ENV=production babel src --out-dir lib --source-maps",
"deps": "dep-check",
"pkg": "pkg-check --skip-import",
"start": "concurrently \"ava -c 4 --verbose --watch\" \"yarn run watch\"",
"watch": "babel src --out-dir lib --watch --source-maps"
},
"babel": {
"presets": [
"babel-preset-commitlint"
]
"pkg": "pkg-check"
},
"engines": {
"node": ">=4"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import parse from '@commitlint/parse';
import bodyCase from './body-case';
import {bodyCase} from './body-case';

const messages = {
empty: 'test: subject',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import * as ensure from '@commitlint/ensure';
import message from '@commitlint/message';
import {Rule} from './types';

export default (parsed, when, value) => {
export const bodyCase: Rule<ensure.TargetCaseType> = (
parsed,
when = 'always',
value = undefined
) => {
const {body} = parsed;

if (!body) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import parse from '@commitlint/parse';
import bodyEmpty from './body-empty';
import {bodyEmpty} from './body-empty';

const messages = {
empty: 'test: subject',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import * as ensure from '@commitlint/ensure';
import message from '@commitlint/message';
import {Rule} from './types';

export default (parsed, when) => {
export const bodyEmpty: Rule = (parsed, when = 'always') => {
const negated = when === 'never';
const notEmpty = ensure.notEmpty(parsed.body);
const notEmpty = ensure.notEmpty(parsed.body || '');

return [
negated ? notEmpty : !notEmpty,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import parse from '@commitlint/parse';
import bodyLeadingBlank from './body-leading-blank';
import {bodyLeadingBlank} from './body-leading-blank';

const messages = {
simple: 'test: subject',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import toLines from '@commitlint/to-lines';
import message from '@commitlint/message';
import {Rule} from './types';

export default (parsed, when) => {
export const bodyLeadingBlank: Rule = (parsed, when) => {
// Flunk if no body is found
if (!parsed.body) {
return [true];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import parse from '@commitlint/parse';
import check from './body-max-length';
import {bodyMaxLength} from './body-max-length';

const short = 'a';
const long = 'ab';
Expand All @@ -19,19 +19,19 @@ const parsed = {
};

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

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

test('with long should fail', async () => {
const [actual] = check(await parsed.long, '', value);
const [actual] = bodyMaxLength(await parsed.long, undefined, value);
const expected = false;
expect(actual).toEqual(expected);
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import {maxLength} from '@commitlint/ensure';
import {Rule} from './types';

export default (parsed, when, value) => {
export const bodyMaxLength: Rule<number> = (
parsed,
when = undefined,
value = 0
) => {
const input = parsed.body;

if (!input) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import parse from '@commitlint/parse';
import check from './body-max-line-length';
import {bodyMaxLineLength} from './body-max-line-length';

const short = 'a';
const long = 'ab';
Expand All @@ -21,31 +21,31 @@ const parsed = {
};

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

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

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

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

test('with long with multiple lines should fail', async () => {
const [actual] = check(await parsed.long, '', value);
const [actual] = bodyMaxLineLength(await parsed.long, undefined, value);
const expected = false;
expect(actual).toEqual(expected);
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import {maxLineLength} from '@commitlint/ensure';
import {Rule} from './types';

export default (parsed, when, value) => {
export const bodyMaxLineLength: Rule<number> = (
parsed,
when = undefined,
value = 0
) => {
const input = parsed.body;

if (!input) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import parse from '@commitlint/parse';
import check from './body-min-length';
import {bodyMinLength} from './body-min-length';

const short = 'a';
const long = 'ab';
Expand All @@ -19,19 +19,19 @@ const parsed = {
};

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

test('with short should fail', async () => {
const [actual] = check(await parsed.short, '', value);
const [actual] = bodyMinLength(await parsed.short, undefined, value);
const expected = false;
expect(actual).toEqual(expected);
});

test('with long should succeed', async () => {
const [actual] = check(await parsed.long, '', value);
const [actual] = bodyMinLength(await parsed.long, undefined, value);
const expected = true;
expect(actual).toEqual(expected);
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import {minLength} from '@commitlint/ensure';
import {Rule} from './types';

export default (parsed, when, value) => {
export const bodyMinLength: Rule<number> = (
parsed,
when = undefined,
value = 0
) => {
if (!parsed.body) {
return [true];
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import parse from '@commitlint/parse';
import footerEmpty from './footer-empty';
import {footerEmpty} from './footer-empty';

const messages = {
simple: 'test: subject',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import * as ensure from '@commitlint/ensure';
import message from '@commitlint/message';
import {Rule} from './types';

export default (parsed, when) => {
export const footerEmpty: Rule = (parsed, when = 'always') => {
const negated = when === 'never';
const notEmpty = ensure.notEmpty(parsed.footer);
const notEmpty = ensure.notEmpty(parsed.footer || '');

return [
negated ? notEmpty : !notEmpty,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import parse from '@commitlint/parse';
import footerLeadingBlank from './footer-leading-blank';
import {footerLeadingBlank} from './footer-leading-blank';

const messages = {
simple: 'test: subject',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import toLines from '@commitlint/to-lines';
import message from '@commitlint/message';
import {Rule} from './types';

export default (parsed, when) => {
export const footerLeadingBlank: Rule = (parsed, when = 'always') => {
// Flunk if no footer is found
if (!parsed.footer) {
return [true];
}

const negated = when === 'never';
const rawLines = toLines(parsed.raw);
const bodyLines = toLines(parsed.body);
const bodyLines = parsed.body ? toLines(parsed.body) : [];
const bodyOffset = bodyLines.length > 0 ? rawLines.indexOf(bodyLines[0]) : 1;
const [leading] = rawLines.slice(bodyLines.length + bodyOffset);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import parse from '@commitlint/parse';
import check from './footer-max-length';
import {footerMaxLength} from './footer-max-length';

const short = 'BREAKING CHANGE: a';
const long = 'BREAKING CHANGE: ab';
Expand All @@ -21,25 +21,25 @@ const parsed = {
};

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

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

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

test('with long should fail', async () => {
const [actual] = check(await parsed.long, '', value);
const [actual] = footerMaxLength(await parsed.long, undefined, value);
const expected = false;
expect(actual).toEqual(expected);
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import {maxLength} from '@commitlint/ensure';
import {Rule} from './types';

export default (parsed, when, value) => {
export const footerMaxLength: Rule<number> = (
parsed,
when = undefined,
value = 0
) => {
const input = parsed.footer;

if (!input) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import parse from '@commitlint/parse';
import check from './footer-max-line-length';
import {footerMaxLineLength} from './footer-max-line-length';

const short = 'BREAKING CHANGE: a';
const long = 'BREAKING CHANGE: ab';
Expand All @@ -23,37 +23,37 @@ const parsed = {
};

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

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

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

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

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

test('with long with multiple lines should fail', async () => {
const [actual] = check(await parsed.long, '', value);
const [actual] = footerMaxLineLength(await parsed.long, undefined, value);
const expected = false;
expect(actual).toEqual(expected);
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import {maxLineLength} from '@commitlint/ensure';
import {Rule} from './types';

export default (parsed, when, value) => {
export const footerMaxLineLength: Rule<number> = (
parsed,
when = undefined,
value = 0
) => {
const input = parsed.footer;

if (!input) {
Expand Down
Loading

0 comments on commit 4cd2208

Please sign in to comment.