Formatting with rubocop should only autocorrect layout offenses
Nobody has claimed this yet.
- Dominant language
- Ruby
- Stars
- 2k
- Forks
- 281
- Avg merge
- 2h 14m
- Merged PRs (30d)
- 6
Description
Use case
Rubocop's more aggressive autocorrects are incredibly useful. The "unsafe" autocorrections don't run by default, and I agree that some "safe" autocorrection should probably be re-categorized as "unsafe". But rubocop has many truly safe autocorrections that I don't want to run every time I save or format a file. I do want to be able to run these auto-corrections manually. Marking these in my own rubocop.yml as unsafe or not auto-correctable would be a significant downgrade of the standard rubocop functionality.
Description
Fortunately, rubocop already provides the ability to only run safe formatting auto-corrections, instead of all safe auto-corrections: the -x (or --fix-layout) option. As an added benefit, it's much faster than running all autocorrections. ruby-lsp should use that option when it uses rubocop for formatting.
All auto-correctable offenses (whether safe or unsafe) should be made available as Code Actions.
Implementation
I believe this can be accomplished by simply changing one character on the following line:
https://github.com/Shopify/ruby-lsp/blob/6d63b6e55ed44df8ca869164a7a770a72ae17418/lib/ruby_lsp/requests/support/rubocop_formatting_runner.rb#L20 should be changed to use -x instead of -a.
However, I haven't read through https://github.com/Shopify/ruby-lsp/blob/main/lib/ruby_lsp/requests/support/rubocop_diagnostic.rb (or other relevant files) yet, and I don't know if that would break the auto-correction code actions.
Originally posted by @nevans in https://github.com/Shopify/ruby-lsp/issues/596#issuecomment-1536783968
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at lib/ruby_lsp/requests/support/rubocop_formatting_runner.rb around line 20 and inspect lib/ruby_lsp/requests/support/rubocop_diagnostic.rb for how formatting and code actions are separated. Confirm that formatting uses RuboCop's layout-only option while all autocorrectable offenses remain available as Code Actions.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- ruby
- Domain
- developer-experience
- Issue type
- Feature
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100