Class: RuboCop::AST::HashNode
- 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::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::MUTABLE_LITERALS, Node::OPERATOR_KEYWORDS, Node::REFERENCES, Node::SHORTHAND_ASSIGNMENTS, Node::SPECIAL_KEYWORDS, Node::TRUTHY_LITERALS, Node::VARIABLES
Instance Method Summary collapse
-
#braces? ⇒ Boolean
Checks whether the ‘hash` literal is delimited by curly braces.
-
#each_key ⇒ self, Enumerator
Calls the given block for each ‘key` node in the `hash` literal.
-
#each_pair ⇒ self, Enumerator
Calls the given block for each ‘pair` node in the `hash` literal.
-
#each_value ⇒ self, Enumerator
Calls the given block for each ‘value` node in the `hash` literal.
-
#empty? ⇒ Boolean
Checks whether the ‘hash` node contains any `pair`- or `kwsplat` nodes.
-
#keys ⇒ Array<Node>
Returns an array of all the keys in the ‘hash` literal.
-
#mixed_delimiters? ⇒ Boolean
Checks whether this ‘hash` uses a mix of hash rocket and colon delimiters for its pairs.
-
#pairs ⇒ Array<PairNode>
Returns an array of all the key value pairs in the ‘hash` literal.
-
#pairs_on_same_line? ⇒ Boolean
Checks whether any of the key value pairs in the ‘hash` literal are on the same line.
-
#values ⇒ Array<Node>
Returns an array of all the values in the ‘hash` literal.
Methods inherited from Node
#ancestors, #argument?, #assignment?, #basic_conditional?, #basic_literal?, #chained?, #child_nodes, #complete!, #complete?, #conditional?, #const_name, #defined_module, #defined_module_name, #descendants, #each_ancestor, #each_child_node, #each_descendant, #each_node, #empty_source?, #equals_asgn?, #falsey_literal?, #first_line, #immutable_literal?, #initialize, #keyword?, #last_line, #line_count, #literal?, #multiline?, #mutable_literal?, #node_parts, #nonempty_line_count, #numeric_type?, #operator_keyword?, #parent, #parent_module_name, #parenthesized_call?, #pure?, #receiver, #reference?, #shorthand_asgn?, #sibling_index, #single_line?, #source, #source_length, #source_range, #special_keyword?, #truthy_literal?, #updated, #value_used?, #variable?
Methods included from NodePattern::Macros
#def_node_matcher, #def_node_search, #node_search, #node_search_all, #node_search_body, #node_search_first
Methods included from Sexp
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.
104 105 106 |
# File 'lib/rubocop/ast/node/hash_node.rb', line 104 def braces? loc.end && loc.end.is?('}') end |
#each_key ⇒ self, Enumerator
Calls the given block for each ‘key` node in the `hash` literal. If no block is given, an `Enumerator` is returned.
50 51 52 53 54 55 56 57 58 |
# File 'lib/rubocop/ast/node/hash_node.rb', line 50 def each_key return pairs.map(&:key).to_enum unless block_given? pairs.map(&:key).each do |key| yield key end self end |
#each_pair ⇒ self, Enumerator
Calls the given block for each ‘pair` node in the `hash` literal. If no block is given, an `Enumerator` is returned.
28 29 30 31 32 33 34 35 36 |
# File 'lib/rubocop/ast/node/hash_node.rb', line 28 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 ⇒ self, Enumerator
Calls the given block for each ‘value` node in the `hash` literal. If no block is given, an `Enumerator` is returned.
72 73 74 75 76 77 78 79 80 |
# File 'lib/rubocop/ast/node/hash_node.rb', line 72 def each_value return pairs.map(&:value).to_enum unless block_given? pairs.map(&:value).each do |value| yield value end self end |
#empty? ⇒ Boolean
Checks whether the ‘hash` node contains any `pair`- or `kwsplat` nodes.
@return whether the ‘hash` is empty
19 20 21 |
# File 'lib/rubocop/ast/node/hash_node.rb', line 19 def empty? children.empty? end |
#keys ⇒ Array<Node>
Returns an array of all the keys in the ‘hash` literal.
41 42 43 |
# File 'lib/rubocop/ast/node/hash_node.rb', line 41 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.
97 98 99 |
# File 'lib/rubocop/ast/node/hash_node.rb', line 97 def mixed_delimiters? pairs.map(&:delimiter).uniq.size > 1 end |
#pairs ⇒ Array<PairNode>
Returns an array of all the key value pairs in the ‘hash` literal.
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
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.
89 90 91 |
# File 'lib/rubocop/ast/node/hash_node.rb', line 89 def pairs_on_same_line? pairs.each_cons(2).any? { |first, second| first.same_line?(second) } end |
#values ⇒ Array<Node>
Returns an array of all the values in the ‘hash` literal.
63 64 65 |
# File 'lib/rubocop/ast/node/hash_node.rb', line 63 def values each_pair.map(&:value) end |