-
-
Notifications
You must be signed in to change notification settings - Fork 265
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
Changes from 4 commits
239c998
62f879b
a7eb2a6
f78531e
d912a64
6dd2d00
2c00a6b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Rails | ||
# This cop checks for use of the helper methods which reference | ||
# instance variables. | ||
# | ||
# Relying on instance variables makes it difficult to re-use helper | ||
# methods. | ||
# | ||
# If it seems awkward to explicitly pass in each dependent | ||
# variable, consider moving the behaviour elsewhere, for | ||
# example to a model, decorator or presenter. | ||
# | ||
# @example | ||
# # bad | ||
# def welcome_message | ||
# "Hello " + @user.name | ||
# end | ||
# | ||
# # good | ||
# def welcome_message(user) | ||
# "Hello " + user.name | ||
# end | ||
class HelperInstanceVariable < Cop | ||
MSG = 'Do not use instance variables in helpers.'.freeze | ||
|
||
def on_ivar(node) | ||
add_offense(node) | ||
end | ||
|
||
def on_ivasgn(node) | ||
add_offense(node) | ||
end | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Rails::HelperInstanceVariable do | ||
subject(:cop) { described_class.new } | ||
|
||
it 'reports uses of instance variables' do | ||
expect_offense(<<-RUBY.strip_indent) | ||
def welcome_message | ||
"Hello" + @user.name | ||
^^^^^ Do not use instance variables in helpers. | ||
end | ||
RUBY | ||
end | ||
|
||
it 'reports instance variable assignments' do | ||
expect_offense(<<-RUBY.strip_indent) | ||
def welcome_message(user) | ||
@user_name = user.name | ||
^^^^^^^^^^^^^^^^^^^^^^ Do not use instance variables in helpers. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 https://www.rubydoc.info/gems/rubocop/RuboCop%2FCop%2FCop:add_offense There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure! 🙂 First, use the
You should see something like this:
So in this case we want to target the So to add an offense on the relevant part: add_offense(node, location: :name) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for that. Fixed in 6dd2d00. |
||
end | ||
RUBY | ||
end | ||
|
||
specify do | ||
expect_no_offenses(<<-RUBY.strip_indent) | ||
def welcome_message(user) | ||
"Hello " + user.name | ||
end | ||
RUBY | ||
end | ||
end |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in d912a64
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.