From 8ffde14039bdc0c12d1edf961461245eb15858e9 Mon Sep 17 00:00:00 2001 From: Charles B Johnson Date: Wed, 8 Apr 2020 22:59:54 -0500 Subject: [PATCH] fixers/xo: support stdin relative to the fixed file --- autoload/ale/fixers/xo.vim | 29 ++++++++++++++++++++-- autoload/ale/handlers/xo.vim | 6 +++++ test/fixers/test_xo_fixer_callback.vader | 15 +++++++++++ test/fixers/test_xots_fixer_callback.vader | 15 +++++++++++ 4 files changed, 63 insertions(+), 2 deletions(-) diff --git a/autoload/ale/fixers/xo.vim b/autoload/ale/fixers/xo.vim index 6c2901db..2c8ab114 100644 --- a/autoload/ale/fixers/xo.vim +++ b/autoload/ale/fixers/xo.vim @@ -14,10 +14,35 @@ function! ale#fixers#xo#Fix(buffer) abort let l:executable = ale#handlers#xo#GetExecutable(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 { - \ 'command': ale#node#Executable(a:buffer, l:executable) + \ 'command': l:executable \ . ' --fix %t' - \ . ale#Pad(l:options), + \ . l:options, \ 'read_temporary_file': 1, \} endfunction diff --git a/autoload/ale/handlers/xo.vim b/autoload/ale/handlers/xo.vim index 3f7c72cb..0bea74e4 100644 --- a/autoload/ale/handlers/xo.vim +++ b/autoload/ale/handlers/xo.vim @@ -27,3 +27,9 @@ endfunction function! ale#handlers#xo#HandleJSON(buffer, lines) abort return ale#handlers#eslint#HandleJSON(a:buffer, a:lines) 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 diff --git a/test/fixers/test_xo_fixer_callback.vader b/test/fixers/test_xo_fixer_callback.vader index d36fe74c..ffbecb6c 100644 --- a/test/fixers/test_xo_fixer_callback.vader +++ b/test/fixers/test_xo_fixer_callback.vader @@ -29,3 +29,18 @@ Execute(The xo callback should include custom xo options): \ . ' --fix %t' \ . ' --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', + \ } diff --git a/test/fixers/test_xots_fixer_callback.vader b/test/fixers/test_xots_fixer_callback.vader index 1ef6a6dd..a26ec03c 100644 --- a/test/fixers/test_xots_fixer_callback.vader +++ b/test/fixers/test_xots_fixer_callback.vader @@ -29,3 +29,18 @@ Execute(The xo callback should include custom xo options): \ . ' --fix %t' \ . ' --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', + \ }