Class: RuboCop::AST::HashNode

Inherits:
Node
  • Object
show all
Defined in:
lib/rubocop/ast/node/hash_node.rb

Overview

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.

Constant Summary

Constants inherited from Node

Node::BASIC_LITERALS, Node::COMPARISON_OPERATORS, Node::COMPOSITE_LITERALS, Node::FALSEY_LITERALS, Node::IMMUTABLE_LITERALS, Node::KEYWORDS, Node::LITERALS, Node::MUTABLE_LITERALS, Node::OPERATOR_KEYWORDS, Node::REFERENCES, Node::SPECIAL_KEYWORDS, Node::TRUTHY_LITERALS, Node::VARIABLES

Instance Method Summary collapse

Methods inherited from Node

#ancestors, #argument?, #asgn_method_call?, #basic_literal?, #binary_operation?, #chained?, #child_nodes, #complete!, #complete?, #const_name, def_matcher, #defined_module, #defined_module_name, #descendants, #each_ancestor, #each_child_node, #each_descendant, #each_node, #falsey_literal?, #immutable_literal?, #initialize, #keyword?, #keyword_bang?, #keyword_not?, #literal?, #multiline?, #mutable_literal?, #numeric_type?, #parent, #parent_module_name, #pure?, #receiver, #reference?, #sibling_index, #single_line?, #source, #source_range, #special_keyword?, #truthy_literal?, #unary_operation?, #updated, #value_used?, #variable?

Methods included from Sexp

#s

Constructor Details

This class inherits a constructor from RuboCop::AST::Node

Instance Method Details

#braces?Boolean

Checks whether the hash literal is delimited by curly braces.

Returns:

  • whether the hash literal is enclosed in braces



97
98
99
# File 'lib/rubocop/ast/node/hash_node.rb', line 97

def braces?
  loc.end
end

#each_keyself, Enumerator

Calls the given block for each key node in the hash literal. If no block is given, an Enumerator is returned.

Returns:

  • if a block is given

  • if no block is given



43
44
45
46
47
48
49
50
51
# File 'lib/rubocop/ast/node/hash_node.rb', line 43

def each_key
  return pairs.map(&:key).to_enum unless block_given?

  pairs.map(&:key).each do |key|
    yield key
  end

  self
end

#each_pairself, Enumerator

Calls the given block for each pair node in the hash literal. If no block is given, an Enumerator is returned.

Returns:

  • if a block is given

  • if no block is given



21
22
23
24
25
26
27
28
29
# File 'lib/rubocop/ast/node/hash_node.rb', line 21

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_valueself, Enumerator

Calls the given block for each value node in the hash literal. If no block is given, an Enumerator is returned.

Returns:

  • if a block is given

  • if no block is given



65
66
67
68
69
70
71
72
73
# File 'lib/rubocop/ast/node/hash_node.rb', line 65

def each_value
  return pairs.map(&:value).to_enum unless block_given?

  pairs.map(&:value).each do |value|
    yield value
  end

  self
end

#keysNode

Returns an array of all the keys in the hash literal.

Returns:

  • an array of keys in the hash literal



34
35
36
# File 'lib/rubocop/ast/node/hash_node.rb', line 34

def keys
  each_key.to_a
end

#mixed_delimiters?Boolean

Checks whether this hash uses a mix of hash rocket and colon delimiters for its pairs.

Returns:

  • whether the hash uses mixed delimiters



90
91
92
# File 'lib/rubocop/ast/node/hash_node.rb', line 90

def mixed_delimiters?
  pairs.map(&:delimiter).uniq.size > 1
end

#pairsArray<PairNode>

Returns an array of all the key value pairs in the hash literal.

Returns:

  • an array of pair nodes



12
13
14
# File 'lib/rubocop/ast/node/hash_node.rb', line 12

def pairs
  each_pair.to_a
end

#pairs_on_same_line?Boolean

Note:

A multiline pair is considered to be on the same line if it shares any of its lines with another pair

Checks whether any of the key value pairs in the hash literal are on the same line.

Returns:

  • whether any pair nodes are on the same line



82
83
84
# File 'lib/rubocop/ast/node/hash_node.rb', line 82

def pairs_on_same_line?
  pairs.each_cons(2).any? { |first, second| first.same_line?(second) }
end

#valuesNode

Returns an array of all the values in the hash literal.

Returns:

  • an array of values in the hash literal



56
57
58
# File 'lib/rubocop/ast/node/hash_node.rb', line 56

def values
  each_pair.map(&:value)
end