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’)

Defined Under Namespace

Classes: RBSSignatureChecker, RBSSuggestion, SigSignatureChecker, SigSuggestion, SignatureChecker

Constant Summary collapse

VALID_STYLES =
["sig", "rbs", "both"].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



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

def_node_matcher(:accessor?, "  (send nil? {:attr_reader :attr_writer :attr_accessor} ...)\n")

#on_def(node) ⇒ Object



37
38
39
# File 'lib/rubocop/cop/sorbet/signatures/enforce_signatures.rb', line 37

def on_def(node)
  check_node(node)
end

#on_defs(node) ⇒ Object



41
42
43
# File 'lib/rubocop/cop/sorbet/signatures/enforce_signatures.rb', line 41

def on_defs(node)
  check_node(node)
end

#on_new_investigationObject



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

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

#on_send(node) ⇒ Object



45
46
47
# File 'lib/rubocop/cop/sorbet/signatures/enforce_signatures.rb', line 45

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

#on_signature(node) ⇒ Object



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

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

#scope(node) ⇒ Object



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

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

  scope(node.parent)
end