Make staticcheck configurable with GOPATH detection

This commit is contained in:
w0rp 2021-05-27 22:03:39 +01:00
parent a02a4f2811
commit 1b08791228
No known key found for this signature in database
GPG Key ID: 0FC1ECAA8C81CD83
6 changed files with 63 additions and 25 deletions

View File

@ -7,20 +7,6 @@ call ale#Set('go_gopls_options', '--mode stdio')
call ale#Set('go_gopls_init_options', {}) call ale#Set('go_gopls_init_options', {})
call ale#Set('go_gopls_use_global', get(g:, 'ale_use_global_executables', 0)) call ale#Set('go_gopls_use_global', get(g:, 'ale_use_global_executables', 0))
function! s:GetGoPathExecutable(suffix) abort
let l:prefix = $GOPATH
if !empty($GOPATH)
let l:prefix = $GOPATH
elseif has('win32')
let l:prefix = $USERPROFILE . '/go'
else
let l:prefix = $HOME . '/go'
endif
return ale#path#Simplify(l:prefix . '/' . a:suffix)
endfunction
function! ale_linters#go#gopls#GetCommand(buffer) abort function! ale_linters#go#gopls#GetCommand(buffer) abort
return ale#go#EnvString(a:buffer) return ale#go#EnvString(a:buffer)
\ . '%e' \ . '%e'
@ -45,7 +31,7 @@ call ale#linter#Define('go', {
\ 'name': 'gopls', \ 'name': 'gopls',
\ 'lsp': 'stdio', \ 'lsp': 'stdio',
\ 'executable': {b -> ale#path#FindExecutable(b, 'go_gopls', [ \ 'executable': {b -> ale#path#FindExecutable(b, 'go_gopls', [
\ s:GetGoPathExecutable('bin/gopls'), \ ale#go#GetGoPathExecutable('bin/gopls'),
\ ])}, \ ])},
\ 'command': function('ale_linters#go#gopls#GetCommand'), \ 'command': function('ale_linters#go#gopls#GetCommand'),
\ 'project_root': function('ale_linters#go#gopls#FindProjectRoot'), \ 'project_root': function('ale_linters#go#gopls#FindProjectRoot'),

View File

@ -1,8 +1,10 @@
" Author: Ben Reedy <https://github.com/breed808> " Author: Ben Reedy <https://github.com/breed808>
" Description: staticcheck for Go files " Description: staticcheck for Go files
call ale#Set('go_staticcheck_executable', 'staticcheck')
call ale#Set('go_staticcheck_options', '') call ale#Set('go_staticcheck_options', '')
call ale#Set('go_staticcheck_lint_package', 0) call ale#Set('go_staticcheck_lint_package', 0)
call ale#Set('go_staticcheck_use_global', get(g:, 'ale_use_global_executables', 0))
function! ale_linters#go#staticcheck#GetCommand(buffer) abort function! ale_linters#go#staticcheck#GetCommand(buffer) abort
let l:options = ale#Var(a:buffer, 'go_staticcheck_options') let l:options = ale#Var(a:buffer, 'go_staticcheck_options')
@ -10,18 +12,20 @@ function! ale_linters#go#staticcheck#GetCommand(buffer) abort
let l:env = ale#go#EnvString(a:buffer) let l:env = ale#go#EnvString(a:buffer)
if l:lint_package if l:lint_package
return l:env . 'staticcheck' return l:env . '%e'
\ . (!empty(l:options) ? ' ' . l:options : '') . ' .' \ . (!empty(l:options) ? ' ' . l:options : '') . ' .'
endif endif
return l:env . 'staticcheck' return l:env . '%e'
\ . (!empty(l:options) ? ' ' . l:options : '') \ . (!empty(l:options) ? ' ' . l:options : '')
\ . ' %s:t' \ . ' %s:t'
endfunction endfunction
call ale#linter#Define('go', { call ale#linter#Define('go', {
\ 'name': 'staticcheck', \ 'name': 'staticcheck',
\ 'executable': 'staticcheck', \ 'executable': {b -> ale#path#FindExecutable(b, 'go_staticcheck', [
\ ale#go#GetGoPathExecutable('bin/staticcheck'),
\ ])},
\ 'cwd': '%s:h', \ 'cwd': '%s:h',
\ 'command': function('ale_linters#go#staticcheck#GetCommand'), \ 'command': function('ale_linters#go#staticcheck#GetCommand'),
\ 'callback': 'ale#handlers#go#Handler', \ 'callback': 'ale#handlers#go#Handler',

View File

@ -42,3 +42,17 @@ function! ale#go#EnvString(buffer) abort
return l:env return l:env
endfunction endfunction
function! ale#go#GetGoPathExecutable(suffix) abort
let l:prefix = $GOPATH
if !empty($GOPATH)
let l:prefix = $GOPATH
elseif has('win32')
let l:prefix = $USERPROFILE . '/go'
else
let l:prefix = $HOME . '/go'
endif
return ale#path#Simplify(l:prefix . '/' . a:suffix)
endfunction

