class RuboCop::AST::HashNode

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.

Public Instance Methods

braces?() click to toggle source

Checks whether the `hash` literal is delimited by curly braces.

@return [Boolean] whether the `hash` literal is enclosed in braces

# File lib/rubocop/ast/node/hash_node.rb, line 117
def braces?
  loc.end&.is?('}')
end
each_key(&block) click to toggle source

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

# File lib/rubocop/ast/node/hash_node.rb, line 59
def each_key(&block)
  return pairs.map(&:key).to_enum unless block

  pairs.map(&:key).each(&block)

  self
end
each_pair() { |*pair| ... } click to toggle source

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

# File lib/rubocop/ast/node/hash_node.rb, line 33
def each_pair
  return each_child_node(:pair).to_enum unless block_given?

  each_child_node(:pair) do |pair|
    yield(*pair)
  end

  self
end
each_value(&block) click to toggle source

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

# File lib/rubocop/ast/node/hash_node.rb, line 83
def each_value(&block)
  return pairs.map(&:value).to_enum unless block

  pairs.map(&:value).each(&block)

  self
end
empty?() click to toggle source

Checks whether the `hash` node contains any `pair`- or `kwsplat` nodes.

@return whether the `hash` is empty

# File lib/rubocop/ast/node/hash_node.rb, line 22
def empty?
  children.empty?
end
keys() click to toggle source

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

# File lib/rubocop/ast/node/hash_node.rb, line 48
def keys
  each_key.to_a
end
mixed_delimiters?() click to toggle source

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

# File lib/rubocop/ast/node/hash_node.rb, line 110
def mixed_delimiters?
  pairs.map(&:delimiter).uniq.size > 1
end
pairs() click to toggle source

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

# File lib/rubocop/ast/node/hash_node.rb, line 15
def pairs
  each_pair.to_a
end
pairs_on_same_line?() click to toggle source

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

# File lib/rubocop/ast/node/hash_node.rb, line 100
def pairs_on_same_line?
  pairs.each_cons(2).any? { |first, second| first.same_line?(second) }
end
values() click to toggle source

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

# File lib/rubocop/ast/node/hash_node.rb, line 72
def values
  each_pair.map(&:value)
end