Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: port rules to typescript #785

Merged
merged 7 commits into from
Jan 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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