Class: RuboCop::Cop::Legion::Framework::ApiStringKeys

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

Overview

Detects string key access on ‘body` (e.g. `body`). `Legion::JSON.load` returns symbol keys, so string access always returns nil. Auto-corrects to symbol key syntax.

Examples:

# bad
body['data']
body['complex-key']

# good
body[:data]
body[:'complex-key']

Constant Summary collapse

MSG =
'`Legion::JSON.load` returns symbol keys. Use `body[:%<key>s]` instead of string keys.'
SEVERITY =
:warning
RESTRICT_ON_SEND =
%i[[]].freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rubocop/cop/legion/framework/api_string_keys.rb', line 26

def on_send(node)
  receiver = node.receiver
  return unless body_receiver?(receiver)

  key_node = node.first_argument
  return unless key_node&.str_type?

  key = key_node.value
  message = format(MSG, key: key)
  add_offense(key_node, message: message, severity: SEVERITY) do |corrector|
    corrector.replace(key_node.source_range, symbol_for(key))
  end
end