Skip to content

lib: add suggestions on invalid subsystems #58

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A small suggestion. It might be better to create the FuzzySet once instead of creating it for every iteration of the for loop.

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: '[email protected]'
, 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()
})