Skip to content

Commit

Permalink
[ReadWriteAttribute] Skip method shadowing check if attr is not sym o…
Browse files Browse the repository at this point in the history
…r str
  • Loading branch information
nvasilevski authored Jan 11, 2022
1 parent ba2a7e3 commit 1f79303
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 5 deletions.
13 changes: 8 additions & 5 deletions lib/rubocop/cop/rails/read_write_attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,15 @@ def on_send(node)
private

def within_shadowing_method?(node)
node.each_ancestor(:def).any? do |enclosing_method|
shadowing_method_name = node.first_argument.value.to_s
shadowing_method_name << '=' if node.method?(:write_attribute)
first_arg = node.first_argument
return false unless first_arg.respond_to?(:value)

enclosing_method.method_name.to_s == shadowing_method_name
end
enclosing_method = node.each_ancestor(:def).first
return false unless enclosing_method

shadowing_method_name = first_arg.value.to_s
shadowing_method_name << '=' if node.method?(:write_attribute)
enclosing_method.method_name.to_s == shadowing_method_name
end

def build_message(node)
Expand Down
72 changes: 72 additions & 0 deletions spec/rubocop/cop/rails/read_write_attribute_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,42 @@ def foo
it 'registers no offense with explicit receiver' do
expect_no_offenses('res = object.read_attribute(:test)')
end

context 'when used within a method' do
context 'when using variable for the attribute name' do
it 'registers an offense and corrects' do
expect_offense(<<~RUBY)
def do_the_read_from(column)
read_attribute(column)
^^^^^^^^^^^^^^^^^^^^^^ Prefer `self[column]`.
end
RUBY

expect_correction(<<~RUBY)
def do_the_read_from(column)
self[column]
end
RUBY
end
end

context 'when using constant for the attribute name' do
it 'registers an offense and corrects' do
expect_offense(<<~RUBY)
def do_the_read
read_attribute(ATTR_NAME)
^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `self[ATTR_NAME]`.
end
RUBY

expect_correction(<<~RUBY)
def do_the_read
self[ATTR_NAME]
end
RUBY
end
end
end
end

context 'write_attribute' do
Expand All @@ -105,6 +141,42 @@ def foo
end
end

context 'when used within a method' do
context 'when using variable for the attribute name' do
it 'registers an offense and corrects' do
expect_offense(<<~RUBY)
def do_the_write_to(column)
write_attribute(column, 'value')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `self[column] = 'value'`.
end
RUBY

expect_correction(<<~RUBY)
def do_the_write_to(column)
self[column] = 'value'
end
RUBY
end
end

context 'when using constant for the attribute name' do
it 'registers an offense and corrects' do
expect_offense(<<~RUBY)
def do_the_write(value)
write_attribute(ATTR_NAME, value)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `self[ATTR_NAME] = value`.
end
RUBY

expect_correction(<<~RUBY)
def do_the_write(value)
self[ATTR_NAME] = value
end
RUBY
end
end
end

context 'when using a string for the attribute' do
it 'registers an offense and corrects' do
expect_offense(<<~RUBY)
Expand Down

0 comments on commit 1f79303

Please sign in to comment.