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) |
| Git Commit Messages | [gitlint](https://github.com/jorisroovers/gitlint) |
| 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) |
| 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) |

View File

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

View File

@ -4,20 +4,23 @@
" Author: John Eikenberry <jae@zhar.net>
" Description: updated to work with go1.10
call ale#Set('go_go_executable', 'go')
call ale#Set('go_govet_options', '')
function! ale_linters#go#govet#GetCommand(buffer) abort
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 : '')
\ . ' .'
endfunction
call ale#linter#Define('go', {
\ 'name': 'govet',
\ 'aliases': ['go vet'],
\ 'output_stream': 'stderr',
\ 'executable': 'go',
\ 'executable_callback': ale#VarFunc('go_go_executable'),
\ 'command_callback': 'ale_linters#go#govet#GetCommand',
\ 'callback': 'ale#handlers#go#Handler',
\ 'lint_file': 1,

View File

@ -145,6 +145,11 @@ let s:default_registry = {
\ 'suggested_filetypes': ['go'],
\ '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': {
\ 'function': 'ale#fixers#tslint#Fix',
\ '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
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*

View File

@ -402,7 +402,7 @@ Notes:
* FusionScript: `fusion-lint`
* Git Commit Messages: `gitlint`
* 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`
* Hack: `hack`, `hackfmt`, `hhast`
* Haml: `haml-lint`

View File

@ -1,9 +1,12 @@
Before:
Save g:ale_go_go_executable
call ale#assert#SetUpLinterTest('go', 'gobuild')
WithChainResults ['/foo/bar', '/foo/baz']
After:
Restore
call ale#assert#TearDownLinterTest()
Execute(The default commands should be correct):
@ -17,3 +20,12 @@ Execute(Extra options should be supported):
AssertLinter 'go',
\ 'cd ' . ale#Escape(expand('%:p:h')) . ' && '
\ . '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:
Save g:ale_go_go_executable
Save g:ale_go_govet_options
call ale#assert#SetUpLinterTest('go', 'govet')
After:
Restore
call ale#assert#TearDownLinterTest()
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):
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 @@