added importjs fixer

- added tests for fixer functions
- added docs
This commit is contained in:
Jeff Willette 2017-12-05 00:29:44 +09:00
parent 2f9869de44
commit fba3c57872
6 changed files with 74 additions and 0 deletions

View File

@ -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'],

View File

@ -0,0 +1,24 @@
" Author: Jeff Willette <jrwillette88@gmail.com>
" 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

View File

@ -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*

View File

@ -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|

View File

@ -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)

View File