Skip to content

Commit

Permalink
fix: failing sentence-case for subjects with slashes (#574)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
byCedric authored Feb 11, 2019
1 parent 4075903 commit 48a8602
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
8 changes: 1 addition & 7 deletions @commitlint/ensure/src/case.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,7 @@ function ensureCase(raw = '', target = 'lowercase') {
.replace(/`.*?`|".*?"|'.*?'/g, '')
.trim();

const delimiters = /(\/|\\)/g;
const transformed = input
.split(delimiters)
.map(segment =>
delimiters.test(segment) ? segment : toCase(segment, target)
)
.join('');
const transformed = toCase(input, target);

if (transformed === '' || transformed.match(/^\d/)) {
return true;
Expand Down
5 changes: 0 additions & 5 deletions @commitlint/ensure/src/case.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,6 @@ test('true for * on pascal-case', t => {
t.is(actual, true);
});

test('true for Modules/Graph on pascal-case', t => {
const actual = ensure('Modules/Graph', 'pascal-case');
t.is(actual, true);
});

test('true for * on start-case', t => {
const actual = ensure('*', 'start-case');
t.is(actual, true);
Expand Down
10 changes: 9 additions & 1 deletion @commitlint/rules/src/scope-case.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,16 @@ export default (parsed, when, value) => {
return check;
});

// Scopes may contain slash-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 = scope.split(delimiters);

const result = checks.some(check => {
const r = ensure.case(scope, check.case);
const r = scopeSegments.every(
segment => delimiters.test(segment) || ensure.case(segment, check.case)
);

return negated(check.when) ? !r : r;
});

Expand Down
14 changes: 14 additions & 0 deletions @commitlint/rules/src/scope-case.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,3 +308,17 @@ test('with uppercase scope should fail for "never [uppercase, lowercase]"', asyn
const expected = false;
t.is(actual, expected);
});

test('with slash in scope should succeed for "always pascal-case"', async t => {
const commit = await parse('feat(Modules/Graph): add Pie Chart');
const [actual] = scopeCase(commit, 'always', 'pascal-case');
const expected = true;
t.is(actual, expected);
});

test('with slash in subject should succeed for "always sentence case"', async t => {
const commit = await parse('chore: Update @angular/core');
const [actual] = scopeCase(commit, 'always', 'sentencecase');
const expected = true;
t.is(actual, expected);
});

0 comments on commit 48a8602

Please sign in to comment.