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 parse to typescript #764

Merged
merged 6 commits into from Sep 11, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 9 additions & 10 deletions @commitlint/parse/src/index.test.ts
Expand Up @@ -2,11 +2,7 @@ import importFrom from 'import-from';
import parse from '.';

test('throws when called without params', () => {
expect(parse()).rejects.toThrowError('Expected a raw commit');
});

test('throws when called with empty message', () => {
expect(parse()).rejects.toThrowError('Expected a raw commit');
expect(parse('')).rejects.toThrowError('Expected a raw commit');
byCedric marked this conversation as resolved.
Show resolved Hide resolved
});

test('returns object with raw message', async () => {
Expand All @@ -20,15 +16,18 @@ test('calls parser with message and passed options', async () => {
const message = 'message';

expect.assertions(1);
await parse(message, (m: string) => {
expect(m).toBe(message);
return {};
});
await parse(
message,
(m: string): any => {
expect(m).toBe(message);
return {};
}
);
});

test('passes object up from parser function', async () => {
const message = 'message';
const result = {};
const result: any = {};
const actual = await parse(message, () => result);

expect(actual).toBe(result);
Expand Down
16 changes: 7 additions & 9 deletions @commitlint/parse/src/index.ts
@@ -1,5 +1,5 @@
import {isArray, mergeWith} from 'lodash';
import {Commit, ParserOptions} from './types';
import {Commit, Parser, ParserOptions} from './types';

const {sync} = require('conventional-commits-parser');
const defaultChangelogOpts = require('conventional-changelog-angular');
Expand All @@ -8,17 +8,15 @@ export default parse;
export * from './types';

async function parse(
message?: string,
parser: any = sync,
message: string,
parser: Parser = sync,
parserOpts?: ParserOptions
): Promise<Commit> {
const defaultOpts = (await defaultChangelogOpts).parserOpts;
const parsed = parser(
message,
mergeWith({}, defaultOpts, parserOpts, (objValue, srcValue) => {
if (isArray(objValue)) return srcValue;
})
);
const opts = mergeWith({}, defaultOpts, parserOpts, (objValue, srcValue) => {
if (isArray(objValue)) return srcValue;
});
const parsed = parser(message, opts) as Commit;
parsed.raw = message;
return parsed;
}
5 changes: 5 additions & 0 deletions @commitlint/parse/src/types.ts
Expand Up @@ -27,6 +27,11 @@ export interface CommitReference {
issue: string | null;
}

export type Parser = (
message: string,
options: ParserOptions
) => Omit<Commit, 'raw'>;

export interface ParserOptions {
commentChar?: string;
headerCorrespondence?: string[];
Expand Down