Skip to content

Commit

Permalink
Add autocorrection for Rails/ReflectionClassName
Browse files Browse the repository at this point in the history
  • Loading branch information
tejasbubane committed Jul 22, 2020
1 parent fa6915f commit dfc20d7
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

* [#297](https://github.com/rubocop-hq/rubocop-rails/pull/297): Handle an upstream Ruby issue where the DidYouMean module is not available, which would break the `Rails/UnknownEnv` cop. ([@taylorthurlow][])

### New features

* [#299](https://github.com/rubocop-hq/rubocop-rails/pull/299): Add autocorrection for `Rails/ReflectionClassName`. ([@tejasbubane][])

## 2.7.0 (2020-07-21)

### New features
Expand Down
2 changes: 1 addition & 1 deletion docs/modules/ROOT/pages/cops_rails.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2903,7 +2903,7 @@ end

| Enabled
| Yes
| No
| Yes
| 0.64
| -
|===
Expand Down
20 changes: 16 additions & 4 deletions lib/rubocop/cop/rails/reflection_class_name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ module Rails
#
# # good
# has_many :accounts, class_name: 'Account'
class ReflectionClassName < Cop
class ReflectionClassName < Base
extend AutoCorrector

MSG = 'Use a string value for `class_name`.'

def_node_matcher :association_with_reflection, <<~PATTERN
Expand All @@ -23,14 +25,24 @@ class ReflectionClassName < Cop
PATTERN

def_node_matcher :reflection_class_name, <<~PATTERN
(pair (sym :class_name) [!dstr !str !sym])
(pair (sym :class_name) $[!dstr !str !sym])
PATTERN

def on_send(node)
association_with_reflection(node) do |reflection_class_name|
add_offense(node, location: reflection_class_name.loc.expression)
association_with_reflection(node) do |reflection|
add_offense(reflection) do |corrector|
class_node = reflection_class_name(reflection)
corrector.replace(class_node, replacement(class_node))
end
end
end

private

def replacement(class_node)
replacement_node = class_node.send_type? ? class_node.children.first : class_node
replacement_node.source.inspect
end
end
end
end
Expand Down
16 changes: 16 additions & 0 deletions spec/rubocop/cop/rails/reflection_class_name_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,43 @@
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 '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 dfc20d7

Please sign in to comment.