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

[Fix #623] Skip method shadowing check if attr is not sym or str for ReadWriteAttribute #619

Merged
merged 1 commit into from
Jan 12, 2022

Conversation

nvasilevski
Copy link
Contributor

@nvasilevski nvasilevski commented Jan 11, 2022

Closes #623

Fixes rubocop exceptions like:

undefined method `value' for s(:lvar, :column):RuboCop::AST::Node

for read/write methods used within a method and using non-string or non-symbol argument names
for example:

def do_the_read
  read_attribute(ATTR_NAME)
end

So I decided to skip the "shadowing" check if we can't explicitly know the attribute name.

Later we may want to exclude cases like this because it's also shadowing and should be fairly easy to detect this one. But for now I decided to fix the exception and consider an improvement in a separate PR

    def define_date_column_writer(column, value)
      define_method("#{column}=") do |value|
        # currently a violation
        write_attribute(column, value)
      end
    end

Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • If this is a new cop, consider making a corresponding update to the Rails Style Guide.


node.each_ancestor(:def).any? do |enclosing_method|
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to this PR.
Wondering, why we need to check all ancestors, or is it sufficient to check if the first one:

  • exists
  • matches the name

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually you are right, we do indeed interested in the first def ancestor
I guess any? is an implicit way of checking both exists and matches the way
Let me try to make it more explicit!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really good catch, I've changed it! Thanks ❤️

@nvasilevski nvasilevski force-pushed the fix-read-write-attribute-cop branch 2 times, most recently from 42ea0cd to 1f79303 Compare January 11, 2022 17:59
Copy link
Member

@pirj pirj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect, thank you!

Would you please add a changelog entry?


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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use method??

Suggested change
enclosing_method.method_name.to_s == shadowing_method_name
enclosing_method.method?(shadowing_method_name)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, that works! Thanks!

@nvasilevski nvasilevski force-pushed the fix-read-write-attribute-cop branch 2 times, most recently from 6ae69af to cc72b73 Compare January 11, 2022 18:43
@nvasilevski
Copy link
Contributor Author

Added a changelog entry

@nvasilevski nvasilevski force-pushed the fix-read-write-attribute-cop branch from cc72b73 to 3a25016 Compare January 11, 2022 18:48
@nvasilevski nvasilevski changed the title [ReadWriteAttribute] Skip method shadowing check if attr is not sym or str [Fix #623] Skip method shadowing check if attr is not sym or str for ReadWriteAttribute Jan 11, 2022
@nvasilevski nvasilevski force-pushed the fix-read-write-attribute-cop branch from 3a25016 to c95b625 Compare January 11, 2022 21:48
CHANGELOG.md Outdated
@@ -2,6 +2,10 @@

## master (unreleased)

### Bug fixes

* [#619](https://github.com/rubocop/rubocop-rails/pull/619): Fix method shadowing check for `Rails/ReadWriteAttribute` cop. ([@nvasilevski][])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you write to the following instead of editing the changelog directly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, sorry, should have done it from the beginning
CI seems to be read because of some github actions being down?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be a GitHub Actions incident. I'll take a look and merge if it works. Thank you!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nvasilevski nvasilevski force-pushed the fix-read-write-attribute-cop branch from c95b625 to 05a3e34 Compare January 12, 2022 02:13
@nvasilevski nvasilevski force-pushed the fix-read-write-attribute-cop branch from 05a3e34 to 0d2c43c Compare January 12, 2022 02:16
@koic koic merged commit 72feeb5 into rubocop:master Jan 12, 2022
@koic
Copy link
Member

koic commented Jan 12, 2022

Thanks!

@nvasilevski nvasilevski deleted the fix-read-write-attribute-cop branch January 12, 2022 15:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Dynamic attribute name causes Rails/ReadWriteAttribute to fail
3 participants