Skip to content

Commit

Permalink
Auto correct rubocop errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeheft committed Jul 2, 2024
1 parent ee52b7e commit f0391dc
Show file tree
Hide file tree
Showing 15 changed files with 154 additions and 19 deletions.
48 changes: 47 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1 +1,47 @@
inherit_from: .rubocop_todo.yml
require:
- rubocop-rails
- ./lib/rubocop/init_autoloader

AllCops:
TargetRubyVersion: 3.3.0
Exclude:
- "spec/**/*.rb"
- "db/**/*"
- "config/**/*"
- "bin/**/*"
###################
# Custom Cops #
###################
Custom/PrivateMethodStyle:
Enabled: true
###################
# End Custom Cops #
###################
Layout/IndentationWidth:
Enabled: true
Width: 2
Lint/UnusedMethodArgument:
AutoCorrect: false
Metrics/AbcSize:
Exclude:
- "lib/mixins/**/*"
Metrics/CyclomaticComplexity:
Exclude:
- "lib/rubocop/cop/custom/*.rb"
Metrics/MethodLength:
Exclude:
- "lib/rubocop/cop/custom/*.rb"
- "lib/mixins/**/*"
Metrics/PerceivedComplexity:
Exclude:
- "lib/rubocop/cop/custom/*.rb"
Style/AccessModifierDeclarations:
EnforcedStyle: inline
Style/Documentation:
Enabled: false
Style/Lambda:
EnforcedStyle: literal
Style/RedundantArgument:
AutoCorrect: false
Style/StringLiterals:
EnforcedStyle: double_quotes
39 changes: 21 additions & 18 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
source 'https://rubygems.org'
# frozen_string_literal: true

ruby '3.3.0'
source "https://rubygems.org"

ruby "3.3.0"

# Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main"
gem 'rails', '~> 7.1.3', '>= 7.1.3.4'
gem "rails", "~> 7.1.3", ">= 7.1.3.4"

# Use postgresql as the database for Active Record
gem 'pg', '~> 1.1'
gem "pg", "~> 1.1"

# Use the Puma web server [https://github.com/puma/puma]
gem 'puma', '>= 5.0'
gem "puma", ">= 5.0"

# Build JSON APIs with ease [https://github.com/rails/jbuilder]
# gem "jbuilder"
Expand All @@ -24,14 +26,15 @@ gem 'puma', '>= 5.0'
# gem "bcrypt", "~> 3.1.7"

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'rubocop', require: false
gem 'tzinfo-data', platforms: %i[windows jruby]
gem "rubocop", require: false
gem "rubocop-rails", require: false
gem "tzinfo-data", platforms: %i[windows jruby]

# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', require: false
gem 'database_cleaner'
gem 'faraday'
gem 'figaro'
gem "bootsnap", require: false
gem "database_cleaner"
gem "faraday"
gem "figaro"
# Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images]
# gem "image_processing", "~> 1.2"

Expand All @@ -40,13 +43,13 @@ gem 'figaro'

group :development, :test do
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
gem 'debug', platforms: %i[mri windows]
gem 'factory_bot_rails', '~> 4.0'
gem 'pry-rails'
gem 'rspec-rails'
gem 'shoulda-matchers'
gem 'vcr'
gem 'webmock'
gem "debug", platforms: %i[mri windows]
gem "factory_bot_rails", "~> 4.0"
gem "pry-rails"
gem "rspec-rails"
gem "shoulda-matchers"
gem "vcr"
gem "webmock"
end

group :development do
Expand Down
6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,11 @@ GEM
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.31.3)
parser (>= 3.3.1.0)
rubocop-rails (2.24.1)
activesupport (>= 4.2.0)
rack (>= 1.1)
rubocop (>= 1.33.0, < 2.0)
rubocop-ast (>= 1.31.1, < 2.0)
ruby-progressbar (1.13.0)
shoulda-matchers (6.2.0)
activesupport (>= 5.2.0)
Expand Down Expand Up @@ -298,6 +303,7 @@ DEPENDENCIES
rails (~> 7.1.3, >= 7.1.3.4)
rspec-rails
rubocop
rubocop-rails
shoulda-matchers
tzinfo-data
vcr
Expand Down
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.

Expand Down
2 changes: 2 additions & 0 deletions app/channels/application_cable/channel.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module ApplicationCable
class Channel < ActionCable::Channel::Base
end
Expand Down
2 changes: 2 additions & 0 deletions app/channels/application_cable/connection.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module ApplicationCable
class Connection < ActionCable::Connection::Base
end
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# frozen_string_literal: true

class ApplicationController < ActionController::API
end
2 changes: 2 additions & 0 deletions app/jobs/application_job.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class ApplicationJob < ActiveJob::Base
# Automatically retry jobs that encountered a deadlock
# retry_on ActiveRecord::Deadlocked
Expand Down
2 changes: 2 additions & 0 deletions app/mailers/application_mailer.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class ApplicationMailer < ActionMailer::Base
default from: "from@example.com"
layout "mailer"
Expand Down
2 changes: 2 additions & 0 deletions app/models/application_record.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class ApplicationRecord < ActiveRecord::Base
primary_abstract_class
end
2 changes: 2 additions & 0 deletions config.ru
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# This file is used by Rack-based servers to start the application.

require_relative "config/environment"
Expand Down
8 changes: 8 additions & 0 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,12 @@

# Raise error when a before_action's only/except options reference missing actions
config.action_controller.raise_on_missing_callback_actions = true

config.generators.after_generate do |files|
parsable_files = files.filter { |file| file.end_with?('.rb') }
unless parsable_files.empty?
system("bundle exec rubocop -A --fail-level=E #{parsable_files.shelljoin}", exception: true)
end
end

end
45 changes: 45 additions & 0 deletions lib/rubocop/cop/custom/private_method_style.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# frozen_string_literal: true
# typed: false

require "rubocop"

module RuboCop
module Cop
module Custom
# This cop enforces that private methods are defined using
# `private def` for instance methods or
# `private_class_method def self` for class methods.
class PrivateMethodStyle < Base
MSG_INSTANCE = "Use `private def` for instance methods."
MSG_CLASS = "Use `private_class_method def self` for class methods."

def on_def(node)
method_name = node.children[0]&.to_s

return unless class_method_definition?(node)

Rails.logger.debug "Checking method: #{method_name}"
add_offense(node, message: MSG_CLASS)
end

private def class_method_definition?(node)
node.defs_type? && node.children[0]&.to_s == "self.use_collection"
end

private def instance_method?(node)
return false unless node.def_type?

method_name, _args, _body = *node
method_name.to_s.start_with?("def") && !node.singleton_method?
end

private def class_method?(node)
return false unless node.defs_type?

_scope, method_name, _args, _body = *node
method_name.to_s.start_with?("self.") && !node.arguments? && !node.singleton_method?
end
end
end
end
end
4 changes: 4 additions & 0 deletions lib/rubocop/init_autoloader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

# Load all Ruby files in the custom directory
Dir[File.join(__dir__, "**/*.rb")].each { |file| require file }
7 changes: 7 additions & 0 deletions lib/tasks/rubcop_require.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

require "rubocop/rake_task"

RuboCop::RakeTask.new do |task|
task.requires << "rubocop-rails"
end

0 comments on commit f0391dc

Please sign in to comment.