Git Product home page Git Product logo

rubocop-gradual's Introduction

RuboCop Gradual

Gem Version Build

RuboCop Gradual is a tool that helps track down and fix RuboCop offenses in your code gradually. It's a more flexible alternative to RuboCop's --auto-gen-config option.

RuboCop Gradual:

  • generates the lock file with all RuboCop offenses and uses hashes to track each offense line by line
  • automatically updates the lock file on every successful run, but returns errors on new offenses
  • does not prevent your editor from showing ignored offenses

Gain full control of gradual improvements: just add rubocop-gradual and use it as proxy for rubocop.

Sponsored by Evil Martians

Installation

Install the gem and add to the application's Gemfile by executing:

$ bundle add rubocop-gradual

Run RuboCop Gradual to create a lock file (defaults to .rubocop_gradual.lock):

$ rubocop-gradual

Commit the lock file to the project repository to keep track of all non-fixed offenses.

Run rubocop-gradual before commiting changes to update the lock file. RuboCop Gradual will keep updating the lock file to keep track of all non-fixed offenses, but it will throw an error if there are any new offenses.

Usage

Proposed workflow:

  • Remove rubocop_todo.yml if it exists.
  • Run rubocop-gradual to generate a lock file and commit it to the project repository.
  • Add rubocop-gradual --check to your CI pipeline instead of rubocop/standard. It will throw an error if the lock file is out of date.
  • Run rubocop-gradual to update the lock file, or rubocop-gradual -a to run autocorrection for all new and changed files and then update the lock file.
  • Optionally, add rubocop-gradual as a pre-commit hook to your repository (using lefthook, for example).
  • RuboCop Gradual will throw an error on any new offense, but if you really want to force update the lock file, run rubocop-gradual --force-update.

