Class: RuboCop::Cop::Cask::ConditionalArgumentsOrder Private

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
RangeHelp
Defined in:
rubocops/cask/conditional_arguments_order.rb,
rubocops/cask/conditional_arguments_order.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.

This cop checks that the keys of a cask's arch/on_arch_conditional and os/on_system_conditional stanza are ordered like the on_<system> blocks: ARM before Intel, and macOS before Linux.

Example

# bad
arch intel: "...", arm: "..."
arch_var = on_arch_conditional intel: "...", arm: "..."
os linux: "...", macos: "..."
os_var = on_system_conditional linux: "...", macos: "..."

# good
arch arm: "...", intel: "..."
arch_var = on_arch_conditional arm: "...", intel: "..."
os macos: "...", linux: "..."
os_var = on_system_conditional macos: "...", linux: "..."

Constant Summary collapse

MESSAGE =

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.

"`%<stanza>s` keys should be ordered: %<keys>s"
ARCH_STANZAS =

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.

[:arch, :on_arch_conditional].freeze
OS_STANZAS =

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.

[:os, :on_system_conditional].freeze
ON_CONDITIONAL_STANZAS =

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.

[:on_arch_conditional, :on_system_conditional].freeze
ARCH_ORDER =

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.

[:arm, :intel].freeze
OS_ORDER =

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.

[:macos, :linux].freeze

Instance Method Summary collapse

Instance Method Details

#conditional_variable?(base_node, &block) ⇒ Boolean

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:

  • (Boolean)


13
# File 'rubocops/cask/conditional_arguments_order.rbi', line 13

def conditional_variable?(base_node, &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)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'rubocops/cask/conditional_arguments_order.rb', line 40

def on_send(node)
  stanza = node.method_name
  order = case stanza
  when *ARCH_STANZAS then ARCH_ORDER
  when *OS_STANZAS then OS_ORDER
  else return
  end
  return if node.receiver || !(hash = node.first_argument)&.hash_type?

  pairs = hash.pairs
  return unless pairs.all? { |pair| pair.key.sym_type? && order.include?(pair.key.value) }

  sorted = pairs.each_with_index
                .sort_by { |pair, index| [order.index(pair.key.value), index] }
                .map(&:first)
  return if pairs == sorted

  node = node.parent if conditional_variable?(node.parent)
  add_offense(node, message: format(MESSAGE, stanza:, keys: order.join(", "))) do |corrector|
    corrector.replace(hash.source_range, rebuild(sorted))
  end
end