Class: RuboCop::Cop::Sorbet::EnforceSignatures

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
SignatureHelp
Defined in:
lib/rubocop/cop/sorbet/signatures/enforce_signatures.rb

Overview

Checks that every method definition and attribute accessor has a Sorbet signature.

It also suggest an autocorrect with placeholders so the following code:

def foo(a, b, c); end

Will be corrected as:

sig { params(a: T.untyped, b: T.untyped, c: T.untyped).returns(T.untyped)
def foo(a, b, c); end

You can configure the placeholders used by changing the following options:

  • ParameterTypePlaceholder: placeholders used for parameter types (default: 'T.untyped')
  • ReturnTypePlaceholder: placeholders used for return types (default: 'T.untyped')
  • Style: signature style to enforce - 'sig' for sig blocks, 'rbs' for RBS comments, 'both' to allow either (default: 'sig')
  • AutocorrectStyle: signature style to use when autocorrecting - 'sig' for sig blocks, 'rbs' for RBS comments (default: 'sig'). Only used when Style is 'both'.

Defined Under Namespace

Classes: RBSSignatureChecker, RBSSuggestion, SigSignatureChecker, SigSuggestion, SignatureChecker

Constant Summary collapse

VALID_STYLES =
["sig", "rbs", "both"].freeze
VALID_AUTOCORRECT_STYLES =
["sig", "rbs"].freeze

Instance Method Summary collapse

Methods included from SignatureHelp

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

Instance Method Details

#accessor?(node) ⇒ Object



35
36
37
# File 'lib/rubocop/cop/sorbet/signatures/enforce_signatures.rb', line 35

def_node_matcher(:accessor?, <<-PATTERN)
  (send nil? {:attr_reader :attr_writer :attr_accessor} ...)
PATTERN

#on_def(node) ⇒ Object



39
40
41
# File 'lib/rubocop/cop/sorbet/signatures/enforce_signatures.rb', line 39

def on_def(node)
  check_node(node)
end

#on_defs(node) ⇒ Object



43
44
45
# File 'lib/rubocop/cop/sorbet/signatures/enforce_signatures.rb', line 43

def on_defs(node)
  check_node(node)
end

#on_new_investigationObject



55
56
57
58
59
# File 'lib/rubocop/cop/sorbet/signatures/enforce_signatures.rb', line 55

def on_new_investigation
  super
  @sig_checker = nil
  @rbs_checker = nil
end

#on_send(node) ⇒ Object



47
48
49
# File 'lib/rubocop/cop/sorbet/signatures/enforce_signatures.rb', line 47

def on_send(node)
  check_node(node) if accessor?(node)
end

#on_signature(node) ⇒ Object



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

def on_signature(node)
  sig_checker.on_signature(node, scope(node))
end

#scope(node) ⇒ Object



61
62
63
64
65
66
# File 'lib/rubocop/cop/sorbet/signatures/enforce_signatures.rb', line 61

def scope(node)
  return unless node.parent
  return node.parent if [:begin, :block, :class, :module].include?(node.parent.type)

  scope(node.parent)
end