Skip to content

Commit

Permalink
lib: add suggestions on invalid subsystems
Browse files Browse the repository at this point in the history
When an invalid subsystem is encountered, attempt to make a suggestion
based on the nearest match (if any) from the valid subsystems.
  • Loading branch information
richardlau committed Apr 16, 2019
1 parent 38d97b9 commit 28b7f63
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 3 deletions.
9 changes: 7 additions & 2 deletions lib/format-pretty.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,18 @@ function formatMessage(msg) {
const pad = utils.rightPad(`${l}:${col}`, MAX_LINE_COL_LEN)
const line = chalk.grey(pad)
const id = formatId(msg.id)
const m = msg.message
const icon = msg.level === 'fail'
? utils.X
: msg.level === 'warn'
? utils.WARN
: utils.CHECK
return ` ${icon} ${line} ${utils.rightPad(m, 40)} ${id}`
const msg_lines = msg.message.split('\n')
let formatted =
[ ` ${icon} ${line} ${utils.rightPad(msg_lines[0], 40)} ${id}` ]
for (const msg_line of msg_lines.slice(1)) {
formatted.push(`${' '.repeat(17)}${msg_line}`)
}
return formatted.join('\n')
}

function formatId(id) {
Expand Down
9 changes: 8 additions & 1 deletion lib/rules/subsystem.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict'

const FuzzySet = require('fuzzyset.js')

const id = 'subsystem'

const validSubsystems = [
Expand Down Expand Up @@ -111,11 +113,16 @@ module.exports = {
for (const sub of parsed.subsystems) {
if (!~subs.indexOf(sub)) {
failed = true
let suggestion = ''
const suggestions = new FuzzySet(subs).get(sub)
if (suggestions) {
suggestion = `\nDid you mean "${suggestions[0][1]}"?`
}
// invalid subsystem
const column = parsed.title.indexOf(sub)
context.report({
id: id
, message: `Invalid subsystem: "${sub}"`
, message: `Invalid subsystem: "${sub}"${suggestion}`
, string: parsed.title
, line: 0
, column: column
Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
},
"dependencies": {
"chalk": "~1.1.1",
"fuzzyset.js": "^0.0.8",
"gitlint-parser-node": "^1.1.0",
"help": "^2.1.3",
"nopt": "^3.0.6"
Expand Down
40 changes: 40 additions & 0 deletions test/rules/subsystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,45 @@ test('rule: subsystem', (t) => {

Rule.validate(context, {options: {subsystems: Rule.defaults.subsystems}})
})

const suggestionTests = [
[ 'docs', 'doc' ]
, [ 'error', 'errors' ]
, [ 'napi', 'n-api' ]
, [ 'perfhooks', 'perf_hooks' ]
, [ 'worker_threads', 'worker' ]
, [ 'V8', 'v8' ]
]
for (const [sub, suggestion] of suggestionTests) {
t.test(`suggestion "${sub}" -> "${suggestion}"`, (tt) => {
tt.plan(7)
const title = `${sub}: come on`
const v = new Validator()
const context = new Commit({
sha: 'e7c077c610afa371430180fbd447bfef60ebc5ea'
, author: {
name: 'Evan Lucas'
, email: 'evanlucas@me.com'
, date: '2016-04-12T19:42:23Z'
}
, message: title
}, v)

context.report = (opts) => {
tt.pass('called report')
tt.equal(opts.id, 'subsystem', 'id')
tt.equal(opts.message
, `Invalid subsystem: "${sub}"\nDid you mean "${suggestion}"?`
, 'message')
tt.equal(opts.string, title, 'string')
tt.equal(opts.line, 0, 'line')
tt.equal(opts.column, 0, 'column')
tt.equal(opts.level, 'fail', 'level')
tt.end()
}

Rule.validate(context, {options: {subsystems: Rule.defaults.subsystems}})
})
}
t.end()
})

0 comments on commit 28b7f63

Please sign in to comment.