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::ARGUMENT_TYPES, Node::ASSIGNMENTS, Node::BASIC_CONDITIONALS, Node::BASIC_LITERALS, Node::COMPARISON_OPERATORS, Node::COMPOSITE_LITERALS, Node::CONDITIONALS, Node::EQUALS_ASSIGNMENTS, Node::FALSEY_LITERALS, Node::IMMUTABLE_LITERALS, Node::KEYWORDS, Node::LITERALS, Node::LOOP_TYPES, Node::MUTABLE_LITERALS, Node::OPERATOR_KEYWORDS, Node::POST_CONDITION_LOOP_TYPES, Node::REFERENCES, Node::SHORTHAND_ASSIGNMENTS, Node::SPECIAL_KEYWORDS, Node::TRUTHY_LITERALS, Node::VARIABLES

Instance Method Summary collapse

Methods inherited from Node

#ancestors, #argument?, #argument_type?, #assignment?, #assignment_or_similar?, #basic_conditional?, #basic_literal?, #boolean_type?, #call_type?, #chained?, #class_constructor?, #class_definition?, #complete!, #complete?, #conditional?, #const_name, #defined_module, #defined_module_name, #each_ancestor, #empty_source?, #equals_asgn?, #falsey_literal?, #first_line, #global_const?, #guard_clause?, #immutable_literal?, #initialize, #keyword?, #lambda?, #lambda_or_proc?, #last_line, #left_sibling, #left_siblings, #line_count, #literal?, #loop_keyword?, #match_guard_clause?, #module_definition?, #multiline?, #mutable_literal?, #node_parts, #nonempty_line_count, #numeric_type?, #operator_keyword?, #parent, #parent?, #parent_module_name, #parenthesized_call?, #post_condition_loop?, #proc?, #pure?, #range_type?, #receiver, #reference?, #right_sibling, #right_siblings, #root?, #shorthand_asgn?, #sibling_index, #single_line?, #source, #source_length, #source_range, #special_keyword?, #str_content, #struct_constructor?, #truthy_literal?, #updated, #value_used?, #variable?

Methods included from NodePattern::Macros

#def_node_matcher, #def_node_search

Methods included from Descendence

#child_nodes, #descendants, #each_child_node, #each_descendant, #each_node

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:

  • (Boolean)

    whether the ‘hash` literal is enclosed in braces



117
118
119
# File 'lib/rubocop/ast/node/hash_node.rb', line 117

def braces?
  loc.end&.is?('}')
end

#each_key(&block) ⇒ self, Enumerator

Note:

‘kwsplat` nodes are ignored.

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

Returns:

  • (self)

    if a block is given

  • (Enumerator)

    if no block is given



59
60
61
62
63
64
65
# 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_pairself, Enumerator

Note:

‘kwsplat` nodes are ignored.

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

Returns:

  • (self)

    if a block is given

  • (Enumerator)

    if no block is given



33
34
35
36
37
38
39
40
41
# 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) ⇒ self, Enumerator

Note:

‘kwsplat` nodes are ignored.

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

Returns:

  • (self)

    if a block is given

  • (Enumerator)

    if no block is given



83
84
85
86
87
88
89
# 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?Boolean

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

@return whether the ‘hash` is empty

Returns:

  • (Boolean)


22
23
24
# File 'lib/rubocop/ast/node/hash_node.rb', line 22

def empty?
  children.empty?
end

#keysArray<Node>

Note:

‘kwsplat` nodes are ignored.

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

Returns:

  • (Array<Node>)

    an array of keys in the ‘hash` literal



48
49
50
# File 'lib/rubocop/ast/node/hash_node.rb', line 48

def keys
  each_key.to_a
end

#mixed_delimiters?Boolean

Note:

‘kwsplat` nodes are ignored.

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

Returns:

  • (Boolean)

    whether the ‘hash` uses mixed delimiters



110
111
112
# File 'lib/rubocop/ast/node/hash_node.rb', line 110

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

#pairsArray<PairNode>

Note:

this may be different from children as ‘kwsplat` nodes are

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

ignored.

Returns:

  • (Array<PairNode>)

    an array of ‘pair` nodes



15
16
17
# File 'lib/rubocop/ast/node/hash_node.rb', line 15

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`

Note:

‘kwsplat` nodes are ignored.

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

Returns:

  • (Boolean)

    whether any ‘pair` nodes are on the same line



100
101
102
# 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

#valuesArray<Node>

Note:

‘kwsplat` nodes are ignored.

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

Returns:

  • (Array<Node>)

    an array of values in the ‘hash` literal



72
73
74
# File 'lib/rubocop/ast/node/hash_node.rb', line 72

def values
  each_pair.map(&:value)
end