diff --git a/commitlint.config.ts b/commitlint.config.ts index 048da458..d67b1f41 100644 --- a/commitlint.config.ts +++ b/commitlint.config.ts @@ -1,3 +1,5 @@ +import {obviousWords} from './obviouswords' + // to convert from 'any' type function convertAnyToString(potentialString: any, paramName: string): string { if (potentialString === null || potentialString === undefined) { @@ -36,6 +38,7 @@ module.exports = { 'trailing-whitespace': [RuleStatus.Error, 'always'], 'prefer-slash-over-backslash': [RuleStatus.Error, 'always'], 'type-space-before-paren': [RuleStatus.Error, 'always'], + 'reject-obvious-words': [RuleStatus.Error, 'always'], }, plugins: [ // TODO (ideas for more rules): @@ -81,6 +84,22 @@ module.exports = { ]; }, + 'reject-obvious-words': ({header}: {header:any}) => { + let headerStr = convertAnyToString(header, "header"); + let offence = false; + + let colonFirstIndex = headerStr.indexOf(":"); + let titleStartIndex = Math.max(0, colonFirstIndex + 1); + let title = headerStr.substring(titleStartIndex, headerStr.length).trim(); + let firstWordInTitle = title.split(' ')[0]; + offence = obviousWords.includes(firstWordInTitle); + + return [ + !offence, + `Please don't use obvious words such as ${firstWordInTitle} in the commit title` + ]; + }, + 'type-space-after-colon': ({header}: {header:any}) => { let headerStr = convertAnyToString(header, "header"); diff --git a/commitlintplugins.test.ts b/commitlintplugins.test.ts index aa700ff1..3f8d9649 100644 --- a/commitlintplugins.test.ts +++ b/commitlintplugins.test.ts @@ -87,6 +87,30 @@ test('prefer-slash-over-backslash2', () => { }); +test('reject-obvious-words1', () => { + let commitMsgWithObviousWordAfterColon = "foo: modify bla bla"; + let rejectObviousWord1 = runCommitLintOnMsg(commitMsgWithObviousWordAfterColon); + // console.log('==========>' + rejectObviousWord1.stdout) + expect(rejectObviousWord1.status).not.toBe(0); +}); + + +test('reject-obvious-words2', () => { + let commitMsgWithObviousWordAfterColon = "foo: change bla bla"; + let rejectObviousWord2 = runCommitLintOnMsg(commitMsgWithObviousWordAfterColon); + // console.log('==========>' + rejectObviousWord2.stdout) + expect(rejectObviousWord2.status).not.toBe(0); +}); + + +test('reject-obvious-words3', () => { + let commitMsgWithoutObviousWordAfterColon = "foo: bla bla bla"; + let rejectObviousWord3 = runCommitLintOnMsg(commitMsgWithoutObviousWordAfterColon); + // console.log('==========>' + rejectObviousWord3.stdout) + expect(rejectObviousWord3.status).toBe(0); +}); + + test('subject-lowercase1', () => { let commitMsgWithUppercaseAfterColon = "foo: Bar baz"; let subjectLowerCase1 = runCommitLintOnMsg(commitMsgWithUppercaseAfterColon); diff --git a/obviouswords.ts b/obviouswords.ts new file mode 100644 index 00000000..e6c6fd34 --- /dev/null +++ b/obviouswords.ts @@ -0,0 +1,5 @@ +export let obviousWords = [ + "change", + "update", + "modify" +] \ No newline at end of file