mirror of
https://github.com/dense-analysis/ale.git
synced 2025-12-07 05:04:28 +08:00
Fix golangci-lint fixer with version 2 (#4960)
Some checks failed
CI / build_image (push) Has been cancelled
CI / test_ale (--linters-only) (push) Has been cancelled
CI / test_ale (--lua-only) (push) Has been cancelled
CI / test_ale (--neovim-07-only) (push) Has been cancelled
CI / test_ale (--neovim-08-only) (push) Has been cancelled
CI / test_ale (--vim-80-only) (push) Has been cancelled
CI / test_ale (--vim-90-only) (push) Has been cancelled
Some checks failed
CI / build_image (push) Has been cancelled
CI / test_ale (--linters-only) (push) Has been cancelled
CI / test_ale (--lua-only) (push) Has been cancelled
CI / test_ale (--neovim-07-only) (push) Has been cancelled
CI / test_ale (--neovim-08-only) (push) Has been cancelled
CI / test_ale (--vim-80-only) (push) Has been cancelled
CI / test_ale (--vim-90-only) (push) Has been cancelled
This commit is contained in:
@@ -1,32 +1,48 @@
|
|||||||
" Author: Ian Stapleton Cordasco <graffatcolmingov@gmail.com>
|
" Author: Ian Stapleton Cordasco <graffatcolmingov@gmail.com>
|
||||||
" Description: Run golangci-lint with the --fix flag to autofix some issues
|
" Description: Run golangci-lint with the --fix flag to autofix some issues
|
||||||
|
|
||||||
call ale#Set('go_golangci_lint_options', '')
|
call ale#Set('go_golangci_formatter_options', '')
|
||||||
call ale#Set('go_golangci_lint_executable', 'golangci-lint')
|
call ale#Set('go_golangci_formatter_executable', 'golangci-lint')
|
||||||
call ale#Set('go_golangci_lint_package', 1)
|
|
||||||
|
|
||||||
function! ale#fixers#golangci_lint#GetCommand(buffer) abort
|
function! ale#fixers#golangci_lint#GetExecutable(buffer) abort
|
||||||
|
let l:executable = ale#Var(a:buffer, 'go_golangci_formatter_executable')
|
||||||
|
|
||||||
|
return l:executable
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! ale#fixers#golangci_lint#GetCommand(buffer, version) abort
|
||||||
let l:filename = expand('#' . a:buffer . ':t')
|
let l:filename = expand('#' . a:buffer . ':t')
|
||||||
let l:executable = ale#Var(a:buffer, 'go_golangci_lint_executable')
|
let l:executable = ale#fixers#golangci_lint#GetExecutable(a:buffer)
|
||||||
let l:options = ale#Var(a:buffer, 'go_golangci_lint_options') . ' --fix'
|
let l:options = ale#Var(a:buffer, 'go_golangci_formatter_options')
|
||||||
let l:package_mode = ale#Var(a:buffer, 'go_golangci_lint_package')
|
|
||||||
let l:env = ale#go#EnvString(a:buffer)
|
let l:env = ale#go#EnvString(a:buffer)
|
||||||
|
|
||||||
|
if ale#semver#GTE(a:version, [2, 0, 0])
|
||||||
if l:package_mode
|
|
||||||
return l:env . ale#Escape(l:executable)
|
return l:env . ale#Escape(l:executable)
|
||||||
\ . ' run '
|
\ . ' fmt --stdin '
|
||||||
\ . l:options
|
\ . l:options
|
||||||
|
else
|
||||||
|
return l:env . ale#Escape(l:executable)
|
||||||
|
\ . ' run --fix '
|
||||||
|
\ . l:options
|
||||||
|
\ . ' '
|
||||||
|
\ . ale#Escape(l:filename)
|
||||||
endif
|
endif
|
||||||
|
endfunction
|
||||||
|
|
||||||
return l:env . ale#Escape(l:executable)
|
function! ale#fixers#golangci_lint#GetCommandForVersion(buffer, version) abort
|
||||||
\ . ' run '
|
return {
|
||||||
\ . l:options
|
\ 'command': ale#fixers#golangci_lint#GetCommand(a:buffer, a:version)
|
||||||
\ . ' ' . ale#Escape(l:filename)
|
\}
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! ale#fixers#golangci_lint#Fix(buffer) abort
|
function! ale#fixers#golangci_lint#Fix(buffer) abort
|
||||||
return {
|
let l:executable = ale#fixers#golangci_lint#GetExecutable(a:buffer)
|
||||||
\ 'command': ale#fixers#golangci_lint#GetCommand(a:buffer),
|
let l:command = ale#fixers#golangci_lint#GetExecutable(a:buffer) . ale#Pad('--version')
|
||||||
\}
|
|
||||||
|
return ale#semver#RunWithVersionCheck(
|
||||||
|
\ a:buffer,
|
||||||
|
\ l:executable,
|
||||||
|
\ l:command,
|
||||||
|
\ function('ale#fixers#golangci_lint#GetCommandForVersion'),
|
||||||
|
\)
|
||||||
endfunction
|
endfunction
|
||||||
|
|||||||
@@ -144,7 +144,7 @@ g:ale_go_golangci_lint_options
|
|||||||
Default: `''`
|
Default: `''`
|
||||||
|
|
||||||
This variable can be changed to alter the command-line arguments to the
|
This variable can be changed to alter the command-line arguments to the
|
||||||
golangci-lint invocation.
|
golangci-lint run invocation.
|
||||||
|
|
||||||
*ale-options.go_golangci_lint_package*
|
*ale-options.go_golangci_lint_package*
|
||||||
*g:ale_go_golangci_lint_package*
|
*g:ale_go_golangci_lint_package*
|
||||||
@@ -157,6 +157,30 @@ g:ale_go_golangci_lint_package
|
|||||||
When set to `1`, the whole Go package will be checked instead of only the
|
When set to `1`, the whole Go package will be checked instead of only the
|
||||||
current file.
|
current file.
|
||||||
|
|
||||||
|
golangci_lint can also be user as a fixer to format go source files. In this
|
||||||
|
case the following configuration variables can be used to configure the
|
||||||
|
formatters:
|
||||||
|
|
||||||
|
*ale-options.go_golangci_formatter_executable*
|
||||||
|
*g:ale_go_golangci_formatter_executable*
|
||||||
|
*b:ale_go_golangci_formatter_executable*
|
||||||
|
go_golangci_formatter_executable
|
||||||
|
g:ale_go_golangci_formatter_executable
|
||||||
|
Type: |String|
|
||||||
|
Default: `'golangci-lint'`
|
||||||
|
|
||||||
|
The executable that will be run for golangci-lint.
|
||||||
|
|
||||||
|
*ale-options.go_golangci_formatter_options*
|
||||||
|
*g:ale_go_golangci_formatter_options*
|
||||||
|
*b:ale_go_golangci_formatter_options*
|
||||||
|
go_golangci_formatter_options
|
||||||
|
g:ale_go_golangci_formatter_options
|
||||||
|
Type: |String|
|
||||||
|
Default: `''`
|
||||||
|
|
||||||
|
This variable can be changed to alter the command-line arguments to the
|
||||||
|
golangci-lint fmt invocation.
|
||||||
|
|
||||||
===============================================================================
|
===============================================================================
|
||||||
golangserver *ale-go-golangserver*
|
golangserver *ale-go-golangserver*
|
||||||
|
|||||||
@@ -1,48 +1,80 @@
|
|||||||
Before:
|
Before:
|
||||||
|
call ale#assert#SetUpFixerTest('go', 'golangci_lint')
|
||||||
Save g:ale_go_go111module
|
Save g:ale_go_go111module
|
||||||
Save g:ale_go_golangci_lint_executable
|
Save g:ale_go_golangci_formatter_executable
|
||||||
Save g:ale_go_golangci_lint_options
|
Save g:ale_go_golangci_formatter_options
|
||||||
Save g:ale_go_golangci_lint_package
|
|
||||||
|
|
||||||
" Use an invalid global executable, so we don't match it.
|
|
||||||
let g:ale_go_golangci_lint_executable = 'xxxinvalid'
|
|
||||||
let g:ale_go_golangci_lint_options = ''
|
|
||||||
|
|
||||||
call ale#test#SetDirectory('/testplugin/test/fixers')
|
call ale#test#SetDirectory('/testplugin/test/fixers')
|
||||||
call ale#test#SetFilename('../test-files/go/testfile.go')
|
call ale#test#SetFilename('../test-files/go/testfile.go')
|
||||||
|
|
||||||
After:
|
After:
|
||||||
Restore
|
call ale#test#RestoreDirectory()
|
||||||
|
call ale#assert#TearDownFixerTest()
|
||||||
|
|
||||||
unlet! b:ale_go_go111module
|
unlet! b:ale_go_go111module
|
||||||
|
|
||||||
call ale#test#RestoreDirectory()
|
Execute(The golangci-lint callback should return the correct default values with v1):
|
||||||
|
|
||||||
Execute(The golangci-lint callback should return the correct default values):
|
GivenCommandOutput ['golangci-lint has version 1.64.8 built with go1.23.0']
|
||||||
|
|
||||||
AssertEqual
|
AssertFixer
|
||||||
\ {
|
\ {
|
||||||
\ 'command': ale#Escape('xxxinvalid') . ' run --fix',
|
\ 'command': ale#Escape('golangci-lint') . ' run --fix ' . ale#Escape('testfile.go'),
|
||||||
|
\ }
|
||||||
|
|
||||||
|
Execute(The golangci-lint callback should include custom golangci-lint options with v1):
|
||||||
|
let g:ale_go_golangci_formatter_options = "--new --config /dev/null"
|
||||||
|
|
||||||
|
GivenCommandOutput ['golangci-lint has version 1.64.8 built with go1.23.0']
|
||||||
|
|
||||||
|
AssertFixer
|
||||||
|
\ {
|
||||||
|
\ 'command': ale#Escape('golangci-lint')
|
||||||
|
\ . ' run --fix ' . g:ale_go_golangci_formatter_options . ' ' . ale#Escape('testfile.go'),
|
||||||
\ },
|
\ },
|
||||||
\ ale#fixers#golangci_lint#Fix(bufnr(''))
|
|
||||||
|
|
||||||
Execute(The golangci-lint callback should include custom golangci-lint options):
|
Execute(The golangci-lint callback should override executable with v1):
|
||||||
let g:ale_go_golangci_lint_options = "--new --config /dev/null"
|
let g:ale_go_golangci_formatter_executable = 'xxxinvalid'
|
||||||
|
|
||||||
AssertEqual
|
GivenCommandOutput ['golangci-lint has version 1.64.8 built with go1.23.0']
|
||||||
|
|
||||||
|
AssertFixer
|
||||||
\ {
|
\ {
|
||||||
\ 'command': ale#Escape('xxxinvalid')
|
\ 'command': ale#Escape('xxxinvalid')
|
||||||
\ . ' run ' . g:ale_go_golangci_lint_options . ' --fix',
|
\ . ' run --fix '
|
||||||
|
\ . g:ale_go_golangci_formatter_options
|
||||||
|
\ . ' ' . ale#Escape('testfile.go'),
|
||||||
\ },
|
\ },
|
||||||
\ ale#fixers#golangci_lint#Fix(bufnr(''))
|
|
||||||
|
|
||||||
Execute(The golangci-lint callback should support per-file mode):
|
Execute(The golangci-lint callback should return the correct default values with v2):
|
||||||
let g:ale_go_golangci_lint_package = 0
|
|
||||||
|
|
||||||
AssertEqual
|
GivenCommandOutput ['golangci-lint has version 2.1.5 built with go1.23.0']
|
||||||
|
|
||||||
|
AssertFixer
|
||||||
|
\ {
|
||||||
|
\ 'command': ale#Escape('golangci-lint') . ' fmt --stdin ',
|
||||||
|
\ }
|
||||||
|
|
||||||
|
Execute(The golangci-lint callback should include custom golangci-lint options with v2):
|
||||||
|
let g:ale_go_golangci_formatter_options = "--new --config /dev/null"
|
||||||
|
|
||||||
|
GivenCommandOutput ['golangci-lint has version 2.1.5 built with go1.23.0']
|
||||||
|
|
||||||
|
AssertFixer
|
||||||
|
\ {
|
||||||
|
\ 'command': ale#Escape('golangci-lint')
|
||||||
|
\ . ' fmt --stdin ' . g:ale_go_golangci_formatter_options,
|
||||||
|
\ },
|
||||||
|
|
||||||
|
Execute(The golangci-lint callback should override executable with v2):
|
||||||
|
let g:ale_go_golangci_formatter_executable = 'xxxinvalid'
|
||||||
|
|
||||||
|
GivenCommandOutput ['golangci-lint has version 2.1.5 built with go1.23.0']
|
||||||
|
|
||||||
|
AssertFixer
|
||||||
\ {
|
\ {
|
||||||
\ 'command': ale#Escape('xxxinvalid')
|
\ 'command': ale#Escape('xxxinvalid')
|
||||||
\ . ' run '
|
\ . ' fmt --stdin '
|
||||||
\ . g:ale_go_golangci_lint_options
|
\ . g:ale_go_golangci_formatter_options
|
||||||
\ . ' --fix ' . ale#Escape('testfile.go'),
|
|
||||||
\ },
|
\ },
|
||||||
\ ale#fixers#golangci_lint#Fix(bufnr(''))
|
|
||||||
|
|||||||
Reference in New Issue
Block a user