diff --git a/autoload/ale/fixers/rubocop.vim b/autoload/ale/fixers/rubocop.vim index 5a1b7959..c0d4315e 100644 --- a/autoload/ale/fixers/rubocop.vim +++ b/autoload/ale/fixers/rubocop.vim @@ -19,20 +19,34 @@ function! ale#fixers#rubocop#PostProcess(buffer, output) abort return a:output[l:line :] endfunction -function! ale#fixers#rubocop#GetCommand(buffer) abort +function! ale#fixers#rubocop#GetCommand(buffer, version) abort let l:executable = ale#Var(a:buffer, 'ruby_rubocop_executable') let l:options = ale#Var(a:buffer, 'ruby_rubocop_options') let l:auto_correct_all = ale#Var(a:buffer, 'ruby_rubocop_auto_correct_all') + let l:editor_mode = ale#semver#GTE(a:version, [1, 61, 0]) return ale#ruby#EscapeExecutable(l:executable, 'rubocop') \ . (!empty(l:options) ? ' ' . l:options : '') \ . (l:auto_correct_all ? ' --auto-correct-all' : ' --auto-correct') + \ . (l:editor_mode ? ' --editor-mode' : '') \ . ' --force-exclusion --stdin %s' endfunction -function! ale#fixers#rubocop#Fix(buffer) abort +function! ale#fixers#rubocop#GetCommandForVersion(buffer, version) abort return { - \ 'command': ale#fixers#rubocop#GetCommand(a:buffer), - \ 'process_with': 'ale#fixers#rubocop#PostProcess' + \ 'command': ale#fixers#rubocop#GetCommand(a:buffer, a:version), + \ 'process_with': 'ale#fixers#rubocop#PostProcess' \} endfunction + +function! ale#fixers#rubocop#Fix(buffer) abort + let l:executable = ale#Var(a:buffer, 'ruby_rubocop_executable') + let l:command = l:executable . ale#Pad('--version') + + return ale#semver#RunWithVersionCheck( + \ a:buffer, + \ l:executable, + \ l:command, + \ function('ale#fixers#rubocop#GetCommandForVersion'), + \) +endfunction diff --git a/test/fixers/test_rubocop_fixer_callback.vader b/test/fixers/test_rubocop_fixer_callback.vader index f7b0eb60..303aa30f 100644 --- a/test/fixers/test_rubocop_fixer_callback.vader +++ b/test/fixers/test_rubocop_fixer_callback.vader @@ -1,53 +1,47 @@ Before: - Save g:ale_ruby_rubocop_executable - Save g:ale_ruby_rubocop_options - - " Use an invalid global executable, so we don't match it. - let g:ale_ruby_rubocop_executable = 'xxxinvalid' - let g:ale_ruby_rubocop_options = '' - - call ale#test#SetDirectory('/testplugin/test/fixers') + call ale#assert#SetUpFixerTest('ruby', 'rubocop') After: - Restore - - call ale#test#RestoreDirectory() + call ale#assert#TearDownFixerTest() Execute(The rubocop callback should return the correct default values): call ale#test#SetFilename('../test-files/ruby/dummy.rb') - AssertEqual + GivenCommandOutput ['1.61.0'] + + AssertFixer \ { \ 'process_with': 'ale#fixers#rubocop#PostProcess', \ 'command': ale#Escape(g:ale_ruby_rubocop_executable) - \ . ' --auto-correct --force-exclusion --stdin %s', - \ }, - \ ale#fixers#rubocop#Fix(bufnr('')) + \ . ' --auto-correct --editor-mode --force-exclusion --stdin %s', + \ } Execute(The rubocop callback should include custom rubocop options): let g:ale_ruby_rubocop_options = '--except Lint/Debugger' call ale#test#SetFilename('../test-files/ruby/with_config/dummy.rb') - AssertEqual + GivenCommandOutput ['1.61.0'] + + AssertFixer \ { \ 'process_with': 'ale#fixers#rubocop#PostProcess', \ 'command': ale#Escape(g:ale_ruby_rubocop_executable) \ . ' --except Lint/Debugger' - \ . ' --auto-correct --force-exclusion --stdin %s', - \ }, - \ ale#fixers#rubocop#Fix(bufnr('')) + \ . ' --auto-correct --editor-mode --force-exclusion --stdin %s', + \ } Execute(The rubocop callback should use auto-correct-all option when set): let g:ale_ruby_rubocop_auto_correct_all = 1 call ale#test#SetFilename('../test-files/ruby/with_config/dummy.rb') - AssertEqual + GivenCommandOutput ['1.61.0'] + + AssertFixer \ { \ 'process_with': 'ale#fixers#rubocop#PostProcess', \ 'command': ale#Escape(g:ale_ruby_rubocop_executable) - \ . ' --auto-correct-all --force-exclusion --stdin %s' - \ }, - \ ale#fixers#rubocop#Fix(bufnr('')) + \ . ' --auto-correct-all --editor-mode --force-exclusion --stdin %s' + \ } Execute(The rubocop post-processor should remove diagnostics content): AssertEqual @@ -87,3 +81,16 @@ Execute(The rubocop post-processor should remove diagnostics content): \ ' ''forrest'',', \ ' ''run'']', \ ]) + +Execute(The rubocop callback should not use editor-mode option with older versions): + call ale#test#SetFilename('../test-files/ruby/with_config/dummy.rb') + + GivenCommandOutput ['1.59.0'] + + AssertFixer + \ { + \ 'process_with': 'ale#fixers#rubocop#PostProcess', + \ 'command': ale#Escape(g:ale_ruby_rubocop_executable) + \ . ' --auto-correct --force-exclusion --stdin %s' + \ } +