Class: RuboCop::AST::HashNode
| Relationships & Source Files | |
| Super Chains via Extension / Inclusion / Inheritance | |
|
Class Chain:
self,
Node,
NodePattern::Macros,
Parser::AST::Node
|
|
|
Instance Chain:
|
|
| Inherits: |
RuboCop::AST::Node
|
| 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
Node - Inherited
ASSIGNMENTS, BASIC_CONDITIONALS, BASIC_LITERALS, COMPARISON_OPERATORS, COMPOSITE_LITERALS, CONDITIONALS, EMPTY_CHILDREN, EMPTY_PROPERTIES, EQUALS_ASSIGNMENTS, FALSEY_LITERALS, GROUP_FOR_TYPE, IMMUTABLE_LITERALS, KEYWORDS, LITERALS, LITERAL_RECURSIVE_METHODS, LITERAL_RECURSIVE_TYPES, LOOP_TYPES, MUTABLE_LITERALS, OPERATOR_KEYWORDS, POST_CONDITION_LOOP_TYPES, REFERENCES, SHORTHAND_ASSIGNMENTS, SPECIAL_KEYWORDS, TRUTHY_LITERALS, VARIABLES
Class Method Summary
Node - Inherited
| .new, | |
| .def_recursive_literal_predicate | Define a |
NodePattern::Macros - Extended
| def_node_matcher | Define a method which applies a pattern to an AST node. |
| def_node_search | Define a method which recurses over the descendants of an AST node, checking whether any of them match the provided pattern. |
Instance Attribute Summary
-
#braces? ⇒ Boolean
readonly
Checks whether the
hashliteral is delimited by curly braces. -
#empty? ⇒ Boolean
readonly
Checks whether the
hashnode contains anypair- orkwsplatnodes. -
#mixed_delimiters? ⇒ Boolean
readonly
Checks whether this
hashuses a mix of hash rocket and colon delimiters for its pairs. -
#pairs_on_same_line? ⇒ Boolean
readonly
Checks whether any of the key value pairs in the
hashliteral are on the same line.
Node - Inherited
| #any_block_type?, #any_def_type?, #any_match_pattern_type?, #any_str_type?, #any_sym_type?, #argument?, #argument_type?, #assignment?, #basic_conditional?, #basic_literal?, #boolean_type?, #call_type?, #chained?, #complete?, #conditional?, #empty_source?, #equals_asgn?, #falsey_literal?, #guard_clause?, #immutable_literal?, #keyword?, #literal?, | |||
| #loop_keyword? |
|
||
| #multiline? | Predicates. |
||
| #mutable_literal?, #numeric_type?, #operator_keyword?, | |||
| #parent | Returns the parent node, or |
||
| #parent?, #parenthesized_call?, #post_condition_loop?, | |||
| #pure? | Some expressions are evaluated for their value, some for their side effects, and some for both. |
||
| #range_type?, #reference?, #root?, | |||
| #send_type? | Most nodes are of 'send' type, so this method is defined separately to make this check as fast as possible. |
||
| #shorthand_asgn?, #single_line?, #special_keyword?, #truthy_literal?, | |||
| #value_used? | Some expressions are evaluated for their value, some for their side effects, and some for both If we know that an expression is useful only for its side effects, that means we can transform it in ways which preserve the side effects, but change the return value So, does the return value of this node matter? If we changed it to |
||
| #variable?, #begin_value_used?, #case_if_value_used?, #for_value_used?, #while_until_value_used? | |||
Instance Method Summary
-
#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. -
#keys ⇒ Array<Node>
Returns an array of all the keys in the
hashliteral. -
#pairs ⇒ Array<PairNode>
Returns an array of all the key value pairs in the
hashliteral. -
#values ⇒ Array<Node>
Returns an array of all the values in the
hashliteral.
Node - Inherited
| #ancestors | Returns an array of ancestor nodes. |
||
| #assignment_or_similar? | Some cops treat the shovel operator as a kind of assignment. |
||
| #class_constructor?, #class_definition?, #complete!, #const_name, #defined_module, #defined_module_name, | |||
| #each_ancestor | Calls the given block for each ancestor node from parent to root. |
||
| #first_line, #global_const?, #lambda?, #lambda_or_proc?, #last_line, | |||
| #left_sibling | Use is discouraged, this is a potentially slow method and can lead to even slower algorithms. |
||
| #left_siblings | Use is discouraged, this is a potentially slow method and can lead to even slower algorithms. |
||
| #line_count, | |||
| #loc? | Shortcut to safely check if a location is present. |
||
| #loc_is? | Shortcut to safely test a particular location, even if this location does not exist or is |
||
| #match_guard_clause?, #module_definition?, | |||
| #node_parts | Common destructuring method. |
||
| #nonempty_line_count, | |||
| #parent_module_name | Searching the AST. |
||
| #proc?, #receiver, #recursive_basic_literal?, #recursive_literal?, | |||
| #right_sibling | Use is discouraged, this is a potentially slow method and can lead to even slower algorithms. |
||
| #right_siblings | Use is discouraged, this is a potentially slow method and can lead to even slower algorithms. |
||
| #sibling_index | Returns the index of the receiver node in its siblings. |
||
| #source |
|
||
| #source_length, #source_range, #str_content, #struct_constructor?, | |||
| #type? | Determine if the node is one of several node types in a single query Allows specific single node types, as well as "grouped" types (e.g. |
||
| #updated | Override Node#updated so that |
||
| #defined_module0, #new_class_or_module_block?, #parent_module_name_for_block, #parent_module_name_for_sclass, #parent_module_name_part, #visit_ancestors | |||
Descendence - Included
| #child_nodes | Returns an array of child nodes. |
| #descendants | Returns an array of descendant nodes. |
| #each_child_node | Calls the given block for each child node. |
| #each_descendant | Calls the given block for each descendant node with depth first order. |
| #each_node | Calls the given block for the receiver and each descendant node in depth-first order. |
Sexp - Included
Constructor Details
This class inherits a constructor from RuboCop::AST::Node
Instance Attribute Details
#braces? ⇒ Boolean (readonly)
Checks whether the hash literal is delimited by curly braces.
# File 'lib/rubocop/ast/node/hash_node.rb', line 117
def braces? loc_is?(:end, '}') end
#empty? ⇒ Boolean (readonly)
Checks whether the hash node contains any pair- or kwsplat nodes.
@return[Boolean] whether the hash is empty
# File 'lib/rubocop/ast/node/hash_node.rb', line 22
def empty? children.empty? end
#mixed_delimiters? ⇒ Boolean (readonly)
kwsplat nodes are ignored.
Checks whether this hash uses a mix of hash rocket and colon
delimiters for its pairs.
# File 'lib/rubocop/ast/node/hash_node.rb', line 110
def mixed_delimiters? pairs.map(&:delimiter).uniq.size > 1 end
#pairs_on_same_line? ⇒ Boolean (readonly)
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.
# 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
Instance Method Details
#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.
#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.
# 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.
#keys ⇒ Array<Node>
kwsplat nodes are ignored.
Returns an array of all the keys in the hash literal.
# File 'lib/rubocop/ast/node/hash_node.rb', line 48
def keys each_key.to_a 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.
# File 'lib/rubocop/ast/node/hash_node.rb', line 15
def pairs each_pair.to_a end
#values ⇒ Array<Node>
kwsplat nodes are ignored.
Returns an array of all the values in the hash literal.
# File 'lib/rubocop/ast/node/hash_node.rb', line 72
def values each_pair.map(&:value) end