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

fix(rules): avoid processing strings with case-less Letter category symbols in subject-case #3586

Merged
merged 3 commits into from
Apr 14, 2023
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
20 changes: 20 additions & 0 deletions @commitlint/rules/src/subject-case.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const messages = {
lowercase: 'test: subject',
lowercase_unicode: 'test: тема', // Bulgarian for `subject`
mixedcase: 'test: sUbJeCt',
caseless: 'test: 这是一次提交', // Chinese for `this is a commit`
uppercase: 'test: SUBJECT',
uppercase_unicode: 'test: ÛNDERWERP', // Frisian for `SUBJECT`
camelcase: 'test: subJect',
Expand All @@ -29,6 +30,7 @@ const parsed = {
lowercase: parse(messages.lowercase),
lowercase_unicode: parse(messages.lowercase_unicode),
mixedcase: parse(messages.mixedcase),
caseless: parse(messages.caseless),
uppercase: parse(messages.uppercase),
uppercase_unicode: parse(messages.uppercase_unicode),
camelcase: parse(messages.camelcase),
Expand Down Expand Up @@ -115,6 +117,24 @@ test('with mixedcase subject should fail for "always uppercase"', async () => {
expect(actual).toEqual(expected);
});

test('with caseless subject should succeed for "never sentensecase"', async () => {
const [actual] = subjectCase(await parsed.caseless, 'never', 'sentense-case');
const expected = true;
expect(actual).toEqual(expected);
});

test('with caseless subject should succeed for "never uppercase"', async () => {
const [actual] = subjectCase(await parsed.caseless, 'never', 'upper-case');
const expected = true;
expect(actual).toEqual(expected);
});

test('with caseless subject should succeed for "always uppercase"', async () => {
const [actual] = subjectCase(await parsed.caseless, 'always', 'upper-case');
const expected = true;
expect(actual).toEqual(expected);
});

test('with uppercase subject should fail for "never uppercase"', async () => {
const [actual] = subjectCase(await parsed.uppercase, 'never', 'uppercase');
const expected = false;
Expand Down
10 changes: 5 additions & 5 deletions @commitlint/rules/src/subject-case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ import {TargetCaseType, SyncRule} from '@commitlint/types';

/**
* Since the rule requires first symbol of a subject to be a letter, use
* combination of Unicode `Cased_Letter` and `Other_Letter` categories now to
* allow non-Latin alphabets as well.
* Unicode `Cased_Letter` category now to allow non-Latin alphabets as well.
*
* Do not use `Letter` category directly to avoid capturing `Modifier_Letter`
* (which just modifiers letters, so we probably shouldn't anyway) and to stay
* close to previous implementation.
* (which just modifiers letters, so we probably shouldn't anyway) and
* `Other_Letter` (they actually are case-less, so they can't be validated)
* categories, and to stay close to previous implementation.
*
* Also, typescript does not seem to support almost any longhand category name
* (and even short for `Cased_Letter` too) so list all required letter
* categories manually just to prevent it from complaining about unknown stuff.
*
* @see [Unicode Categories]{@link https://www.regular-expressions.info/unicode.html}
*/
const startsWithLetterRegex = /^[\p{Ll}\p{Lu}\p{Lt}\p{Lo}]/iu;
const startsWithLetterRegex = /^[\p{Ll}\p{Lu}\p{Lt}]/iu;

const negated = (when?: string) => when === 'never';

Expand Down