Skip to content

Commit 1f46b9f

Browse files
SimeonCmarionebl
authored andcommitted
feat(rules): support array for scope-case and type-case (#312)
As per issue #307 port the logic from subject-case for array definitions over to scope-case and type-case.
1 parent ec868ef commit 1f46b9f

File tree

5 files changed

+206
-9
lines changed

5 files changed

+206
-9
lines changed

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

+20-4
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,34 @@
11
import * as ensure from '@commitlint/ensure';
22
import message from '@commitlint/message';
33

4+
const negated = when => when === 'never';
5+
46
export default (parsed, when, value) => {
57
const {scope} = parsed;
68

79
if (!scope) {
810
return [true];
911
}
1012

11-
const negated = when === 'never';
13+
const checks = (Array.isArray(value) ? value : [value]).map(check => {
14+
if (typeof check === 'string') {
15+
return {
16+
when: 'always',
17+
case: check
18+
};
19+
}
20+
return check;
21+
});
22+
23+
const result = checks.some(check => {
24+
const r = ensure.case(scope, check.case);
25+
return negated(check.when) ? !r : r;
26+
});
27+
28+
const list = checks.map(c => c.case).join(', ');
1229

13-
const result = ensure.case(scope, value);
1430
return [
15-
negated ? !result : result,
16-
message([`scope must`, negated ? `not` : null, `be ${value}`])
31+
negated(when) ? !result : result,
32+
message([`scope must`, negated(when) ? `not` : null, `be ${list}`])
1733
];
1834
};

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

+56-1
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,63 @@ test('with uppercase scope should fail for "never uppercase"', async t => {
248248
t.is(actual, expected);
249249
});
250250

251-
test('with lowercase scope should succeed for "always uppercase"', async t => {
251+
test('with uppercase scope should succeed for "always uppercase"', async t => {
252252
const [actual] = scopeCase(await parsed.uppercase, 'always', 'uppercase');
253253
const expected = true;
254254
t.is(actual, expected);
255255
});
256+
257+
test('with uppercase scope should succeed for "always [uppercase, lowercase]"', async t => {
258+
const [actual] = scopeCase(await parsed.uppercase, 'always', [
259+
'uppercase',
260+
'lowercase'
261+
]);
262+
const expected = true;
263+
t.is(actual, expected);
264+
});
265+
266+
test('with lowercase scope should succeed for "always [uppercase, lowercase]"', async t => {
267+
const [actual] = scopeCase(await parsed.lowercase, 'always', [
268+
'uppercase',
269+
'lowercase'
270+
]);
271+
const expected = true;
272+
t.is(actual, expected);
273+
});
274+
275+
test('with mixedcase scope should fail for "always [uppercase, lowercase]"', async t => {
276+
const [actual] = scopeCase(await parsed.mixedcase, 'always', [
277+
'uppercase',
278+
'lowercase'
279+
]);
280+
const expected = false;
281+
t.is(actual, expected);
282+
});
283+
284+
test('with mixedcase scope should pass for "always [uppercase, lowercase, camel-case]"', async t => {
285+
const [actual] = scopeCase(await parsed.mixedcase, 'always', [
286+
'uppercase',
287+
'lowercase',
288+
'camel-case'
289+
]);
290+
const expected = true;
291+
t.is(actual, expected);
292+
});
293+
294+
test('with mixedcase scope should pass for "never [uppercase, lowercase]"', async t => {
295+
const [actual] = scopeCase(await parsed.mixedcase, 'never', [
296+
'uppercase',
297+
'lowercase'
298+
]);
299+
const expected = true;
300+
t.is(actual, expected);
301+
});
302+
303+
test('with uppercase scope should fail for "never [uppercase, lowercase]"', async t => {
304+
const [actual] = scopeCase(await parsed.uppercase, 'never', [
305+
'uppercase',
306+
'lowercase'
307+
]);
308+
const expected = false;
309+
t.is(actual, expected);
310+
});

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

+55
Original file line numberDiff line numberDiff line change
@@ -269,3 +269,58 @@ test('should use expected message with "never"', async t => {
269269
);
270270
t.true(message.indexOf('must not be upper-case') > -1);
271271
});
272+
273+
test('with uppercase scope should succeed for "always [uppercase, lowercase]"', async t => {
274+
const [actual] = subjectCase(await parsed.uppercase, 'always', [
275+
'uppercase',
276+
'lowercase'
277+
]);
278+
const expected = true;
279+
t.is(actual, expected);
280+
});
281+
282+
test('with lowercase subject should succeed for "always [uppercase, lowercase]"', async t => {
283+
const [actual] = subjectCase(await parsed.lowercase, 'always', [
284+
'uppercase',
285+
'lowercase'
286+
]);
287+
const expected = true;
288+
t.is(actual, expected);
289+
});
290+
291+
test('with mixedcase subject should fail for "always [uppercase, lowercase]"', async t => {
292+
const [actual] = subjectCase(await parsed.mixedcase, 'always', [
293+
'uppercase',
294+
'lowercase'
295+
]);
296+
const expected = false;
297+
t.is(actual, expected);
298+
});
299+
300+
test('with mixedcase subject should pass for "always [uppercase, lowercase, camel-case]"', async t => {
301+
const [actual] = subjectCase(await parsed.mixedcase, 'always', [
302+
'uppercase',
303+
'lowercase',
304+
'camel-case'
305+
]);
306+
const expected = true;
307+
t.is(actual, expected);
308+
});
309+
310+
test('with mixedcase scope should pass for "never [uppercase, lowercase]"', async t => {
311+
const [actual] = subjectCase(await parsed.mixedcase, 'never', [
312+
'uppercase',
313+
'lowercase'
314+
]);
315+
const expected = true;
316+
t.is(actual, expected);
317+
});
318+
319+
test('with uppercase scope should fail for "never [uppercase, lowercase]"', async t => {
320+
const [actual] = subjectCase(await parsed.uppercase, 'never', [
321+
'uppercase',
322+
'lowercase'
323+
]);
324+
const expected = false;
325+
t.is(actual, expected);
326+
});

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

+20-4
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,34 @@
11
import * as ensure from '@commitlint/ensure';
22
import message from '@commitlint/message';
33

4+
const negated = when => when === 'never';
5+
46
export default (parsed, when, value) => {
57
const {type} = parsed;
68

79
if (!type) {
810
return [true];
911
}
1012

11-
const negated = when === 'never';
13+
const checks = (Array.isArray(value) ? value : [value]).map(check => {
14+
if (typeof check === 'string') {
15+
return {
16+
when: 'always',
17+
case: check
18+
};
19+
}
20+
return check;
21+
});
22+
23+
const result = checks.some(check => {
24+
const r = ensure.case(type, check.case);
25+
return negated(check.when) ? !r : r;
26+
});
27+
28+
const list = checks.map(c => c.case).join(', ');
1229

13-
const result = ensure.case(type, value);
1430
return [
15-
negated ? !result : result,
16-
message([`type must`, negated ? `not` : null, `be ${value}`])
31+
negated(when) ? !result : result,
32+
message([`type must`, negated(when) ? `not` : null, `be ${list}`])
1733
];
1834
};

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

+55
Original file line numberDiff line numberDiff line change
@@ -268,3 +268,58 @@ test('with startcase type should succeed for "always startcase"', async t => {
268268
const expected = true;
269269
t.is(actual, expected);
270270
});
271+
272+
test('with uppercase scope should succeed for "always [uppercase, lowercase]"', async t => {
273+
const [actual] = typeCase(await parsed.uppercase, 'always', [
274+
'uppercase',
275+
'lowercase'
276+
]);
277+
const expected = true;
278+
t.is(actual, expected);
279+
});
280+
281+
test('with lowercase subject should succeed for "always [uppercase, lowercase]"', async t => {
282+
const [actual] = typeCase(await parsed.lowercase, 'always', [
283+
'uppercase',
284+
'lowercase'
285+
]);
286+
const expected = true;
287+
t.is(actual, expected);
288+
});
289+
290+
test('with mixedcase subject should fail for "always [uppercase, lowercase]"', async t => {
291+
const [actual] = typeCase(await parsed.mixedcase, 'always', [
292+
'uppercase',
293+
'lowercase'
294+
]);
295+
const expected = false;
296+
t.is(actual, expected);
297+
});
298+
299+
test('with mixedcase subject should pass for "always [uppercase, lowercase, camel-case]"', async t => {
300+
const [actual] = typeCase(await parsed.mixedcase, 'always', [
301+
'uppercase',
302+
'lowercase',
303+
'camel-case'
304+
]);
305+
const expected = true;
306+
t.is(actual, expected);
307+
});
308+
309+
test('with mixedcase scope should pass for "never [uppercase, lowercase]"', async t => {
310+
const [actual] = typeCase(await parsed.mixedcase, 'never', [
311+
'uppercase',
312+
'lowercase'
313+
]);
314+
const expected = true;
315+
t.is(actual, expected);
316+
});
317+
318+
test('with uppercase scope should fail for "never [uppercase, lowercase]"', async t => {
319+
const [actual] = typeCase(await parsed.uppercase, 'never', [
320+
'uppercase',
321+
'lowercase'
322+
]);
323+
const expected = false;
324+
t.is(actual, expected);
325+
});

0 commit comments

Comments
 (0)