Class: RuboCop::Cop::Homebrew::NoUntypedKeywordRest Private

Inherits:
Base
  • Object
show all
Defined in:
rubocops/no_untyped_keyword_rest.rb,
sorbet/rbi/dsl/rubo_cop/cop/homebrew/no_untyped_keyword_rest.rbi

Overview

This class is part of a private API. This class may only be used in the Homebrew/brew repository. Third parties should avoid using this class if possible, as it may be removed or changed without warning.

Flags the T.untyped a sig gives a keyword rest parameter such as **options. Sorbet checks no keyword passed to such a method, so a mistyped one only fails at runtime, in whichever code path reaches it. Name the keywords the method accepts instead.

Where arbitrary keywords are the point, such as stdlib options or API query parameters, type the values T.anything so the method has to narrow them before use. Disable it on the sig line, with a comment saying why, where neither fits.

Example

# bad
sig { params(args: String, options: T.untyped).returns(SystemCommand::Result) }
def curl(*args, **options)
  curl_with_workarounds(*args, **options)
end

# good
sig { params(args: String, secrets: T::Array[String]).returns(SystemCommand::Result) }
def curl(*args, secrets: [])
  curl_with_workarounds(*args, secrets:)
end

Constant Summary collapse

MSG =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

"Replace `**%<name>s` typed `T.untyped` with explicit keyword arguments so Sorbet checks them."

Instance Method Summary collapse

Instance Method Details

#on_def(node) ⇒ void Also known as: on_defs

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:

  • node (RuboCop::AST::DefNode)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'rubocops/no_untyped_keyword_rest.rb', line 48

def on_def(node)
  keyword_rest = node.arguments.find(&:kwrestarg_type?)
  # An anonymous `**` has no name for a `sig` to type.
  name = keyword_rest&.name
  return if name.nil?

  parent = node.parent
  # `private def foo` and `private_class_method def self.foo` wrap the definition in a send.
  definition = (parent.is_a?(RuboCop::AST::SendNode) && parent.adjacent_def_modifier?) ? parent : node
  # `left_sibling` scans every sibling, so only reach it for a named `**`.
  signature = definition.left_sibling
  return if !signature.is_a?(RuboCop::AST::BlockNode) || !signature?(signature)

  # Follow the builder chain so a `T.proc.params` nested in the hash is never reached.
  builder = T.let(signature.body, T.nilable(RuboCop::AST::Node))
  builder = builder.receiver while builder.is_a?(RuboCop::AST::SendNode) && !builder.method?(:params)
  untyped_type = untyped_param_type(builder, name) if builder
  return if untyped_type.nil?

  add_offense(untyped_type, message: format(MSG, name:))
end

#signature?(node, **kwargs, &block) ⇒ T.untyped

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:

  • (T.untyped)


10
# File 'sorbet/rbi/dsl/rubo_cop/cop/homebrew/no_untyped_keyword_rest.rbi', line 10

def signature?(node, **kwargs, &block); end

#untyped_param_type(node, *pattern, **kwargs, &block) ⇒ T.untyped

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:

  • (T.untyped)


20
# File 'sorbet/rbi/dsl/rubo_cop/cop/homebrew/no_untyped_keyword_rest.rbi', line 20

def untyped_param_type(node, *pattern, **kwargs, &block); end