Class: RuboCop::Cop::Sidekiq::JobLocation

Inherits:
Base
  • Object
show all
Includes:
Helpers
Defined in:
lib/rubocop/cop/sidekiq/job_location.rb

Overview

This cop checks that Sidekiq job classes with “Job” suffix are placed in the ‘app/jobs` directory instead of `app/workers` directory. This follows the modern Sidekiq convention where jobs should be organized in the appropriate directory structure.

Examples:

# bad - Job class in app/workers directory
# app/workers/my_job.rb
class MyJob
  include Sidekiq::Job
end

# good - Job class in app/jobs directory
# app/jobs/my_job.rb
class MyJob
  include Sidekiq::Job
end

# good - Worker class can stay in app/workers (though not recommended)
# app/workers/my_worker.rb
class MyWorker
  include Sidekiq::Worker
end

Constant Summary collapse

MSG =
'Job class with `Job` suffix should be placed in `app/jobs` directory instead of `app/workers`.'

Constants included from Helpers

Helpers::NODE_MATCHERS

Instance Method Summary collapse

Methods included from Helpers

#approve_node, #expand_array_node, #expand_hash_array_node, #expand_hash_node, #expand_node, #expand_nodes, #in_sidekiq_worker?, included, #node_approved?, #sidekiq_arguments, #within?

Instance Method Details

#on_class(node) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rubocop/cop/sidekiq/job_location.rb', line 36

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

  class_name = extract_class_name(node)
  return unless class_name
  return unless class_name.end_with?('Job')

  file_path = processed_source.buffer.name
  return unless file_path.include?('app/workers/')

  add_offense(node.children.first, message: MSG)
end