Class: Prism::HashPatternNode
- Inherits:
-
PrismNode
- Object
- PrismNode
- Prism::HashPatternNode
- Defined in:
- lib/prism/node.rb,
ext/prism/api_node.c
Overview
Represents a hash pattern in pattern matching.
foo => { a: 1, b: 2 }
^^^^^^^^^^^^^^
foo => { a: 1, b: 2, **c }
^^^^^^^^^^^^^^^^^^^
foo => Bar[a: 1, b: 2]
^^^^^^^^^^^^^^^
foo in { a: 1, b: 2 }
^^^^^^^^^^^^^^
Instance Attribute Summary collapse
-
#constant ⇒ Object
readonly
Represents the optional constant preceding the Hash.
-
#elements ⇒ Object
readonly
Represents the explicit named hash keys and values.
-
#rest ⇒ Object
readonly
Represents the rest of the Hash keys and values.
Class Method Summary collapse
-
.type ⇒ Object
Return a symbol representation of this node type.
Instance Method Summary collapse
-
#===(other) ⇒ Object
Implements case-equality for the node.
-
#accept(visitor) ⇒ Object
def accept: (Visitor visitor) -> void.
-
#child_nodes ⇒ Object
(also: #deconstruct)
def child_nodes: () -> Array.
-
#closing ⇒ Object
def closing: () -> String?.
-
#closing_loc ⇒ Object
The location of the closing brace.
-
#comment_targets ⇒ Object
def comment_targets: () -> Array[Node | Location].
-
#compact_child_nodes ⇒ Object
def compact_child_nodes: () -> Array.
-
#copy(node_id: self.node_id, location: self.location, flags: self.flags, constant: self.constant, elements: self.elements, rest: self.rest, opening_loc: self.opening_loc, closing_loc: self.closing_loc) ⇒ Object
def copy: (?node_id: Integer, ?location: Location, ?flags: Integer, ?constant: ConstantPathNode | ConstantReadNode | nil, ?elements: Array, ?rest: AssocSplatNode | NoKeywordsParameterNode | nil, ?opening_loc: Location?, ?closing_loc: Location?) -> HashPatternNode.
- #deconstruct_keys(keys) ⇒ Object
-
#each_child_node {|constant| ... } ⇒ Object
def each_child_node: () { (Prism::node) -> void } -> void | () -> Enumerator.
-
#initialize(source, node_id, location, flags, constant, elements, rest, opening_loc, closing_loc) ⇒ HashPatternNode
constructor
Initialize a new HashPatternNode node.
-
#inspect ⇒ Object
def inspect -> String.
-
#opening ⇒ Object
def opening: () -> String?.
-
#opening_loc ⇒ Object
The location of the opening brace.
-
#save_closing_loc(repository) ⇒ Object
Save the closing_loc location using the given saved source so that it can be retrieved later.
-
#save_opening_loc(repository) ⇒ Object
Save the opening_loc location using the given saved source so that it can be retrieved later.
-
#type ⇒ Object
Return a symbol representation of this node type.
Constructor Details
#initialize(source, node_id, location, flags, constant, elements, rest, opening_loc, closing_loc) ⇒ HashPatternNode
Initialize a new HashPatternNode node.
8904 8905 8906 8907 8908 8909 8910 8911 8912 8913 8914 |
# File 'lib/prism/node.rb', line 8904 def initialize(source, node_id, location, flags, constant, elements, rest, opening_loc, closing_loc) @source = source @node_id = node_id @location = location @flags = flags @constant = constant @elements = elements @rest = rest @opening_loc = opening_loc @closing_loc = closing_loc end |
Instance Attribute Details
#constant ⇒ Object (readonly)
Represents the optional constant preceding the Hash.
foo => Bar[a: 1, b: 2]
^^^
foo => Bar::Baz[a: 1, b: 2]
^^^^^^^^
8969 8970 8971 |
# File 'lib/prism/node.rb', line 8969 def constant @constant end |
#elements ⇒ Object (readonly)
Represents the explicit named hash keys and values.
foo => { a: 1, b:, ** }
^^^^^^^^
8975 8976 8977 |
# File 'lib/prism/node.rb', line 8975 def elements @elements end |
#rest ⇒ Object (readonly)
Represents the rest of the Hash keys and values. This can be named, unnamed, or explicitly forbidden via ‘**nil`, this last one results in a NoKeywordsParameterNode.
foo => { a: 1, b:, **c }
^^^
foo => { a: 1, b:, ** }
^^
foo => { a: 1, b:, **nil }
^^^^^
8987 8988 8989 |
# File 'lib/prism/node.rb', line 8987 def rest @rest end |
Class Method Details
.type ⇒ Object
Return a symbol representation of this node type. See Node::type.
9060 9061 9062 |
# File 'lib/prism/node.rb', line 9060 def self.type :hash_pattern_node end |
Instance Method Details
#===(other) ⇒ Object
Implements case-equality for the node. This is effectively == but without comparing the value of locations. Locations are checked only for presence.
9066 9067 9068 9069 9070 9071 9072 9073 9074 |
# File 'lib/prism/node.rb', line 9066 def ===(other) other.is_a?(HashPatternNode) && (constant === other.constant) && (elements.length == other.elements.length) && elements.zip(other.elements).all? { |left, right| left === right } && (rest === other.rest) && (opening_loc.nil? == other.opening_loc.nil?) && (closing_loc.nil? == other.closing_loc.nil?) end |
#accept(visitor) ⇒ Object
def accept: (Visitor visitor) -> void
8917 8918 8919 |
# File 'lib/prism/node.rb', line 8917 def accept(visitor) visitor.visit_hash_pattern_node(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
def child_nodes: () -> Array
8922 8923 8924 |
# File 'lib/prism/node.rb', line 8922 def child_nodes [constant, *elements, rest] end |
#closing ⇒ Object
def closing: () -> String?
9045 9046 9047 |
# File 'lib/prism/node.rb', line 9045 def closing closing_loc&.slice end |
#closing_loc ⇒ Object
The location of the closing brace.
foo => { a: 1 }
^
foo => Bar[a: 1]
^
9021 9022 9023 9024 9025 9026 9027 9028 9029 9030 9031 |
# File 'lib/prism/node.rb', line 9021 def closing_loc location = @closing_loc case location when nil nil when Location location else @closing_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF) end end |
#comment_targets ⇒ Object
def comment_targets: () -> Array[Node | Location]
8945 8946 8947 |
# File 'lib/prism/node.rb', line 8945 def comment_targets [*constant, *elements, *rest, *opening_loc, *closing_loc] #: Array[Prism::node | Location] end |
#compact_child_nodes ⇒ Object
def compact_child_nodes: () -> Array
8936 8937 8938 8939 8940 8941 8942 |
# File 'lib/prism/node.rb', line 8936 def compact_child_nodes compact = [] #: Array[Prism::node] compact << constant if constant compact.concat(elements) compact << rest if rest compact end |
#copy(node_id: self.node_id, location: self.location, flags: self.flags, constant: self.constant, elements: self.elements, rest: self.rest, opening_loc: self.opening_loc, closing_loc: self.closing_loc) ⇒ Object
def copy: (?node_id: Integer, ?location: Location, ?flags: Integer, ?constant: ConstantPathNode | ConstantReadNode | nil, ?elements: Array, ?rest: AssocSplatNode | NoKeywordsParameterNode | nil, ?opening_loc: Location?, ?closing_loc: Location?) -> HashPatternNode
8950 8951 8952 |
# File 'lib/prism/node.rb', line 8950 def copy(node_id: self.node_id, location: self.location, flags: self.flags, constant: self.constant, elements: self.elements, rest: self.rest, opening_loc: self.opening_loc, closing_loc: self.closing_loc) HashPatternNode.new(source, node_id, location, flags, constant, elements, rest, opening_loc, closing_loc) end |
#deconstruct_keys(keys) ⇒ Object
8958 8959 8960 |
# File 'lib/prism/node.rb', line 8958 def deconstruct_keys(keys) { node_id: node_id, location: location, constant: constant, elements: elements, rest: rest, opening_loc: opening_loc, closing_loc: closing_loc } end |
#each_child_node {|constant| ... } ⇒ Object
def each_child_node: () { (Prism::node) -> void } -> void | () -> Enumerator
8927 8928 8929 8930 8931 8932 8933 |
# File 'lib/prism/node.rb', line 8927 def each_child_node return to_enum(:each_child_node) unless block_given? yield constant if constant elements.each { |node| yield node } yield rest if rest end |
#inspect ⇒ Object
def inspect -> String
9050 9051 9052 |
# File 'lib/prism/node.rb', line 9050 def inspect InspectVisitor.compose(self) end |
#opening ⇒ Object
def opening: () -> String?
9040 9041 9042 |
# File 'lib/prism/node.rb', line 9040 def opening opening_loc&.slice end |
#opening_loc ⇒ Object
The location of the opening brace.
foo => { a: 1 }
^
foo => Bar[a: 1]
^
8996 8997 8998 8999 9000 9001 9002 9003 9004 9005 9006 |
# File 'lib/prism/node.rb', line 8996 def opening_loc location = @opening_loc case location when nil nil when Location location else @opening_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF) end end |
#save_closing_loc(repository) ⇒ Object
Save the closing_loc location using the given saved source so that it can be retrieved later.
9035 9036 9037 |
# File 'lib/prism/node.rb', line 9035 def save_closing_loc(repository) repository.enter(node_id, :closing_loc) unless @closing_loc.nil? end |
#save_opening_loc(repository) ⇒ Object
Save the opening_loc location using the given saved source so that it can be retrieved later.
9010 9011 9012 |
# File 'lib/prism/node.rb', line 9010 def save_opening_loc(repository) repository.enter(node_id, :opening_loc) unless @opening_loc.nil? end |
#type ⇒ Object
Return a symbol representation of this node type. See ‘Node#type`.
9055 9056 9057 |
# File 'lib/prism/node.rb', line 9055 def type :hash_pattern_node end |