DPI-WE / style-guides-linters

0 stars 1 forks source link

develop simplified rubocop config #3

Closed heratyian closed 23 hours ago

heratyian commented 3 months ago

want to give it a try @SaraDawner2000 ?

SaraDawner2000 commented 3 months ago

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 commented 3 months ago

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. https://github.com/appdev-projects/rails-7-template/issues/44 Otherwise, we'll add some guidelines to this lesson.

heratyian commented 3 weeks ago

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

heratyian commented 3 weeks ago

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

heratyian commented 1 week ago

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

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

armstrca commented 6 days ago

In the Rails 7 Template gemfile?

heratyian commented 6 days ago

In the Rails 7 Template gemfile?

no, this lesson.

SaraDawner2000 commented 6 days ago

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 commented 3 days ago

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

heratyian commented 3 days ago

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

try now