From 82dee7df7be27191a1386019dd7ad6a867884ca7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Zasso?= Date: Sun, 18 Nov 2018 15:43:05 +0100 Subject: [PATCH] rules: check spaces in commit title - Do not allow consecutive spaces - Require a space after the subsystems --- lib/rules/title-format.js | 30 ++++++++++++++++++ test/rules/title-format.js | 63 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 test/rules/title-format.js diff --git a/lib/rules/title-format.js b/lib/rules/title-format.js index b107fa7..5003665 100644 --- a/lib/rules/title-format.js +++ b/lib/rules/title-format.js @@ -22,6 +22,36 @@ module.exports = { pass = false } + { + const result = /^(.+?:)[^ ]/.exec(context.title) + if (result) { + context.report({ + id: id + , message: 'Add a space after subsystem(s).' + , string: context.title + , line: 0 + , column: result[1].length + , level: 'fail' + }) + pass = false + } + } + + { + const result = /\s\s/.exec(context.title) + if (result) { + context.report({ + id: id + , message: 'Do not use consecutive spaces in title.' + , string: context.title + , line: 0 + , column: result.index + 1 + , level: 'fail' + }) + pass = false + } + } + const result = /^(.+?): [A-Z]/.exec(context.title) if (result) { context.report({ diff --git a/test/rules/title-format.js b/test/rules/title-format.js new file mode 100644 index 0000000..b648721 --- /dev/null +++ b/test/rules/title-format.js @@ -0,0 +1,63 @@ +'use strict' + +const test = require('tap').test +const Rule = require('../../lib/rules/title-format') +const Commit = require('gitlint-parser-node') +const Validator = require('../../') + +function makeCommit(title) { + const v = new Validator() + return new Commit({ + sha: 'e7c077c610afa371430180fbd447bfef60ebc5ea' + , author: { + name: 'Evan Lucas' + , email: 'evanlucas@me.com' + , date: '2016-04-12T19:42:23Z' + } + , message: title + }, v) +} + +test('rule: title-format', (t) => { + t.test('space after subsystem', (tt) => { + tt.plan(2) + const context = makeCommit('test:missing space') + + context.report = (opts) => { + tt.pass('called report') + tt.strictSame(opts, { + id: 'title-format' + , message: 'Add a space after subsystem(s).' + , string: 'test:missing space' + , line: 0 + , column: 5 + , level: 'fail' + }) + } + + Rule.validate(context) + tt.end() + }) + + t.test('consecutive spaces', (tt) => { + tt.plan(2) + const context = makeCommit('test: with two spaces') + + context.report = (opts) => { + tt.pass('called report') + tt.strictSame(opts, { + id: 'title-format' + , message: 'Do not use consecutive spaces in title.' + , string: 'test: with two spaces' + , line: 0 + , column: 11 + , level: 'fail' + }) + } + + Rule.validate(context) + tt.end() + }) + + t.end() +})