Skip to content

Commit

Permalink
feat: enable multiple scopes in scope-enum and scope-case rules (#901)
Browse files Browse the repository at this point in the history
  • Loading branch information
karl-run committed Jun 21, 2020
1 parent 94f0b53 commit 73632ce
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
4 changes: 2 additions & 2 deletions @commitlint/rules/src/scope-case.ts
Expand Up @@ -25,9 +25,9 @@ export const scopeCase: SyncRule<TargetCaseType | TargetCaseType[]> = (
return check;
});

// Scopes may contain slash-delimiters to separate them and mark them as individual segments.
// Scopes may contain slash or comma delimiters to separate them and mark them as individual segments.
// This means that each of these segments should be tested separately with `ensure`.
const delimiters = /(\/|\\)/g;
const delimiters = /\/|\\|,/g;
const scopeSegments = scope.split(delimiters);

const result = checks.some(check => {
Expand Down
24 changes: 22 additions & 2 deletions @commitlint/rules/src/scope-enum.test.ts
Expand Up @@ -4,13 +4,15 @@ import {scopeEnum} from './scope-enum';
const messages = {
plain: 'foo(bar): baz',
superfluous: 'foo(): baz',
empty: 'foo: baz'
empty: 'foo: baz',
multiple: 'foo(bar,baz): qux'
};

const parsed = {
plain: parse(messages.plain),
superfluous: parse(messages.superfluous),
empty: parse(messages.empty)
empty: parse(messages.empty),
multiple: parse(messages.multiple)
};

test('scope-enum with plain message and always should succeed empty enum', async () => {
Expand Down Expand Up @@ -90,3 +92,21 @@ test('scope-enum with empty scope and never should succeed empty enum', async ()
const expected = true;
expect(actual).toEqual(expected);
});

test('scope-enum with multiple scope should succeed on message with multiple scope', async () => {
const [actual] = scopeEnum(await parsed.multiple, 'never', ['bar', 'baz']);
const expected = false;
expect(actual).toEqual(expected);
});

test('scope-enum with multiple scope should error on message with forbidden enum', async () => {
const [actual] = scopeEnum(await parsed.multiple, 'never', ['bar', 'qux']);
const expected = true;
expect(actual).toEqual(expected);
});

test('scope-enum with multiple scope should error on message with superfluous scope', async () => {
const [actual] = scopeEnum(await parsed.multiple, 'never', ['bar']);
const expected = true;
expect(actual).toEqual(expected);
});
9 changes: 8 additions & 1 deletion @commitlint/rules/src/scope-enum.ts
Expand Up @@ -11,8 +11,15 @@ export const scopeEnum: SyncRule<string[]> = (
return [true, ''];
}

// Scopes may contain slash or comma delimiters to separate them and mark them as individual segments.
// This means that each of these segments should be tested separately with `ensure`.
const delimiters = /\/|\\|,/g;
const scopeSegments = parsed.scope.split(delimiters);

const negated = when === 'never';
const result = value.length === 0 || ensure.enum(parsed.scope, value);
const result =
value.length === 0 ||
scopeSegments.every(scope => ensure.enum(scope, value));

return [
negated ? !result : result,
Expand Down

0 comments on commit 73632ce

Please sign in to comment.