View File

@ -20,8 +20,8 @@ the benefit of running a number of linters, more than ALE would by default,
while ensuring it doesn't run any linters known to be slow or resource while ensuring it doesn't run any linters known to be slow or resource
intensive. intensive.
g:ale_go_go_executable *g:ale_go_go_options* g:ale_go_go_executable *g:ale_go_go_executable*
*b:ale_go_go_options* *b:ale_go_go_executable*
Type: |String| Type: |String|
Default: `'go'` Default: `'go'`
@ -289,6 +289,18 @@ g:ale_go_revive_options *g:ale_go_revive_options*
=============================================================================== ===============================================================================
staticcheck *ale-go-staticcheck* staticcheck *ale-go-staticcheck*
g:ale_go_staticcheck_executable *g:ale_go_staticcheck_executable*
*b:ale_go_staticcheck_executable*
Type: |String|
Default: `'staticcheck'`
See |ale-integrations-local-executables|
ALE will search for `staticcheck` in locally installed directories first by
default, and fall back on a globally installed `staticcheck` if it can't be
found otherwise.
g:ale_go_staticcheck_options *g:ale_go_staticcheck_options* g:ale_go_staticcheck_options *g:ale_go_staticcheck_options*
*b:ale_go_staticcheck_options* *b:ale_go_staticcheck_options*
Type: |String| Type: |String|
@ -307,5 +319,13 @@ g:ale_go_staticcheck_lint_package *g:ale_go_staticcheck_lint_package*
current file. current file.
g:ale_go_staticcheck_use_global *g:ale_go_staticcheck_use_global*
*b:ale_go_staticcheck_use_global*
Type: |String|
Default: `get(g:, 'ale_use_global_executables', 0)`
See |ale-integrations-local-executables|
=============================================================================== ===============================================================================
vim:tw=78:ts=2:sts=2:sw=2:ft=help:norl: vim:tw=78:ts=2:sts=2:sw=2:ft=help:norl:

View File

@ -1,5 +1,8 @@
Before: Before:
Save g:ale_go_go111module Save g:ale_go_go111module
Save $GOPATH
let $GOPATH = '/non/existent/directory'
call ale#assert#SetUpLinterTest('go', 'staticcheck') call ale#assert#SetUpLinterTest('go', 'staticcheck')
call ale#test#SetFilename('test.go') call ale#test#SetFilename('test.go')
@ -11,25 +14,36 @@ After:
Execute(The staticcheck callback should return the right defaults): Execute(The staticcheck callback should return the right defaults):
AssertLinterCwd '%s:h' AssertLinterCwd '%s:h'
AssertLinter 'staticcheck', 'staticcheck %s:t' AssertLinter 'staticcheck', ale#Escape('staticcheck') . ' %s:t'
Execute(staticcheck should be found in GOPATH):
" This is a directory with a fake executable
let $GOPATH = ale#test#GetFilename('../test-files/go/gopath')
AssertLinter
\ ale#test#GetFilename('../test-files/go/gopath/bin/staticcheck'),
\ ale#Escape(ale#test#GetFilename('../test-files/go/gopath/bin/staticcheck'))
\ . ' %s:t'
Execute(The staticcheck callback should use configured options): Execute(The staticcheck callback should use configured options):
let b:ale_go_staticcheck_options = '-test' let b:ale_go_staticcheck_options = '-test'
AssertLinter 'staticcheck', 'staticcheck -test %s:t' AssertLinter 'staticcheck', ale#Escape('staticcheck') . ' -test %s:t'
Execute(The staticcheck `lint_package` option should use the correct command): Execute(The staticcheck `lint_package` option should use the correct command):
let b:ale_go_staticcheck_lint_package = 1 let b:ale_go_staticcheck_lint_package = 1
AssertLinterCwd '%s:h' AssertLinterCwd '%s:h'
AssertLinter 'staticcheck', 'staticcheck .' AssertLinter 'staticcheck', ale#Escape('staticcheck') . ' .'
Execute(The staticcheck callback should use the `GO111MODULE` option if set): Execute(The staticcheck callback should use the `GO111MODULE` option if set):
let b:ale_go_go111module = 'off' let b:ale_go_go111module = 'off'
AssertLinter 'staticcheck', ale#Env('GO111MODULE', 'off') . 'staticcheck %s:t' AssertLinter 'staticcheck',
\ ale#Env('GO111MODULE', 'off') . ale#Escape('staticcheck') . ' %s:t'
" Test with lint_package option set " Test with lint_package option set
let b:ale_go_staticcheck_lint_package = 1 let b:ale_go_staticcheck_lint_package = 1
AssertLinter 'staticcheck', ale#Env('GO111MODULE', 'off') . 'staticcheck .' AssertLinter 'staticcheck',
\ ale#Env('GO111MODULE', 'off') . ale#Escape('staticcheck') . ' .'