Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic attribute name causes Rails/ReadWriteAttribute to fail #623

Closed
davidenglishmusic opened this issue Jan 11, 2022 · 2 comments · Fixed by #619
Closed

Dynamic attribute name causes Rails/ReadWriteAttribute to fail #623

davidenglishmusic opened this issue Jan 11, 2022 · 2 comments · Fixed by #619
Labels
duplicate This issue or pull request already exists

Comments

@davidenglishmusic
Copy link

I have a stress case that causes the Rails/ReadWriteAttribute cop to fail for both read_attribute and write_attribute infractions. I have a method that should either return a value if present or commit a new value to that field.


Expected behavior

Rubocop should not throw an error.

Actual behavior

It throws an error:

% bundle exec rubocop -a app/models/klass.rb
Scanning app/models/klass.rb
An error occurred while Rails/ReadWriteAttribute cop was inspecting app/models/klass.rb:5:12.
undefined method `value' for s(:lvar, :attr):RuboCop::AST::Node
.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rubocop-rails-2.13.1/lib/rubocop/cop/rails/read_write_attribute.rb:70:in `block in within_shadowing_method?'
...
An error occurred while Rails/ReadWriteAttribute cop was inspecting app/models/klass.rb:8:6.
undefined method `value' for s(:lvar, :attr):RuboCop::AST::Node
rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rubocop-rails-2.13.1/lib/rubocop/cop/rails/read_write_attribute.rb:70:in `block in within_shadowing_method?'

Steps to reproduce the problem

Enable the cop and run rubocop on the following code:

class Klass < ApplicationRecord
  def attr_or_query(attr, val)
    if (x = read_attribute(attr).present?)
      x
    else
      write_attribute(attr, val)
    end
  end
end

RuboCop version

$ [bundle exec] rubocop -V
1.24.1 (using Parser 3.0.3.2, rubocop-ast 1.15.1, running on ruby 2.6.6 -darwin20)
  - rubocop-performance 1.13.1
  - rubocop-rails 2.13.1
  - rubocop-rspec 2.7.0
@pirj
Copy link
Member

pirj commented Jan 11, 2022

Luckily, the fix for this is underway, see #619
I've tested and it fixes your case.

nvasilevski added a commit to nvasilevski/rubocop-rails that referenced this issue Jan 11, 2022
nvasilevski added a commit to nvasilevski/rubocop-rails that referenced this issue Jan 12, 2022
nvasilevski added a commit to nvasilevski/rubocop-rails that referenced this issue Jan 12, 2022
@koic
Copy link
Member

koic commented Jan 12, 2022

I will a bugfix release in a few days after merging #619.

@koic koic closed this as completed Jan 12, 2022
@koic koic added the duplicate This issue or pull request already exists label Jan 12, 2022
koic added a commit that referenced this issue Jan 12, 2022
[Fix #623] Skip method shadowing check if attr is not sym or str for ReadWriteAttribute
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Projects
None yet
3 participants