Merge pull request #1232 from languitar/vale-json

Use JSON output with vale
This commit is contained in:
w0rp 2017-12-18 10:11:57 +00:00 committed by GitHub
commit fdde8af894
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 109 additions and 6 deletions

View file

@ -4,6 +4,6 @@
call ale#linter#Define('mail', { call ale#linter#Define('mail', {
\ 'name': 'vale', \ 'name': 'vale',
\ 'executable': 'vale', \ 'executable': 'vale',
\ 'command': 'vale --output=line %t', \ 'command': 'vale --output=JSON %t',
\ 'callback': 'ale#handlers#unix#HandleAsWarning', \ 'callback': 'ale#handlers#vale#Handle',
\}) \})

View file

@ -4,6 +4,6 @@
call ale#linter#Define('markdown', { call ale#linter#Define('markdown', {
\ 'name': 'vale', \ 'name': 'vale',
\ 'executable': 'vale', \ 'executable': 'vale',
\ 'command': 'vale --output=line %t', \ 'command': 'vale --output=JSON %t',
\ 'callback': 'ale#handlers#unix#HandleAsWarning', \ 'callback': 'ale#handlers#vale#Handle',
\}) \})

View file

@ -4,6 +4,6 @@
call ale#linter#Define('text', { call ale#linter#Define('text', {
\ 'name': 'vale', \ 'name': 'vale',
\ 'executable': 'vale', \ 'executable': 'vale',
\ 'command': 'vale --output=line %t', \ 'command': 'vale --output=JSON %t',
\ 'callback': 'ale#handlers#unix#HandleAsWarning', \ 'callback': 'ale#handlers#vale#Handle',
\}) \})

View file

@ -0,0 +1,36 @@
" Author: Johannes Wienke <languitar@semipol.de>
" Description: output handler for the vale JSON format
function! ale#handlers#vale#GetType(severity) abort
if a:severity is? 'warning'
return 'W'
endif
return 'E'
endfunction
function! ale#handlers#vale#Handle(buffer, lines) abort
try
let l:errors = json_decode(join(a:lines, ''))
catch
return []
endtry
if empty(l:errors)
return []
endif
let l:output = []
for l:error in l:errors[keys(l:errors)[0]]
call add(l:output, {
\ 'lnum': l:error['Line'],
\ 'col': l:error['Span'][0],
\ 'end_col': l:error['Span'][1],
\ 'code': l:error['Check'],
\ 'text': l:error['Message'],
\ 'type': ale#handlers#vale#GetType(l:error['Severity']),
\})
endfor
return l:output
endfunction

View file

@ -0,0 +1,67 @@
Execute(The vale handler should handle broken JSON):
AssertEqual
\ [],
\ ale#handlers#vale#Handle(bufnr(''), ["{asdf"])
Execute(The vale handler should handle am empty string response):
AssertEqual
\ [],
\ ale#handlers#vale#Handle(bufnr(''), [])
Execute(The vale handler should handle an empty result):
AssertEqual
\ [],
\ ale#handlers#vale#Handle(bufnr(''), ["{}"])
Execute(The vale handler should handle a normal example):
AssertEqual
\ [
\ {
\ 'lnum': 5,
\ 'col': 195,
\ 'end_col': 201,
\ 'type': 'W',
\ 'text': "Consider removing 'usually'",
\ 'code': 'vale.Hedging',
\ },
\ {
\ 'lnum': 7,
\ 'col': 1,
\ 'end_col': 27,
\ 'type': 'E',
\ 'text': "'Documentation' is repeated!",
\ 'code': 'vale.Repetition',
\ },
\ ],
\ ale#handlers#vale#Handle(bufnr(''), [
\ '{',
\ ' "/home/languitar/src/autosuspend/README.md": [',
\ ' {',
\ ' "Check": "vale.Hedging",',
\ ' "Description": "",',
\ ' "Line": 5,',
\ ' "Link": "",',
\ " \"Message\": \"Consider removing 'usually'\",",
\ ' "Severity": "warning",',
\ ' "Span": [',
\ ' 195,',
\ ' 201',
\ ' ],',
\ ' "Hide": false',
\ ' },',
\ ' {',
\ ' "Check": "vale.Repetition",',
\ ' "Description": "",',
\ ' "Line": 7,',
\ ' "Link": "",',
\ " \"Message\": \"'Documentation' is repeated!\",",
\ ' "Severity": "error",',
\ ' "Span": [',
\ ' 1,',
\ ' 27',
\ ' ],',
\ ' "Hide": false',
\ ' }',
\ ' ]',
\ '}',
\ ])