Available options

    -U, --force-update               Force update Gradual lock file.
        --check                      Check Gradual lock file is up-to-date.
    -a, --autocorrect                Autocorrect offenses (only when it's safe).
    -A, --autocorrect-all            Autocorrect offenses (safe and unsafe).
        --gradual-file FILE          Specify Gradual lock file.
    -v, --version                    Display version.
    -h, --help                       Prints this help.

Rake tasks

To use built-in Rake tasks add the following to your Rakefile:

# Rakefile
require "rubocop/gradual/rake_task"

RuboCop::Gradual::RakeTask.new

This will add rake tasks:

bundle exec rake -T
rake rubocop_gradual                  # Run RuboCop Gradual
rake rubocop_gradual:autocorrect      # Run RuboCop Gradual with autocorrect (only when it's safe)
rake rubocop_gradual:autocorrect_all  # Run RuboCop Gradual with autocorrect (safe and unsafe)
rake rubocop_gradual:check            # Run RuboCop Gradual to check the lock file
rake rubocop_gradual:force_update     # Run RuboCop Gradual to force update the lock file

It's possible to customize the Rake task name and options:

# Rakefile

require "rubocop/gradual/rake_task"

RuboCop::Gradual::RakeTask.new(:custom_task_name) do |task|
  task.options = %w[--gradual-file custom_gradual_file.lock]
  task.verbose = false
end

Partial linting (experimental)

RuboCop Gradual supports partial linting. It's useful when you want to run RuboCop Gradual on a subset of files, for example, on changed files in a pull request:

rubocop-gradual path/to/file # run `rubocop-gradual` on a subset of files
rubocop-gradual --staged # run `rubocop-gradual` on staged files
rubocop-gradual --unstaged # run `rubocop-gradual` on unstaged files
rubocop-gradual --commit origin/main # run `rubocop-gradual` on changed files since the commit

# it's possible to combine options with autocorrect:
rubocop-gradual --staged --autocorrect # run `rubocop-gradual` with autocorrect on staged files

Require mode (experimental)

RuboCop Gradual can be used in "Require mode", which is a way to replace rubocop with rubocop-gradual:

# .rubocop.yml

require:
  - rubocop/gradual/patch

Now base rubocop command will run rubocop-gradual:

rubocop # run `rubocop-gradual`
rubocop -a # run `rubocop-gradual` with autocorrect (only when it's safe)
rubocop -A # run `rubocop-gradual` with autocorrect (safe and unsafe)
rubocop gradual check # run `rubocop-gradual` to check the lock file
rubocop gradual force_update # run `rubocop-gradual` to force update the lock file

To set a custom path to Gradual lock file, add --gradual-file FILE to a special .rubocop-gradual file:

# .rubocop-gradual
--rubocop-gradual-file path/to/my_lock_file.lock

To temporarily disable RuboCop Gradual, prepend command with NO_GRADUAL=1:

NO_GRADUAL=1 rubocop # run `rubocop`

Alternatives

  • RuboCop TODO file. Comes out of the box with RuboCop. Provides a way to ignore offenses on the file level, which is problematic since it is possible to introduce new offenses without any signal from linter.
  • Pronto. Checks for offenses only on changed files. Does not provide a way to temporarily ignore offenses.
  • Betterer. Universal test runner that helps make incremental improvements witten in JavaScript. RuboCop Gradual is highly inspired by Betterer.

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/skryukov/rubocop-gradual

License

The gem is available as open source under the terms of the MIT License.

rubocop-gradual's People

Contributors

skryukov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

rubocop-gradual's Issues

Crash during rake rubocop_gradual

Firstly, I need to say: I love this gem - the idea is brilliant, and I'd love to help however I can!

I hacked the gem source to see what was going on:

   59:           str.each_byte.inject(5381) do |hash, b|
   60:             ((hash << 5) + hash) ^ b
   61:           end & 0xFFFFFFFF
   62:         rescue NoMethodError => error
   63:           byebug
   64:         end

Before hitting my byebug, I got this output:

❯ bundle exec rake rubocop_gradual
Running RuboCop Gradual...
................................
Found 13 files with 233 issue(s).
Processing results...
Return value is: nil

I am not familiar with that last line, having run rubocop_gradual many times now.

Here's some introspection after hitting my byebug:

(byebug) str
nil
(byebug) error.class
NoMethodError
(byebug) error.message
"undefined method `each_byte' for nil:NilClass"
(byebug) puts error.backtrace
/Users/pboling/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/rubocop-gradual-0.3.1/lib/rubocop/gradual/results/file.rb:59:in `djb2a'
/Users/pboling/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/rubocop-gradual-0.3.1/lib/rubocop/gradual/results/file.rb:38:in `issue_hash'
/Users/pboling/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/rubocop-gradual-0.3.1/lib/rubocop/gradual/results/file.rb:32:in `block in prepare_issues'
/Users/pboling/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/rubocop-gradual-0.3.1/lib/rubocop/gradual/results/file.rb:32:in `map'
/Users/pboling/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/rubocop-gradual-0.3.1/lib/rubocop/gradual/results/file.rb:32:in `prepare_issues'
/Users/pboling/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/rubocop-gradual-0.3.1/lib/rubocop/gradual/results/file.rb:15:in `initialize'
/Users/pboling/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/rubocop-gradual-0.3.1/lib/rubocop/gradual/results.rb:14:in `new'
/Users/pboling/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/rubocop-gradual-0.3.1/lib/rubocop/gradual/results.rb:14:in `block in initialize'

I am not sure which file it is dying on... because str is nil... πŸ˜†

Unable to lint a single file?

I'm using the shim, so rubocop_gradual runs instead of rubocop normally.

When I run:

bundle exec rubocop -a Gemfile

the entire project is scanned as if I had not specified a file.

When I run:

NO_GRADUAL=1 bundle exec rubocop -a Gemfile

It lints the Gemfile only.

Is this a known limitation of rubocop-gradual?

Is it intentional?

Odd encoding churn

Running rubocop-gradual is resulting in many iterations of churn between encoded strings and unencoded strings across the entire lockfile. Some pics of one of the recent ones:

First we went one way:
Screenshot 2023-10-23 at 23 06 15
Screenshot 2023-10-23 at 23 06 05
Screenshot 2023-10-23 at 23 05 40
Screenshot 2023-10-23 at 23 05 32

And then later the same day:
Screenshot 2023-10-23 at 23 01 55
Screenshot 2023-10-23 at 23 01 47
Screenshot 2023-10-23 at 23 01 19
Screenshot 2023-10-23 at 23 01 04

Unable to use CLI --list option

I am using the rubocop-gradual shim, so it runs instead of the standard rubocop command normally.

When I run:

NO_GRADUAL=1 bundle exec rubocop -a --list

it only prints the list of files that would be processed.

If I instead run:

bundle exec rubocop -a --list

It runs rubocop gradual ignoring the --list option entirely.

Is this intentional?

Suggestion: rename the `--update` option to `--force-update`

Following the lengthy brainstorming and discussion in #1, I suggest renaming the --update option, and possibly removing (or changing?) its short name.

Rationale:

  1. Without any options, rubocop-gradual already updates the lock file in the normal development workflow. The README even suggests using that command without options in git hooks to update the lock file. So having a separate --update command that does something else is confusing.
  2. Given that first point, the --update command should be named in a way that conveys its specificity (e.g. what type of update it does or when it does it).

I suggest using --force-update, because it disregards the "error" case of introducing new issues in the lock file.

It might also be useful to delete the -u short option (if you don't want to incentivize using it too much), or maybe change it to something else? Possible values for a short command:

  • -f (of rm -f fame)
  • -U with an upercase U only (uppercase is sometimes used to convey that you're asking for the "more powerful" version of a command)
  • -uu (used by ripgrep for instance)
  • -fu (which reads like… hmm maybe that joke is too colorful)

Suggestion: rename --ci option to a context-neutral name

The --ci option is ambiguous:

  • Continuous integration covers a wide range of practices.
  • It suggests that this command should only be ran on CI servers, but it might be useful to run it on developer workstations as well.
  • It doesn't actually describe what the option does.

If the functionality is "check code against the lock file and error out if there is any new linting output", maybe a name like --check might be a better fit?

Edit: other brainstormed names: --test, --strict.

Add `standard` support out of the box

I'm thinking of including exe/standard-gradual into the gem to support standard proxying.

Probably this will lead to rewriting of the glue code, that connects rubocop and rubocop-gradual.

Bug: Discrepancy between lint failures in `autocorrect` and `force_update`?

My CI failed with a RuboCop violation. So locally I ran:

❯ bundle exec rake rubocop_gradual:autocorrect
Running RuboCop Gradual...
Inspecting 181 file(s) for autocorrection...
.....................................................................................................................................................................................
Fixed 0 file(s).
.......................................................................................................................................................................................................................................................................................................................................................................................................................................................
Found 258 files with 5513 issue(s).
Processing results...
RuboCop Gradual got no changes.
noglob bundle exec rake rubocop_gradual:autocorrect  53.01s user 8.48s system 228% cpu 26.863 total

This is a surprising and invalid result, since I saw the result of running rubocop on CI (which runs check).

Instead of saying it "got no changes" it should have reported the new failures we are about to see below...

So I ran force_update:

❯ bundle exec rake rubocop_gradual:force_update
Running RuboCop Gradual...
.......................................................................................................................................................................................................................................................................................................................................................................................................................................................
Found 258 files with 5515 issue(s).
Processing results...
Uh oh, RuboCop Gradual got worse:
-> spec/channels/modified_relays_channel_spec.rb (1 new issues)
    (line 7) "Betterment/UnscopedFind: Records are being retrieved directly using user input.
Please query for the associated record in a way that enforces authorization (e.g. "trust-root chaining").

INSTEAD OF THIS:
Post.find(params[:post_id])

DO THIS:
current_user.posts.find(params[:post_id])

See here for more information on this error:
https://github.com/Betterment/betterlint/blob/main/README.md#bettermentunscopedfind
 (https://github.com/Betterment/betterlint#bettermentunscopedfind)"
-> spec/channels/sensor_streams_channel_spec.rb (1 new issues)
    (line 7) "Betterment/UnscopedFind: Records are being retrieved directly using user input.
Please query for the associated record in a way that enforces authorization (e.g. "trust-root chaining").

INSTEAD OF THIS:
Post.find(params[:post_id])

DO THIS:
current_user.posts.find(params[:post_id])

See here for more information on this error:
https://github.com/Betterment/betterlint/blob/main/README.md#bettermentunscopedfind
 (https://github.com/Betterment/betterlint#bettermentunscopedfind)"
Force updating lock file...
noglob bundle exec rake rubocop_gradual:force_update  20.56s user 4.62s system 226% cpu 11.099 total

The force_update caught the change somehow, when the autocorrect did not.

Config loads differently based on something

Or... maybe it is doing something extra at the end of the task, but only sometimes?

Using the rubocop shim hack (with autocorrect):

❯ bundle exec rubocop -a
Inspecting 109 file(s) for autocorrection...
.............................................................................................................
Fixed 0 file(s).
...........................................................................................................................................................................................................................................................
Found 142 files with 6203 issue(s).
Processing results...
RuboCop Gradual got 1 issue(s) fixed, 6203 left. Keep going!
bundle exec rubocop -a  48.45s user 6.64s system 200% cpu 27.541 total

Using the rake check task locally the process appears to run as expected:

❯ bundle exec rake rubocop_gradual:check
Running RuboCop Gradual...
...........................................................................................................................................................................................................................................................
Found 142 files with 6203 issue(s).
Processing results...
RuboCop Gradual got no changes.
bundle exec rake rubocop_gradual:check  45.73s user 6.49s system 172% cpu 30.274 total

However, in CI we get a different results from the same rake check task, running against the same code, same git SHA:

+ bundle exec rake rubocop_gradual:check
Program Name: /opt/atlassian/pipelines/agent/build/vendor/bundle/ruby/2.7.0/bin/rake
Not using any observers because program name isn't rails, passenger, or rspec
Running RuboCop Gradual...
...........................................................................................................................................................................................................................................................
Found 142 files with 6203 issue(s).
Processing results...
RuboCop Gradual got 1 issue(s) fixed, 6203 left. Keep going!
RuboCop Gradual failed!
Unexpected Changes!
RuboCop Gradual lock file is outdated, to fix this message:
- Run `rubocop-gradual` locally and commit the results, or
- EVEN BETTER: before doing the above, try to fix the remaining issues in those files!
`.rubocop_gradual.lock` diff:
+  "config/environments/development.rb:4085136686": [
+    [13, 5, 42, "Rails/Blank: Use `ENV[\"TOPICS\"].blank?` instead of `ENV[\"TOPICS\"].nil? || ENV[\"TOPICS\"].empty?`.", 75732059]
+  ],

I'm at a loss for how this is possible.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.