From 47d941b491844153304b3c87664f847df9d5dae4 Mon Sep 17 00:00:00 2001 From: Ian2020 Date: Fri, 24 Apr 2020 11:39:45 +0100 Subject: Add shellcheck as linter for bats files --- ale_linters/bats/shellcheck.vim | 78 +++++++++++++++++++++++++++++++ doc/ale-bats.txt | 48 +++++++++++++++++++ doc/ale-supported-languages-and-tools.txt | 2 + supported-tools.md | 2 + 4 files changed, 130 insertions(+) create mode 100644 ale_linters/bats/shellcheck.vim create mode 100644 doc/ale-bats.txt diff --git a/ale_linters/bats/shellcheck.vim b/ale_linters/bats/shellcheck.vim new file mode 100644 index 00000000..64130a2d --- /dev/null +++ b/ale_linters/bats/shellcheck.vim @@ -0,0 +1,78 @@ +" Author: Ian2020 +" Description: This file adds support for using the shellcheck linter with +" bats scripts. Heavily inspired by/copied from work by w0rp on shellcheck +" for sh files. + +" This global variable can be set with a string of comma-separated error +" codes to exclude from shellcheck. For example: +" +" let g:ale_bats_shellcheck_exclusions = 'SC2002,SC2004' +call ale#Set('bats_shellcheck_exclusions', get(g:, 'ale_linters_bats_shellcheck_exclusions', '')) +call ale#Set('bats_shellcheck_executable', 'shellcheck') +call ale#Set('bats_shellcheck_options', '') +call ale#Set('bats_shellcheck_change_directory', 1) + +function! ale_linters#bats#shellcheck#GetCommand(buffer, version) abort + let l:options = ale#Var(a:buffer, 'bats_shellcheck_options') + let l:exclude_option = ale#Var(a:buffer, 'bats_shellcheck_exclusions') + let l:external_option = ale#semver#GTE(a:version, [0, 4, 0]) ? ' -x' : '' + let l:cd_string = ale#Var(a:buffer, 'bats_shellcheck_change_directory') + \ ? ale#path#BufferCdString(a:buffer) + \ : '' + + return l:cd_string + \ . '%e' + \ . ' -s bats' + \ . (!empty(l:options) ? ' ' . l:options : '') + \ . (!empty(l:exclude_option) ? ' -e ' . l:exclude_option : '') + \ . l:external_option + \ . ' -f gcc -' +endfunction + +function! ale_linters#bats#shellcheck#Handle(buffer, lines) abort + let l:pattern = '\v^([a-zA-Z]?:?[^:]+):(\d+):(\d+)?:? ([^:]+): (.+) \[([^\]]+)\]$' + let l:output = [] + + for l:match in ale#util#GetMatches(a:lines, l:pattern) + if l:match[4] is# 'error' + let l:type = 'E' + elseif l:match[4] is# 'note' + let l:type = 'I' + else + let l:type = 'W' + endif + + let l:item = { + \ 'lnum': str2nr(l:match[2]), + \ 'type': l:type, + \ 'text': l:match[5], + \ 'code': l:match[6], + \} + + if !empty(l:match[3]) + let l:item.col = str2nr(l:match[3]) + endif + + " If the filename is something like , or -, then + " this is an error for the file we checked. + if l:match[1] isnot# '-' && l:match[1][0] isnot# '<' + let l:item['filename'] = l:match[1] + endif + + call add(l:output, l:item) + endfor + + return l:output +endfunction + +call ale#linter#Define('bats', { +\ 'name': 'shellcheck', +\ 'executable': {buffer -> ale#Var(buffer, 'bats_shellcheck_executable')}, +\ 'command': {buffer -> ale#semver#RunWithVersionCheck( +\ buffer, +\ ale#Var(buffer, 'bats_shellcheck_executable'), +\ '%e --version', +\ function('ale_linters#bats#shellcheck#GetCommand'), +\ )}, +\ 'callback': 'ale_linters#bats#shellcheck#Handle', +\}) diff --git a/doc/ale-bats.txt b/doc/ale-bats.txt new file mode 100644 index 00000000..2ad98402 --- /dev/null +++ b/doc/ale-bats.txt @@ -0,0 +1,48 @@ +=============================================================================== +ALE Bats Integration *ale-bats-options* + + +=============================================================================== +shellcheck *ale-bats-shellcheck* + +g:ale_bats_shellcheck_executable *g:ale_bats_shellcheck_executable* + *b:ale_bats_shellcheck_executable* + Type: |String| + Default: `'shellcheck'` + + This variable sets executable used for shellcheck. + + +g:ale_bats_shellcheck_options *g:ale_bats_shellcheck_options* + *b:ale_bats_shellcheck_options* + Type: |String| + Default: `''` + + With this variable we are able to pass extra arguments for shellcheck + for shellcheck invocation. + + For example, if we want shellcheck to follow external sources (`see SC1091`) + we can set the variable as such: +> + let g:ale_bats_shellcheck_options = '-x' +< + + +g:ale_bats_shellcheck_change_directory *g:ale_bats_shellcheck_change_directory* + *b:ale_bats_shellcheck_change_directory* + Type: |Number| + Default: `1` + + If set to `1`, ALE will switch to the directory the shell file being + checked with `shellcheck` is in before checking it. This helps `shellcheck` + determine the path to sourced files more easily. This option can be turned + off if you want to control the directory `shellcheck` is executed from + yourself. + + + autocmd BufEnter PKGBUILD,.env + \ let b:ale_bats_shellcheck_exclusions = 'SC2034,SC2154,SC2164' +< + +=============================================================================== + vim:tw=78:ts=2:sts=2:sw=2:ft=help:norl: diff --git a/doc/ale-supported-languages-and-tools.txt b/doc/ale-supported-languages-and-tools.txt index 636985fb..9fe7bfe5 100644 --- a/doc/ale-supported-languages-and-tools.txt +++ b/doc/ale-supported-languages-and-tools.txt @@ -35,6 +35,8 @@ Notes: * `shell` (-n flag) * `shellcheck` * `shfmt` +* Bats + * `shellcheck` * BibTeX * `bibclean` * Bourne Shell diff --git a/supported-tools.md b/supported-tools.md index d5c0c9d4..f7dd9676 100644 --- a/supported-tools.md +++ b/supported-tools.md @@ -44,6 +44,8 @@ formatting. * shell [-n flag](https://www.gnu.org/software/bash/manual/bash.html#index-set) * [shellcheck](https://www.shellcheck.net/) * [shfmt](https://github.com/mvdan/sh) +* Bats + * [shellcheck](https://www.shellcheck.net/) * BibTeX * [bibclean](http://ftp.math.utah.edu/pub/bibclean/) * Bourne Shell -- cgit v1.2.3 From 76cd6b0f92e7e3baffe0dc83c6d8a75ccb517a1f Mon Sep 17 00:00:00 2001 From: Ian2020 Date: Tue, 28 Apr 2020 16:02:46 +0100 Subject: Fix documentation oversights --- doc/ale-bats.txt | 2 +- doc/ale.txt | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/doc/ale-bats.txt b/doc/ale-bats.txt index 2ad98402..8554d186 100644 --- a/doc/ale-bats.txt +++ b/doc/ale-bats.txt @@ -1,5 +1,5 @@ =============================================================================== -ALE Bats Integration *ale-bats-options* +ALE Bats Integration *ale-bats-options* =============================================================================== diff --git a/doc/ale.txt b/doc/ale.txt index 16b204a4..acca23f8 100644 --- a/doc/ale.txt +++ b/doc/ale.txt @@ -2279,6 +2279,8 @@ documented in additional help files. gcc...................................|ale-asm-gcc| awk.....................................|ale-awk-options| gawk..................................|ale-awk-gawk| + bats....................................|ale-bats-options| + shellcheck............................|ale-bats-shellcheck| bib.....................................|ale-bib-options| bibclean..............................|ale-bib-bibclean| c.......................................|ale-c-options| -- cgit v1.2.3 From d4e1c57026395c53547e18ab290fc588e0645d22 Mon Sep 17 00:00:00 2001 From: Ian2020 Date: Tue, 28 Apr 2020 17:46:15 +0100 Subject: Moved common code to ale handlers, updated bats doc --- ale_linters/bats/shellcheck.vim | 73 +----------------------- ale_linters/sh/shellcheck.vim | 103 +-------------------------------- autoload/ale/handlers/shellcheck.vim | 107 +++++++++++++++++++++++++++++++++++ doc/ale-bats.txt | 39 +------------ 4 files changed, 115 insertions(+), 207 deletions(-) create mode 100644 autoload/ale/handlers/shellcheck.vim diff --git a/ale_linters/bats/shellcheck.vim b/ale_linters/bats/shellcheck.vim index 64130a2d..b5a1184b 100644 --- a/ale_linters/bats/shellcheck.vim +++ b/ale_linters/bats/shellcheck.vim @@ -3,76 +3,9 @@ " bats scripts. Heavily inspired by/copied from work by w0rp on shellcheck " for sh files. -" This global variable can be set with a string of comma-separated error -" codes to exclude from shellcheck. For example: -" -" let g:ale_bats_shellcheck_exclusions = 'SC2002,SC2004' -call ale#Set('bats_shellcheck_exclusions', get(g:, 'ale_linters_bats_shellcheck_exclusions', '')) -call ale#Set('bats_shellcheck_executable', 'shellcheck') -call ale#Set('bats_shellcheck_options', '') -call ale#Set('bats_shellcheck_change_directory', 1) - -function! ale_linters#bats#shellcheck#GetCommand(buffer, version) abort - let l:options = ale#Var(a:buffer, 'bats_shellcheck_options') - let l:exclude_option = ale#Var(a:buffer, 'bats_shellcheck_exclusions') - let l:external_option = ale#semver#GTE(a:version, [0, 4, 0]) ? ' -x' : '' - let l:cd_string = ale#Var(a:buffer, 'bats_shellcheck_change_directory') - \ ? ale#path#BufferCdString(a:buffer) - \ : '' - - return l:cd_string - \ . '%e' - \ . ' -s bats' - \ . (!empty(l:options) ? ' ' . l:options : '') - \ . (!empty(l:exclude_option) ? ' -e ' . l:exclude_option : '') - \ . l:external_option - \ . ' -f gcc -' -endfunction - -function! ale_linters#bats#shellcheck#Handle(buffer, lines) abort - let l:pattern = '\v^([a-zA-Z]?:?[^:]+):(\d+):(\d+)?:? ([^:]+): (.+) \[([^\]]+)\]$' - let l:output = [] - - for l:match in ale#util#GetMatches(a:lines, l:pattern) - if l:match[4] is# 'error' - let l:type = 'E' - elseif l:match[4] is# 'note' - let l:type = 'I' - else - let l:type = 'W' - endif - - let l:item = { - \ 'lnum': str2nr(l:match[2]), - \ 'type': l:type, - \ 'text': l:match[5], - \ 'code': l:match[6], - \} - - if !empty(l:match[3]) - let l:item.col = str2nr(l:match[3]) - endif - - " If the filename is something like , or -, then - " this is an error for the file we checked. - if l:match[1] isnot# '-' && l:match[1][0] isnot# '<' - let l:item['filename'] = l:match[1] - endif - - call add(l:output, l:item) - endfor - - return l:output -endfunction - call ale#linter#Define('bats', { \ 'name': 'shellcheck', -\ 'executable': {buffer -> ale#Var(buffer, 'bats_shellcheck_executable')}, -\ 'command': {buffer -> ale#semver#RunWithVersionCheck( -\ buffer, -\ ale#Var(buffer, 'bats_shellcheck_executable'), -\ '%e --version', -\ function('ale_linters#bats#shellcheck#GetCommand'), -\ )}, -\ 'callback': 'ale_linters#bats#shellcheck#Handle', +\ 'executable': function('ale#handlers#shellcheck#GetExecutable'), +\ 'command': function('ale#handlers#shellcheck#GetCommand'), +\ 'callback': 'ale#handlers#shellcheck#Handle', \}) diff --git a/ale_linters/sh/shellcheck.vim b/ale_linters/sh/shellcheck.vim index 1d8b6096..fbd7e49b 100644 --- a/ale_linters/sh/shellcheck.vim +++ b/ale_linters/sh/shellcheck.vim @@ -2,106 +2,9 @@ " Description: This file adds support for using the shellcheck linter with " 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_linters#sh#shellcheck#GetDialectArgument(buffer) abort - let l:shell_type = ale#handlers#sh#GetShellType(a:buffer) - - if !empty(l:shell_type) - " Use the dash dialect for /bin/ash, etc. - if l:shell_type is# 'ash' - return 'dash' - endif - - return l:shell_type - endif - - " If there's no hashbang, try using Vim's buffer variables. - if getbufvar(a:buffer, 'is_bash', 0) - return 'bash' - elseif getbufvar(a:buffer, 'is_sh', 0) - return 'sh' - elseif getbufvar(a:buffer, 'is_kornshell', 0) - return 'ksh' - endif - - return '' -endfunction - -function! ale_linters#sh#shellcheck#GetCommand(buffer, version) abort - 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#Var(a:buffer, 'sh_shellcheck_dialect') - let l:external_option = ale#semver#GTE(a:version, [0, 4, 0]) ? ' -x' : '' - let l:cd_string = ale#Var(a:buffer, 'sh_shellcheck_change_directory') - \ ? ale#path#BufferCdString(a:buffer) - \ : '' - - if l:dialect is# 'auto' - let l:dialect = ale_linters#sh#shellcheck#GetDialectArgument(a:buffer) - endif - - return l:cd_string - \ . '%e' - \ . (!empty(l:dialect) ? ' -s ' . l:dialect : '') - \ . (!empty(l:options) ? ' ' . l:options : '') - \ . (!empty(l:exclude_option) ? ' -e ' . l:exclude_option : '') - \ . l:external_option - \ . ' -f gcc -' -endfunction - -function! ale_linters#sh#shellcheck#Handle(buffer, lines) abort - let l:pattern = '\v^([a-zA-Z]?:?[^:]+):(\d+):(\d+)?:? ([^:]+): (.+) \[([^\]]+)\]$' - let l:output = [] - - for l:match in ale#util#GetMatches(a:lines, l:pattern) - if l:match[4] is# 'error' - let l:type = 'E' - elseif l:match[4] is# 'note' - let l:type = 'I' - else - let l:type = 'W' - endif - - let l:item = { - \ 'lnum': str2nr(l:match[2]), - \ 'type': l:type, - \ 'text': l:match[5], - \ 'code': l:match[6], - \} - - if !empty(l:match[3]) - let l:item.col = str2nr(l:match[3]) - endif - - " If the filename is something like , or -, then - " this is an error for the file we checked. - if l:match[1] isnot# '-' && l:match[1][0] isnot# '<' - let l:item['filename'] = l:match[1] - endif - - call add(l:output, l:item) - endfor - - return l:output -endfunction - call ale#linter#Define('sh', { \ 'name': 'shellcheck', -\ 'executable': {buffer -> ale#Var(buffer, 'sh_shellcheck_executable')}, -\ 'command': {buffer -> ale#semver#RunWithVersionCheck( -\ buffer, -\ ale#Var(buffer, 'sh_shellcheck_executable'), -\ '%e --version', -\ function('ale_linters#sh#shellcheck#GetCommand'), -\ )}, -\ 'callback': 'ale_linters#sh#shellcheck#Handle', +\ 'executable': function('ale#handlers#shellcheck#GetExecutable'), +\ 'command': function('ale#handlers#shellcheck#GetCommand'), +\ 'callback': 'ale#handlers#shellcheck#Handle', \}) diff --git a/autoload/ale/handlers/shellcheck.vim b/autoload/ale/handlers/shellcheck.vim new file mode 100644 index 00000000..b2de4eef --- /dev/null +++ b/autoload/ale/handlers/shellcheck.vim @@ -0,0 +1,107 @@ +" Author: w0rp +" Description: This file adds support for using the shellcheck linter with +" 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 + let l:shell_type = ale#handlers#sh#GetShellType(a:buffer) + + if !empty(l:shell_type) + " Use the dash dialect for /bin/ash, etc. + if l:shell_type is# 'ash' + return 'dash' + endif + + return l:shell_type + endif + + " If there's no hashbang, try using Vim's buffer variables. + if getbufvar(a:buffer, 'is_bash', 0) + return 'bash' + elseif getbufvar(a:buffer, 'is_sh', 0) + return 'sh' + elseif getbufvar(a:buffer, 'is_kornshell', 0) + return 'ksh' + endif + + return '' +endfunction + +function! ale#handlers#shellcheck#GetCommandWithVersion(buffer, version) abort + 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#Var(a:buffer, 'sh_shellcheck_dialect') + let l:external_option = ale#semver#GTE(a:version, [0, 4, 0]) ? ' -x' : '' + let l:cd_string = ale#Var(a:buffer, 'sh_shellcheck_change_directory') + \ ? ale#path#BufferCdString(a:buffer) + \ : '' + + if l:dialect is# 'auto' + let l:dialect = ale#handlers#shellcheck#GetDialectArgument(a:buffer) + endif + + return l:cd_string + \ . '%e' + \ . (!empty(l:dialect) ? ' -s ' . l:dialect : '') + \ . (!empty(l:options) ? ' ' . l:options : '') + \ . (!empty(l:exclude_option) ? ' -e ' . l:exclude_option : '') + \ . l:external_option + \ . ' -f gcc -' +endfunction + +function! ale#handlers#shellcheck#Handle(buffer, lines) abort + let l:pattern = '\v^([a-zA-Z]?:?[^:]+):(\d+):(\d+)?:? ([^:]+): (.+) \[([^\]]+)\]$' + let l:output = [] + + for l:match in ale#util#GetMatches(a:lines, l:pattern) + if l:match[4] is# 'error' + let l:type = 'E' + elseif l:match[4] is# 'note' + let l:type = 'I' + else + let l:type = 'W' + endif + + let l:item = { + \ 'lnum': str2nr(l:match[2]), + \ 'type': l:type, + \ 'text': l:match[5], + \ 'code': l:match[6], + \} + + if !empty(l:match[3]) + let l:item.col = str2nr(l:match[3]) + endif + + " If the filename is something like , or -, then + " this is an error for the file we checked. + if l:match[1] isnot# '-' && l:match[1][0] isnot# '<' + let l:item['filename'] = l:match[1] + endif + + call add(l:output, l:item) + endfor + + return l:output +endfunction + +function! ale#handlers#shellcheck#GetExecutable(buffer) abort + return ale#Var(a:buffer, 'sh_shellcheck_executable') +endfunction + +function! ale#handlers#shellcheck#GetCommand(buffer) abort + return ale#semver#RunWithVersionCheck(a:buffer, +\ ale#Var(a:buffer, 'sh_shellcheck_executable'), +\ '%e --version', +\ function('ale#handlers#shellcheck#GetCommandWithVersion'), +\ ) +endfunction diff --git a/doc/ale-bats.txt b/doc/ale-bats.txt index 8554d186..cf2199ec 100644 --- a/doc/ale-bats.txt +++ b/doc/ale-bats.txt @@ -5,44 +5,9 @@ ALE Bats Integration *ale-bats-options =============================================================================== shellcheck *ale-bats-shellcheck* -g:ale_bats_shellcheck_executable *g:ale_bats_shellcheck_executable* - *b:ale_bats_shellcheck_executable* - Type: |String| - Default: `'shellcheck'` +The `shellcheck` linter for Bats uses the sh options for `shellcheck`; see: +|ale-sh-shellcheck|. - This variable sets executable used for shellcheck. - - -g:ale_bats_shellcheck_options *g:ale_bats_shellcheck_options* - *b:ale_bats_shellcheck_options* - Type: |String| - Default: `''` - - With this variable we are able to pass extra arguments for shellcheck - for shellcheck invocation. - - For example, if we want shellcheck to follow external sources (`see SC1091`) - we can set the variable as such: -> - let g:ale_bats_shellcheck_options = '-x' -< - - -g:ale_bats_shellcheck_change_directory *g:ale_bats_shellcheck_change_directory* - *b:ale_bats_shellcheck_change_directory* - Type: |Number| - Default: `1` - - If set to `1`, ALE will switch to the directory the shell file being - checked with `shellcheck` is in before checking it. This helps `shellcheck` - determine the path to sourced files more easily. This option can be turned - off if you want to control the directory `shellcheck` is executed from - yourself. - - - autocmd BufEnter PKGBUILD,.env - \ let b:ale_bats_shellcheck_exclusions = 'SC2034,SC2154,SC2164' -< =============================================================================== vim:tw=78:ts=2:sts=2:sw=2:ft=help:norl: -- cgit v1.2.3 From 3f3426515107ae62b2e0d158ab192fcec95cfd10 Mon Sep 17 00:00:00 2001 From: Ian2020 Date: Tue, 28 Apr 2020 18:13:34 +0100 Subject: Fix handler test, function name has changed --- test/handler/test_shellcheck_handler.vader | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/handler/test_shellcheck_handler.vader b/test/handler/test_shellcheck_handler.vader index bfb73ffa..33f12989 100644 --- a/test/handler/test_shellcheck_handler.vader +++ b/test/handler/test_shellcheck_handler.vader @@ -22,7 +22,7 @@ Execute(The shellcheck handler should handle basic errors or warnings): \ 'code': 'SC1068', \ }, \ ], - \ ale_linters#sh#shellcheck#Handle(bufnr(''), [ + \ ale#handlers#shellcheck#Handle(bufnr(''), [ \ '-:2:1: warning: In POSIX sh, ''let'' is not supported. [SC2039]', \ '-:2:3: error: Don''t put spaces around the = in assignments. [SC1068]', \ ]) @@ -38,6 +38,6 @@ Execute(The shellcheck handler should handle notes): \ 'code': 'SC2086', \ }, \ ], - \ ale_linters#sh#shellcheck#Handle(bufnr(''), [ + \ ale#handlers#shellcheck#Handle(bufnr(''), [ \ '-:3:3: note: Double quote to prevent globbing and word splitting. [SC2086]', \ ]) -- cgit v1.2.3 From f67cb56e5e73f00da643b050bfb6ab6e4416c5a2 Mon Sep 17 00:00:00 2001 From: Ian2020 Date: Tue, 28 Apr 2020 18:20:10 +0100 Subject: Fix shell detection test, shellcheck function name has changed --- test/test_shell_detection.vader | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/test/test_shell_detection.vader b/test/test_shell_detection.vader index 88ee462d..6452287f 100644 --- a/test/test_shell_detection.vader +++ b/test/test_shell_detection.vader @@ -15,7 +15,7 @@ Given(A file with a Bash hashbang): Execute(/bin/bash should be detected appropriately): AssertEqual 'bash', ale#handlers#sh#GetShellType(bufnr('')) AssertEqual 'bash', ale_linters#sh#shell#GetExecutable(bufnr('')) - AssertEqual 'bash', ale_linters#sh#shellcheck#GetDialectArgument(bufnr('')) + AssertEqual 'bash', ale#handlers#shellcheck#GetDialectArgument(bufnr('')) Given(A file with /bin/sh): #!/usr/bin/env sh -eu --foobar @@ -23,7 +23,7 @@ Given(A file with /bin/sh): Execute(/bin/sh should be detected appropriately): AssertEqual 'sh', ale#handlers#sh#GetShellType(bufnr('')) AssertEqual 'sh', ale_linters#sh#shell#GetExecutable(bufnr('')) - AssertEqual 'sh', ale_linters#sh#shellcheck#GetDialectArgument(bufnr('')) + AssertEqual 'sh', ale#handlers#shellcheck#GetDialectArgument(bufnr('')) Given(A file with bash as an argument to env): #!/usr/bin/env bash @@ -31,7 +31,7 @@ Given(A file with bash as an argument to env): Execute(/usr/bin/env bash should be detected appropriately): AssertEqual 'bash', ale#handlers#sh#GetShellType(bufnr('')) AssertEqual 'bash', ale_linters#sh#shell#GetExecutable(bufnr('')) - AssertEqual 'bash', ale_linters#sh#shellcheck#GetDialectArgument(bufnr('')) + AssertEqual 'bash', ale#handlers#shellcheck#GetDialectArgument(bufnr('')) Given(A file with a tcsh hash bang and arguments): #!/usr/bin/env tcsh -eu --foobar @@ -39,7 +39,7 @@ Given(A file with a tcsh hash bang and arguments): Execute(tcsh should be detected appropriately): AssertEqual 'tcsh', ale#handlers#sh#GetShellType(bufnr('')) AssertEqual 'tcsh', ale_linters#sh#shell#GetExecutable(bufnr('')) - AssertEqual 'tcsh', ale_linters#sh#shellcheck#GetDialectArgument(bufnr('')) + AssertEqual 'tcsh', ale#handlers#shellcheck#GetDialectArgument(bufnr('')) Given(A file with a zsh hash bang and arguments): #!/usr/bin/env zsh -eu --foobar @@ -47,7 +47,7 @@ Given(A file with a zsh hash bang and arguments): Execute(zsh should be detected appropriately): AssertEqual 'zsh', ale#handlers#sh#GetShellType(bufnr('')) AssertEqual 'zsh', ale_linters#sh#shell#GetExecutable(bufnr('')) - AssertEqual 'zsh', ale_linters#sh#shellcheck#GetDialectArgument(bufnr('')) + AssertEqual 'zsh', ale#handlers#shellcheck#GetDialectArgument(bufnr('')) Given(A file with a csh hash bang and arguments): #!/usr/bin/env csh -eu --foobar @@ -55,7 +55,7 @@ Given(A file with a csh hash bang and arguments): Execute(csh should be detected appropriately): AssertEqual 'csh', ale#handlers#sh#GetShellType(bufnr('')) AssertEqual 'csh', ale_linters#sh#shell#GetExecutable(bufnr('')) - AssertEqual 'csh', ale_linters#sh#shellcheck#GetDialectArgument(bufnr('')) + AssertEqual 'csh', ale#handlers#shellcheck#GetDialectArgument(bufnr('')) Given(A file with a ksh hashbang): #!/bin/ksh @@ -63,7 +63,7 @@ Given(A file with a ksh hashbang): Execute(/bin/ksh should be detected appropriately): AssertEqual 'ksh', ale#handlers#sh#GetShellType(bufnr('')) AssertEqual 'ksh', ale_linters#sh#shell#GetExecutable(bufnr('')) - AssertEqual 'ksh', ale_linters#sh#shellcheck#GetDialectArgument(bufnr('')) + AssertEqual 'ksh', ale#handlers#shellcheck#GetDialectArgument(bufnr('')) Given(A file with a ksh as an argument to env): #!/usr/bin/env ksh @@ -71,7 +71,7 @@ Given(A file with a ksh as an argument to env): Execute(ksh should be detected appropriately): AssertEqual 'ksh', ale#handlers#sh#GetShellType(bufnr('')) AssertEqual 'ksh', ale_linters#sh#shell#GetExecutable(bufnr('')) - AssertEqual 'ksh', ale_linters#sh#shellcheck#GetDialectArgument(bufnr('')) + AssertEqual 'ksh', ale#handlers#shellcheck#GetDialectArgument(bufnr('')) Given(A file with a sh hash bang and arguments): #!/usr/bin/env sh -eu --foobar @@ -79,24 +79,24 @@ Given(A file with a sh hash bang and arguments): Execute(sh should be detected appropriately): AssertEqual 'sh', ale#handlers#sh#GetShellType(bufnr('')) AssertEqual 'sh', ale_linters#sh#shell#GetExecutable(bufnr('')) - AssertEqual 'sh', ale_linters#sh#shellcheck#GetDialectArgument(bufnr('')) + AssertEqual 'sh', ale#handlers#shellcheck#GetDialectArgument(bufnr('')) Given(A file without a hashbang): Execute(The bash dialect should be used for shellcheck if b:is_bash is 1): let b:is_bash = 1 - AssertEqual 'bash', ale_linters#sh#shellcheck#GetDialectArgument(bufnr('')) + AssertEqual 'bash', ale#handlers#shellcheck#GetDialectArgument(bufnr('')) Execute(The sh dialect should be used for shellcheck if b:is_sh is 1): let b:is_sh = 1 - AssertEqual 'sh', ale_linters#sh#shellcheck#GetDialectArgument(bufnr('')) + AssertEqual 'sh', ale#handlers#shellcheck#GetDialectArgument(bufnr('')) Execute(The ksh dialect should be used for shellcheck if b:is_kornshell is 1): let b:is_kornshell = 1 - AssertEqual 'ksh', ale_linters#sh#shellcheck#GetDialectArgument(bufnr('')) + AssertEqual 'ksh', ale#handlers#shellcheck#GetDialectArgument(bufnr('')) Given(A file with /bin/ash): #!/bin/ash @@ -106,7 +106,7 @@ Execute(The ash dialect should be used for the shell and the base function): AssertEqual 'ash', ale_linters#sh#shell#GetExecutable(bufnr('')) Execute(dash should be used for shellcheck, which has no ash dialect): - AssertEqual 'dash', ale_linters#sh#shellcheck#GetDialectArgument(bufnr('')) + AssertEqual 'dash', ale#handlers#shellcheck#GetDialectArgument(bufnr('')) Given(A file with /bin/dash): #!/bin/dash @@ -116,4 +116,4 @@ Execute(The dash dialect should be used for the shell and the base function): AssertEqual 'dash', ale_linters#sh#shell#GetExecutable(bufnr('')) Execute(dash should be used for shellcheck): - AssertEqual 'dash', ale_linters#sh#shellcheck#GetDialectArgument(bufnr('')) + AssertEqual 'dash', ale#handlers#shellcheck#GetDialectArgument(bufnr('')) -- cgit v1.2.3 From 716f9a9bbb46162e967cb95b892d0516a06ef129 Mon Sep 17 00:00:00 2001 From: Ian2020 Date: Tue, 28 Apr 2020 20:53:42 +0100 Subject: Fix linting issue - indentation incorrect --- autoload/ale/handlers/shellcheck.vim | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/autoload/ale/handlers/shellcheck.vim b/autoload/ale/handlers/shellcheck.vim index b2de4eef..2a5eadc3 100644 --- a/autoload/ale/handlers/shellcheck.vim +++ b/autoload/ale/handlers/shellcheck.vim @@ -100,8 +100,8 @@ endfunction function! ale#handlers#shellcheck#GetCommand(buffer) abort return ale#semver#RunWithVersionCheck(a:buffer, -\ ale#Var(a:buffer, 'sh_shellcheck_executable'), -\ '%e --version', -\ function('ale#handlers#shellcheck#GetCommandWithVersion'), -\ ) + \ ale#Var(a:buffer, 'sh_shellcheck_executable'), + \ '%e --version', + \ function('ale#handlers#shellcheck#GetCommandWithVersion'), + \) endfunction -- cgit v1.2.3 From 65bea1a5cbbc8ff2557410263c87c2e02cb4a42c Mon Sep 17 00:00:00 2001 From: Ian2020 Date: Wed, 29 Apr 2020 17:45:16 +0100 Subject: Main logic of shellcheck has moved, updated backward compat test --- test/test_backwards_compatibility.vader | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_backwards_compatibility.vader b/test/test_backwards_compatibility.vader index e4e3756f..e0e52bd4 100644 --- a/test/test_backwards_compatibility.vader +++ b/test/test_backwards_compatibility.vader @@ -8,7 +8,7 @@ After: Execute(Old variable name for the 'shellcheck' linter should still work): let g:ale_linters_sh_shellcheck_exclusions = 'SC1234' - runtime ale_linters/sh/shellcheck.vim + runtime autoload/ale/handlers/shellcheck.vim AssertEqual 'SC1234', g:ale_sh_shellcheck_exclusions -- cgit v1.2.3 From ca97f32258cc6e3f32b5f7605801f053e7dbe320 Mon Sep 17 00:00:00 2001 From: Ian2020 Date: Fri, 8 May 2020 12:55:54 +0100 Subject: Use a function to define shellcheck linters and vars needed, fixes tests --- ale_linters/bats/shellcheck.vim | 11 ++------ ale_linters/sh/shellcheck.vim | 10 ++----- autoload/ale/handlers/shellcheck.vim | 46 ++++++++++++++++----------------- test/test_backwards_compatibility.vader | 2 +- 4 files changed, 28 insertions(+), 41 deletions(-) diff --git a/ale_linters/bats/shellcheck.vim b/ale_linters/bats/shellcheck.vim index b5a1184b..5c2a0ea9 100644 --- a/ale_linters/bats/shellcheck.vim +++ b/ale_linters/bats/shellcheck.vim @@ -1,11 +1,4 @@ " Author: Ian2020 -" Description: This file adds support for using the shellcheck linter with -" bats scripts. Heavily inspired by/copied from work by w0rp on shellcheck -" for sh files. +" Description: shellcheck linter for bats scripts. -call ale#linter#Define('bats', { -\ 'name': 'shellcheck', -\ 'executable': function('ale#handlers#shellcheck#GetExecutable'), -\ 'command': function('ale#handlers#shellcheck#GetCommand'), -\ 'callback': 'ale#handlers#shellcheck#Handle', -\}) +call ale#handlers#shellcheck#DefineLinter('bats') diff --git a/ale_linters/sh/shellcheck.vim b/ale_linters/sh/shellcheck.vim index fbd7e49b..d9945126 100644 --- a/ale_linters/sh/shellcheck.vim +++ b/ale_linters/sh/shellcheck.vim @@ -1,10 +1,4 @@ " Author: w0rp -" Description: This file adds support for using the shellcheck linter with -" shell scripts. +" Description: shellcheck linter for shell scripts. -call ale#linter#Define('sh', { -\ 'name': 'shellcheck', -\ 'executable': function('ale#handlers#shellcheck#GetExecutable'), -\ 'command': function('ale#handlers#shellcheck#GetCommand'), -\ 'callback': 'ale#handlers#shellcheck#Handle', -\}) +call ale#handlers#shellcheck#DefineLinter('sh') diff --git a/autoload/ale/handlers/shellcheck.vim b/autoload/ale/handlers/shellcheck.vim index 2a5eadc3..b16280f0 100644 --- a/autoload/ale/handlers/shellcheck.vim +++ b/autoload/ale/handlers/shellcheck.vim @@ -1,16 +1,5 @@ " Author: w0rp -" Description: This file adds support for using the shellcheck linter with -" 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) +" Description: This file adds support for using the shellcheck linter function! ale#handlers#shellcheck#GetDialectArgument(buffer) abort let l:shell_type = ale#handlers#sh#GetShellType(a:buffer) @@ -36,7 +25,7 @@ function! ale#handlers#shellcheck#GetDialectArgument(buffer) abort return '' 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:exclude_option = ale#Var(a:buffer, 'sh_shellcheck_exclusions') 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 endfunction -function! ale#handlers#shellcheck#GetExecutable(buffer) abort - return ale#Var(a:buffer, 'sh_shellcheck_executable') -endfunction - -function! ale#handlers#shellcheck#GetCommand(buffer) abort - return ale#semver#RunWithVersionCheck(a:buffer, - \ ale#Var(a:buffer, 'sh_shellcheck_executable'), - \ '%e --version', - \ function('ale#handlers#shellcheck#GetCommandWithVersion'), - \) +function! ale#handlers#shellcheck#DefineLinter(filetype) abort + " 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) + + call ale#linter#Define(a:filetype, { + \ 'name': 'shellcheck', + \ 'executable': {buffer -> ale#Var(buffer, 'sh_shellcheck_executable')}, + \ 'command': {buffer -> ale#semver#RunWithVersionCheck( + \ buffer, + \ ale#Var(buffer, 'sh_shellcheck_executable'), + \ '%e --version', + \ function('ale#handlers#shellcheck#GetCommand'), + \ )}, + \ 'callback': 'ale#handlers#shellcheck#Handle', + \}) endfunction diff --git a/test/test_backwards_compatibility.vader b/test/test_backwards_compatibility.vader index e0e52bd4..e4e3756f 100644 --- a/test/test_backwards_compatibility.vader +++ b/test/test_backwards_compatibility.vader @@ -8,7 +8,7 @@ After: Execute(Old variable name for the 'shellcheck' linter should still work): 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 -- cgit v1.2.3