mirror of
https://github.com/dense-analysis/ale
synced 2024-12-26 08:02:25 +00:00
Merge pull request #3134 from Ian2020/master
Add shellcheck as linter for bats files
This commit is contained in:
commit
c2b01f0e28
4
ale_linters/bats/shellcheck.vim
Normal file
4
ale_linters/bats/shellcheck.vim
Normal file
@ -0,0 +1,4 @@
|
||||
" Author: Ian2020 <https://github.com/Ian2020>
|
||||
" Description: shellcheck linter for bats scripts.
|
||||
|
||||
call ale#handlers#shellcheck#DefineLinter('bats')
|
@ -1,107 +1,4 @@
|
||||
" Author: w0rp <devw0rp@gmail.com>
|
||||
" Description: This file adds support for using the shellcheck linter with
|
||||
" shell scripts.
|
||||
" Description: shellcheck linter for 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 <stdin>, <nofile> 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',
|
||||
\})
|
||||
call ale#handlers#shellcheck#DefineLinter('sh')
|
||||
|
107
autoload/ale/handlers/shellcheck.vim
Normal file
107
autoload/ale/handlers/shellcheck.vim
Normal file
@ -0,0 +1,107 @@
|
||||
" Author: w0rp <devw0rp@gmail.com>
|
||||
" 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)
|
||||
|
||||
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#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#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 <stdin>, <nofile> 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#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
|
13
doc/ale-bats.txt
Normal file
13
doc/ale-bats.txt
Normal file
@ -0,0 +1,13 @@
|
||||
===============================================================================
|
||||
ALE Bats Integration *ale-bats-options*
|
||||
|
||||
|
||||
===============================================================================
|
||||
shellcheck *ale-bats-shellcheck*
|
||||
|
||||
The `shellcheck` linter for Bats uses the sh options for `shellcheck`; see:
|
||||
|ale-sh-shellcheck|.
|
||||
|
||||
|
||||
===============================================================================
|
||||
vim:tw=78:ts=2:sts=2:sw=2:ft=help:norl:
|
@ -35,6 +35,8 @@ Notes:
|
||||
* `shell` (-n flag)
|
||||
* `shellcheck`
|
||||
* `shfmt`
|
||||
* Bats
|
||||
* `shellcheck`
|
||||
* BibTeX
|
||||
* `bibclean`
|
||||
* Bourne Shell
|
||||
|
@ -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|
|
||||
|
@ -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
|
||||
|
@ -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]',
|
||||
\ ])
|
||||
|
@ -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(''))
|
||||
|
Loading…
Reference in New Issue
Block a user