diff options
author | w0rp <devw0rp@gmail.com> | 2017-11-03 22:08:26 +0000 |
---|---|---|
committer | w0rp <devw0rp@gmail.com> | 2017-11-03 22:08:26 +0000 |
commit | c26e5e277e0a0e0849d416775b63753e3aae4be6 (patch) | |
tree | 38fcd8547faaae600f642481c12b90f32ef8ba23 /ale_linters/sh | |
parent | 54f44c2d0f61211c5d2643a9f8b9edbc4c6c5e5e (diff) | |
download | ale-c26e5e277e0a0e0849d416775b63753e3aae4be6.zip |
Fix #491 - Only set -x for shellcheck for versions which support the option
Diffstat (limited to 'ale_linters/sh')
-rw-r--r-- | ale_linters/sh/shellcheck.vim | 46 |
1 files changed, 42 insertions, 4 deletions
diff --git a/ale_linters/sh/shellcheck.vim b/ale_linters/sh/shellcheck.vim index 32b47e22..1f6bdf81 100644 --- a/ale_linters/sh/shellcheck.vim +++ b/ale_linters/sh/shellcheck.vim @@ -15,6 +15,8 @@ let g:ale_sh_shellcheck_executable = let g:ale_sh_shellcheck_options = \ get(g:, 'ale_sh_shellcheck_options', '') +let s:version_cache = {} + function! ale_linters#sh#shellcheck#GetExecutable(buffer) abort return ale#Var(a:buffer, 'sh_shellcheck_executable') endfunction @@ -43,22 +45,58 @@ function! ale_linters#sh#shellcheck#GetDialectArgument(buffer) abort return '' endfunction -function! ale_linters#sh#shellcheck#GetCommand(buffer) abort +function! ale_linters#sh#shellcheck#VersionCheck(buffer) abort + let l:executable = ale_linters#sh#shellcheck#GetExecutable(a:buffer) + + " Don't check the version again if we've already cached it. + if has_key(s:version_cache, l:executable) + return '' + endif + + return ale#Escape(l:executable) . ' --version' +endfunction + +" Get the shellcheck version from the cache, or parse it and cache it. +function! s:GetVersion(executable, output) abort + let l:version = get(s:version_cache, a:executable, []) + + for l:match in ale#util#GetMatches(a:output, '\v\d+\.\d+\.\d+') + let l:version = ale#semver#Parse(l:match[0]) + let s:version_cache[a:executable] = l:version + endfor + + return l:version +endfunction + +function! s:CanUseExternalOption(version) abort + return !empty(a:version) + \ && ale#semver#GreaterOrEqual(a:version, [0, 4, 0]) +endfunction + +function! ale_linters#sh#shellcheck#GetCommand(buffer, version_output) abort + let l:executable = ale_linters#sh#shellcheck#GetExecutable(a:buffer) + let l:version = s:GetVersion(l:executable, a:version_output) + let l:options = ale#Var(a:buffer, 'sh_shellcheck_options') let l:exclude_option = ale#Var(a:buffer, 'sh_shellcheck_exclusions') let l:dialect = ale_linters#sh#shellcheck#GetDialectArgument(a:buffer) + let l:external_option = s:CanUseExternalOption(l:version) ? ' -x' : '' return ale#path#BufferCdString(a:buffer) - \ . ale#Escape(ale_linters#sh#shellcheck#GetExecutable(a:buffer)) + \ . ale#Escape(l:executable) \ . (!empty(l:dialect) ? ' -s ' . l:dialect : '') \ . (!empty(l:options) ? ' ' . l:options : '') \ . (!empty(l:exclude_option) ? ' -e ' . l:exclude_option : '') - \ . ' -x -f gcc -' + \ . l:external_option + \ . ' -f gcc -' endfunction call ale#linter#Define('sh', { \ 'name': 'shellcheck', \ 'executable_callback': 'ale_linters#sh#shellcheck#GetExecutable', -\ 'command_callback': 'ale_linters#sh#shellcheck#GetCommand', +\ 'command_chain': [ +\ {'callback': 'ale_linters#sh#shellcheck#VersionCheck'}, +\ {'callback': 'ale_linters#sh#shellcheck#GetCommand'}, +\ ], \ 'callback': 'ale#handlers#gcc#HandleGCCFormat', \}) |