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

Avoid instance variables in helpers #29

Merged
merged 7 commits into from
Jan 7, 2019
Merged

Avoid instance variables in helpers #29

merged 7 commits into from
Jan 7, 2019

Conversation

andyw8
Copy link
Contributor

@andyw8 andyw8 commented Dec 28, 2018

Fixes #19

# This cop checks for use of the helper methods which reference
# instance variables.
#
# Relying on the existing of a particular instance variable
Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are others reasons, but I'm still trying to find a good way to write out about.

Copy link
Contributor

Choose a reason for hiding this comment

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

The wording is a bit off here. Consider:

Relying on instance variables makes it difficult to re-use helper methods.

The next part about moving the behavior out of the helper sounds good to me. 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, fixed in f78531e.

@mikegee
Copy link
Contributor

mikegee commented Dec 29, 2018

Does on_ivar catch instance variable assignment? That seems like a much worse, but completely different problem.

@andyw8
Copy link
Contributor Author

andyw8 commented Dec 29, 2018

@mikegee Good point, on_ivar doesn't catch instance variable assignments. Fixed in 62f879b.

Copy link
Contributor

@bquorning bquorning left a comment

Choose a reason for hiding this comment

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

❤️

#
# # good
# def welcome_message(user)
# "Hello " + user.name
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick: I would recommend string interpolation instead of concatenation, but that might make the example less clear.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in d912a64

Copy link
Contributor

Choose a reason for hiding this comment

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

The specs were changed, but the comment/documentation was not. Is that intentional?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@bquorning Sorry, missed that. 2c00a6b.

expect_offense(<<-RUBY.strip_indent)
def welcome_message(user)
@user_name = user.name
^^^^^^^^^^^^^^^^^^^^^^ Do not use instance variables in helpers.
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor nit: would it be more consistent, and slightly more clear, if the highlight was limited to just the instance variable being assigned?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can you give me some pointers on how to do that? The docs for add_offense seem a little sparse.

https://www.rubydoc.info/gems/rubocop/RuboCop%2FCop%2FCop:add_offense

Copy link
Contributor

Choose a reason for hiding this comment

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

Sure! 🙂

First, use the ruby-parse utility provided with the parser gem, to find the locations available for this source map:

$ ruby-parse -L -e '@user_name = user.name'

You should see something like this:

@user_name = user.name
~~~~~~~~~~ name      
           ~ operator            
~~~~~~~~~~~~~~~~~~~~~~ expression

So in this case we want to target the name location. The #add_offense method takes an optional location argument that is automatically turned into a range. (Defaults to :expression.)

So to add an offense on the relevant part:

add_offense(node, location: :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.

Thanks for that. Fixed in 6dd2d00.

Copy link
Contributor

@Drenmi Drenmi left a comment

Choose a reason for hiding this comment

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

Nice work, @andyw8! 🚀

@andyw8 andyw8 changed the title WIP: Avoid instance variables in helpers Avoid instance variables in helpers Jan 2, 2019
@Drenmi
Copy link
Contributor

Drenmi commented Jan 2, 2019

Think this is ready for merging, @koic. 🙂

@koic koic merged commit a54c273 into rubocop:master Jan 7, 2019
@koic
Copy link
Member

koic commented Jan 7, 2019

I think this cop is reasonable. Thanks!

koic added a commit that referenced this pull request Feb 25, 2019
`Rails/HelperInstanceVariable` cop added by #29
isn't backported to RuboCop core.

2.0 has been proposed as the release version of RuboCop Rails, it did so.
toshimaru/rubocop-rails#31 (comment)

Cops importing from the RuboCop core has lower than 1.0 so I think it works.
@koic koic mentioned this pull request Feb 25, 2019
koic added a commit that referenced this pull request Feb 26, 2019
`Rails/HelperInstanceVariable` cop added by #29
isn't backported to RuboCop core.

2.0 has been proposed as the release version of RuboCop Rails, it did so.
toshimaru/rubocop-rails#31 (comment)

Cops importing from the RuboCop core has lower than 1.0 so I think it works.

And this commit updates `rake yard_for_generate_documentation` task to
add metadata and adds document regenerated by `rake yard_for_generate_documentation`.
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.

5 participants