forked from rubocop/rubocop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request rubocop#6704 from Bhacaz/add_cop_relfection_class_…
…name_string Added ReflectionClassNameString cop
- Loading branch information
Showing
7 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Rails | ||
# This cop checks if the value of the option `class_name`, in | ||
# the definition of a reflection is a string. | ||
# | ||
# @example | ||
# # bad | ||
# has_many :accounts, class_name: Account | ||
# has_many :accounts, class_name: Account.name | ||
# | ||
# # good | ||
# has_many :accounts, class_name: 'Account' | ||
class ReflectionClassName < Cop | ||
MSG = 'Use a string has value for a class_name.'.freeze | ||
|
||
def_node_matcher :association_with_options?, <<-PATTERN | ||
(send nil? {:has_many :has_one :belongs_to} _ (hash $...)) | ||
PATTERN | ||
|
||
def_node_search :reflection_class_name, <<-PATTERN | ||
(pair (sym :class_name) !str) | ||
PATTERN | ||
|
||
def on_send(node) | ||
return unless association_with_options?(node) | ||
|
||
reflection_class_name = reflection_class_name(node).first | ||
return unless reflection_class_name | ||
|
||
add_offense(node, location: reflection_class_name.loc.expression) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Rails::ReflectionClassName do | ||
subject(:cop) { described_class.new(config) } | ||
|
||
let(:config) { RuboCop::Config.new } | ||
|
||
context "registers an offense when using `foreign_key: 'account_id'`" do | ||
it 'has_many' do | ||
expect_offense(<<-RUBY.strip_indent) | ||
has_many :accounts, class_name: Account, foreign_key: :account_id | ||
^^^^^^^^^^^^^^^^^^^ Use a string has value for a class_name. | ||
RUBY | ||
end | ||
|
||
it '.name' do | ||
expect_offense(<<-RUBY.strip_indent) | ||
has_many :accounts, class_name: Account.name | ||
^^^^^^^^^^^^^^^^^^^^^^^^ Use a string has value for a class_name. | ||
RUBY | ||
end | ||
|
||
it 'has_one' do | ||
expect_offense(<<-RUBY.strip_indent) | ||
has_one :account, class_name: Account | ||
^^^^^^^^^^^^^^^^^^^ Use a string has value for a class_name. | ||
RUBY | ||
end | ||
|
||
it 'belongs_to' do | ||
expect_offense(<<-RUBY.strip_indent) | ||
belongs_to :account, class_name: Account | ||
^^^^^^^^^^^^^^^^^^^ Use a string has value for a class_name. | ||
RUBY | ||
end | ||
end | ||
|
||
it 'does not register an offense when using `foreign_key :account_id`' do | ||
expect_no_offenses(<<-RUBY.strip_indent) | ||
has_many :accounts, class_name: 'Account', foreign_key: :account_id | ||
has_one :account, class_name: 'Account' | ||
belongs_to :account, class_name: 'Account' | ||
RUBY | ||
end | ||
end |