Class: RuboCop::Cop::DevDoc::Style::AvoidOptionsHash
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::DevDoc::Style::AvoidOptionsHash
- Defined in:
- lib/rubocop/cop/dev_doc/style/avoid_options_hash.rb
Overview
Avoid ‘**options`-style kwargs in method signatures; use explicit keyword args.
## Rationale Keyword args raise ‘ArgumentError` on typos, are self-labeled at the call site, and get IDE autocomplete. Options hashes (via `**opts`) silently swallow misspelled keys, hide what’s accepted, and depend on doc/source-reading to use correctly.
❌ Options hash — typos pass silently
def configure(name:, **)
title = [:title]
color = [:color]
end
configure(name: 'x', titel: 'wrong') # silently ignored — title is nil
✔️ Keyword args — typo raises immediately
def configure(name:, title: nil, color: nil)
end
configure(name: 'x', titel: 'wrong') # ArgumentError: unknown keyword: :titel
Pure-forwarding kwargs are exempt — when the only use of the kwrestarg is to splat it into another call, there is no options-hash behaviour:
✔️ Pure forwarding — exempt
def foo(**args)
other(**args)
end
Anonymous double-splat (‘**`) is also always exempt.
Constant Summary collapse
- MSG =
'Use keyword arguments instead of `**%<name>s` — ' \ 'typos in keyword args raise `ArgumentError`; options hashes swallow them silently.'.freeze
Instance Method Summary collapse
- #on_def(node) ⇒ Object (also: #on_defs)
Instance Method Details
#on_def(node) ⇒ Object Also known as: on_defs
53 54 55 |
# File 'lib/rubocop/cop/dev_doc/style/avoid_options_hash.rb', line 53 def on_def(node) check_method(node) end |