diff options
author | Lin Jen-Shin <godfat@godfat.org> | 2017-11-22 15:50:36 +0800 |
---|---|---|
committer | Lin Jen-Shin <godfat@godfat.org> | 2017-11-22 17:06:57 +0800 |
commit | 07d3d44775f6cc5b7a1b768cb4e5b7900d543815 (patch) | |
tree | 9b87200493dfc56a64aa6716ff4f03794b8f24c3 /rubocop | |
parent | 15edf741a14a53443fb39ecb59888e75697b9c2b (diff) | |
download | gitlab-ce-07d3d44775f6cc5b7a1b768cb4e5b7900d543815.tar.gz |
Move ModuleWithInstanceVariables to Gitlab namespace
And use .rubocop.yml to exclude paths we don't care,
rather than using the cop itself to exclude.
Diffstat (limited to 'rubocop')
-rw-r--r-- | rubocop/cop/gitlab/module_with_instance_variables.rb | 63 | ||||
-rw-r--r-- | rubocop/cop/module_with_instance_variables.rb | 82 | ||||
-rw-r--r-- | rubocop/rubocop.rb | 2 |
3 files changed, 64 insertions, 83 deletions
diff --git a/rubocop/cop/gitlab/module_with_instance_variables.rb b/rubocop/cop/gitlab/module_with_instance_variables.rb new file mode 100644 index 00000000000..5c9cde98512 --- /dev/null +++ b/rubocop/cop/gitlab/module_with_instance_variables.rb @@ -0,0 +1,63 @@ +module RuboCop + module Cop + module Gitlab + class ModuleWithInstanceVariables < RuboCop::Cop::Cop + MSG = <<~EOL.freeze + Do not use instance variables in a module. Please read this + for the rationale behind it: + + https://docs.gitlab.com/ee/development/module_with_instance_variables.html + EOL + + def on_module(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 + + def check_method_definition(node) + node.each_child_node(:def) do |definition| + # We allow this pattern: + # + # def f + # @f ||= true + # end + if only_ivar_or_assignment?(definition) + # We don't allow if any other ivar is used + definition.each_descendant(:ivar) do |offense| + add_offense(offense, :expression) + end + # We allow initialize method and single ivar + elsif !initialize_method?(definition) && !single_ivar?(definition) + definition.each_descendant(:ivar, :ivasgn) do |offense| + add_offense(offense, :expression) + end + end + end + end + + def only_ivar_or_assignment?(definition) + node = definition.child_nodes.last + + definition.child_nodes.size == 2 && + node.or_asgn_type? && node.child_nodes.first.ivasgn_type? + end + + def single_ivar?(definition) + node = definition.child_nodes.last + + definition.child_nodes.size == 2 && node.ivar_type? + end + + def initialize_method?(definition) + definition.children.first == :initialize + end + end + end + end +end diff --git a/rubocop/cop/module_with_instance_variables.rb b/rubocop/cop/module_with_instance_variables.rb deleted file mode 100644 index f101ae09ad2..00000000000 --- a/rubocop/cop/module_with_instance_variables.rb +++ /dev/null @@ -1,82 +0,0 @@ -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: - - https://docs.gitlab.com/ee/development/module_with_instance_variables.html - 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| - # We allow this pattern: - # - # def f - # @f ||= true - # end - if only_ivar_or_assignment?(definition) - # We don't allow if any other ivar is used - definition.each_descendant(:ivar) do |offense| - add_offense(offense, :expression) - end - # We allow initialize method and single ivar - elsif !initialize_method?(definition) && !single_ivar?(definition) - definition.each_descendant(:ivar, :ivasgn) do |offense| - add_offense(offense, :expression) - end - end - end - end - - def only_ivar_or_assignment?(definition) - node = definition.child_nodes.last - - definition.child_nodes.size == 2 && - node.or_asgn_type? && node.child_nodes.first.ivasgn_type? - end - - def single_ivar?(definition) - node = definition.child_nodes.last - - definition.child_nodes.size == 2 && node.ivar_type? - end - - def initialize_method?(definition) - definition.children.first == :initialize - end - end - end -end diff --git a/rubocop/rubocop.rb b/rubocop/rubocop.rb index 65a37f578cd..c4bab18faee 100644 --- a/rubocop/rubocop.rb +++ b/rubocop/rubocop.rb @@ -5,8 +5,8 @@ require_relative 'cop/gem_fetcher' require_relative 'cop/in_batches' require_relative 'cop/polymorphic_associations' require_relative 'cop/project_path_helper' -require_relative 'cop/module_with_instance_variables' require_relative 'cop/redirect_with_status' +require_relative 'cop/gitlab/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' |