Skip to content

Commit 48a8602

Browse files
authored
fix: failing sentence-case for subjects with slashes (#574)
* test(ensure): add failing subject-slash scenario for sentence-case * refactor(ensure): remove special slash delimiter code * feat(rules): add special scope segments to scope case rule
1 parent 4075903 commit 48a8602

File tree

4 files changed

+24
-13
lines changed

4 files changed

+24
-13
lines changed

@commitlint/ensure/src/case.js

+1-7
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,7 @@ function ensureCase(raw = '', target = 'lowercase') {
99
.replace(/`.*?`|".*?"|'.*?'/g, '')
1010
.trim();
1111

12-
const delimiters = /(\/|\\)/g;
13-
const transformed = input
14-
.split(delimiters)
15-
.map(segment =>
16-
delimiters.test(segment) ? segment : toCase(segment, target)
17-
)
18-
.join('');
12+
const transformed = toCase(input, target);
1913

2014
if (transformed === '' || transformed.match(/^\d/)) {
2115
return true;

@commitlint/ensure/src/case.test.js

-5
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,6 @@ test('true for * on pascal-case', t => {
110110
t.is(actual, true);
111111
});
112112

113-
test('true for Modules/Graph on pascal-case', t => {
114-
const actual = ensure('Modules/Graph', 'pascal-case');
115-
t.is(actual, true);
116-
});
117-
118113
test('true for * on start-case', t => {
119114
const actual = ensure('*', 'start-case');
120115
t.is(actual, true);

@commitlint/rules/src/scope-case.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,16 @@ export default (parsed, when, value) => {
2020
return check;
2121
});
2222

23+
// Scopes may contain slash-delimiters to separate them and mark them as individual segments.
24+
// This means that each of these segments should be tested separately with `ensure`.
25+
const delimiters = /(\/|\\)/g;
26+
const scopeSegments = scope.split(delimiters);
27+
2328
const result = checks.some(check => {
24-
const r = ensure.case(scope, check.case);
29+
const r = scopeSegments.every(
30+
segment => delimiters.test(segment) || ensure.case(segment, check.case)
31+
);
32+
2533
return negated(check.when) ? !r : r;
2634
});
2735

@commitlint/rules/src/scope-case.test.js

+14
Original file line numberDiff line numberDiff line change
@@ -308,3 +308,17 @@ test('with uppercase scope should fail for "never [uppercase, lowercase]"', asyn
308308
const expected = false;
309309
t.is(actual, expected);
310310
});
311+
312+
test('with slash in scope should succeed for "always pascal-case"', async t => {
313+
const commit = await parse('feat(Modules/Graph): add Pie Chart');
314+
const [actual] = scopeCase(commit, 'always', 'pascal-case');
315+
const expected = true;
316+
t.is(actual, expected);
317+
});
318+
319+
test('with slash in subject should succeed for "always sentence case"', async t => {
320+
const commit = await parse('chore: Update @angular/core');
321+
const [actual] = scopeCase(commit, 'always', 'sentencecase');
322+
const expected = true;
323+
t.is(actual, expected);
324+
});

0 commit comments

Comments
 (0)