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

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

Constant Summary collapse

NODE_MATCHERS =
lambda do
  def_node_matcher :sidekiq_include?, <<~PATTERN
    (send nil? :include (const (const nil? :Sidekiq) :Worker))
  PATTERN

  def_node_matcher :includes_sidekiq?, <<~PATTERN
    {
      (begin <#sidekiq_include? ...>)
      #sidekiq_include?
    }
  PATTERN

  def_node_matcher :worker_class_def?, <<~PATTERN
    (class _ _ #includes_sidekiq?)
  PATTERN

  def_node_matcher :worker_anon_class_def?, <<~PATTERN
    (block (send (const nil? :Class) :new ...) _ #includes_sidekiq?)
  PATTERN

  def_node_matcher :worker_class_application_worker?, <<~PATTERN
    (class _ (const {nil? cbase} :ApplicationWorker) ...)
  PATTERN

  def_node_matcher :worker_anon_class_application_worker_def?, <<~PATTERN
    (block (send (const nil? :Class) :new (const {nil? cbase} :ApplicationWorker)) ...)
  PATTERN

  def_node_matcher :sidekiq_worker?, <<~PATTERN
    {#worker_class_def? #worker_anon_class_def? #worker_class_application_worker? #worker_anon_class_application_worker_def?}
  PATTERN

  def_node_matcher :sidekiq_perform?, <<~PATTERN
    (send const ${:perform_async :perform_in :perform_at} ...)
  PATTERN
end

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



76
77
78
79
# File 'lib/rubocop/cop/sidekiq/helpers.rb', line 76

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

#expand_arguments(arguments) ⇒ Object



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

def expand_arguments(arguments)
  arguments.flat_map do |argument|
    if argument.array_type? || argument.hash_type?
      expand_arguments(argument.values)
    else
      argument
    end
  end
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)


70
71
72
73
# File 'lib/rubocop/cop/sidekiq/helpers.rb', line 70

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

#sidekiq_arguments(node) ⇒ Object



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

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

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

#within?(inner, outer) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/rubocop/cop/sidekiq/helpers.rb', line 82

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