Skip to content

Commit d280cd9

Browse files
targosjoyeecheung
authored andcommitted
rules: check spaces in commit title (#35)
1 parent 64c7867 commit d280cd9

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

lib/rules/title-format.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,36 @@ module.exports = {
2222
pass = false
2323
}
2424

25+
{
26+
const result = /^(.+?:)[^ ]/.exec(context.title)
27+
if (result) {
28+
context.report({
29+
id: id
30+
, message: 'Add a space after subsystem(s).'
31+
, string: context.title
32+
, line: 0
33+
, column: result[1].length
34+
, level: 'fail'
35+
})
36+
pass = false
37+
}
38+
}
39+
40+
{
41+
const result = /\s\s/.exec(context.title)
42+
if (result) {
43+
context.report({
44+
id: id
45+
, message: 'Do not use consecutive spaces in title.'
46+
, string: context.title
47+
, line: 0
48+
, column: result.index + 1
49+
, level: 'fail'
50+
})
51+
pass = false
52+
}
53+
}
54+
2555
const result = /^(.+?): [A-Z]/.exec(context.title)
2656
if (result) {
2757
context.report({

test/rules/title-format.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
'use strict'
2+
3+
const test = require('tap').test
4+
const Rule = require('../../lib/rules/title-format')
5+
const Commit = require('gitlint-parser-node')
6+
const Validator = require('../../')
7+
8+
function makeCommit(title) {
9+
const v = new Validator()
10+
return new Commit({
11+
sha: 'e7c077c610afa371430180fbd447bfef60ebc5ea'
12+
, author: {
13+
name: 'Evan Lucas'
14+
, email: '[email protected]'
15+
, date: '2016-04-12T19:42:23Z'
16+
}
17+
, message: title
18+
}, v)
19+
}
20+
21+
test('rule: title-format', (t) => {
22+
t.test('space after subsystem', (tt) => {
23+
tt.plan(2)
24+
const context = makeCommit('test:missing space')
25+
26+
context.report = (opts) => {
27+
tt.pass('called report')
28+
tt.strictSame(opts, {
29+
id: 'title-format'
30+
, message: 'Add a space after subsystem(s).'
31+
, string: 'test:missing space'
32+
, line: 0
33+
, column: 5
34+
, level: 'fail'
35+
})
36+
}
37+
38+
Rule.validate(context)
39+
tt.end()
40+
})
41+
42+
t.test('consecutive spaces', (tt) => {
43+
tt.plan(2)
44+
const context = makeCommit('test: with two spaces')
45+
46+
context.report = (opts) => {
47+
tt.pass('called report')
48+
tt.strictSame(opts, {
49+
id: 'title-format'
50+
, message: 'Do not use consecutive spaces in title.'
51+
, string: 'test: with two spaces'
52+
, line: 0
53+
, column: 11
54+
, level: 'fail'
55+
})
56+
}
57+
58+
Rule.validate(context)
59+
tt.end()
60+
})
61+
62+
t.end()
63+
})

0 commit comments

Comments
 (0)