Class: RuboCop::Cop::Homebrew::ApiNameMembership Private

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
rubocops/api_name_membership.rb,
sorbet/rbi/dsl/rubo_cop/cop/homebrew/api_name_membership.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.

Enforces the use of Homebrew::API.formula_name?/Homebrew::API.cask_token? over membership checks on Homebrew::API.formula_names/Homebrew::API.cask_tokens, which allocate and linearly scan an array of every name in the API.

Example

# bad
Homebrew::API.formula_names.include?(name)
Homebrew::API.cask_tokens.exclude?(token)

# good
Homebrew::API.formula_name?(name)
!Homebrew::API.cask_token?(token)

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.

"Use `Homebrew::API.%<predicate>s` instead of scanning `Homebrew::API.%<list>s`."
RESTRICT_ON_SEND =

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.

[:include?, :exclude?].freeze
PREDICATES =

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.

T.let({
  formula_names: "formula_name?",
  cask_tokens:   "cask_token?",
}.freeze, T::Hash[Symbol, String])

Instance Method Summary collapse

Instance Method Details

#api_name_membership?(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/api_name_membership.rbi', line 10

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

#on_send(node) ⇒ void

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::SendNode)


42
43
44
45
46
47
48
49
50
# File 'rubocops/api_name_membership.rb', line 42

def on_send(node)
  return unless (api, list, arg = api_name_membership?(node))

  predicate = PREDICATES.fetch(list)
  add_offense(node, message: format(MSG, predicate:, list:)) do |corrector|
    negation = (node.method_name == :exclude?) ? "!" : ""
    corrector.replace(node, "#{negation}#{api.source}.#{predicate}(#{arg.source})")
  end
end