Class: RuboCop::Cop::Legion::Extension::SettingsBracketMultiArg

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/legion/extension/settings_bracket_multi_arg.rb

Overview

Detects ‘Legion::Settings[:a, :b]` (2+ arguments to `[]`). `Legion::Settings#[]` only accepts exactly one key; multi-key access must use `Legion::Settings.dig(:a, :b)` instead.

Examples:

# bad
Legion::Settings[:logging, :enabled]

# good
Legion::Settings.dig(:logging, :enabled)

Constant Summary collapse

RESTRICT_ON_SEND =
%i[[]].freeze
MSG =
'`Legion::Settings#[]` takes exactly 1 argument. ' \
'Use `Legion::Settings.dig(...)` for nested access.'

Instance Method Summary collapse

Instance Method Details

#legion_settings_multi_bracket?(node) ⇒ Object



26
27
28
29
30
31
# File 'lib/rubocop/cop/legion/extension/settings_bracket_multi_arg.rb', line 26

def_node_matcher :legion_settings_multi_bracket?, <<~PATTERN
  (send
    (const {nil? cbase} :Legion)
    ...
  )
PATTERN

#on_send(node) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/rubocop/cop/legion/extension/settings_bracket_multi_arg.rb', line 33

def on_send(node)
  return unless node.receiver&.const_type? && node.receiver.short_name == :Settings
  return unless legion_settings_receiver_is_legion?(node.receiver)
  return if node.arguments.size < 2

  add_offense(node) do |corrector|
    args_source = node.arguments.map(&:source).join(', ')
    corrector.replace(node, "Legion::Settings.dig(#{args_source})")
  end
end