Class: RuboCop::Cop::Gusto::RakeConstants

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/gusto/rake_constants.rb

Overview

Detects constants in a rake file because they are defined at the top level. It is confusing because the scope looks like it would be in the task or namespace, but actually it is defined at the top level.

Examples:

# bad
task :foo do
  class C
  end
end

# bad
namespace :foo do
  module M
  end
end

# good - It is also defined at the top level,
#        but it looks like intended behavior.
class C
end
task :foo do
end

Constant Summary collapse

MSG =
"Do not define a constant in rake file, because they are sometimes `load`ed, instead of `require`d which can lead to warnings about redefining constants"

Instance Method Summary collapse

Instance Method Details

#on_casgn(node) ⇒ Object



42
43
44
45
46
# File 'lib/rubocop/cop/gusto/rake_constants.rb', line 42

def on_casgn(node)
  return unless in_task_or_namespace?(node)

  add_offense(node)
end

#on_class(node) ⇒ Object



48
49
50
51
52
# File 'lib/rubocop/cop/gusto/rake_constants.rb', line 48

def on_class(node)
  return unless in_task_or_namespace?(node)

  add_offense(node)
end

#on_module(node) ⇒ Object



54
55
56
57
58
# File 'lib/rubocop/cop/gusto/rake_constants.rb', line 54

def on_module(node)
  return unless in_task_or_namespace?(node)

  add_offense(node)
end

#task_or_namespace?(node) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/rubocop/cop/gusto/rake_constants.rb', line 34

def_node_matcher :task_or_namespace?, <<-PATTERN
    (block
      (send _ {:task :namespace} ...)
      args
      _
    )
PATTERN