summaryrefslogtreecommitdiff
path: root/rubocop
diff options
context:
space:
mode:
authorLin Jen-Shin <godfat@godfat.org>2017-07-12 02:29:33 +0800
committerLin Jen-Shin <godfat@godfat.org>2017-09-18 21:23:45 +0800
commit9ae92b8caa6c11d8860f86b7d6378062215d1b72 (patch)
tree06b7416abad46cb1dd44c19a18a7e40caed30e15 /rubocop
parent4cadf22e208e3be401824f43ab13d5e6f2ff6465 (diff)
downloadgitlab-ce-9ae92b8caa6c11d8860f86b7d6378062215d1b72.tar.gz
Add cop to make sure we don't use ivar in a module
Diffstat (limited to 'rubocop')
-rw-r--r--rubocop/cop/module_with_instance_variables.rb55
-rw-r--r--rubocop/rubocop.rb1
2 files changed, 56 insertions, 0 deletions
diff --git a/rubocop/cop/module_with_instance_variables.rb b/rubocop/cop/module_with_instance_variables.rb
new file mode 100644
index 00000000000..6ed1b986fdd
--- /dev/null
+++ b/rubocop/cop/module_with_instance_variables.rb
@@ -0,0 +1,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
diff --git a/rubocop/rubocop.rb b/rubocop/rubocop.rb
index 1b6e8991a17..bb0d25f3c48 100644
--- a/rubocop/rubocop.rb
+++ b/rubocop/rubocop.rb
@@ -6,6 +6,7 @@ require_relative 'cop/polymorphic_associations'
require_relative 'cop/project_path_helper'
require_relative 'cop/active_record_dependent'
require_relative 'cop/in_batches'
+require_relative 'cop/module_with_instance_variables'
require_relative 'cop/migration/add_column'
require_relative 'cop/migration/add_column_with_default_to_large_table'
require_relative 'cop/migration/add_concurrent_foreign_key'