Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

develop simplified rubocop config #3

Closed
heratyian opened this issue Jun 10, 2024 · 10 comments · Fixed by #4
Closed

develop simplified rubocop config #3

heratyian opened this issue Jun 10, 2024 · 10 comments · Fixed by #4

Comments

@heratyian
Copy link
Contributor

want to give it a try @SaraDawner2000 ?

@SaraDawner2000
Copy link

First, there is a default .rubocop.yml config now. These gems need to be added to the Gemfile for it to work:

group :rubocop do
  gem "rubocop", ">= 1.25.1", require: false
  gem "rubocop-minitest", require: false
  gem "rubocop-packaging", require: false
  gem "rubocop-performance", require: false
  gem "rubocop-rails", require: false
  gem "rubocop-md", require: false

  # This gem is used in Railties tests so it must be a development dependency.
  gem "rubocop-rails-omakase", require: false
end

Using the default rails config in Ragu's base Ruby on Rails repository lessens the number of rubocop offences from 388 to 193 and gets rid of pretty much all non-autocorrectible offences.
spec/support/json_output_formatter.rb and config/initializers/nicer_errors.rb bring up a couple of layout offences each, so it might make sense to exclude them in:

AllCops:
  Exclude:
    - 'config/initializers/nicer_errors.rb' # removes 14 offences 
    - 'spec/support/json_output_formatter.rb' # removes 11 offences, including one non-autocorrectible

Next, the big offenders are:

# Check for magic frozen literal comment as first line (https://rubystyle.guide/#magic-comments-first)
Style/FrozenStringLiteralComment:
  Enabled: false #removes 52 offences (autocorrectable)

and

# Check quotes usage according to lint rule below.
Style/StringLiterals:
  Enabled: false #removes 107 offences

Regarding the latter, it is completely autocorrectible, and perhaps it would be beneficial for consistency's sake to enforce one or the other (the default for rails is double quotes unless single quotes are used within double quotes).
The remaining 9 offences are autocorrectible, and maybe it would make sense just to listen. The only one I might turn off is checking that hashes are written in Ruby >= 1.9 syntax, if only because Ragu uses the older one and actually recommends it on occasion.

# Use Ruby >= 1.9 syntax for hashes. Prefer { a: :b } over { :a => :b }.
Style/HashSyntax:
  Enabled: false # removes 1 offence and matches Ragu's style better

Below is the terminal output for the remaining 8 offences.

Inspecting 51 files
.C..........C....C...................C..C.....C....

Offenses:

Gemfile:105:23: C: [Correctable] Layout/LeadingCommentSpace: Missing space after #. (https://rubystyle.guide#hash-space)
  gem "draft_matchers"#, "0.0.2"#path: "../../my_stuff/draft_matchers"
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Gemfile:105:23: C: [Correctable] Layout/SpaceBeforeComment: Put a space before an end-of-line comment.
  gem "draft_matchers"#, "0.0.2"#path: "../../my_stuff/draft_matchers"
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
bin/bundle:33:24: C: [Correctable] Performance/ConstantRegexp: Extract this regexp into a constant, memoize it, or append an /o option to its options.
      next unless a =~ /\A--bundler(?:[= ](#{Gem::Version::VERSION_PATTERN}))?\z/
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
bin/bundle:59:40: C: [Correctable] Performance/ConstantRegexp: Extract this regexp into a constant, memoize it, or append an /o option to its options.
    return unless lockfile_contents =~ /\n\nBUNDLED WITH\n\s{2,}(#{Gem::Version::VERSION_PATTERN})\n/
                                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
config.ru:12:1: C: [Correctable] Layout/IndentationStyle: Tab detected in indentation. (https://rubystyle.guide#spaces-indentation)
        run Rails.application
^
config.ru:12:1: C: [Correctable] Layout/IndentationWidth: Use 2 (not 1) spaces for indentation. (https://rubystyle.guide#spaces-indentation)
        run Rails.application
^
db/schema.rb:16:1: C: [Correctable] Layout/EmptyLinesAroundBlockBody: Extra empty line detected at block body end. (https://rubystyle.guide#empty-lines-around-bodies)
lib/tasks/dev.rake:2:8: C: [Correctable] Style/HashSyntax: Use the new Ruby 1.9 hash syntax. (https://rubystyle.guide#hash-literals)
task({ :sample_data => :environment }) do
       ^^^^^^^^^^^^^^^
spec/support/hint_formatter.rb:10:1: C: [Correctable] Layout/EmptyLinesAroundClassBody: Extra empty line detected at class body end. (https://rubystyle.guide#empty-lines-around-bodies)

51 files inspected, 9 offenses detected, 9 offenses autocorrectable

@heratyian
Copy link
Contributor Author

Thanks for investigating this @SaraDawner2000. Reading through this I'm thinking it might make sense to include this in the rails template https://github.com/appdev-projects/rails-7-template. I'll create an issue to see what first draft thinks. appdev-projects/rails-7-template#44 Otherwise, we'll add some guidelines to this lesson.

@heratyian
Copy link
Contributor Author

I think this will help for a baseline https://gorails.com/episodes/omakase-rubocop-linter-rules-for-rails

@heratyian
Copy link
Contributor Author

Reasonably good starting point https://github.com/rails/rubocop-rails-omakase

@heratyian
Copy link
Contributor Author

Reasonably good starting point https://github.com/rails/rubocop-rails-omakase

any @DPI-WE/technical-associates want to replace rubocop section with this ^^

@armstrca
Copy link
Contributor

armstrca commented Sep 6, 2024

In the Rails 7 Template gemfile?

@heratyian
Copy link
Contributor Author

In the Rails 7 Template gemfile?

no, this lesson.

@SaraDawner2000
Copy link

including the rubocop rails gem in the Gemfile with

gem "rubocop-rails", require: false
gem "rubocop-rails_config"

Worked great for me in the final project. The only adjustments I had to make to the .rubocop.yml file was changing the frozen string literal rule and excluding the Gemfile.lock:

Style/FrozenStringLiteralComment:
  Enabled: false

AllCops:
  Exclude:
    - "**/Gemfile.lock"

@armstrca
Copy link
Contributor

armstrca commented Sep 9, 2024

I need permissions for the repo, but I have a PR ready to go when I have them

@heratyian
Copy link
Contributor Author

I need permissions for the repo, but I have a PR ready to go when I have them

try now

@armstrca armstrca linked a pull request Sep 9, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants