Skip to content

Commit edccdfa

Browse files
committed
Make shellcheck respect ale_warn_about_trailing_whitespace
1 parent 9e49019 commit edccdfa

File tree

2 files changed

+77
-5
lines changed

2 files changed

+77
-5
lines changed

autoload/ale/handlers/shellcheck.vim

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ function! ale#handlers#shellcheck#GetCommand(buffer, version) abort
6363
\ . ' -f ' . l:format . ' -'
6464
endfunction
6565

66+
function! s:ShouldIgnoreErrorCode(buffer, code) abort
67+
" Skip warnings for trailing whitespace if the option is off.
68+
return a:code is# 'SC1101'
69+
\ && !ale#Var(a:buffer, 'warn_about_trailing_whitespace')
70+
endfunction
71+
6672
function! s:HandleShellcheckJSON(buffer, lines) abort
6773
try
6874
let l:errors = json_decode(a:lines[0])
@@ -87,11 +93,17 @@ function! s:HandleShellcheckJSON(buffer, lines) abort
8793
let l:type = 'W'
8894
endif
8995

96+
let l:code = 'SC' . l:error['code']
97+
98+
if s:ShouldIgnoreErrorCode(a:buffer, l:code)
99+
continue
100+
endif
101+
90102
let l:item = {
91103
\ 'lnum': l:error['line'],
92104
\ 'type': l:type,
93105
\ 'text': l:error['message'],
94-
\ 'code': 'SC' . l:error['code'],
106+
\ 'code': l:code,
95107
\ 'detail': l:error['message'] . "\n\nFor more information:\n https://www.shellcheck.net/wiki/SC" . l:error['code'],
96108
\}
97109

@@ -107,7 +119,6 @@ function! s:HandleShellcheckJSON(buffer, lines) abort
107119
let l:item.end_lnum = l:error['endLine']
108120
endif
109121

110-
111122
" If the filename is something like <stdin>, <nofile> or -, then
112123
" this is an error for the file we checked.
113124
if has_key(l:error, 'file')
@@ -135,11 +146,17 @@ function! s:HandleShellcheckGCC(buffer, lines) abort
135146
let l:type = 'W'
136147
endif
137148

149+
let l:code = l:match[6]
150+
151+
if s:ShouldIgnoreErrorCode(a:buffer, l:code)
152+
continue
153+
endif
154+
138155
let l:item = {
139156
\ 'lnum': str2nr(l:match[2]),
140157
\ 'type': l:type,
141158
\ 'text': l:match[5],
142-
\ 'code': l:match[6],
159+
\ 'code': l:code,
143160
\ 'detail': l:match[5] . "\n\nFor more information:\n https://www.shellcheck.net/wiki/" . l:match[6],
144161
\}
145162

test/handler/test_shellcheck_handler.vader

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
Before:
2-
runtime ale_linters/shell/shellcheck.vim
2+
Save g:ale_warn_about_trailing_whitespace
33

44
After:
5-
call ale#linter#Reset()
5+
Restore
66

77
Execute(The shellcheck handler should handle basic errors or warnings <0.7.0):
88
AssertEqual
@@ -150,3 +150,58 @@ Execute(The shellcheck handler should handle info and style >=0.7.0):
150150
\ ]
151151
\ }'
152152
\ ])
153+
154+
Execute(shellcheck errors for trailing whitespace should show by default):
155+
AssertEqual
156+
\ [
157+
\ {
158+
\ 'lnum': 8,
159+
\ 'col': 12,
160+
\ 'code': 'SC1101',
161+
\ 'end_lnum': 8,
162+
\ 'type': 'E',
163+
\ 'end_col': 11,
164+
\ 'text': 'Delete trailing spaces after \ to break line (or use quotes for literal space).',
165+
\ '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',
166+
\ },
167+
\ ],
168+
\ ale#handlers#shellcheck#Handle(bufnr(''), [0, 7, 0], [
169+
\ '{
170+
\ "comments": [
171+
\ {
172+
\ "file": "-",
173+
\ "line": 8,
174+
\ "endLine": 8,
175+
\ "column": 12,
176+
\ "endColumn": 12,
177+
\ "level": "error",
178+
\ "code":1101,
179+
\ "message":"Delete trailing spaces after \\ to break line (or use quotes for literal space).",
180+
\ "fix":null
181+
\ }
182+
\ ]
183+
\ }',
184+
\ ])
185+
186+
Execute(shellcheck errors for trailing whitepsace should be able to be silenced by ale_warn_about_trailing_whitespace):
187+
let g:ale_warn_about_trailing_whitespace = 0
188+
189+
AssertEqual
190+
\ [],
191+
\ ale#handlers#shellcheck#Handle(bufnr(''), [0, 7, 0], [
192+
\ '{
193+
\ "comments": [
194+
\ {
195+
\ "file": "-",
196+
\ "line": 8,
197+
\ "endLine": 8,
198+
\ "column": 12,
199+
\ "endColumn": 12,
200+
\ "level": "error",
201+
\ "code":1101,
202+
\ "message":"Delete trailing spaces after \\ to break line (or use quotes for literal space).",
203+
\ "fix":null
204+
\ }
205+
\ ]
206+
\ }',
207+
\ ])

0 commit comments

Comments
 (0)