Class: RuboCop::Cop::Sorbet::RuntimeOnFailureDependsOnChecked

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

Overview

Checks that on_failure is not used without checked(:tests) or checked(:always).

Examples:


# bad
sig { params(x: Integer).returns(Integer).on_failure(:raise) }
def plus_one(x)
  x + 1
end

# good
sig { params(x: Integer).returns(Integer).checked(:always).on_failure(:raise) }
def plus_one(x)
  x + 1
end

Constant Summary collapse

MSG =
"To use .on_failure you must additionally call .checked(:tests) or .checked(:always), otherwise, the .on_failure has no effect."

Instance Method Summary collapse

Methods included from SignatureHelp

#bare_sig?, #on_block, #sig_with_runtime?, #sig_without_runtime?, #signature?

Instance Method Details

#checked_tests_or_always?(node) ⇒ Object



33
34
35
# File 'lib/rubocop/cop/sorbet/signatures/runtime_on_failure_depends_on_checked.rb', line 33

def_node_matcher :checked_tests_or_always?, <<~PATTERN
  (send _ :checked (sym {:tests | :always}))
PATTERN

#on_failure_call?(node) ⇒ Object



28
29
30
# File 'lib/rubocop/cop/sorbet/signatures/runtime_on_failure_depends_on_checked.rb', line 28

def_node_matcher :on_failure_call?, <<~PATTERN
  (send _ :on_failure ...)
PATTERN

#on_signature(node) ⇒ Object



37
38
39
40
41
42
# File 'lib/rubocop/cop/sorbet/signatures/runtime_on_failure_depends_on_checked.rb', line 37

def on_signature(node)
  return unless node.descendants.any? { |n| on_failure_call?(n) }
  return if node.descendants.any? { |n| checked_tests_or_always?(n) }

  add_offense(node)
end