Skip to content

Commit 42b6e84

Browse files
committed
[Change Rails/HelperInstanceVariable not to detect offenses for ivars within classes
Resolves #1525
1 parent 96f6a88 commit 42b6e84

File tree

3 files changed

+28
-25
lines changed

3 files changed

+28
-25
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1525](https://github.com/rubocop/rubocop-rails/issues/1525): Change `Rails::HelperInstanceVariable` not to detect offenses for instance variables within classes. ([@viralpraxis][])

lib/rubocop/cop/rails/helper_instance_variable.rb

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module Rails
1313
# variable, consider moving the behavior elsewhere, for
1414
# example to a model, decorator or presenter.
1515
#
16-
# Provided that a class inherits `ActionView::Helpers::FormBuilder`,
16+
# Provided that an instance variable belongs to a class,
1717
# an offense will not be registered.
1818
#
1919
# @example
@@ -28,38 +28,37 @@ module Rails
2828
# end
2929
#
3030
# # good
31-
# class MyFormBuilder < ActionView::Helpers::FormBuilder
32-
# @template.do_something
31+
# module ButtonHelper
32+
# class Welcome
33+
# def initialize(text:)
34+
# @text = text
35+
# end
36+
# end
37+
#
38+
# def welcome(**)
39+
# render Welcome.new(**)
40+
# end
3341
# end
42+
#
3443
class HelperInstanceVariable < Base
3544
MSG = 'Do not use instance variables in helpers.'
3645

37-
def_node_matcher :form_builder_class?, <<~PATTERN
38-
(const
39-
(const
40-
(const {nil? cbase} :ActionView) :Helpers) :FormBuilder)
41-
PATTERN
42-
4346
def on_ivar(node)
44-
return if inherit_form_builder?(node)
47+
return if class_instance_variable?(node)
4548

4649
add_offense(node)
4750
end
4851

4952
def on_ivasgn(node)
50-
return if node.parent.or_asgn_type? || inherit_form_builder?(node)
53+
return if node.parent.or_asgn_type? || class_instance_variable?(node)
5154

5255
add_offense(node.loc.name)
5356
end
5457

5558
private
5659

57-
def inherit_form_builder?(node)
58-
node.each_ancestor(:class) do |class_node|
59-
return true if form_builder_class?(class_node.parent_class)
60-
end
61-
62-
false
60+
def class_instance_variable?(node)
61+
node.each_ancestor(:class, :module).first&.class_type?
6362
end
6463
end
6564
end

spec/rubocop/cop/rails/helper_instance_variable_spec.rb

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,17 @@ def do_something
5757
RUBY
5858
end
5959

60-
it 'registers an offense when using a class which does not inherit `ActionView::Helpers::FormBuilder`' do
61-
expect_offense(<<~RUBY)
62-
class Foo
63-
def do_something
64-
@template
65-
^^^^^^^^^ Do not use instance variables in helpers.
66-
@template = do_something
67-
^^^^^^^^^ Do not use instance variables in helpers.
60+
it 'does not register an offense when an instance variable is defined within a class' do
61+
expect_no_offenses(<<~RUBY)
62+
module ButtonHelper
63+
class Button
64+
def initialize(text:)
65+
@text = text
66+
end
67+
end
68+
69+
def button(**args)
70+
render Button.new(**args)
6871
end
6972
end
7073
RUBY

0 commit comments

Comments
 (0)