1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 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 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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
# frozen_string_literal: true
module RuboCop
module AST
# A node extension for `hash` nodes. This will be used in place of a plain
# node when the builder constructs the AST, making its methods available
# to all `hash` nodes within RuboCop.
class HashNode < Node
# Returns an array of all the key value pairs in the `hash` literal.
#
# @note this may be different from children as `kwsplat` nodes are
# ignored.
#
# @return [Array<PairNode>] an array of `pair` nodes
def pairs
each_pair.to_a
end
# Checks whether the `hash` node contains any `pair`- or `kwsplat` nodes.
#
# @return[Boolean] whether the `hash` is empty
def empty?
children.empty?
end
# Calls the given block for each `pair` node in the `hash` literal.
# If no block is given, an `Enumerator` is returned.
#
# @note `kwsplat` nodes are ignored.
#
# @return [self] if a block is given
# @return [Enumerator] if no block is given
def each_pair
return each_child_node(:pair).to_enum unless block_given?
each_child_node(:pair) do |pair|
yield(*pair)
end
self
end
# Returns an array of all the keys in the `hash` literal.
#
# @note `kwsplat` nodes are ignored.
#
# @return [Array<Node>] an array of keys in the `hash` literal
def keys
each_key.to_a
end
# Calls the given block for each `key` node in the `hash` literal.
# If no block is given, an `Enumerator` is returned.
#
# @note `kwsplat` nodes are ignored.
#
# @return [self] if a block is given
# @return [Enumerator] if no block is given
def each_key(&block)
return pairs.map(&:key).to_enum unless block_given?
pairs.map(&:key).each(&block)
self
end
# Returns an array of all the values in the `hash` literal.
#
# @note `kwsplat` nodes are ignored.
#
# @return [Array<Node>] an array of values in the `hash` literal
def values
each_pair.map(&:value)
end
# Calls the given block for each `value` node in the `hash` literal.
# If no block is given, an `Enumerator` is returned.
#
# @note `kwsplat` nodes are ignored.
#
# @return [self] if a block is given
# @return [Enumerator] if no block is given
def each_value(&block)
return pairs.map(&:value).to_enum unless block_given?
pairs.map(&:value).each(&block)
self
end
# Checks whether any of the key value pairs in the `hash` literal are on
# the same line.
#
# @note A multiline `pair` is considered to be on the same line if it
# shares any of its lines with another `pair`
#
# @note `kwsplat` nodes are ignored.
#
# @return [Boolean] whether any `pair` nodes are on the same line
def pairs_on_same_line?
pairs.each_cons(2).any? { |first, second| first.same_line?(second) }
end
# Checks whether this `hash` uses a mix of hash rocket and colon
# delimiters for its pairs.
#
# @note `kwsplat` nodes are ignored.
#
# @return [Boolean] whether the `hash` uses mixed delimiters
def mixed_delimiters?
pairs.map(&:delimiter).uniq.size > 1
end
# Checks whether the `hash` literal is delimited by curly braces.
#
# @return [Boolean] whether the `hash` literal is enclosed in braces
def braces?
loc.end&.is?('}')
end
end
end
end
|