Add fixer for Go modules (#1873)

* Add fixer for Go modules
This commit is contained in:
Martin Tournoij 2018-09-19 19:33:23 +01:00 committed by w0rp
parent a6c6e24d61
commit e82bcdb8a6
11 changed files with 80 additions and 8 deletions

View file

@ -124,7 +124,7 @@ formatting.
| FusionScript | [fusion-lint](https://github.com/RyanSquared/fusionscript) | | FusionScript | [fusion-lint](https://github.com/RyanSquared/fusionscript) |
| Git Commit Messages | [gitlint](https://github.com/jorisroovers/gitlint) | | Git Commit Messages | [gitlint](https://github.com/jorisroovers/gitlint) |
| GLSL | [glslang](https://github.com/KhronosGroup/glslang), [glslls](https://github.com/svenstaro/glsl-language-server) | | GLSL | [glslang](https://github.com/KhronosGroup/glslang), [glslls](https://github.com/svenstaro/glsl-language-server) |
| Go | [gofmt](https://golang.org/cmd/gofmt/), [goimports](https://godoc.org/golang.org/x/tools/cmd/goimports), [go vet](https://golang.org/cmd/vet/) !!, [golint](https://godoc.org/github.com/golang/lint), [gotype](https://godoc.org/golang.org/x/tools/cmd/gotype) !!, [gometalinter](https://github.com/alecthomas/gometalinter) !!, [go build](https://golang.org/cmd/go/) !!, [gosimple](https://github.com/dominikh/go-tools/tree/master/cmd/gosimple) !!, [staticcheck](https://github.com/dominikh/go-tools/tree/master/cmd/staticcheck) !!, [golangserver](https://github.com/sourcegraph/go-langserver), [golangci-lint](https://github.com/golangci/golangci-lint) !! | | Go | [gofmt](https://golang.org/cmd/gofmt/), [goimports](https://godoc.org/golang.org/x/tools/cmd/goimports), [go mod](https://golang.org/cmd/go/) !!, [go vet](https://golang.org/cmd/vet/) !!, [golint](https://godoc.org/github.com/golang/lint), [gotype](https://godoc.org/golang.org/x/tools/cmd/gotype) !!, [gometalinter](https://github.com/alecthomas/gometalinter) !!, [go build](https://golang.org/cmd/go/) !!, [gosimple](https://github.com/dominikh/go-tools/tree/master/cmd/gosimple) !!, [staticcheck](https://github.com/dominikh/go-tools/tree/master/cmd/staticcheck) !!, [golangserver](https://github.com/sourcegraph/go-langserver), [golangci-lint](https://github.com/golangci/golangci-lint) !! |
| GraphQL | [eslint](http://eslint.org/), [gqlint](https://github.com/happylinks/gqlint), [prettier](https://github.com/prettier/prettier) | | GraphQL | [eslint](http://eslint.org/), [gqlint](https://github.com/happylinks/gqlint), [prettier](https://github.com/prettier/prettier) |
| Hack | [hack](http://hacklang.org/), [hackfmt](https://github.com/facebook/hhvm/tree/master/hphp/hack/hackfmt), [hhast](https://github.com/hhvm/hhast) (disabled by default; see `:help ale-integration-hack`) | | Hack | [hack](http://hacklang.org/), [hackfmt](https://github.com/facebook/hhvm/tree/master/hphp/hack/hackfmt), [hhast](https://github.com/hhvm/hhast) (disabled by default; see `:help ale-integration-hack`) |
| Haml | [haml-lint](https://github.com/brigade/haml-lint) | | Haml | [haml-lint](https://github.com/brigade/haml-lint) |

View file

@ -3,6 +3,7 @@
" Description: go build for Go files " Description: go build for Go files
" inspired by work from dzhou121 <dzhou121@gmail.com> " inspired by work from dzhou121 <dzhou121@gmail.com>
call ale#Set('go_go_executable', 'go')
call ale#Set('go_gobuild_options', '') call ale#Set('go_gobuild_options', '')
function! ale_linters#go#gobuild#GetCommand(buffer) abort function! ale_linters#go#gobuild#GetCommand(buffer) abort
@ -10,7 +11,7 @@ function! ale_linters#go#gobuild#GetCommand(buffer) abort
" Run go test in local directory with relative path " Run go test in local directory with relative path
return ale#path#BufferCdString(a:buffer) return ale#path#BufferCdString(a:buffer)
\ . 'go test' \ . ale#Var(a:buffer, 'go_go_executable') . ' test'
\ . (!empty(l:options) ? ' ' . l:options : '') \ . (!empty(l:options) ? ' ' . l:options : '')
\ . ' -c -o /dev/null ./' \ . ' -c -o /dev/null ./'
endfunction endfunction
@ -47,7 +48,7 @@ endfunction
call ale#linter#Define('go', { call ale#linter#Define('go', {
\ 'name': 'gobuild', \ 'name': 'gobuild',
\ 'aliases': ['go build'], \ 'aliases': ['go build'],
\ 'executable': 'go', \ 'executable_callback': ale#VarFunc('go_go_executable'),
\ 'command_callback': 'ale_linters#go#gobuild#GetCommand', \ 'command_callback': 'ale_linters#go#gobuild#GetCommand',
\ 'output_stream': 'stderr', \ 'output_stream': 'stderr',
\ 'callback': 'ale_linters#go#gobuild#Handler', \ 'callback': 'ale_linters#go#gobuild#Handler',

View file

@ -4,20 +4,23 @@
" Author: John Eikenberry <jae@zhar.net> " Author: John Eikenberry <jae@zhar.net>
" Description: updated to work with go1.10 " Description: updated to work with go1.10
call ale#Set('go_go_executable', 'go')
call ale#Set('go_govet_options', '') call ale#Set('go_govet_options', '')
function! ale_linters#go#govet#GetCommand(buffer) abort function! ale_linters#go#govet#GetCommand(buffer) abort
let l:options = ale#Var(a:buffer, 'go_govet_options') let l:options = ale#Var(a:buffer, 'go_govet_options')
return ale#path#BufferCdString(a:buffer) . ' go vet .' return ale#path#BufferCdString(a:buffer) . ' '
\ . ale#Var(a:buffer, 'go_go_executable') . ' vet '
\ . (!empty(l:options) ? ' ' . l:options : '') \ . (!empty(l:options) ? ' ' . l:options : '')
\ . ' .'
endfunction endfunction
call ale#linter#Define('go', { call ale#linter#Define('go', {
\ 'name': 'govet', \ 'name': 'govet',
\ 'aliases': ['go vet'], \ 'aliases': ['go vet'],
\ 'output_stream': 'stderr', \ 'output_stream': 'stderr',
\ 'executable': 'go', \ 'executable_callback': ale#VarFunc('go_go_executable'),
\ 'command_callback': 'ale_linters#go#govet#GetCommand', \ 'command_callback': 'ale_linters#go#govet#GetCommand',
\ 'callback': 'ale#handlers#go#Handler', \ 'callback': 'ale#handlers#go#Handler',
\ 'lint_file': 1, \ 'lint_file': 1,

View file

@ -145,6 +145,11 @@ let s:default_registry = {
\ 'suggested_filetypes': ['go'], \ 'suggested_filetypes': ['go'],
\ 'description': 'Fix Go files imports with goimports.', \ 'description': 'Fix Go files imports with goimports.',
\ }, \ },
\ 'gomod': {
\ 'function': 'ale#fixers#gomod#Fix',
\ 'suggested_filetypes': ['gomod'],
\ 'description': 'Fix Go module files with go mod edit -fmt.',
\ },
\ 'tslint': { \ 'tslint': {
\ 'function': 'ale#fixers#tslint#Fix', \ 'function': 'ale#fixers#tslint#Fix',
\ 'suggested_filetypes': ['typescript'], \ 'suggested_filetypes': ['typescript'],

View file

@ -0,0 +1,10 @@
call ale#Set('go_go_executable', 'go')
function! ale#fixers#gomod#Fix(buffer) abort
let l:executable = ale#Var(a:buffer, 'go_go_executable')
return {
\ 'command': ale#Escape(l:executable) . ' mod edit -fmt %t',
\ 'read_temporary_file': 1,
\}
endfunction

View file

@ -20,6 +20,15 @@ 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*
*b:ale_go_go_options*
Type: |String|
Default: `'go'`
The executable that will be run for the `gobuild` and `govet` linters, and
the gomod` fixer.
=============================================================================== ===============================================================================
gobuild *ale-go-gobuild* gobuild *ale-go-gobuild*

View file

@ -402,7 +402,7 @@ Notes:
* FusionScript: `fusion-lint` * FusionScript: `fusion-lint`
* Git Commit Messages: `gitlint` * Git Commit Messages: `gitlint`
* GLSL: glslang, `glslls` * GLSL: glslang, `glslls`
* Go: `gofmt`, `goimports`, `go vet`!!, `golint`, `gotype`!!, `gometalinter`!!, `go build`!!, `gosimple`!!, `staticcheck`!!, `golangserver`, `golangci-lint`!! * Go: `gofmt`, `goimports`, `go mod`!!, `go vet`!!, `golint`, `gotype`!!, `gometalinter`!!, `go build`!!, `gosimple`!!, `staticcheck`!!, `golangserver`, `golangci-lint`!!
* GraphQL: `eslint`, `gqlint`, `prettier` * GraphQL: `eslint`, `gqlint`, `prettier`
* Hack: `hack`, `hackfmt`, `hhast` * Hack: `hack`, `hackfmt`, `hhast`
* Haml: `haml-lint` * Haml: `haml-lint`

View file

@ -1,9 +1,12 @@
Before: Before:
Save g:ale_go_go_executable
call ale#assert#SetUpLinterTest('go', 'gobuild') call ale#assert#SetUpLinterTest('go', 'gobuild')
WithChainResults ['/foo/bar', '/foo/baz'] WithChainResults ['/foo/bar', '/foo/baz']
After: After:
Restore
call ale#assert#TearDownLinterTest() call ale#assert#TearDownLinterTest()
Execute(The default commands should be correct): Execute(The default commands should be correct):
@ -17,3 +20,12 @@ Execute(Extra options should be supported):
AssertLinter 'go', AssertLinter 'go',
\ 'cd ' . ale#Escape(expand('%:p:h')) . ' && ' \ 'cd ' . ale#Escape(expand('%:p:h')) . ' && '
\ . 'go test --foo-bar -c -o /dev/null ./' \ . 'go test --foo-bar -c -o /dev/null ./'
let g:ale_go_gobuild_options = ''
Execute(The executable should be configurable):
let g:ale_go_go_executable = 'foobar'
AssertLinter 'foobar',
\ 'cd ' . ale#Escape(expand('%:p:h')) . ' && '
\ . 'foobar test -c -o /dev/null ./'

View file

@ -1,12 +1,19 @@
Before: Before:
Save g:ale_go_go_executable
Save g:ale_go_govet_options
call ale#assert#SetUpLinterTest('go', 'govet') call ale#assert#SetUpLinterTest('go', 'govet')
After: After:
Restore
call ale#assert#TearDownLinterTest() call ale#assert#TearDownLinterTest()
Execute(The default command should be correct): Execute(The default command should be correct):
AssertLinter 'go', 'cd ' . ale#Escape(expand('%:p:h')) . ' && go vet .' AssertLinter 'go', 'cd ' . ale#Escape(expand('%:p:h')) . ' && go vet .'
Execute(Extra options should be supported): Execute(Extra options should be supported):
let g:ale_go_govet_options = '--foo-bar' let g:ale_go_govet_options = '--foo-bar'
AssertLinter 'go', 'cd ' . ale#Escape(expand('%:p:h')) . ' && go vet . --foo-bar' AssertLinter 'go', 'cd ' . ale#Escape(expand('%:p:h')) . ' && go vet --foo-bar .'
Execute(The executable should be configurable):
let g:ale_go_go_executable = 'foobar'
AssertLinter 'foobar', 'cd ' . ale#Escape(expand('%:p:h')) . ' && foobar vet .'

View file

@ -0,0 +1,24 @@
Before:
Save g:ale_go_go_executable
" Use an invalid global executable, so we don't match it.
let g:ale_go_go_executable = 'xxxinvalid'
call ale#test#SetDirectory('/testplugin/test/fixers')
After:
Restore
call ale#test#RestoreDirectory()
Execute(The gomod callback should return the correct default values):
call ale#test#SetFilename('../go_files/go.mod')
setl ft=gomod
AssertEqual
\ {
\ 'read_temporary_file': 1,
\ 'command': ale#Escape('xxxinvalid')
\ . ' mod edit -fmt'
\ . ' %t',
\ },
\ ale#fixers#gomod#Fix(bufnr(''))

1
test/go_files/go.mod Normal file
View file

@ -0,0 +1 @@