Class: RuboCop::Cop::Sorbet::EnforceSignatures
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::Sorbet::EnforceSignatures
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: Param, RBSSignatureChecker, RBSSuggestion, SigSignatureChecker, SigSuggestion, SignatureChecker
Constant Summary
collapse
- VALID_STYLES =
["sig", "rbs", "both"].freeze
- VALID_AUTOCORRECT_STYLES =
["sig", "rbs"].freeze
Instance Method Summary
collapse
#bare_sig?, #on_block, #sig_with_runtime?, #sig_without_runtime?, #signature?
Instance Method Details
#accessor?(node) ⇒ Object
40
41
42
|
# File 'lib/rubocop/cop/sorbet/signatures/enforce_signatures.rb', line 40
def_node_matcher(:accessor?, <<-PATTERN)
(send nil? {:attr_reader :attr_writer :attr_accessor} ...)
PATTERN
|
#on_def(node) ⇒ Object
44
45
46
|
# File 'lib/rubocop/cop/sorbet/signatures/enforce_signatures.rb', line 44
def on_def(node)
check_node(node)
end
|
#on_defs(node) ⇒ Object
48
49
50
|
# File 'lib/rubocop/cop/sorbet/signatures/enforce_signatures.rb', line 48
def on_defs(node)
check_node(node)
end
|
#on_new_investigation ⇒ Object
60
61
62
63
64
|
# File 'lib/rubocop/cop/sorbet/signatures/enforce_signatures.rb', line 60
def on_new_investigation
super
@sig_checker = nil
@rbs_checker = nil
end
|
#on_send(node) ⇒ Object
52
53
54
|
# File 'lib/rubocop/cop/sorbet/signatures/enforce_signatures.rb', line 52
def on_send(node)
check_node(node) if accessor?(node)
end
|
#on_signature(node) ⇒ Object
56
57
58
|
# File 'lib/rubocop/cop/sorbet/signatures/enforce_signatures.rb', line 56
def on_signature(node)
sig_checker.on_signature(node, scope(node))
end
|
#scope(node) ⇒ Object
66
67
68
69
70
71
|
# File 'lib/rubocop/cop/sorbet/signatures/enforce_signatures.rb', line 66
def scope(node)
return unless node.parent
return node.parent if [:begin, :block, :class, :module].include?(node.parent.type)
scope(node.parent)
end
|