From e82bcdb8a6dc888130c03bc80cba492051c5ffbf Mon Sep 17 00:00:00 2001 From: Martin Tournoij Date: Wed, 19 Sep 2018 19:33:23 +0100 Subject: [PATCH] Add fixer for Go modules (#1873) * Add fixer for Go modules --- README.md | 2 +- ale_linters/go/gobuild.vim | 5 ++-- ale_linters/go/govet.vim | 7 ++++-- autoload/ale/fix/registry.vim | 5 ++++ autoload/ale/fixers/gomod.vim | 10 ++++++++ doc/ale-go.txt | 9 +++++++ doc/ale.txt | 2 +- .../test_gobuild_command_callback.vader | 12 ++++++++++ .../test_govet_command_callback.vader | 11 +++++++-- test/fixers/test_gomod_fixer_callback.vader | 24 +++++++++++++++++++ test/go_files/go.mod | 1 + 11 files changed, 80 insertions(+), 8 deletions(-) create mode 100644 autoload/ale/fixers/gomod.vim create mode 100644 test/fixers/test_gomod_fixer_callback.vader create mode 100644 test/go_files/go.mod diff --git a/README.md b/README.md index 93639a0d..b225c9c3 100644 --- a/README.md +++ b/README.md @@ -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) | diff --git a/ale_linters/go/gobuild.vim b/ale_linters/go/gobuild.vim index a44449f1..cef1ff88 100644 --- a/ale_linters/go/gobuild.vim +++ b/ale_linters/go/gobuild.vim @@ -3,6 +3,7 @@ " Description: go build for Go files " inspired by work from dzhou121 +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', diff --git a/ale_linters/go/govet.vim b/ale_linters/go/govet.vim index 84c23236..3d0d2adf 100644 --- a/ale_linters/go/govet.vim +++ b/ale_linters/go/govet.vim @@ -4,20 +4,23 @@ " Author: John Eikenberry " 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, diff --git a/autoload/ale/fix/registry.vim b/autoload/ale/fix/registry.vim index eb12f22d..76cce87f 100644 --- a/autoload/ale/fix/registry.vim +++ b/autoload/ale/fix/registry.vim @@ -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'], diff --git a/autoload/ale/fixers/gomod.vim b/autoload/ale/fixers/gomod.vim new file mode 100644 index 00000000..68895f9b --- /dev/null +++ b/autoload/ale/fixers/gomod.vim @@ -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 diff --git a/doc/ale-go.txt b/doc/ale-go.txt index 1d55763e..71b248ee 100644 --- a/doc/ale-go.txt +++ b/doc/ale-go.txt @@ -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* diff --git a/doc/ale.txt b/doc/ale.txt index 9a81b237..46ff08b0 100644 --- a/doc/ale.txt +++ b/doc/ale.txt @@ -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` diff --git a/test/command_callback/test_gobuild_command_callback.vader b/test/command_callback/test_gobuild_command_callback.vader index f9673213..8acbec56 100644 --- a/test/command_callback/test_gobuild_command_callback.vader +++ b/test/command_callback/test_gobuild_command_callback.vader @@ -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 ./' diff --git a/test/command_callback/test_govet_command_callback.vader b/test/command_callback/test_govet_command_callback.vader index 3718e0a7..ab93a5cb 100644 --- a/test/command_callback/test_govet_command_callback.vader +++ b/test/command_callback/test_govet_command_callback.vader @@ -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 .' diff --git a/test/fixers/test_gomod_fixer_callback.vader b/test/fixers/test_gomod_fixer_callback.vader new file mode 100644 index 00000000..a378e961 --- /dev/null +++ b/test/fixers/test_gomod_fixer_callback.vader @@ -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('')) diff --git a/test/go_files/go.mod b/test/go_files/go.mod new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/test/go_files/go.mod @@ -0,0 +1 @@ +