Class: RuboCop::Cop::Legion::Framework::NoShapeDuckTyping

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/legion/framework/no_shape_duck_typing.rb

Overview

Flags ‘respond_to?` checks for canonical response shape methods (`thinking`, `tool_calls`, `content`, `stop_reason`) and string-key fallback access (`x || x`). Per R10, downstream consumers of canonical types should rely on the struct interface, not duck-type.

Ships **disabled by default**. Enable per-repo or per-directory once the canonical migration is complete.

Examples:

# bad
response.respond_to?(:thinking)
response.respond_to?(:tool_calls)
tc[:name] || tc['name']

# good
response.thinking
response.tool_calls
tc[:name]

Constant Summary collapse

MSG_RESPOND =
'Use canonical struct access instead of `respond_to?(:%<method>s)`. ' \
'Downstream of translators, the shape is guaranteed.'
MSG_FALLBACK =
'String-key fallback `%<key>s` suggests duck-typed access. ' \
'Use canonical struct access or symbol keys consistently.'
CANONICAL_METHODS =
%w[thinking tool_calls content stop_reason].freeze

Instance Method Summary collapse

Instance Method Details

#on_or(node) ⇒ Object



37
38
39
# File 'lib/rubocop/cop/legion/framework/no_shape_duck_typing.rb', line 37

def on_or(node)
  check_string_key_fallback(node)
end

#on_send(node) ⇒ Object



33
34
35
# File 'lib/rubocop/cop/legion/framework/no_shape_duck_typing.rb', line 33

def on_send(node)
  check_respond_to(node)
end