Class: RuboCop::Cop::Cask::EmptyArchArgument Private

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
RangeHelp
Defined in:
rubocops/cask/empty_arch_argument.rb

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 for empty strings in the arch stanza.

Example

# bad
arch arm: "-arm64", intel: ""

# good
arch arm: "-arm64"

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.

"Remove the empty `%<key>s:` argument from the `arch` stanza."
MSG_STANZA =

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.

"Remove the `arch` stanza as all its arguments are empty."

Instance Method Summary collapse

Instance Method Details

#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)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'rubocops/cask/empty_arch_argument.rb', line 26

def on_send(node)
  return if node.method_name != :arch || node.receiver
  return unless (hash = node.first_argument)&.hash_type?

  pairs = hash.pairs
  return if pairs.none? { |pair| empty_string_value?(pair) }

  if pairs.all? { |pair| empty_string_value?(pair) }
    add_offense(node, message: MSG_STANZA) do |corrector|
      corrector.remove(range_by_whole_lines(node.source_range, include_final_newline: true))
    end
    return
  end

  pairs.each_with_index do |pair, index|
    next unless empty_string_value?(pair)

    key = (pair.key.sym_type? || pair.key.str_type?) ? pair.key.value : pair.key.source

    add_offense(pair, message: format(MSG, key:)) do |corrector|
      range = if index.zero?
        pair.source_range.join(pairs.fetch(1).source_range.begin)
      else
        pairs.fetch(index - 1).source_range.end.join(pair.source_range.end)
      end
      corrector.remove(range)
    end
  end
end