Class: Prism::FindPatternNode

Inherits:
PrismNode
  • Object
show all
Defined in:
lib/prism/node.rb,
ext/prism/api_node.c

Overview

Represents a find pattern in pattern matching.

foo in *bar, baz, *qux
^^^^^^^^^^^^^^^^^^^^^^

foo in [*bar, baz, *qux]
^^^^^^^^^^^^^^^^^^^^^^^^

foo in Foo(*bar, baz, *qux)
^^^^^^^^^^^^^^^^^^^^^^^^^^^

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(constant, left, requireds, right, opening_loc, closing_loc, location) ⇒ FindPatternNode

def initialize: (constant: Node?, left: Node, requireds: Array, right: Node, opening_loc: Location?, closing_loc: Location?, location: Location) -> void



5326
5327
5328
5329
5330
5331
5332
5333
5334
# File 'lib/prism/node.rb', line 5326

def initialize(constant, left, requireds, right, opening_loc, closing_loc, location)
  @constant = constant
  @left = left
  @requireds = requireds
  @right = right
  @opening_loc = opening_loc
  @closing_loc = closing_loc
  @location = location
end

Instance Attribute Details

#closing_locObject (readonly)

attr_reader closing_loc: Location?



5323
5324
5325
# File 'lib/prism/node.rb', line 5323

def closing_loc
  @closing_loc
end

#constantObject (readonly)

attr_reader constant: Node?



5308
5309
5310
# File 'lib/prism/node.rb', line 5308

def constant
  @constant
end

#leftObject (readonly)

attr_reader left: Node



5311
5312
5313
# File 'lib/prism/node.rb', line 5311

def left
  @left
end

#opening_locObject (readonly)

attr_reader opening_loc: Location?



5320
5321
5322
# File 'lib/prism/node.rb', line 5320

def opening_loc
  @opening_loc
end

#requiredsObject (readonly)

attr_reader requireds: Array



5314
5315
5316
# File 'lib/prism/node.rb', line 5314

def requireds
  @requireds
end

#rightObject (readonly)

attr_reader right: Node



5317
5318
5319
# File 'lib/prism/node.rb', line 5317

def right
  @right
end

Instance Method Details

#accept(visitor) ⇒ Object

def accept: (visitor: Visitor) -> void



5337
5338
5339
# File 'lib/prism/node.rb', line 5337

def accept(visitor)
  visitor.visit_find_pattern_node(self)
end

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array[nil | Node]



5342
5343
5344
# File 'lib/prism/node.rb', line 5342

def child_nodes
  [constant, left, *requireds, right]
end

#closingObject

def closing: () -> String?



5388
5389
5390
# File 'lib/prism/node.rb', line 5388

def closing
  closing_loc&.slice
end

#comment_targetsObject

def comment_targets: () -> Array[Node | Location]



5357
5358
5359
# File 'lib/prism/node.rb', line 5357

def comment_targets
  [*constant, left, *requireds, right, *opening_loc, *closing_loc]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



5347
5348
5349
5350
5351
5352
5353
5354
# File 'lib/prism/node.rb', line 5347

def compact_child_nodes
  compact = []
  compact << constant if constant
  compact << left
  compact.concat(requireds)
  compact << right
  compact
end

#copy(**params) ⇒ Object

def copy: (**params) -> FindPatternNode



5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
# File 'lib/prism/node.rb', line 5362

def copy(**params)
  FindPatternNode.new(
    params.fetch(:constant) { constant },
    params.fetch(:left) { left },
    params.fetch(:requireds) { requireds },
    params.fetch(:right) { right },
    params.fetch(:opening_loc) { opening_loc },
    params.fetch(:closing_loc) { closing_loc },
    params.fetch(:location) { location },
  )
end

#deconstruct_keys(keys) ⇒ Object

def deconstruct_keys: (keys: Array) -> Hash[Symbol, nil | Node | Array | String | Token | Array | Location]



5378
5379
5380
# File 'lib/prism/node.rb', line 5378

def deconstruct_keys(keys)
  { constant: constant, left: left, requireds: requireds, right: right, opening_loc: opening_loc, closing_loc: closing_loc, location: location }
end

#inspect(inspector = NodeInspector.new) ⇒ Object



5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
# File 'lib/prism/node.rb', line 5392

def inspect(inspector = NodeInspector.new)
  inspector << inspector.header(self)
  if (constant = self.constant).nil?
    inspector << "├── constant: ∅\n"
  else
    inspector << "├── constant:\n"
    inspector << constant.inspect(inspector.child_inspector("")).delete_prefix(inspector.prefix)
  end
  inspector << "├── left:\n"
  inspector << inspector.child_node(left, "")
  inspector << "├── requireds: #{inspector.list("#{inspector.prefix}", requireds)}"
  inspector << "├── right:\n"
  inspector << inspector.child_node(right, "")
  inspector << "├── opening_loc: #{inspector.location(opening_loc)}\n"
  inspector << "└── closing_loc: #{inspector.location(closing_loc)}\n"
  inspector.to_str
end

#openingObject

def opening: () -> String?



5383
5384
5385
# File 'lib/prism/node.rb', line 5383

def opening
  opening_loc&.slice
end

#typeObject

Sometimes you want to check an instance of a node against a list of classes to see what kind of behavior to perform. Usually this is done by calling ‘[cls1, cls2].include?(node.class)` or putting the node into a case statement and doing `case node; when cls1; when cls2; end`. Both of these approaches are relatively slow because of the constant lookups, method calls, and/or array allocations.

Instead, you can call #type, which will return to you a symbol that you can use for comparison. This is faster than the other approaches because it uses a single integer comparison, but also because if you’re on CRuby you can take advantage of the fact that case statements with all symbol keys will use a jump table.

def type: () -> Symbol



5424
5425
5426
# File 'lib/prism/node.rb', line 5424

def type
  :find_pattern_node
end