class RuboCop::Cop::Sorbet::EnforceSignatures
This cop 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')
Public Class Methods
new(config = nil, options = nil)
click to toggle source
Calls superclass method
# File lib/rubocop/cop/sorbet/signatures/enforce_signatures.rb, line 30 def initialize(config = nil, options = nil) super(config, options) @last_sig_for_scope = {} end
Public Instance Methods
autocorrect(node)
click to toggle source
# File lib/rubocop/cop/sorbet/signatures/enforce_signatures.rb, line 55 def autocorrect(node) lambda do |corrector| suggest = SigSuggestion.new(node.loc.column, param_type_placeholder, return_type_placeholder) if node.is_a?(RuboCop::AST::DefNode) # def something node.arguments.each do |arg| suggest.params << arg.children.first end elsif accessor?(node) # attr reader, writer, accessor method = node.children[1] symbol = node.children[2] suggest.params << symbol.value if symbol && (method == :attr_writer || method == :attr_accessor) suggest.returns = 'void' if method == :attr_writer end corrector.insert_before(node.loc.expression, suggest.to_autocorrect) end end
on_block(node)
click to toggle source
# File lib/rubocop/cop/sorbet/signatures/enforce_signatures.rb, line 51 def on_block(node) @last_sig_for_scope[scope(node)] = node if signature?(node) end
on_def(node)
click to toggle source
# File lib/rubocop/cop/sorbet/signatures/enforce_signatures.rb, line 39 def on_def(node) check_node(node) end
on_defs(node)
click to toggle source
# File lib/rubocop/cop/sorbet/signatures/enforce_signatures.rb, line 43 def on_defs(node) check_node(node) end
on_send(node)
click to toggle source
# File lib/rubocop/cop/sorbet/signatures/enforce_signatures.rb, line 47 def on_send(node) check_node(node) if accessor?(node) end
scope(node)
click to toggle source
# File lib/rubocop/cop/sorbet/signatures/enforce_signatures.rb, line 74 def scope(node) return nil unless node.parent return node.parent if [:begin, :block, :class, :module].include?(node.parent.type) scope(node.parent) end
Private Instance Methods
check_node(node)
click to toggle source
# File lib/rubocop/cop/sorbet/signatures/enforce_signatures.rb, line 82 def check_node(node) scope = self.scope(node) unless @last_sig_for_scope[scope] add_offense( node, message: "Each method is required to have a signature." ) end @last_sig_for_scope[scope] = nil end
param_type_placeholder()
click to toggle source
# File lib/rubocop/cop/sorbet/signatures/enforce_signatures.rb, line 93 def param_type_placeholder cop_config['ParameterTypePlaceholder'] || 'T.untyped' end
return_type_placeholder()
click to toggle source
# File lib/rubocop/cop/sorbet/signatures/enforce_signatures.rb, line 97 def return_type_placeholder cop_config['ReturnTypePlaceholder'] || 'T.untyped' end