Skip to content

Commit

Permalink
fix: support using delimiter in scope-enum
Browse files Browse the repository at this point in the history
  • Loading branch information
colinaaa committed Sep 27, 2024
1 parent 9be7624 commit c80ec53
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
21 changes: 21 additions & 0 deletions @commitlint/rules/src/scope-enum.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const messagesByScope = {
multiple: {
multiple: 'foo(bar,baz): qux',
multipleCommaSpace: 'foo(bar, baz): qux',
multipleSlash: 'foo(bar/baz): qux',
},
none: {
empty: 'foo: baz',
Expand Down Expand Up @@ -143,6 +144,16 @@ describe('Scope Enum Validation', () => {
expect(message).toEqual('scope must be one of [bar]');
});
});

test(`Succeeds with a 'multipleSlash' message when the scopes are included in enum`, async () => {
const [actual, message] = scopeEnum(
await parse(messages['multipleSlash']),
'always',
['bar/baz']
);
expect(actual).toBeTruthy();
expect(message).toEqual('scope must be one of [bar/baz]');
});
});
});

Expand Down Expand Up @@ -181,6 +192,16 @@ describe('Scope Enum Validation', () => {
expect(message).toEqual('scope must not be one of [bar, baz]');
});
});

test(`Fails with a 'multipleSlash' message when the scopes are included in enum`, async () => {
const [actual, message] = scopeEnum(
await parse(messages['multipleSlash']),
'never',
['bar/baz']
);
expect(actual).toBeFalsy();
expect(message).toEqual('scope must not be one of [bar/baz]');
});
});
});
});
4 changes: 2 additions & 2 deletions @commitlint/rules/src/scope-enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ export const scopeEnum: SyncRule<string[]> = (
let isValid;

if (when === 'never') {
isValid = !messageScopes.some(isScopeInEnum);
isValid = !messageScopes.some(isScopeInEnum) && !isScopeInEnum(scope);
errorMessage.splice(1, 0, 'not');
} else {
isValid = messageScopes.every(isScopeInEnum);
isValid = messageScopes.every(isScopeInEnum) || isScopeInEnum(scope);
}

return [isValid, message(errorMessage)];
Expand Down

0 comments on commit c80ec53

Please sign in to comment.