Make shellcheck respect ale_warn_about_trailing_whitespace
Some checks failed
CI / build_image (push) Has been cancelled
CI / test_ale (--linters-only) (push) Has been cancelled
CI / test_ale (--lua-only) (push) Has been cancelled
CI / test_ale (--neovim-07-only) (push) Has been cancelled
CI / test_ale (--neovim-08-only) (push) Has been cancelled
CI / test_ale (--vim-80-only) (push) Has been cancelled
CI / test_ale (--vim-90-only) (push) Has been cancelled

This commit is contained in:
w0rp
2025-07-06 18:47:41 +01:00
parent 9e49019a26
commit edccdfa9ef
2 changed files with 77 additions and 5 deletions

View File

@@ -63,6 +63,12 @@ function! ale#handlers#shellcheck#GetCommand(buffer, version) abort
\ . ' -f ' . l:format . ' -'
endfunction
function! s:ShouldIgnoreErrorCode(buffer, code) abort
" Skip warnings for trailing whitespace if the option is off.
return a:code is# 'SC1101'
\ && !ale#Var(a:buffer, 'warn_about_trailing_whitespace')
endfunction
function! s:HandleShellcheckJSON(buffer, lines) abort
try
let l:errors = json_decode(a:lines[0])
@@ -87,11 +93,17 @@ function! s:HandleShellcheckJSON(buffer, lines) abort
let l:type = 'W'
endif
let l:code = 'SC' . l:error['code']
if s:ShouldIgnoreErrorCode(a:buffer, l:code)
continue
endif
let l:item = {
\ 'lnum': l:error['line'],
\ 'type': l:type,
\ 'text': l:error['message'],
\ 'code': 'SC' . l:error['code'],
\ 'code': l:code,
\ 'detail': l:error['message'] . "\n\nFor more information:\n https://www.shellcheck.net/wiki/SC" . l:error['code'],
\}
@@ -107,7 +119,6 @@ function! s:HandleShellcheckJSON(buffer, lines) abort
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')
@@ -135,11 +146,17 @@ function! s:HandleShellcheckGCC(buffer, lines) abort
let l:type = 'W'
endif
let l:code = l:match[6]
if s:ShouldIgnoreErrorCode(a:buffer, l:code)
continue
endif
let l:item = {
\ 'lnum': str2nr(l:match[2]),
\ 'type': l:type,
\ 'text': l:match[5],
\ 'code': l:match[6],
\ 'code': l:code,
\ 'detail': l:match[5] . "\n\nFor more information:\n https://www.shellcheck.net/wiki/" . l:match[6],
\}

View File

@@ -1,8 +1,8 @@
Before:
runtime ale_linters/shell/shellcheck.vim
Save g:ale_warn_about_trailing_whitespace
After:
call ale#linter#Reset()
Restore
Execute(The shellcheck handler should handle basic errors or warnings <0.7.0):
AssertEqual
@@ -150,3 +150,58 @@ Execute(The shellcheck handler should handle info and style >=0.7.0):
\ ]
\ }'
\ ])
Execute(shellcheck errors for trailing whitespace should show by default):
AssertEqual
\ [
\ {
\ 'lnum': 8,
\ 'col': 12,
\ 'code': 'SC1101',
\ 'end_lnum': 8,
\ 'type': 'E',
\ 'end_col': 11,
\ 'text': 'Delete trailing spaces after \ to break line (or use quotes for literal space).',
\ 'detail': 'Delete trailing spaces after \ to break line (or use quotes for literal space).' . "\n\nFor more information:\n https://www.shellcheck.net/wiki/" . 'SC1101',
\ },
\ ],
\ ale#handlers#shellcheck#Handle(bufnr(''), [0, 7, 0], [
\ '{
\ "comments": [
\ {
\ "file": "-",
\ "line": 8,
\ "endLine": 8,
\ "column": 12,
\ "endColumn": 12,
\ "level": "error",
\ "code":1101,
\ "message":"Delete trailing spaces after \\ to break line (or use quotes for literal space).",
\ "fix":null
\ }
\ ]
\ }',
\ ])
Execute(shellcheck errors for trailing whitepsace should be able to be silenced by ale_warn_about_trailing_whitespace):
let g:ale_warn_about_trailing_whitespace = 0
AssertEqual
\ [],
\ ale#handlers#shellcheck#Handle(bufnr(''), [0, 7, 0], [
\ '{
\ "comments": [
\ {
\ "file": "-",
\ "line": 8,
\ "endLine": 8,
\ "column": 12,
\ "endColumn": 12,
\ "level": "error",
\ "code":1101,
\ "message":"Delete trailing spaces after \\ to break line (or use quotes for literal space).",
\ "fix":null
\ }
\ ]
\ }',
\ ])