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: failing sentence-case for subjects with slashes #574

Merged
merged 3 commits into from
Feb 11, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is moved to the scope-case rule.

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)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We still need to check if the segment here is the delimiter itself. But I think this looks pretty ok!

);

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');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the full commit described from #291, instead of Modules/Graph only.

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);
});