Class: RuboCop::Cop::Sorbet::AllowIncompatibleOverride

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/sorbet/signatures/allow_incompatible_override.rb

Overview

Disallows using ‘.override(allow_incompatible: true)`. Using `allow_incompatible` suggests a violation of the Liskov Substitution Principle, meaning that a subclass is not a valid subtype of its superclass. This Cop prevents these design smells from occurring.

Examples:


# bad
sig.override(allow_incompatible: true)

# good
sig.override

Constant Summary collapse

MSG =
"Usage of `allow_incompatible` suggests a violation of the Liskov Substitution Principle. " \
"Instead, strive to write interfaces which respect subtyping principles and remove `allow_incompatible`"
RESTRICT_ON_SEND =
[:override].freeze

Instance Method Summary collapse

Instance Method Details

#on_block(node) ⇒ Object Also known as: on_numblock



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rubocop/cop/sorbet/signatures/allow_incompatible_override.rb', line 55

def on_block(node)
  return unless sig?(node.send_node)

  block = node.children.last
  return unless block&.send_type?

  receiver = block.receiver
  while receiver
    allow_incompatible_pair = override?(receiver)
    if allow_incompatible_pair
      add_offense(allow_incompatible_pair)
      break
    end
    receiver = receiver.receiver
  end
end

#on_send(node) ⇒ Object



49
50
51
52
53
# File 'lib/rubocop/cop/sorbet/signatures/allow_incompatible_override.rb', line 49

def on_send(node)
  sig_dot_override?(node) do |allow_incompatible_pair|
    add_offense(allow_incompatible_pair)
  end
end

#override?(node) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/rubocop/cop/sorbet/signatures/allow_incompatible_override.rb', line 41

def_node_matcher(:override?, <<~PATTERN)
  (send
    _
    :override
    (hash <$(pair (sym :allow_incompatible) true) ...>)
  )
PATTERN

#sig?(node) ⇒ Object



36
37
38
# File 'lib/rubocop/cop/sorbet/signatures/allow_incompatible_override.rb', line 36

def_node_search(:sig?, <<~PATTERN)
  (send _ :sig ...)
PATTERN

#sig_dot_override?(node) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/rubocop/cop/sorbet/signatures/allow_incompatible_override.rb', line 27

def_node_matcher(:sig_dot_override?, <<~PATTERN)
  (send
    [!nil? #sig?]
    :override
    (hash <$(pair (sym :allow_incompatible) true) ...>)
  )
PATTERN