Skip to content

Update type-empty rule #3047

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

Closed
wants to merge 4 commits into from
Closed
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
60 changes: 60 additions & 0 deletions @commitlint/rules/src/type-empty.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@ import {typeEmpty} from './type-empty';
const messages = {
empty: '(scope):',
filled: 'type: subject',
fullyFilled: 'type(scope): subject',
subjectOnly: 'subject',
separator: ': subject',
};

const parsed = {
empty: parse(messages.empty),
filled: parse(messages.filled),
fullyFilled: parse(messages.fullyFilled),
subjectOnly: parse(messages.subjectOnly),
separator: parse(messages.separator),
};

test('without type should succeed for empty keyword', async () => {
Expand Down Expand Up @@ -46,3 +52,57 @@ test('with type fail for "always"', async () => {
const expected = false;
expect(actual).toEqual(expected);
});

test('with title, scope and subject should fail for empty keyword', async () => {
const [actual] = typeEmpty(await parsed.fullyFilled);
const expected = false;
expect(actual).toEqual(expected);
});

test('with title, scope and subject should succeed for "never"', async () => {
const [actual] = typeEmpty(await parsed.fullyFilled, 'never');
const expected = true;
expect(actual).toEqual(expected);
});

test('with title, scope and subject should fail for "always"', async () => {
const [actual] = typeEmpty(await parsed.fullyFilled, 'always');
const expected = false;
expect(actual).toEqual(expected);
});

test('with only subject should succeed for empty keyword', async () => {
const [actual] = typeEmpty(await parsed.subjectOnly);
const expected = true;
expect(actual).toEqual(expected);
});

test('with only subject should fail for "never"', async () => {
const [actual] = typeEmpty(await parsed.subjectOnly, 'never');
const expected = false;
expect(actual).toEqual(expected);
});

test('with only subject should succeed for "always"', async () => {
const [actual] = typeEmpty(await parsed.subjectOnly, 'always');
const expected = true;
expect(actual).toEqual(expected);
});

test('without type but with a separator should fail for empty keyword', async () => {
const [actual] = typeEmpty(await parsed.separator);
const expected = false;
expect(actual).toEqual(expected);
});

test('without type but with a separator should succeed for "never"', async () => {
const [actual] = typeEmpty(await parsed.separator, 'never');
const expected = true;
expect(actual).toEqual(expected);
});

test('without type but with a separator should fail for "always"', async () => {
const [actual] = typeEmpty(await parsed.separator, 'always');
const expected = false;
expect(actual).toEqual(expected);
});
9 changes: 8 additions & 1 deletion @commitlint/rules/src/type-empty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ import {SyncRule} from '@commitlint/types';

export const typeEmpty: SyncRule = (parsed, when = 'always') => {
const negated = when === 'never';
const notEmpty = ensure.notEmpty(parsed.type || '');
const notEmptyType = ensure.notEmpty(parsed.type || '');
let notEmpty = notEmptyType;
if (
!notEmptyType &&
parsed.subject !== parsed.header &&
parsed.header.substring(0, 1) === ':'
)
notEmpty = true;
return [
negated ? notEmpty : !notEmpty,
message(['type', negated ? 'may not' : 'must', 'be empty']),
Expand Down