Skip to content

Commit

Permalink
feat(rules): support array for scope-case and type-case
Browse files Browse the repository at this point in the history
As per issue conventional-changelog#307 port the logic from subject-case for array definitions over to scope-case and type-case.
  • Loading branch information
Simeon Cheeseman committed Mar 27, 2018
1 parent 86c34f1 commit d2a5fb5
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 9 deletions.
24 changes: 20 additions & 4 deletions @commitlint/rules/src/scope-case.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
import * as ensure from '@commitlint/ensure';
import message from '@commitlint/message';

const negated = when => when === 'never';

export default (parsed, when, value) => {
const {scope} = parsed;

if (!scope) {
return [true];
}

const negated = when === 'never';
const checks = (Array.isArray(value) ? value : [value]).map(check => {
if (typeof check === 'string') {
return {
when: 'always',
case: check
};
}
return check;
});

const result = checks.some(check => {
const r = ensure.case(scope, check.case);
return negated(check.when) ? !r : r;
});

const list = checks.map(c => c.case).join(', ');

const result = ensure.case(scope, value);
return [
negated ? !result : result,
message([`scope must`, negated ? `not` : null, `be ${value}`])
negated(when) ? !result : result,
message([`scope must`, negated(when) ? `not` : null, `be ${list}`])
];
};
57 changes: 56 additions & 1 deletion @commitlint/rules/src/scope-case.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,63 @@ test('with uppercase scope should fail for "never uppercase"', async t => {
t.is(actual, expected);
});

test('with lowercase scope should succeed for "always uppercase"', async t => {
test('with uppercase scope should succeed for "always uppercase"', async t => {
const [actual] = scopeCase(await parsed.uppercase, 'always', 'uppercase');
const expected = true;
t.is(actual, expected);
});

test('with uppercase scope should succeed for "always [uppercase, lowercase]"', async t => {
const [actual] = scopeCase(await parsed.uppercase, 'always', [
'uppercase',
'lowercase'
]);
const expected = true;
t.is(actual, expected);
});

test('with lowercase scope should succeed for "always [uppercase, lowercase]"', async t => {
const [actual] = scopeCase(await parsed.lowercase, 'always', [
'uppercase',
'lowercase'
]);
const expected = true;
t.is(actual, expected);
});

test('with mixedcase scope should fail for "always [uppercase, lowercase]"', async t => {
const [actual] = scopeCase(await parsed.mixedcase, 'always', [
'uppercase',
'lowercase'
]);
const expected = false;
t.is(actual, expected);
});

test('with mixedcase scope should pass for "always [uppercase, lowercase, camel-case]"', async t => {
const [actual] = scopeCase(await parsed.mixedcase, 'always', [
'uppercase',
'lowercase',
'camel-case'
]);
const expected = true;
t.is(actual, expected);
});

test('with mixedcase scope should pass for "never [uppercase, lowercase]"', async t => {
const [actual] = scopeCase(await parsed.mixedcase, 'never', [
'uppercase',
'lowercase'
]);
const expected = true;
t.is(actual, expected);
});

test('with uppercase scope should fail for "never [uppercase, lowercase]"', async t => {
const [actual] = scopeCase(await parsed.uppercase, 'never', [
'uppercase',
'lowercase'
]);
const expected = false;
t.is(actual, expected);
});
55 changes: 55 additions & 0 deletions @commitlint/rules/src/subject-case.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,58 @@ test('should use expected message with "never"', async t => {
);
t.true(message.indexOf('must not be upper-case') > -1);
});

test('with uppercase scope should succeed for "always [uppercase, lowercase]"', async t => {
const [actual] = subjectCase(await parsed.uppercase, 'always', [
'uppercase',
'lowercase'
]);
const expected = true;
t.is(actual, expected);
});

test('with lowercase subject should succeed for "always [uppercase, lowercase]"', async t => {
const [actual] = subjectCase(await parsed.lowercase, 'always', [
'uppercase',
'lowercase'
]);
const expected = true;
t.is(actual, expected);
});

test('with mixedcase subject should fail for "always [uppercase, lowercase]"', async t => {
const [actual] = subjectCase(await parsed.mixedcase, 'always', [
'uppercase',
'lowercase'
]);
const expected = false;
t.is(actual, expected);
});

test('with mixedcase subject should pass for "always [uppercase, lowercase, camel-case]"', async t => {
const [actual] = subjectCase(await parsed.mixedcase, 'always', [
'uppercase',
'lowercase',
'camel-case'
]);
const expected = true;
t.is(actual, expected);
});

test('with mixedcase scope should pass for "never [uppercase, lowercase]"', async t => {
const [actual] = subjectCase(await parsed.mixedcase, 'never', [
'uppercase',
'lowercase'
]);
const expected = true;
t.is(actual, expected);
});

test('with uppercase scope should fail for "never [uppercase, lowercase]"', async t => {
const [actual] = subjectCase(await parsed.uppercase, 'never', [
'uppercase',
'lowercase'
]);
const expected = false;
t.is(actual, expected);
});
24 changes: 20 additions & 4 deletions @commitlint/rules/src/type-case.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
import * as ensure from '@commitlint/ensure';
import message from '@commitlint/message';

const negated = when => when === 'never';

export default (parsed, when, value) => {
const {type} = parsed;

if (!type) {
return [true];
}

const negated = when === 'never';
const checks = (Array.isArray(value) ? value : [value]).map(check => {
if (typeof check === 'string') {
return {
when: 'always',
case: check
};
}
return check;
});

const result = checks.some(check => {
const r = ensure.case(type, check.case);
return negated(check.when) ? !r : r;
});

const list = checks.map(c => c.case).join(', ');

const result = ensure.case(type, value);
return [
negated ? !result : result,
message([`type must`, negated ? `not` : null, `be ${value}`])
negated(when) ? !result : result,
message([`type must`, negated(when) ? `not` : null, `be ${list}`])
];
};
55 changes: 55 additions & 0 deletions @commitlint/rules/src/type-case.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,58 @@ test('with startcase type should succeed for "always startcase"', async t => {
const expected = true;
t.is(actual, expected);
});

test('with uppercase scope should succeed for "always [uppercase, lowercase]"', async t => {
const [actual] = typeCase(await parsed.uppercase, 'always', [
'uppercase',
'lowercase'
]);
const expected = true;
t.is(actual, expected);
});

test('with lowercase subject should succeed for "always [uppercase, lowercase]"', async t => {
const [actual] = typeCase(await parsed.lowercase, 'always', [
'uppercase',
'lowercase'
]);
const expected = true;
t.is(actual, expected);
});

test('with mixedcase subject should fail for "always [uppercase, lowercase]"', async t => {
const [actual] = typeCase(await parsed.mixedcase, 'always', [
'uppercase',
'lowercase'
]);
const expected = false;
t.is(actual, expected);
});

test('with mixedcase subject should pass for "always [uppercase, lowercase, camel-case]"', async t => {
const [actual] = typeCase(await parsed.mixedcase, 'always', [
'uppercase',
'lowercase',
'camel-case'
]);
const expected = true;
t.is(actual, expected);
});

test('with mixedcase scope should pass for "never [uppercase, lowercase]"', async t => {
const [actual] = typeCase(await parsed.mixedcase, 'never', [
'uppercase',
'lowercase'
]);
const expected = true;
t.is(actual, expected);
});

test('with uppercase scope should fail for "never [uppercase, lowercase]"', async t => {
const [actual] = typeCase(await parsed.uppercase, 'never', [
'uppercase',
'lowercase'
]);
const expected = false;
t.is(actual, expected);
});

0 comments on commit d2a5fb5

Please sign in to comment.