From cadcfedd0eae71d166483062e1c00442194a017c Mon Sep 17 00:00:00 2001 From: Cedric van Putten Date: Fri, 5 Oct 2018 13:44:55 +0200 Subject: [PATCH] fix: handle case rules for numerics correctly * fix(ensure): return true for values starting with numeric character * fix(rules): ignore values starting with non-alphabetic character --- @commitlint/ensure/src/case.js | 4 ++-- @commitlint/ensure/src/case.test.js | 28 ++++++++++++++++++++++ @commitlint/rules/src/subject-case.js | 2 +- @commitlint/rules/src/subject-case.test.js | 26 ++++++++++++++++++++ 4 files changed, 57 insertions(+), 3 deletions(-) diff --git a/@commitlint/ensure/src/case.js b/@commitlint/ensure/src/case.js index cd0341aff9..6b47df7b77 100644 --- a/@commitlint/ensure/src/case.js +++ b/@commitlint/ensure/src/case.js @@ -14,7 +14,7 @@ function ensureCase(raw = '', target = 'lowercase') { .trim(); const transformed = toCase(input, target); - if (transformed === '') { + if (transformed === '' || transformed.match(/^\d/)) { return true; } @@ -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' diff --git a/@commitlint/ensure/src/case.test.js b/@commitlint/ensure/src/case.test.js index d8592766b5..ec517ee88d 100644 --- a/@commitlint/ensure/src/case.test.js +++ b/@commitlint/ensure/src/case.test.js @@ -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')); +}); diff --git a/@commitlint/rules/src/subject-case.js b/@commitlint/rules/src/subject-case.js index 519029735b..5f2ad8fdd7 100644 --- a/@commitlint/rules/src/subject-case.js +++ b/@commitlint/rules/src/subject-case.js @@ -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]; } diff --git a/@commitlint/rules/src/subject-case.test.js b/@commitlint/rules/src/subject-case.test.js index 8add629847..a915234706 100644 --- a/@commitlint/rules/src/subject-case.test.js +++ b/@commitlint/rules/src/subject-case.test.js @@ -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', @@ -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), @@ -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); +});