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

Defined Under Namespace

Classes: SigSuggestion

Instance Method Summary collapse

Methods included from SignatureHelp

#on_block, #signature?, #with_runtime?, #without_runtime?

Constructor Details

#initialize(config = nil, options = nil) ⇒ EnforceSignatures

Returns a new instance of EnforceSignatures.



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

def initialize(config = nil, options = nil)
  super(config, options)
  @last_sig_for_scope = {}
end

Instance Method Details

#accessor?(node) ⇒ Object



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

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

#on_def(node) ⇒ Object



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

def on_def(node)
  check_node(node)
end

#on_defs(node) ⇒ Object



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

def on_defs(node)
  check_node(node)
end

#on_send(node) ⇒ Object



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

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

#on_signature(node) ⇒ Object



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

def on_signature(node)
  @last_sig_for_scope[scope(node)] = node
end

#scope(node) ⇒ Object



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

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

  scope(node.parent)
end