From ae416a68dc719ca3b6f7ceadcf2a01ac26752f0d Mon Sep 17 00:00:00 2001 From: John Andrews Date: Mon, 21 Apr 2025 16:46:58 -0400 Subject: [PATCH] Update rubocop version - Some plugins changed - Must now inherit from RuboCop::Cop::Base - investigate renamed to on_new_investigation - location is not a separate parameter to add_offense anymore --- .rubocop.yml | 9 ++- Gemfile.lock | 59 +++++++++++-------- lib/rubocop/cop/root_cops/avoid_ruby_prof.rb | 6 +- .../cop/root_cops/envvar_assignment.rb | 8 +-- lib/rubocop/cop/root_cops/eq_be_eql.rb | 6 +- .../root_cops/factories/factory_file_name.rb | 7 +-- .../cop/root_cops/factories/factory_name.rb | 10 ++-- lib/rubocop/cop/root_cops/job_has_queue.rb | 4 +- lib/rubocop/cop/root_cops/must_include.rb | 8 +-- lib/rubocop/cop/root_cops/must_inherit.rb | 6 +- .../no_backfills_in_data_migration.rb | 4 +- .../cop/root_cops/no_index_on_add_column.rb | 4 +- lib/rubocop/cop/root_cops/no_tracers.rb | 4 +- .../private_methods/called_private_method.rb | 4 +- .../private_methods/called_protected.rb | 4 +- .../private_methods/underscore_prefix.rb | 6 +- lib/rubocop/cop/root_cops/raise_i18n.rb | 4 +- lib/rubocop/cop/root_cops/retry_on_warning.rb | 4 +- .../cop/root_cops/shared_context_name.rb | 6 +- lib/rubocop/cop/root_cops/spec_file_name.rb | 6 +- .../unnecessary_aggregate_failures.rb | 4 +- .../cop/root_cops/up_and_down_or_change.rb | 4 +- .../cop/root_cops/use_before_action.rb | 4 +- lib/rubocop/cop/root_cops/use_detect.rb | 6 +- lib/rubocop/cop/root_cops/use_envvars.rb | 4 +- .../cop/root_cops/use_lonely_operator.rb | 4 +- root-ruby-style.gemspec | 13 ++-- .../cop/root_cops/envvar_assignment_spec.rb | 22 ++----- .../cop/root_cops/support/shared_examples.rb | 7 +-- .../cop/root_cops/use_lonely_operator_spec.rb | 2 +- 30 files changed, 118 insertions(+), 121 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index f22ce7a..5d4ee27 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,7 +1,9 @@ -require: +plugins: - rubocop-rails - rubocop-performance - rubocop-rspec + - rubocop-factory_bot +require: - ./lib/rubocop/cop/root_cops/avoid_ruby_prof.rb - ./lib/rubocop/cop/root_cops/eq_be_eql.rb - ./lib/rubocop/cop/root_cops/factories/factory_file_name.rb @@ -93,6 +95,9 @@ Rails/FilePath: RSpec: Enabled: true +RSpec/BeEq: + Enabled: false + RSpec/ContextWording: Enabled: false @@ -193,7 +198,7 @@ RootCops/MustInherit: RootCops/MustInclude: Enabled: false -RSpec/FactoryBot/CreateList: +FactoryBot/CreateList: Enabled: false Lint/ConstantDefinitionInBlock: diff --git a/Gemfile.lock b/Gemfile.lock index 9672f98..cb2abb9 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,12 +1,13 @@ PATH remote: . specs: - root-ruby-style (0.0.17) + root-ruby-style (0.0.18) activesupport (>= 5.0, < 8) - rubocop (> 1.60) - rubocop-performance (= 1.5.2) - rubocop-rails (~> 2.12.0) - rubocop-rspec (~> 1.38.1) + rubocop (~> 1.75) + rubocop-factory_bot (~> 2.27.1) + rubocop-performance (~> 1.25.0) + rubocop-rails (~> 2.31.0) + rubocop-rspec (~> 3.6.0) GEM remote: https://rubygems.org/ @@ -27,11 +28,11 @@ GEM base64 (0.2.0) benchmark (0.4.0) bigdecimal (3.1.9) - byebug (11.1.3) + byebug (12.0.0) coderay (1.1.3) concurrent-ruby (1.3.5) - connection_pool (2.5.0) - diff-lcs (1.5.1) + connection_pool (2.5.1) + diff-lcs (1.6.1) drb (2.2.1) i18n (1.14.7) concurrent-ruby (~> 1.0) @@ -41,26 +42,26 @@ GEM logger (1.7.0) method_source (1.1.0) minitest (5.25.5) - parallel (1.26.3) - parser (3.3.7.4) + parallel (1.27.0) + parser (3.3.8.0) ast (~> 2.4.1) racc prism (1.4.0) - pry (0.14.2) + pry (0.15.2) coderay (~> 1.1) method_source (~> 1.0) - pry-byebug (3.10.1) - byebug (~> 11.0) - pry (>= 0.13, < 0.15) + pry-byebug (3.11.0) + byebug (~> 12.0) + pry (>= 0.13, < 0.16) racc (1.8.1) - rack (3.1.12) + rack (3.1.13) rainbow (3.1.1) regexp_parser (2.10.0) rspec (3.13.0) rspec-core (~> 3.13.0) rspec-expectations (~> 3.13.0) rspec-mocks (~> 3.13.0) - rspec-core (3.13.2) + rspec-core (3.13.3) rspec-support (~> 3.13.0) rspec-expectations (3.13.3) diff-lcs (>= 1.2.0, < 2.0) @@ -80,17 +81,25 @@ GEM rubocop-ast (>= 1.44.0, < 2.0) ruby-progressbar (~> 1.7) unicode-display_width (>= 2.4.0, < 4.0) - rubocop-ast (1.44.0) + rubocop-ast (1.44.1) parser (>= 3.3.7.2) prism (~> 1.4) - rubocop-performance (1.5.2) - rubocop (>= 0.71.0) - rubocop-rails (2.12.4) + rubocop-factory_bot (2.27.1) + lint_roller (~> 1.1) + rubocop (~> 1.72, >= 1.72.1) + rubocop-performance (1.25.0) + lint_roller (~> 1.1) + rubocop (>= 1.75.0, < 2.0) + rubocop-ast (>= 1.38.0, < 2.0) + rubocop-rails (2.31.0) activesupport (>= 4.2.0) + lint_roller (~> 1.1) rack (>= 1.1) - rubocop (>= 1.7.0, < 2.0) - rubocop-rspec (1.38.1) - rubocop (>= 0.68.1) + rubocop (>= 1.75.0, < 2.0) + rubocop-ast (>= 1.38.0, < 2.0) + rubocop-rspec (3.6.0) + lint_roller (~> 1.1) + rubocop (~> 1.72, >= 1.72.1) ruby-progressbar (1.13.0) securerandom (0.4.1) tzinfo (2.0.6) @@ -106,7 +115,7 @@ PLATFORMS DEPENDENCIES pry-byebug root-ruby-style! - rspec (~> 3.8) + rspec (~> 3.13) BUNDLED WITH - 2.2.22 + 2.6.8 diff --git a/lib/rubocop/cop/root_cops/avoid_ruby_prof.rb b/lib/rubocop/cop/root_cops/avoid_ruby_prof.rb index fb9a319..2bddcb6 100644 --- a/lib/rubocop/cop/root_cops/avoid_ruby_prof.rb +++ b/lib/rubocop/cop/root_cops/avoid_ruby_prof.rb @@ -3,7 +3,7 @@ module RuboCop module Cop module RootCops - class AvoidRubyProf < Cop + class AvoidRubyProf < RuboCop::Cop::Base ERROR = ":ruby_prof is for local use only and should not be committed." FILE_NAME_MATCHER = /_spec\.rb\z/ @@ -11,14 +11,14 @@ class AvoidRubyProf < Cop (send nil? {:describe :context :it} ...) PATTERN - def investigate(processed_source) + def on_new_investigation @in_spec_file = processed_source.file_path =~ FILE_NAME_MATCHER end def on_sym(node) return unless @in_spec_file && node.value == :ruby_prof && spec_block?(node.parent) - add_offense(node, location: :expression, message: ERROR) + add_offense(node.loc.expression, message: ERROR) end end end diff --git a/lib/rubocop/cop/root_cops/envvar_assignment.rb b/lib/rubocop/cop/root_cops/envvar_assignment.rb index 87ef26a..5ff8941 100644 --- a/lib/rubocop/cop/root_cops/envvar_assignment.rb +++ b/lib/rubocop/cop/root_cops/envvar_assignment.rb @@ -1,12 +1,12 @@ module RuboCop module Cop module RootCops - class EnvvarAssignment < Cop + class EnvvarAssignment < RuboCop::Cop::Base MSG = "Do not assign an ENVVAR to a constant. Assigning an ENVVAR to a constant has unexpected behavior when used with set_environment_variable. Instead, return the ENVVAR from a method".freeze def_node_search :envvar_assignment?, "(send (const _ :ENVVARS) :[] (:str _))" - def investigate(processed_source) + def on_new_investigation file_path = processed_source.file_path @is_initializer_file = file_path.include?("/initializers/") end @@ -17,7 +17,7 @@ def on_casgn(node) return if value.nil? return if @is_initializer_file - add_offense(node, location: :expression, message: MSG) if envvar_assignment?(value) + add_offense(node.loc.expression, message: MSG) if envvar_assignment?(value) end def on_or_asgn(node) @@ -26,7 +26,7 @@ def on_or_asgn(node) return unless lhs&.casgn_type? return if @is_initializer_file - add_offense(node, location: :expression, message: MSG) if envvar_assignment?(value) + add_offense(node.loc.expression, message: MSG) if envvar_assignment?(value) end end end diff --git a/lib/rubocop/cop/root_cops/eq_be_eql.rb b/lib/rubocop/cop/root_cops/eq_be_eql.rb index d5c8e53..e125b26 100644 --- a/lib/rubocop/cop/root_cops/eq_be_eql.rb +++ b/lib/rubocop/cop/root_cops/eq_be_eql.rb @@ -1,5 +1,5 @@ module RootCops - class EqBeEql < ::RuboCop::Cop::Cop + class EqBeEql < ::RuboCop::Cop::Base MSG = "Prefer `eq` over `be` and `eql` when checking booleans, numbers, symbols, strings and nil".freeze def_node_matcher :be_or_eql, <<-PATTERN @@ -19,11 +19,11 @@ class EqBeEql < ::RuboCop::Cop::Cop def on_send(node) be_or_eql(node) do |offending_node| - add_offense(offending_node, location: :selector) + add_offense(offending_node.loc.selector) end be_true_false_nil(node) do |offending_node| - add_offense(offending_node, location: :selector) + add_offense(offending_node.loc.selector) end end end diff --git a/lib/rubocop/cop/root_cops/factories/factory_file_name.rb b/lib/rubocop/cop/root_cops/factories/factory_file_name.rb index cf9cd6e..f65c1ef 100644 --- a/lib/rubocop/cop/root_cops/factories/factory_file_name.rb +++ b/lib/rubocop/cop/root_cops/factories/factory_file_name.rb @@ -5,8 +5,8 @@ module RuboCop module Cop module RootCops module Factories - class FactoryFileName < Cop - def investigate(processed_source) + class FactoryFileName < RuboCop::Cop::Base + def on_new_investigation file_path = processed_source.buffer.name @base_file_name = File.basename(file_path, ".rb") @base_file_name_last_word = @base_file_name.split("_").last @@ -18,8 +18,7 @@ def on_send(node) if receiver_name == :FactoryBot && method_name == :define && Helpers::Factories.file_name_has_error?(@base_file_name) add_offense( - node, - location: :expression, + node.loc.expression, severity: :fatal, message: "Factory file name should be plural (#{@base_file_name.pluralize})." ) diff --git a/lib/rubocop/cop/root_cops/factories/factory_name.rb b/lib/rubocop/cop/root_cops/factories/factory_name.rb index 3f7dc39..897e5f2 100644 --- a/lib/rubocop/cop/root_cops/factories/factory_name.rb +++ b/lib/rubocop/cop/root_cops/factories/factory_name.rb @@ -5,10 +5,10 @@ module RuboCop module Cop module RootCops module Factories - class FactoryName < Cop + class FactoryName < RuboCop::Cop::Base SYSTEM_IN_PATH = %r{systems/([^/]+)/} - def investigate(processed_source) + def on_new_investigation file_path = processed_source.buffer.name system_name_match = file_path.match(SYSTEM_IN_PATH) @@ -36,16 +36,14 @@ def on_send(node) if factory_name_array.size > 1 if factory_name_array[0] != @base_file_name add_offense( - node, - location: :expression, + node.loc.expression, message: "Factory name uses incorrect prefix, should be '#{@base_file_name}__#{factory_name_array[1]}'." ) end elsif (factory_name_body != @base_file_name_body) || (factory_name_last_word.pluralize != @base_file_name_last_word) add_offense( - node, - location: :expression, + node.loc.expression, message: "Factory should be in own file or be named the singular form of the file name. OR group closely related factories in the same file and prefix their names with '#{@base_file_name}__'." ) end diff --git a/lib/rubocop/cop/root_cops/job_has_queue.rb b/lib/rubocop/cop/root_cops/job_has_queue.rb index 88ffee1..9c0c86a 100644 --- a/lib/rubocop/cop/root_cops/job_has_queue.rb +++ b/lib/rubocop/cop/root_cops/job_has_queue.rb @@ -1,7 +1,7 @@ module RuboCop module Cop module RootCops - class JobHasQueue < Cop + class JobHasQueue < RuboCop::Cop::Base MESSAGE = "Configure the job to run in a specific queue using queue_as, sharded_queue_as or a whitelisted mixin.".freeze MIXIN_ALLOW_LIST = %i[LookupQueueConcern NewBusinessQuoteCreationQueueConcern PricingBackfillQueueConcern RatesQueueConcern MonitoringConcern].freeze QUEUEING_OPTIONS = %i[queue_as sharded_queue_as].freeze @@ -12,7 +12,7 @@ def on_class(node) send_descendants = node.descendants.select(&:send_type?) unless send_descendants.any? { |d| QUEUEING_OPTIONS.include?(d.to_a[1]) || _whitelisted_mixin?(d) } - add_offense(node, location: :expression, message: MESSAGE) + add_offense(node.loc.expression, message: MESSAGE) end end diff --git a/lib/rubocop/cop/root_cops/must_include.rb b/lib/rubocop/cop/root_cops/must_include.rb index ff90b9f..cba8904 100644 --- a/lib/rubocop/cop/root_cops/must_include.rb +++ b/lib/rubocop/cop/root_cops/must_include.rb @@ -20,22 +20,22 @@ module RootCops # include ClaimsJobConcern # end - class MustInclude < Cop + class MustInclude < RuboCop::Cop::Base # entirely so i can stub this in the tests def self.expand_path(filename) File.expand_path(filename) end - def investigate(processed_source) + def on_new_investigation @source_file_path = self.class.expand_path(processed_source.buffer.name) @module_to_include = module_to_include_for_current_file @proper_module_is_included = false @class_node = nil end - def investigate_post_walk(_processed_source) + def on_investigation_end if search_for_inclusion? && !@proper_module_is_included - add_offense(@class_node, location: :expression, message: "Classes in this directory must include #{@module_to_include} module") + add_offense(@class_node.loc.expression, message: "Classes in this directory must include #{@module_to_include} module") end end diff --git a/lib/rubocop/cop/root_cops/must_inherit.rb b/lib/rubocop/cop/root_cops/must_inherit.rb index 4a501a1..ad6d337 100644 --- a/lib/rubocop/cop/root_cops/must_inherit.rb +++ b/lib/rubocop/cop/root_cops/must_inherit.rb @@ -25,13 +25,13 @@ module RootCops # - ResqueJob # - ShoryukenJob - class MustInherit < Cop + class MustInherit < RuboCop::Cop::Base # entirely so i can stub this in the tests def self.expand_path(filename) File.expand_path(filename) end - def investigate(processed_source) + def on_new_investigation @source_file_path = self.class.expand_path(processed_source.buffer.name) end @@ -41,7 +41,7 @@ def on_class(node) class_name = node.identifier.const_name superclass_name = node.parent_class&.const_name unless class_name_match?(class_name, superclass_name, class_options) - add_offense(node, location: :expression, message: "Classes in this directory must inherit from #{class_options_to_s(class_options)}") + add_offense(node.loc.expression, message: "Classes in this directory must inherit from #{class_options_to_s(class_options)}") end end diff --git a/lib/rubocop/cop/root_cops/no_backfills_in_data_migration.rb b/lib/rubocop/cop/root_cops/no_backfills_in_data_migration.rb index 837c6fd..1b2fe0c 100644 --- a/lib/rubocop/cop/root_cops/no_backfills_in_data_migration.rb +++ b/lib/rubocop/cop/root_cops/no_backfills_in_data_migration.rb @@ -1,7 +1,7 @@ module RuboCop module Cop module RootCops - class NoBackfillsInDataMigration < Cop + class NoBackfillsInDataMigration < RuboCop::Cop::Base ACTIVE_RECORD_REGEX = /ActiveRecord::Migration\[\d\.\d\]/ COLLAPSED_MIGRATION_CLASSNAME = "CollapsedMigrations".freeze MESSAGE = "Backfills should happen outside of database migrations".freeze @@ -43,7 +43,7 @@ def on_send(node) return if @is_collapsed_migration_class && method_name.to_s == "execute" if FORBIDDEN_METHODS.include?(method_name) - add_offense(node, location: :expression, message: MESSAGE) + add_offense(node.loc.expression, message: MESSAGE) end end end diff --git a/lib/rubocop/cop/root_cops/no_index_on_add_column.rb b/lib/rubocop/cop/root_cops/no_index_on_add_column.rb index 1addec4..d32c0a9 100644 --- a/lib/rubocop/cop/root_cops/no_index_on_add_column.rb +++ b/lib/rubocop/cop/root_cops/no_index_on_add_column.rb @@ -1,7 +1,7 @@ module RuboCop module Cop module RootCops - class NoIndexOnAddColumn < Cop + class NoIndexOnAddColumn < RuboCop::Cop::Base ACTIVE_RECORD_REGEX = /ActiveRecord::Migration\[\d\.\d\]/ COLLAPSED_MIGRATION_CLASSNAME = "CollapsedMigrations".freeze MESSAGE = "Do not use :index option on add_column".freeze @@ -23,7 +23,7 @@ def on_send(node) keys = last.keys.collect(&:value) - add_offense(node, location: :expression, message: MESSAGE) if keys.include?(:index) + add_offense(node.loc.expression, message: MESSAGE) if keys.include?(:index) end end end diff --git a/lib/rubocop/cop/root_cops/no_tracers.rb b/lib/rubocop/cop/root_cops/no_tracers.rb index 28415d2..8767ead 100644 --- a/lib/rubocop/cop/root_cops/no_tracers.rb +++ b/lib/rubocop/cop/root_cops/no_tracers.rb @@ -1,5 +1,5 @@ module RootCops - class NoTracers < ::RuboCop::Cop::Cop + class NoTracers < ::RuboCop::Cop::Base MSG = "Remove all `Tracer` occurrences".freeze def_node_matcher :tracer, <<-PATTERN @@ -7,7 +7,7 @@ class NoTracers < ::RuboCop::Cop::Cop PATTERN def on_send(node) - add_offense(node, location: :selector) if tracer(node) + add_offense(node.loc.selector) if tracer(node) end end end diff --git a/lib/rubocop/cop/root_cops/private_methods/called_private_method.rb b/lib/rubocop/cop/root_cops/private_methods/called_private_method.rb index b222093..6dbb239 100644 --- a/lib/rubocop/cop/root_cops/private_methods/called_private_method.rb +++ b/lib/rubocop/cop/root_cops/private_methods/called_private_method.rb @@ -2,13 +2,13 @@ module RuboCop module Cop module RootCops module PrivateMethods - class CalledPrivateMethod < Cop + class CalledPrivateMethod < RuboCop::Cop::Base MSG = "Do not call private class methods from outside the class. Make the method public if necessary.".freeze def on_send(node) receiver, method_name = *node if receiver && (method_name =~ /^_/) - add_offense(node, location: :selector, message: MSG) + add_offense(node.loc.selector, message: MSG) end end end diff --git a/lib/rubocop/cop/root_cops/private_methods/called_protected.rb b/lib/rubocop/cop/root_cops/private_methods/called_protected.rb index 2e6622a..66c86b4 100644 --- a/lib/rubocop/cop/root_cops/private_methods/called_protected.rb +++ b/lib/rubocop/cop/root_cops/private_methods/called_protected.rb @@ -2,13 +2,13 @@ module RuboCop module Cop module RootCops module PrivateMethods - class CalledProtected < Cop + class CalledProtected < RuboCop::Cop::Base MSG = "Do not use protected. Use private instead.".freeze def on_send(node) _receiver, method_name = *node if method_name.to_s == "protected" - add_offense(node, location: :selector, message: MSG) + add_offense(node.loc.selector, message: MSG) end end end diff --git a/lib/rubocop/cop/root_cops/private_methods/underscore_prefix.rb b/lib/rubocop/cop/root_cops/private_methods/underscore_prefix.rb index 5314507..71eddfe 100644 --- a/lib/rubocop/cop/root_cops/private_methods/underscore_prefix.rb +++ b/lib/rubocop/cop/root_cops/private_methods/underscore_prefix.rb @@ -2,7 +2,7 @@ module RuboCop module Cop module RootCops module PrivateMethods - class UnderscorePrefix < Cop + class UnderscorePrefix < RuboCop::Cop::Base NEED_UNDERSCORE = "Prefix private method names with an underscore. If method should be public, move it above the private scope.".freeze NEED_PRIVATE = "Include a private declaration above the private methods.".freeze @@ -50,10 +50,10 @@ def _check_for_instance_method(node) return if method_name.nil? if method_name =~ /^_/ && @visible - add_offense(node, location: :expression, message: NEED_PRIVATE) + add_offense(node.loc.expression, message: NEED_PRIVATE) end if method_name !~ /^_/ && !@visible - add_offense(node, location: :expression, message: NEED_UNDERSCORE) + add_offense(node.loc.expression, message: NEED_UNDERSCORE) end end diff --git a/lib/rubocop/cop/root_cops/raise_i18n.rb b/lib/rubocop/cop/root_cops/raise_i18n.rb index 25a7f6f..e4227bc 100644 --- a/lib/rubocop/cop/root_cops/raise_i18n.rb +++ b/lib/rubocop/cop/root_cops/raise_i18n.rb @@ -1,14 +1,14 @@ module RuboCop module Cop module RootCops - class RaiseI18n < Cop + class RaiseI18n < RuboCop::Cop::Base MESSAGE = "Use raise with I18n instead of hard-coded strings".freeze def on_send(node) return unless node.command?(:raise) node.child_nodes.each do |child| - add_offense(child, location: :expression, message: MESSAGE) if _str?(child) + add_offense(child.loc.expression, message: MESSAGE) if _str?(child) end end diff --git a/lib/rubocop/cop/root_cops/retry_on_warning.rb b/lib/rubocop/cop/root_cops/retry_on_warning.rb index 54bc5df..ede8f71 100644 --- a/lib/rubocop/cop/root_cops/retry_on_warning.rb +++ b/lib/rubocop/cop/root_cops/retry_on_warning.rb @@ -1,14 +1,14 @@ module RuboCop module Cop module RootCops - class RetryOnWarning < Cop + class RetryOnWarning < RuboCop::Cop::Base MSG = %(Use care when implementing "retry_on". Look for side effects on the retried job before disabling warning. NOTE: Jobs running on SQS cannot wait longer than 15 minutes for retry.).freeze def on_send(node) _receiver, method_name = *node if method_name == :retry_on - add_offense(node, location: :expression, message: MSG) + add_offense(node.location.expression, message: MSG) end end end diff --git a/lib/rubocop/cop/root_cops/shared_context_name.rb b/lib/rubocop/cop/root_cops/shared_context_name.rb index c49f603..cbe06a8 100644 --- a/lib/rubocop/cop/root_cops/shared_context_name.rb +++ b/lib/rubocop/cop/root_cops/shared_context_name.rb @@ -1,10 +1,10 @@ module RuboCop module Cop module RootCops - class SharedContextName < Cop + class SharedContextName < RuboCop::Cop::Base PREFIX_OR_MATCH = "Shared context name should match file name or be prefixed with (filename)__".freeze - def investigate(processed_source) + def on_new_investigation file_path = processed_source.buffer.name return unless /shared_contexts/.match?(file_path) @@ -23,7 +23,7 @@ def on_send(node) method_args = *args[0] context_name = method_args[0].to_s unless context_name == @base_name || context_name.start_with?(@expected_context_prefix) - add_offense(node, location: :expression, message: PREFIX_OR_MATCH) + add_offense(node.loc.expression, message: PREFIX_OR_MATCH) end end end diff --git a/lib/rubocop/cop/root_cops/spec_file_name.rb b/lib/rubocop/cop/root_cops/spec_file_name.rb index 820cf53..c1c967c 100644 --- a/lib/rubocop/cop/root_cops/spec_file_name.rb +++ b/lib/rubocop/cop/root_cops/spec_file_name.rb @@ -1,10 +1,10 @@ module RuboCop module Cop module RootCops - class SpecFileName < Cop + class SpecFileName < RuboCop::Cop::Base FILE_NAME_ERROR = "Specs should be in files ending in _spec.rb".freeze - def investigate(processed_source) + def on_new_investigation @file_path = processed_source.buffer.name end @@ -14,7 +14,7 @@ def on_send(node) return unless receiver_name == :RSpec && method_name == :describe unless @file_path.end_with?("_spec.rb") - add_offense(node, location: :expression, message: FILE_NAME_ERROR) + add_offense(node.loc.expression, message: FILE_NAME_ERROR) end end end diff --git a/lib/rubocop/cop/root_cops/unnecessary_aggregate_failures.rb b/lib/rubocop/cop/root_cops/unnecessary_aggregate_failures.rb index 673ec6c..f81d7f0 100644 --- a/lib/rubocop/cop/root_cops/unnecessary_aggregate_failures.rb +++ b/lib/rubocop/cop/root_cops/unnecessary_aggregate_failures.rb @@ -1,7 +1,7 @@ module RuboCop module Cop module RootCops - class UnnecessaryAggregateFailures < Cop + class UnnecessaryAggregateFailures < RuboCop::Cop::Base ERROR = ":aggregate_failures is unnecessary, it is enabled by default.".freeze def_node_matcher :it_block?, <<~PATTERN @@ -11,7 +11,7 @@ class UnnecessaryAggregateFailures < Cop def on_sym(node) return unless node.value == :aggregate_failures && it_block?(node.parent) - add_offense(node, location: :expression, message: ERROR) + add_offense(node.loc.expression, message: ERROR) end end end diff --git a/lib/rubocop/cop/root_cops/up_and_down_or_change.rb b/lib/rubocop/cop/root_cops/up_and_down_or_change.rb index 2537153..5542b55 100644 --- a/lib/rubocop/cop/root_cops/up_and_down_or_change.rb +++ b/lib/rubocop/cop/root_cops/up_and_down_or_change.rb @@ -1,7 +1,7 @@ module RuboCop module Cop module RootCops - class UpAndDownOrChange < Cop + class UpAndDownOrChange < RuboCop::Cop::Base MSG = "Migration must have either #change or #up/#down".freeze def on_class(node) @@ -12,7 +12,7 @@ def on_class(node) return if methods.include?(:change) return if methods.include?(:up) && methods.include?(:down) - add_offense(node, location: :expression, message: MSG) + add_offense(node.loc.expression, message: MSG) end def_node_matcher :active_record_migration?, <<~PATTERN diff --git a/lib/rubocop/cop/root_cops/use_before_action.rb b/lib/rubocop/cop/root_cops/use_before_action.rb index 54112bd..167475a 100644 --- a/lib/rubocop/cop/root_cops/use_before_action.rb +++ b/lib/rubocop/cop/root_cops/use_before_action.rb @@ -1,13 +1,13 @@ module RuboCop module Cop module RootCops - class UseBeforeAction < Cop + class UseBeforeAction < RuboCop::Cop::Base MSG = "Use #before_action instead of #before_filter.".freeze def on_send(node) _receiver, method_name = *node if method_name == :before_filter - add_offense(node, location: :expression, message: MSG) + add_offense(node.loc.expression, message: MSG) end end end diff --git a/lib/rubocop/cop/root_cops/use_detect.rb b/lib/rubocop/cop/root_cops/use_detect.rb index d025adc..0d1a634 100644 --- a/lib/rubocop/cop/root_cops/use_detect.rb +++ b/lib/rubocop/cop/root_cops/use_detect.rb @@ -12,7 +12,7 @@ module RootCops # [1,2,3].detect { |x| x.even? } # [1,2,3].detect(&:even?) - class UseDetect < Cop + class UseDetect < RuboCop::Cop::Base MSG = "Use #detect instead of #find.".freeze def anything_other_than_class_constant?(node) @@ -32,7 +32,7 @@ def anything_other_than_class_constant?(node) def on_block(node) find_called_with_a_block?(node) do - add_offense(node, location: :expression, message: MSG) + add_offense(node.loc.expression, message: MSG) end end @@ -45,7 +45,7 @@ def on_block(node) def on_send(node) find_called_with_a_block_as_proc?(node) do - add_offense(node, location: :expression, message: MSG) + add_offense(node.loc.expression, message: MSG) end end end diff --git a/lib/rubocop/cop/root_cops/use_envvars.rb b/lib/rubocop/cop/root_cops/use_envvars.rb index 50ae452..bd54603 100644 --- a/lib/rubocop/cop/root_cops/use_envvars.rb +++ b/lib/rubocop/cop/root_cops/use_envvars.rb @@ -1,7 +1,7 @@ module RuboCop module Cop module RootCops - class UseEnvvars < Cop + class UseEnvvars < RuboCop::Cop::Base MSG = %(Use ENVVARS["..."] instead of ENV["..."].).freeze def on_send(node) @@ -9,7 +9,7 @@ def on_send(node) _, name = *receiver if name == :ENV && method_name == :[] - add_offense(node, location: :expression, message: MSG) + add_offense(node.loc.expression, message: MSG) end end end diff --git a/lib/rubocop/cop/root_cops/use_lonely_operator.rb b/lib/rubocop/cop/root_cops/use_lonely_operator.rb index 065aa07..e3bac71 100644 --- a/lib/rubocop/cop/root_cops/use_lonely_operator.rb +++ b/lib/rubocop/cop/root_cops/use_lonely_operator.rb @@ -1,14 +1,14 @@ module RuboCop module Cop module RootCops - class UseLonelyOperator < Cop + class UseLonelyOperator < RuboCop::Cop::Base MSG = "Use the lonely operator foo&.bar instead of foo.try!(:bar)".freeze def on_send(node) _receiver, method_name = *node if method_name == :try! && node.arguments? - add_offense(node, location: :expression, message: MSG) + add_offense(node.loc.selector, message: MSG) end end end diff --git a/root-ruby-style.gemspec b/root-ruby-style.gemspec index cb00ad4..0f65653 100644 --- a/root-ruby-style.gemspec +++ b/root-ruby-style.gemspec @@ -5,7 +5,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) Gem::Specification.new do |gem| gem.name = "root-ruby-style" - gem.version = "0.0.17" + gem.version = "0.0.18" gem.authors = ["Root Devs"] gem.email = ["devs@joinroot.com"] @@ -18,13 +18,14 @@ Gem::Specification.new do |gem| end gem.add_runtime_dependency "activesupport", ">= 5.0", "< 8" - gem.add_runtime_dependency "rubocop", "> 1.60" - gem.add_runtime_dependency "rubocop-performance", "1.5.2" - gem.add_runtime_dependency "rubocop-rails", "~> 2.12.0" - gem.add_runtime_dependency "rubocop-rspec", "~> 1.38.1" + gem.add_runtime_dependency "rubocop", "~> 1.75" + gem.add_runtime_dependency "rubocop-factory_bot", "~> 2.27.1" + gem.add_runtime_dependency "rubocop-performance", "~> 1.25.0" + gem.add_runtime_dependency "rubocop-rails", "~> 2.31.0" + gem.add_runtime_dependency "rubocop-rspec", "~> 3.6.0" gem.add_development_dependency "pry-byebug" - gem.add_development_dependency "rspec", "~> 3.8" + gem.add_development_dependency "rspec", "~> 3.13" end # rubocop:enable RootCops/UseEnvvars diff --git a/spec/rubocop/cop/root_cops/envvar_assignment_spec.rb b/spec/rubocop/cop/root_cops/envvar_assignment_spec.rb index ac79426..c66eb4d 100644 --- a/spec/rubocop/cop/root_cops/envvar_assignment_spec.rb +++ b/spec/rubocop/cop/root_cops/envvar_assignment_spec.rb @@ -198,36 +198,22 @@ module MyModule end context "when the file is an initializer" do - let(:processed_source) { parse_source(source) } let(:filename) { "/config/initializers/file.rb" } - before do - allow(processed_source.buffer).to receive(:name).and_return(filename) - _investigate(cop, processed_source) - end - context "ENVVARS constant assignment" do - let(:source) do - <<~RUBY + it "does not report an offense" do + expect_no_offenses(<<~RUBY, filename) CONSTANT = ENVVARS["VAR"] RUBY end - - it "does not report an offense" do - expect(cop.offenses.size).to eq(0) - end end context "ENVVARS constant ||= assignment" do - let(:source) do - <<~RUBY + it "does not report an offense" do + expect_no_offenses(<<~RUBY, filename) CONSTANT ||= ENVVARS["VAR"] RUBY end - - it "does not report an offense" do - expect(cop.offenses.size).to eq(0) - end end end end diff --git a/spec/rubocop/cop/root_cops/support/shared_examples.rb b/spec/rubocop/cop/root_cops/support/shared_examples.rb index ab0be9c..3612d12 100644 --- a/spec/rubocop/cop/root_cops/support/shared_examples.rb +++ b/spec/rubocop/cop/root_cops/support/shared_examples.rb @@ -1,12 +1,11 @@ RSpec.shared_examples_for "registers an offense" do |offending_form, error_message| it "when using '#{offending_form}'" do - inspect_source(<<-RUBY) + offenses = inspect_source(<<-RUBY) #{offending_form} RUBY - expect(cop.offenses.size).to eq(1) - offense = cop.offenses.first - expect(offense.message).to eq(error_message) + expect(offenses.size).to eq(1) + expect(offenses.first.message).to eq(error_message) end end diff --git a/spec/rubocop/cop/root_cops/use_lonely_operator_spec.rb b/spec/rubocop/cop/root_cops/use_lonely_operator_spec.rb index 0fe0fef..2cd98e7 100644 --- a/spec/rubocop/cop/root_cops/use_lonely_operator_spec.rb +++ b/spec/rubocop/cop/root_cops/use_lonely_operator_spec.rb @@ -4,7 +4,7 @@ it "reports an offense for try!" do expect_offense(<<~RUBY) foo.try!(:to_s) - ^^^^^^^^^^^^^^^ RootCops/UseLonelyOperator: Use the lonely operator foo&.bar instead of foo.try!(:bar) + ^^^^ RootCops/UseLonelyOperator: Use the lonely operator foo&.bar instead of foo.try!(:bar) RUBY end