Class: RuboCop::Cop::Legion::Extension::CoreExtendGuard

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

Overview

Detects ‘extend Legion::Extensions::Core` without a `const_defined?` guard, which causes failures when running specs in isolation without the full Legion framework loaded.

Examples:

# bad
extend Legion::Extensions::Core

# good
extend Legion::Extensions::Core if Legion::Extensions.const_defined?(:Core)

Constant Summary collapse

MSG =
'Guard `extend Core` with `if Legion::Extensions.const_defined?(:Core)` ' \
'for standalone test compatibility.'
GUARD_SUFFIX =
' if Legion::Extensions.const_defined?(:Core)'

Instance Method Summary collapse

Instance Method Details

#extend_core?(node) ⇒ Object



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

def_node_matcher :extend_core?, <<~PATTERN
  (send nil? :extend
    (const
      (const
        (const nil? :Legion) :Extensions)
      :Core))
PATTERN

#on_send(node) ⇒ Object



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

def on_send(node)
  return unless extend_core?(node)
  return if node.parent&.if_type?

  add_offense(node) do |corrector|
    corrector.insert_after(node, GUARD_SUFFIX)
  end
end