Module: RuboCop::Cop::Sidekiq::Helpers

Included in:
ConstArgument, DateTimeArgument, JobLocation, JobNaming, KeywordArguments, SymbolArgument
Defined in:
lib/rubocop/cop/sidekiq/helpers.rb

Constant Summary collapse

NODE_MATCHERS =
lambda do
  def_node_matcher :sidekiq_include?, "    (send nil? :include (const (const {nil? cbase} :Sidekiq) {:Worker :Job}))\n  PATTERN\n\n  def_node_matcher :includes_sidekiq?, <<~PATTERN\n    {\n      (begin <#sidekiq_include? ...>)\n      #sidekiq_include?\n    }\n  PATTERN\n\n  def_node_matcher :worker_class_def?, <<~PATTERN\n    (class _ _ #includes_sidekiq?)\n  PATTERN\n\n  def_node_matcher :worker_anon_class_def?, <<~PATTERN\n    (block (send (const nil? :Class) :new ...) _ #includes_sidekiq?)\n  PATTERN\n\n  def_node_matcher :worker_class_application_worker?, <<~PATTERN\n    (class _ (const {nil? cbase} :ApplicationWorker) ...)\n  PATTERN\n\n  def_node_matcher :worker_anon_class_application_worker_def?, <<~PATTERN\n    (block (send (const nil? :Class) :new (const {nil? cbase} :ApplicationWorker)) ...)\n  PATTERN\n\n  def_node_matcher :sidekiq_worker?, <<~PATTERN\n    {#worker_class_def? #worker_anon_class_def? #worker_class_application_worker? #worker_anon_class_application_worker_def?}\n  PATTERN\n\n  def_node_matcher :sidekiq_perform?, <<~PATTERN\n    (send const ${:perform_async :perform_in :perform_at} ...)\n  PATTERN\nend\n"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



45
46
47
# File 'lib/rubocop/cop/sidekiq/helpers.rb', line 45

def self.included(klass)
  klass.class_exec(&NODE_MATCHERS)
end

Instance Method Details

#approve_node(node) ⇒ Object Also known as: deny_node



105
106
107
108
# File 'lib/rubocop/cop/sidekiq/helpers.rb', line 105

def approve_node(node)
  @approved_nodes ||= []
  @approved_nodes << node.source_range
end

#expand_array_node(node, &block) ⇒ Object



88
89
90
91
92
93
94
95
96
97
# File 'lib/rubocop/cop/sidekiq/helpers.rb', line 88

def expand_array_node(node, &block)
  return unless node.array_type?
  return enum_for(:expand_array_node, node) unless block

  if node.square_brackets?
    expand_nodes(node.values, &block)
  else
    yield node
  end
end

#expand_hash_array_node(node, &block) ⇒ Object



73
74
75
# File 'lib/rubocop/cop/sidekiq/helpers.rb', line 73

def expand_hash_array_node(node, &block)
  expand_hash_node(node, &block) || expand_array_node(node, &block)
end

#expand_hash_node(node, &block) ⇒ Object



77
78
79
80
81
82
83
84
85
86
# File 'lib/rubocop/cop/sidekiq/helpers.rb', line 77

def expand_hash_node(node, &block)
  return unless node.hash_type?
  return enum_for(:expand_hash_node, node) unless block

  node.pairs.each do |pair_node|
    expand_hash_array_node(pair_node.key, &block) ||
      expand_hash_array_node(pair_node.value, &block) ||
      yield(pair_node)
  end
end

#expand_node(node, &block) ⇒ Object



67
68
69
70
71
# File 'lib/rubocop/cop/sidekiq/helpers.rb', line 67

def expand_node(node, &block)
  return enum_for(:expand_node, node) unless block

  expand_hash_array_node(node, &block) || yield(node)
end

#expand_nodes(nodes, &block) ⇒ Object



61
62
63
64
65
# File 'lib/rubocop/cop/sidekiq/helpers.rb', line 61

def expand_nodes(nodes, &block)
  return enum_for(:expand_nodes, nodes) unless block

  nodes.each { |node| expand_node(node, &block) }
end

#in_sidekiq_worker?(node) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/rubocop/cop/sidekiq/helpers.rb', line 49

def in_sidekiq_worker?(node)
  node.each_ancestor(:class, :block).detect { |anc| sidekiq_worker?(anc) }
end

#node_approved?(node) ⇒ Boolean Also known as: node_denied?

Returns:

  • (Boolean)


99
100
101
102
# File 'lib/rubocop/cop/sidekiq/helpers.rb', line 99

def node_approved?(node)
  @approved_nodes ||= []
  @approved_nodes.any? { |r| within?(node.source_range, r) }
end

#sidekiq_arguments(node, &block) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/rubocop/cop/sidekiq/helpers.rb', line 53

def sidekiq_arguments(node, &block)
  return [] unless node.send_type? && (method_name = sidekiq_perform?(node))
  return enum_for(:sidekiq_arguments, node) unless block

  # Drop the first argument for perform_at and perform_in
  expand_nodes(method_name == :perform_async ? node.arguments : node.arguments[1..], &block)
end

#within?(inner, outer) ⇒ Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/rubocop/cop/sidekiq/helpers.rb', line 111

def within?(inner, outer)
  inner.begin_pos >= outer.begin_pos && inner.end_pos <= outer.end_pos
end