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

feat(rules): support array for scope-case and type-case #312

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