Add --editor-mode flag when invoking rubocop (#5049)

* Add --editor-mode flag when invoking rubocop

Since RuboCop 1.61.0 (released in February 2024), RuboCop accepts an
`--editor-mode` flag which improves editor integrations like ale.

Some of RuboCop's auto-corrections can be surprising or annoying to run
on save. When RuboCop is running via an LSP or when the `--editor-mode`
flag is passed, it will understand that it is running in an editor, and
it will hold off on making changes that might be surprising or annoying.

For example, if I write

```ruby
def call
  results = some_process
end
```

This has an unused variable, and RuboCop will remove the unused variable
when you run it. However, if you're in the middle of editing, you may
not want it to remove that unused variable, because you may be about to
add a usage of it.

More context:

- PR which introduced it: https://github.com/rubocop/rubocop/pull/12682
- Release notes for 1.61: https://github.com/rubocop/rubocop/releases/tag/v1.61.0
- Docs: https://docs.rubocop.org/rubocop/1.80/configuration.html#contextual

This will be a breaking change for anyone who is running an old version
of RuboCop, because the flag will not exist for them. If they would like
to opt out of this change, they can set an option to omit the flag. I
think this ought to be enabled by default so that people will get this
benefit out of the box.

In the meantime, I am opting into this behavior by setting this option:

```vim
let g:ale_ruby_rubocop_options = "--editor-mode"
```

So I appreciate that this seam was already introduced.

* Make this a non-breaking change

This will detect the current rubocop version and auto-enable
--editor-mode for newer version of rubocop without affecting users of
older versions of rubocop.
This commit is contained in:
Max Jacobson
2025-10-26 02:14:30 -04:00
committed by GitHub
parent 9811114948
commit ed26d1f1d9
2 changed files with 48 additions and 27 deletions

View File

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

View File

@@ -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'
\ }