Class: RuboCop::Cop::Legion::HelperMigration::DefinedTransportGuard

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

Overview

Detects ‘defined?(Legion::Transport)` guards and suggests using `transport_connected?` from `Legion::Transport::Helper` instead. The helper checks both that the module is loaded and that the transport is actually connected, which is the correct semantics.

Examples:

# bad
return unless defined?(Legion::Transport)
if defined?(Legion::Transport) && Legion::Transport.connected?

# good
return unless transport_connected?
if transport_connected?

Constant Summary collapse

MSG =
'Use `transport_connected?` instead of `defined?(Legion::Transport)`. ' \
'Include `Legion::Transport::Helper` via the transport helper mixin.'

Instance Method Summary collapse

Instance Method Details

#on_defined?(node) ⇒ Boolean

Cannot use def_node_matcher with ‘defined?` node type — Ruby treats `defined?` as a keyword and the matcher DSL chokes on the `?` suffix. Use manual AST inspection instead (same approach as LoggingGuard).

Returns:

  • (Boolean)


27
28
29
30
31
# File 'lib/rubocop/cop/legion/helper_migration/defined_transport_guard.rb', line 27

def on_defined?(node)
  return false unless legion_transport_defined?(node)

  add_offense(node, message: MSG)
end