Class: RuboCop::Cop::Operandi::NoHashArgument

Inherits:
Base
  • Object
show all
Defined in:
lib/operandi/rubocop/cop/operandi/no_hash_argument.rb

Overview

Detects when .run or .run! is called with a hash argument instead of keyword arguments.

Since Operandi services now only accept keyword arguments, passing a hash variable or hash expression will cause an error. Use keyword splatting (**) to convert hash arguments to keyword arguments.

Examples:

# bad
UserService.run(args)
UserService.run!(params)
UserService.run(args.merge(new: true))
UserService.run({ name: "John" })
Auth::SignIn.run(service_args)

# good
UserService.run(name: "John")
UserService.run(**args)
UserService.run(**args.merge(new: true))
UserService.run(**args, new: true)
Auth::SignIn.run(**service_args)

ServicePattern: nil (default - checks all classes)

# Checks all .run and .run! calls
UserService.run(args)       # offense
Auth::SignIn.run(args)      # offense
SomeClass.run(args)         # offense

ServicePattern: 'Service$'

# Only matches class names ending with "Service"
UserService.run(args)       # offense
Auth::SignIn.run(args)      # no offense (doesn't match pattern)

Constant Summary collapse

MSG =
"Use keyword arguments or `**` splat instead of hash argument for `.%<method>s`."
RESTRICT_ON_SEND =
[:run, :run!].freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/operandi/rubocop/cop/operandi/no_hash_argument.rb', line 47

def on_send(node)
  return unless RESTRICT_ON_SEND.include?(node.method_name)
  return unless service_class?(node.receiver)
  return if node.arguments.empty?
  return if valid_arguments?(node.arguments)

  add_offense(node, message: format(MSG, method: node.method_name), severity: :fatal)
end