Skip to content

Commit

Permalink
Add autocorrection for Rails/ReflectionClassName cop
Browse files Browse the repository at this point in the history
  • Loading branch information
tejasbubane committed Jan 4, 2023
1 parent 05be11b commit 504f14d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#299](https://github.com/rubocop/rubocop-rails/pull/299): Add autocorrection for `Rails/ReflectionClassName`. ([@tejasbubane][])
18 changes: 17 additions & 1 deletion lib/rubocop/cop/rails/reflection_class_name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ module Rails
# # good
# has_many :accounts, class_name: 'Account'
class ReflectionClassName < Base
extend AutoCorrector

MSG = 'Use a string value for `class_name`.'
RESTRICT_ON_SEND = %i[has_many has_one belongs_to].freeze
ALLOWED_REFLECTION_CLASS_TYPES = %i[dstr str sym].freeze
Expand All @@ -32,12 +34,18 @@ class ReflectionClassName < Base
(pair (sym :class_name) #reflection_class_value?)
PATTERN

def_node_matcher :const_or_string, <<~PATTERN
{$(const nil? _) (send $(const nil? _) :name) (send $(const nil? _) :to_s)}
PATTERN

def on_send(node)
association_with_reflection(node) do |reflection_class_name|
return if reflection_class_name.value.send_type? && reflection_class_name.value.receiver.nil?
return if reflection_class_name.value.lvar_type? && str_assigned?(reflection_class_name)

add_offense(reflection_class_name.loc.expression)
add_offense(reflection_class_name.loc.expression) do |corrector|
autocorrect(corrector, reflection_class_name)
end
end
end

Expand All @@ -64,6 +72,14 @@ def reflection_class_value?(class_value)
!ALLOWED_REFLECTION_CLASS_TYPES.include?(class_value.type)
end
end

def autocorrect(corrector, class_config)
class_value = class_config.value
replacement = const_or_string(class_value)
return unless replacement.present?

corrector.replace(class_value, replacement.source.inspect)
end
end
end
end
Expand Down
20 changes: 20 additions & 0 deletions spec/rubocop/cop/rails/reflection_class_name_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,54 @@
has_many :accounts, class_name: Account, foreign_key: :account_id
^^^^^^^^^^^^^^^^^^^ Use a string value for `class_name`.
RUBY

expect_correction(<<~RUBY)
has_many :accounts, class_name: "Account", foreign_key: :account_id
RUBY
end

it '.name' do
expect_offense(<<~RUBY)
has_many :accounts, class_name: Account.name
^^^^^^^^^^^^^^^^^^^^^^^^ Use a string value for `class_name`.
RUBY

expect_correction(<<~RUBY)
has_many :accounts, class_name: "Account"
RUBY
end

it '.to_s' do
expect_offense(<<~RUBY)
has_many :accounts, class_name: Account.to_s
^^^^^^^^^^^^^^^^^^^^^^^^ Use a string value for `class_name`.
RUBY

expect_correction(<<~RUBY)
has_many :accounts, class_name: "Account"
RUBY
end

it 'has_one' do
expect_offense(<<~RUBY)
has_one :account, class_name: Account
^^^^^^^^^^^^^^^^^^^ Use a string value for `class_name`.
RUBY

expect_correction(<<~RUBY)
has_one :account, class_name: "Account"
RUBY
end

it 'belongs_to' do
expect_offense(<<~RUBY)
belongs_to :account, class_name: Account
^^^^^^^^^^^^^^^^^^^ Use a string value for `class_name`.
RUBY

expect_correction(<<~RUBY)
belongs_to :account, class_name: "Account"
RUBY
end
end

Expand Down

0 comments on commit 504f14d

Please sign in to comment.