fixers/xo: support stdin relative to the fixed file

This commit is contained in:
Charles B Johnson 2020-04-08 22:59:54 -05:00
parent e75ac9f497
commit 8ffde14039
No known key found for this signature in database
GPG key ID: 390184033B3454E3
4 changed files with 63 additions and 2 deletions

View file

@ -14,10 +14,35 @@ function! ale#fixers#xo#Fix(buffer) abort
let l:executable = ale#handlers#xo#GetExecutable(a:buffer, l:type) let l:executable = ale#handlers#xo#GetExecutable(a:buffer, l:type)
let l:options = ale#handlers#xo#GetOptions(a:buffer, l:type) let l:options = ale#handlers#xo#GetOptions(a:buffer, l:type)
return ale#semver#RunWithVersionCheck(
\ a:buffer,
\ l:executable,
\ '%e --version',
\ {b, v -> ale#fixers#xo#ApplyFixForVersion(b, v, l:executable, l:options)}
\)
endfunction
function! ale#fixers#xo#ApplyFixForVersion(buffer, version, executable, options) abort
let l:executable = ale#node#Executable(a:buffer, a:executable)
let l:options = ale#Pad(a:options)
" 0.30.0 is the first version with a working --stdin --fix
if ale#semver#GTE(a:version, [0, 30, 0])
let l:project_root = ale#handlers#xo#GetProjectRoot(a:buffer)
return {
\ 'command': ale#path#CdString(l:project_root)
\ . l:executable
\ . ' --stdin --stdin-filename %s'
\ . ' --fix'
\ . l:options,
\}
endif
return { return {
\ 'command': ale#node#Executable(a:buffer, l:executable) \ 'command': l:executable
\ . ' --fix %t' \ . ' --fix %t'
\ . ale#Pad(l:options), \ . l:options,
\ 'read_temporary_file': 1, \ 'read_temporary_file': 1,
\} \}
endfunction endfunction

View file

@ -27,3 +27,9 @@ endfunction
function! ale#handlers#xo#HandleJSON(buffer, lines) abort function! ale#handlers#xo#HandleJSON(buffer, lines) abort
return ale#handlers#eslint#HandleJSON(a:buffer, a:lines) return ale#handlers#eslint#HandleJSON(a:buffer, a:lines)
endfunction endfunction
function! ale#handlers#xo#GetProjectRoot(buffer) abort
let l:package_path = ale#path#FindNearestFile(a:buffer, 'package.json')
return empty(l:package_path) ? '' : fnamemodify(l:package_path, ':p:h')
endfunction

View file

@ -29,3 +29,18 @@ Execute(The xo callback should include custom xo options):
\ . ' --fix %t' \ . ' --fix %t'
\ . ' --space', \ . ' --space',
\ } \ }
Execute(--stdin should be used when xo is new enough):
let g:ale_javascript_xo_options = '--space'
call ale#test#SetFilename('../eslint-test-files/react-app/subdir/testfile.js')
GivenCommandOutput ['0.30.0']
AssertFixer
\ {
\ 'command': ale#path#CdString(ale#path#Simplify(g:dir . '/../eslint-test-files'))
\ . (has('win32') ? 'node.exe ' : '')
\ . ale#Escape(ale#path#Simplify(g:dir . '/../eslint-test-files/react-app/node_modules/xo/cli.js'))
\ . ' --stdin --stdin-filename %s'
\ . ' --fix'
\ . ' --space',
\ }

View file

@ -29,3 +29,18 @@ Execute(The xo callback should include custom xo options):
\ . ' --fix %t' \ . ' --fix %t'
\ . ' --space', \ . ' --space',
\ } \ }
Execute(--stdin should be used when xo is new enough):
let g:ale_typescript_xo_options = '--space'
call ale#test#SetFilename('../eslint-test-files/react-app/subdir/testfile.ts')
GivenCommandOutput ['0.30.0']
AssertFixer
\ {
\ 'command': ale#path#CdString(ale#path#Simplify(g:dir . '/../eslint-test-files'))
\ . (has('win32') ? 'node.exe ' : '')
\ . ale#Escape(ale#path#Simplify(g:dir . '/../eslint-test-files/react-app/node_modules/xo/cli.js'))
\ . ' --stdin --stdin-filename %s'
\ . ' --fix'
\ . ' --space',
\ }