Class: RuboCop::Cop::Cask::EmptyConditionalArgument Private

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

Example

# bad
arch arm: "-arm64", intel: ""
arch_var = on_arch_conditional arm: "", intel: "-x86_64"
os macos: "-darwin", linux: ""
os_var = on_system_conditional macos: "", linux: "-gnu"

# good
arch arm: "-arm64"
arch_var = on_arch_conditional intel: "-x86_64"
os macos: "-darwin"
os_var = on_system_conditional linux: "-gnu"

Constant Summary collapse

MSG_KEY =

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 `%<stanza>s` 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 `%<stanza>s` stanza as all its arguments are empty."
SYSTEM_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, :os].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
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.

T.let(
  [*SYSTEM_STANZAS, *ON_CONDITIONAL_STANZAS].freeze,
  T::Array[Symbol],
)

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/empty_conditional_argument.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)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'rubocops/cask/empty_conditional_argument.rb', line 39

def on_send(node)
  stanza = node.method_name
  return if !CONDITIONAL_STANZAS.include?(stanza) || node.receiver
  return unless (hash = node.first_argument)&.hash_type?

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

  if SYSTEM_STANZAS.include?(stanza) && pairs.all? { |pair| empty_string_value?(pair) }
    add_offense(node, message: format(MSG_STANZA, stanza:)) do |corrector|
      corrector.remove(range_by_whole_lines(node.source_range, include_final_newline: true))
    end
    return
  elsif ON_CONDITIONAL_STANZAS.include?(stanza) && pairs.all? { |pair| empty_string_value?(pair) }
    if conditional_variable?(node.parent)
      add_offense(node.parent, message: format(MSG_STANZA, stanza:)) do |corrector|
        corrector.remove(range_by_whole_lines(node.parent.source_range, include_final_newline: true))
      end
    else
      add_offense(node, message: format(MSG_STANZA, stanza:)) do |corrector|
        corrector.replace(node.source_range, '""')
      end
    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, stanza:, 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