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

feat: support using delimiter in scope-enum #4161

Merged
merged 2 commits into from
Sep 30, 2024
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
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