Use a function to define shellcheck linters and vars needed, fixes tests

This commit is contained in:
Ian2020 2020-05-08 12:55:54 +01:00
parent 65bea1a5cb
commit ca97f32258
4 changed files with 27 additions and 40 deletions

View file

@ -1,11 +1,4 @@
" Author: Ian2020 <https://github.com/Ian2020> " Author: Ian2020 <https://github.com/Ian2020>
" Description: This file adds support for using the shellcheck linter with " Description: shellcheck linter for bats scripts.
" bats scripts. Heavily inspired by/copied from work by w0rp on shellcheck
" for sh files.
call ale#linter#Define('bats', { call ale#handlers#shellcheck#DefineLinter('bats')
\ 'name': 'shellcheck',
\ 'executable': function('ale#handlers#shellcheck#GetExecutable'),
\ 'command': function('ale#handlers#shellcheck#GetCommand'),
\ 'callback': 'ale#handlers#shellcheck#Handle',
\})

View file

@ -1,10 +1,4 @@
" Author: w0rp <devw0rp@gmail.com> " Author: w0rp <devw0rp@gmail.com>
" Description: This file adds support for using the shellcheck linter with " Description: shellcheck linter for shell scripts.
" shell scripts.
call ale#linter#Define('sh', { call ale#handlers#shellcheck#DefineLinter('sh')
\ 'name': 'shellcheck',
\ 'executable': function('ale#handlers#shellcheck#GetExecutable'),
\ 'command': function('ale#handlers#shellcheck#GetCommand'),
\ 'callback': 'ale#handlers#shellcheck#Handle',
\})

View file

@ -1,16 +1,5 @@
" Author: w0rp <devw0rp@gmail.com> " Author: w0rp <devw0rp@gmail.com>
" Description: This file adds support for using the shellcheck linter with " Description: This file adds support for using the shellcheck linter
" shell scripts.
" This global variable can be set with a string of comma-separated error
" codes to exclude from shellcheck. For example:
"
" let g:ale_sh_shellcheck_exclusions = 'SC2002,SC2004'
call ale#Set('sh_shellcheck_exclusions', get(g:, 'ale_linters_sh_shellcheck_exclusions', ''))
call ale#Set('sh_shellcheck_executable', 'shellcheck')
call ale#Set('sh_shellcheck_dialect', 'auto')
call ale#Set('sh_shellcheck_options', '')
call ale#Set('sh_shellcheck_change_directory', 1)
function! ale#handlers#shellcheck#GetDialectArgument(buffer) abort function! ale#handlers#shellcheck#GetDialectArgument(buffer) abort
let l:shell_type = ale#handlers#sh#GetShellType(a:buffer) let l:shell_type = ale#handlers#sh#GetShellType(a:buffer)
@ -36,7 +25,7 @@ function! ale#handlers#shellcheck#GetDialectArgument(buffer) abort
return '' return ''
endfunction endfunction
function! ale#handlers#shellcheck#GetCommandWithVersion(buffer, version) abort function! ale#handlers#shellcheck#GetCommand(buffer, version) abort
let l:options = ale#Var(a:buffer, 'sh_shellcheck_options') let l:options = ale#Var(a:buffer, 'sh_shellcheck_options')
let l:exclude_option = ale#Var(a:buffer, 'sh_shellcheck_exclusions') let l:exclude_option = ale#Var(a:buffer, 'sh_shellcheck_exclusions')
let l:dialect = ale#Var(a:buffer, 'sh_shellcheck_dialect') let l:dialect = ale#Var(a:buffer, 'sh_shellcheck_dialect')
@ -94,14 +83,25 @@ function! ale#handlers#shellcheck#Handle(buffer, lines) abort
return l:output return l:output
endfunction endfunction
function! ale#handlers#shellcheck#GetExecutable(buffer) abort function! ale#handlers#shellcheck#DefineLinter(filetype) abort
return ale#Var(a:buffer, 'sh_shellcheck_executable') " This global variable can be set with a string of comma-separated error
endfunction " codes to exclude from shellcheck. For example:
" let g:ale_sh_shellcheck_exclusions = 'SC2002,SC2004'
call ale#Set('sh_shellcheck_exclusions', get(g:, 'ale_linters_sh_shellcheck_exclusions', ''))
call ale#Set('sh_shellcheck_executable', 'shellcheck')
call ale#Set('sh_shellcheck_dialect', 'auto')
call ale#Set('sh_shellcheck_options', '')
call ale#Set('sh_shellcheck_change_directory', 1)
function! ale#handlers#shellcheck#GetCommand(buffer) abort call ale#linter#Define(a:filetype, {
return ale#semver#RunWithVersionCheck(a:buffer, \ 'name': 'shellcheck',
\ ale#Var(a:buffer, 'sh_shellcheck_executable'), \ 'executable': {buffer -> ale#Var(buffer, 'sh_shellcheck_executable')},
\ '%e --version', \ 'command': {buffer -> ale#semver#RunWithVersionCheck(
\ function('ale#handlers#shellcheck#GetCommandWithVersion'), \ buffer,
\) \ ale#Var(buffer, 'sh_shellcheck_executable'),
\ '%e --version',
\ function('ale#handlers#shellcheck#GetCommand'),
\ )},
\ 'callback': 'ale#handlers#shellcheck#Handle',
\})
endfunction endfunction

View file

@ -8,7 +8,7 @@ After:
Execute(Old variable name for the 'shellcheck' linter should still work): Execute(Old variable name for the 'shellcheck' linter should still work):
let g:ale_linters_sh_shellcheck_exclusions = 'SC1234' let g:ale_linters_sh_shellcheck_exclusions = 'SC1234'
runtime autoload/ale/handlers/shellcheck.vim runtime ale_linters/sh/shellcheck.vim
AssertEqual 'SC1234', g:ale_sh_shellcheck_exclusions AssertEqual 'SC1234', g:ale_sh_shellcheck_exclusions