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::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
-
#braces? ⇒ Boolean
Checks whether the
hashliteral is delimited by curly braces. -
#each_key(&block) ⇒ self, Enumerator
Calls the given block for each
keynode in thehashliteral. -
#each_pair ⇒ self, Enumerator
Calls the given block for each
pairnode in thehashliteral. -
#each_value(&block) ⇒ self, Enumerator
Calls the given block for each
valuenode in thehashliteral. -
#empty? ⇒ Boolean
Checks whether the
hashnode contains anypair- orkwsplatnodes. -
#keys ⇒ Array<Node>
Returns an array of all the keys in the
hashliteral. -
#mixed_delimiters? ⇒ Boolean
Checks whether this
hashuses 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
hashliteral. -
#pairs_on_same_line? ⇒ Boolean
Checks whether any of the key value pairs in the
hashliteral are on the same line. -
#values ⇒ Array<Node>
Returns an array of all the values in the
hashliteral.
Methods inherited from Node
#ancestors, #any_block_type?, #any_def_type?, #any_match_pattern_type?, #any_str_type?, #any_sym_type?, #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?, #loc?, #loc_is?, #loop_keyword?, #match_guard_clause?, #module_definition?, #multiline?, #mutable_literal?, #nonempty_line_count, #numeric_type?, #operator_keyword?, #parent, #parent?, #parent_module_name, #parenthesized_call?, #post_condition_loop?, #proc?, #pure?, #range_type?, #receiver, #recursive_basic_literal?, #recursive_literal?, #reference?, #right_sibling, #right_siblings, #root?, #send_type?, #shorthand_asgn?, #sibling_index, #single_line?, #source, #source_length, #source_range, #special_keyword?, #str_content, #struct_constructor?, #truthy_literal?, #type?, #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
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.
117 118 119 |
# File 'lib/rubocop/ast/node/hash_node.rb', line 117 def braces? loc_is?(:end, '}') end |
#each_key(&block) ⇒ self, Enumerator
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.
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_pair ⇒ self, Enumerator
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.
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
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.
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
22 23 24 |
# File 'lib/rubocop/ast/node/hash_node.rb', line 22 def empty? children.empty? end |
#keys ⇒ Array<Node>
kwsplat nodes are ignored.
Returns an array of all the 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
kwsplat nodes are ignored.
Checks whether this hash uses a mix of hash rocket and colon delimiters for its pairs.
110 111 112 |
# File 'lib/rubocop/ast/node/hash_node.rb', line 110 def mixed_delimiters? pairs.map(&:delimiter).uniq.size > 1 end |
#pairs ⇒ Array<PairNode>
this may be different from children as kwsplat nodes are
Returns an array of all the key value pairs in the hash literal.
ignored.
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
A multiline pair is considered to be on the same line if it shares any of its lines with another pair
kwsplat nodes are ignored.
Checks whether any of the key value pairs in the hash literal 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 |
#values ⇒ Array<Node>
kwsplat nodes are ignored.
Returns an array of all the 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 |