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

ReflectionClassName: accept method calls on local variables #1180

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

exterm
Copy link

@exterm exterm commented Nov 10, 2023

Fix #1179

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.
  • If this is a new cop, consider making a corresponding update to the Rails Style Guide.

@exterm exterm force-pushed the reflection-class-name-accept-method-calls-on-local-variables branch from 12b998a to 5932448 Compare November 10, 2023 17:18
@exterm exterm force-pushed the reflection-class-name-accept-method-calls-on-local-variables branch from 5932448 to db9a8e9 Compare November 14, 2023 14:01
@exterm
Copy link
Author

exterm commented Nov 14, 2023

@koic you're probably pretty busy, but I would love a review on this 😃

@exterm exterm force-pushed the reflection-class-name-accept-method-calls-on-local-variables branch from db9a8e9 to a9eb6ae Compare November 14, 2023 14:04
@@ -124,6 +124,12 @@
RUBY
end

it 'does not register an offense when parameter value is a method call on an object in a variable' do
expect_no_offenses(<<~RUBY)
has_many :accounts, class_name: the_class.name
Copy link
Member

Choose a reason for hiding this comment

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

The ability to call the name method suggests that, similar to has_many :accounts, class_name: Account.name, an offense should be registered:
https://github.com/rubocop/rubocop-rails/pull/1180/files#diff-9d8842c039d0144f6548e139d525048bb9e0651ba63906ca875e35ff52140498R16-R25

Copy link
Author

@exterm exterm Nov 29, 2023

Choose a reason for hiding this comment

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

I understand the purpose of this cop to be to avoid the use of constant references, which lead to loading all the referenced classes.

Account.name will resolve Account, some_class_in_a_variable.name will not load the class (it will already have been loaded).

I can see how this is kind of an edge case though since it does seem to require the loading of a class (unless the name method here is not Class#name).

I wonder - isn't your concern covered by it 'registers an offense when parameter value is a local variable assigned a constant'?

I can look into this, change this test case to use different variable / method names and add another test case for

expect_offense(<<~RUBY)
  the_class = Account

  has_many :accounts, class_name: the_class.name
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^ Use a string value for `class_name`.
RUBY

(Although I think this makes me think that the message shouldn't be "Use a string value for class_name", but rather "Don't use a constant reference for class_name")

Copy link
Author

Choose a reason for hiding this comment

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

OK, I tried it -

  the_class = Account

  has_many :accounts, class_name: the_class.name

does not register an offense even on main. So for now I've just changed the variable and method name in this test. I believe the behavior of the cop is as expected except for the above example which didn't work before either. But it's better to have a false negative than a false positive.

@exterm exterm force-pushed the reflection-class-name-accept-method-calls-on-local-variables branch from a9eb6ae to e4b0f53 Compare December 1, 2023 02:52
@exterm
Copy link
Author

exterm commented Feb 21, 2024

@koic can we merge this? If not, what else is required?

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.

Rails/ReflectionClassName complains on variables
2 participants