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

A rule that rejects obvious words in the commit title #15

Closed
Closed
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
19 changes: 19 additions & 0 deletions commitlint.config.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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");

Expand Down
24 changes: 24 additions & 0 deletions commitlintplugins.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions obviouswords.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export let obviousWords = [
"change",
"update",
"modify"
]