diff --git a/autoload/ale/fix/registry.vim b/autoload/ale/fix/registry.vim index 4fb229bd..5c09e785 100644 --- a/autoload/ale/fix/registry.vim +++ b/autoload/ale/fix/registry.vim @@ -54,6 +54,11 @@ let s:default_registry = { \ 'description': 'Apply prettier-eslint to a file.', \ 'aliases': ['prettier-eslint'], \ }, +\ 'importjs': { +\ 'function': 'ale#fixers#importjs#Fix', +\ 'suggested_filetypes': ['javascript'], +\ 'description': 'automatic imports for javascript', +\ }, \ 'puppetlint': { \ 'function': 'ale#fixers#puppetlint#Fix', \ 'suggested_filetypes': ['puppet'], diff --git a/autoload/ale/fixers/importjs.vim b/autoload/ale/fixers/importjs.vim new file mode 100644 index 00000000..e8eedb12 --- /dev/null +++ b/autoload/ale/fixers/importjs.vim @@ -0,0 +1,24 @@ +" Author: Jeff Willette +" Description: Integration of importjs with ALE. + +call ale#Set('js_importjs_executable', 'importjs') + +function! ale#fixers#importjs#ProcessOutput(buffer, output) abort + let l:result = ale#util#FuzzyJSONDecode(a:output, []) + return split(get(l:result, 'fileContent', ''), "\n") +endfunction + +function! ale#fixers#importjs#Fix(buffer) abort + let l:executable = ale#Var(a:buffer, 'js_importjs_executable') + + if !executable(l:executable) + return 0 + endif + + return { + \ 'command': ale#Escape(l:executable) + \ . ' fix' + \ . ' %s', + \ 'process_with': 'ale#fixers#importjs#ProcessOutput', + \} +endfunction diff --git a/doc/ale-javascript.txt b/doc/ale-javascript.txt index 3934dfb2..f625fd7f 100644 --- a/doc/ale-javascript.txt +++ b/doc/ale-javascript.txt @@ -103,6 +103,15 @@ g:ale_javascript_flow_use_global *g:ale_javascript_flow_use_global* See |ale-integrations-local-executables| +=============================================================================== +importjs *ale-javascript-importjs* + +g:ale_javascript_importjs_executable *g:ale_javascript_importjs_executable* + *b:ale_javascript_importjs_executable* + Type: |String| + Default: `'importjs'` + + =============================================================================== jscs *ale-javascript-jscs* diff --git a/doc/ale.txt b/doc/ale.txt index 3f90223e..8e8f5f4f 100644 --- a/doc/ale.txt +++ b/doc/ale.txt @@ -95,6 +95,7 @@ CONTENTS *ale-contents* javascript............................|ale-javascript-options| eslint..............................|ale-javascript-eslint| flow................................|ale-javascript-flow| + importjs............................|ale-javascript-importjs| jscs................................|ale-javascript-jscs| jshint..............................|ale-javascript-jshint| prettier............................|ale-javascript-prettier| diff --git a/test/fixers/test_importjs_fixer_callback.vader b/test/fixers/test_importjs_fixer_callback.vader new file mode 100644 index 00000000..c3e57f8b --- /dev/null +++ b/test/fixers/test_importjs_fixer_callback.vader @@ -0,0 +1,35 @@ +Before: + Save g:ale_js_importjs_executable + + " Use an invalid global executable, so we don't match it. + let g:ale_js_importjs_executable = 'xxxinvalid' + + call ale#test#SetDirectory('/testplugin/test/fixers') + call ale#test#SetFilename('../javascript_files/test.js') + +After: + Restore + + call ale#test#RestoreDirectory() + +Execute(The importjs callback should return 0 when the executable isn't executable): + AssertEqual + \ 0, + \ ale#fixers#importjs#Fix(bufnr('')) + +Execute(The importjs callback should run the command when the executable test passes): + let g:ale_js_importjs_executable = has('win32') ? 'cmd' : 'echo' + + AssertEqual + \ { + \ 'process_with': 'ale#fixers#importjs#ProcessOutput', + \ 'command': ale#Escape(g:ale_js_importjs_executable) . ' fix %s' + \ }, + \ ale#fixers#importjs#Fix(bufnr('')) + +Execute(The ProcessOutput callback should return the expected output): + let g:testOutput = '{"messages":[],"fileContent":"one\ntwo","unresolvedImports":{}}' + + AssertEqual + \ ['one', 'two'], + \ ale#fixers#importjs#ProcessOutput(bufnr(''), g:testOutput) diff --git a/test/javascript_files/test.js b/test/javascript_files/test.js new file mode 100644 index 00000000..e69de29b