Skip to content

Commit

Permalink
fix: handle case rules for numerics correctly
Browse files Browse the repository at this point in the history
* fix(ensure): return true for values starting with numeric character

* fix(rules): ignore values starting with non-alphabetic character
  • Loading branch information
byCedric authored and marionebl committed Oct 5, 2018
1 parent b32bc93 commit cadcfed
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
4 changes: 2 additions & 2 deletions @commitlint/ensure/src/case.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function ensureCase(raw = '', target = 'lowercase') {
.trim();
const transformed = toCase(input, target);

if (transformed === '') {
if (transformed === '' || transformed.match(/^\d/)) {
return true;
}

Expand All @@ -38,7 +38,7 @@ function toCase(input, target) {
return input.toUpperCase();
case 'sentence-case':
case 'sentencecase': {
const word = input.split(' ')[0];
const [word] = input.split(' ');
return `${toCase(word.charAt(0), 'upper-case')}${toCase(
word.slice(1),
'lower-case'
Expand Down
28 changes: 28 additions & 0 deletions @commitlint/ensure/src/case.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,3 +291,31 @@ test('false for `LOWERCASE on lowercase', t => {
const actual = ensure('`LOWERCASE', 'lowercase');
t.is(actual, false);
});

test('true for numeric on camel-case', t => {
t.true(ensure('1.0.0', 'camel-case'));
});

test('true for numeric on kebab-case', t => {
t.true(ensure('1.0.0', 'kebab-case'));
});

test('true for numeric on snake-case', t => {
t.true(ensure('1.0.0', 'snake-case'));
});

test('true for numeric on pascal-case', t => {
t.true(ensure('1.0.0', 'pascal-case'));
});

test('true for numeric on uppercase', t => {
t.true(ensure('1.0.0', 'uppercase'));
});

test('true for numeric on sentencecase', t => {
t.true(ensure('1.0.0', 'sentencecase'));
});

test('true for numeric on lowercase', t => {
t.true(ensure('1.0.0', 'lowercase'));
});
2 changes: 1 addition & 1 deletion @commitlint/rules/src/subject-case.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const negated = when => when === 'never';
export default (parsed, when, value) => {
const {subject} = parsed;

if (typeof subject !== 'string') {
if (typeof subject !== 'string' || !subject.match(/^[a-z]/i)) {
return [true];
}

Expand Down
26 changes: 26 additions & 0 deletions @commitlint/rules/src/subject-case.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import subjectCase from './subject-case';

const messages = {
empty: 'test:\n',
numeric: 'test: 1.0.0',
lowercase: 'test: subject',
mixedcase: 'test: sUbJeCt',
uppercase: 'test: SUBJECT',
Expand All @@ -16,6 +17,7 @@ const messages = {

const parsed = {
empty: parse(messages.empty),
numeric: parse(messages.numeric),
lowercase: parse(messages.lowercase),
mixedcase: parse(messages.mixedcase),
uppercase: parse(messages.uppercase),
Expand Down Expand Up @@ -324,3 +326,27 @@ test('with uppercase scope should fail for "never [uppercase, lowercase]"', asyn
const expected = false;
t.is(actual, expected);
});

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

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

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

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

0 comments on commit cadcfed

Please sign in to comment.