blob: 6ed1b986fdda4f5258cf198f3d6fc6ad5ae963a9 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
module RuboCop
module Cop
class ModuleWithInstanceVariables < RuboCop::Cop::Cop
MSG = <<~EOL.freeze
Do not use instance variables in a module. Please read this
for the rationale behind it:
doc/development/module_with_instance_variables.md
If you think the use for this is fine, please just add:
# rubocop:disable Cop/ModuleWithInstanceVariables
EOL
def on_module(node)
return if
rails_helper?(node) || rails_mailer?(node) || spec_helper?(node)
check_method_definition(node)
# Not sure why some module would have an extra begin wrapping around
node.each_child_node(:begin) do |begin_node|
check_method_definition(begin_node)
end
end
private
# We ignore Rails helpers right now because it's hard to workaround it
def rails_helper?(node)
node.source_range.source_buffer.name =~
%r{app/helpers/\w+_helper.rb\z}
end
# We ignore Rails mailers right now because it's hard to workaround it
def rails_mailer?(node)
node.source_range.source_buffer.name =~
%r{app/mailers/emails/}
end
# We ignore spec helpers because it usually doesn't matter
def spec_helper?(node)
node.source_range.source_buffer.name =~
%r{spec/support/|features/steps/}
end
def check_method_definition(node)
node.each_child_node(:def) do |definition|
definition.each_descendant(:ivar, :ivasgn) do |offense|
add_offense(offense, :expression)
end
end
end
end
end
end
|