From edccdfa9efb7a4b37fb846e55b2137b55275d39e Mon Sep 17 00:00:00 2001 From: w0rp Date: Sun, 6 Jul 2025 18:47:41 +0100 Subject: [PATCH] Make shellcheck respect ale_warn_about_trailing_whitespace --- autoload/ale/handlers/shellcheck.vim | 23 +++++++-- test/handler/test_shellcheck_handler.vader | 59 +++++++++++++++++++++- 2 files changed, 77 insertions(+), 5 deletions(-) diff --git a/autoload/ale/handlers/shellcheck.vim b/autoload/ale/handlers/shellcheck.vim index 002c4651..80e0bfd7 100644 --- a/autoload/ale/handlers/shellcheck.vim +++ b/autoload/ale/handlers/shellcheck.vim @@ -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 , 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], \} diff --git a/test/handler/test_shellcheck_handler.vader b/test/handler/test_shellcheck_handler.vader index e5c972d7..e0f904cd 100644 --- a/test/handler/test_shellcheck_handler.vader +++ b/test/handler/test_shellcheck_handler.vader @@ -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 + \ } + \ ] + \ }', + \ ])