Add end_col and end_lnum to ShellCheck (#4692)

* Add end_col and end_lnum to ShellCheck

ShellCheck supports a JSON format mode which includes an 'endLine' and
'endColumn' field.

We must use the newer 'json1' format as it properly treats tabs as a
single character. 'json1' was not supported until v0.7.0 in 2019[1], so
we maintain support for the older GCC based format.

[1] https://github.com/koalaman/shellcheck/blob/v0.9.0/CHANGELOG.md?plain=1#L121

* Add wiki link to ShellCheck json output
This commit is contained in:
Brandon Maier
2024-01-14 05:47:42 -06:00
committed by GitHub
parent ebc6718c0a
commit d63f5e6a77
2 changed files with 187 additions and 7 deletions

View File

@@ -49,6 +49,7 @@ function! ale#handlers#shellcheck#GetCommand(buffer, version) abort
let l:exclude_option = ale#Var(a:buffer, 'sh_shellcheck_exclusions')
let l:dialect = ale#Var(a:buffer, 'sh_shellcheck_dialect')
let l:external_option = ale#semver#GTE(a:version, [0, 4, 0]) ? ' -x' : ''
let l:format = ale#semver#GTE(a:version, [0, 7, 0]) ? 'json1' : 'gcc'
if l:dialect is# 'auto'
let l:dialect = ale#handlers#shellcheck#GetDialectArgument(a:buffer)
@@ -59,10 +60,69 @@ function! ale#handlers#shellcheck#GetCommand(buffer, version) abort
\ . (!empty(l:options) ? ' ' . l:options : '')
\ . (!empty(l:exclude_option) ? ' -e ' . l:exclude_option : '')
\ . l:external_option
\ . ' -f gcc -'
\ . ' -f ' . l:format . ' -'
endfunction
function! ale#handlers#shellcheck#Handle(buffer, lines) abort
function! s:HandleShellcheckJSON(buffer, lines) abort
try
let l:errors = json_decode(a:lines[0])
catch
return []
endtry
if !has_key(l:errors, 'comments')
return []
endif
let l:output = []
for l:error in l:errors['comments']
if l:error['level'] is# 'error'
let l:type = 'E'
elseif l:error['level'] is# 'info'
let l:type = 'I'
elseif l:error['level'] is# 'style'
let l:type = 'I'
else
let l:type = 'W'
endif
let l:item = {
\ 'lnum': l:error['line'],
\ 'type': l:type,
\ 'text': l:error['message'],
\ 'code': 'SC' . l:error['code'],
\ 'detail': l:error['message'] . "\n\nFor more information:\n https://www.shellcheck.net/wiki/SC" . l:error['code'],
\}
if has_key(l:error, 'column')
let l:item.col = l:error['column']
endif
if has_key(l:error, 'endColumn')
let l:item.end_col = l:error['endColumn'] - 1
endif
if has_key(l:error, 'endLine')
let l:item.end_lnum = l:error['endLine']
endif
" If the filename is something like <stdin>, <nofile> or -, then
" this is an error for the file we checked.
if has_key(l:error, 'file')
if l:error['file'] isnot# '-' && l:error['file'][0] isnot# '<'
let l:item['filename'] = l:error['file']
endif
endif
call add(l:output, l:item)
endfor
return l:output
endfunction
function! s:HandleShellcheckGCC(buffer, lines) abort
let l:pattern = '\v^([a-zA-Z]?:?[^:]+):(\d+):(\d+)?:? ([^:]+): (.+) \[([^\]]+)\]$'
let l:output = []
@@ -98,6 +158,12 @@ function! ale#handlers#shellcheck#Handle(buffer, lines) abort
return l:output
endfunction
function! ale#handlers#shellcheck#Handle(buffer, version, lines) abort
return ale#semver#GTE(a:version, [0, 7, 0])
\ ? s:HandleShellcheckJSON(a:buffer, a:lines)
\ : s:HandleShellcheckGCC(a:buffer, a:lines)
endfunction
function! ale#handlers#shellcheck#DefineLinter(filetype) abort
" This global variable can be set with a string of comma-separated error
" codes to exclude from shellcheck. For example:
@@ -118,6 +184,14 @@ function! ale#handlers#shellcheck#DefineLinter(filetype) abort
\ '%e --version',
\ function('ale#handlers#shellcheck#GetCommand'),
\ )},
\ 'callback': 'ale#handlers#shellcheck#Handle',
\ 'callback': {buffer, lines -> ale#semver#RunWithVersionCheck(
\ buffer,
\ ale#Var(buffer, 'sh_shellcheck_executable'),
\ '%e --version',
\ {buffer, version -> ale#handlers#shellcheck#Handle(
\ buffer,
\ l:version,
\ lines)},
\ )},
\})
endfunction