Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VSCode Ruby RuboCop formatter removes focused specs `fit` -> `it`

I've been happily using https://github.com/rubyide/vscode-ruby in VSCode which has been auto-formatting my code on save, until this was merged https://github.com/rubocop-hq/rubocop-rspec/pull/1109 (which is great in itself).

Now when I save a Rspec file with a focused spec, it removes it! eg

On saving fit "something" do, it updates it to it 'something'! (It does not remove disabled specs xit)

vscode-ruby config:

    "ruby.intellisense": "rubyLocate",
    "ruby.useLanguageServer": true,
    "ruby.codeCompletion": "rcodetools",
    "ruby.format": "rubocop", // this line causes the formatter to kick in
    "ruby.lint": {
        "rubocop": true
    },

Options

  1. I can bypass this by adding # rubocop:disable RSpec/Focus to the end, but that is annoying
  2. I can disable the cop in my local .rubocop.yml file, but then
    1. either have a local diff, and lose the check against all files when running rubocop on the command line
    2. have to check it in and everyone loses the check
  3. AFAICT there is no command-line option to disable a cop. The inverse of only would be good!
  4. But even if that option was present, can vscode-ruby be configured to modify the command line options?
  5. Others?
like image 718
Ian Vaughan Avatar asked Oct 27 '25 06:10

Ian Vaughan


2 Answers

To have ruby-lsp extension still fix most auto-correctable offences on save in VS Code, but allow for focussed tests to still work, you could rely on an extra safety next in your continuous integration environment.

Disable the cop in .rubocop.yml:

RSpec/Focus:
  Enabled: false

and modify the focus configuration in rails_helper.rb to say:

if ENV["CI"]
  config.before(:example, :focus) { |example| raise "Focused spec found at #{example.location}" }
else
  config.filter_run_when_matching :focus
end

I figured this out via Stephanie Viccari’s blog post.

like image 98
weston Avatar answered Oct 28 '25 22:10

weston


If your version of rubocop/rubocop-rspec supports it, you can do:

RSpec/Focus:
  AutoCorrect: contextual

For more info, see:

  • https://github.com/rubocop/rubocop/pull/12657
  • https://github.com/rubocop/rubocop-rspec/issues/1883
  • https://github.com/rubocop/rubocop-rspec/pull/1899
like image 23
Alexander Popov Avatar answered Oct 28 '25 22:10

Alexander Popov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!