Class: Prism::FlipFlopNode

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

Overview

Represents the use of the ‘..` or `…` operators to create flip flops.

baz if foo .. bar
       ^^^^^^^^^^

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(left, right, operator_loc, flags, location) ⇒ FlipFlopNode

def initialize: (left: Node?, right: Node?, operator_loc: Location, flags: Integer, location: Location) -> void



5447
5448
5449
5450
5451
5452
5453
# File 'lib/prism/node.rb', line 5447

def initialize(left, right, operator_loc, flags, location)
  @left = left
  @right = right
  @operator_loc = operator_loc
  @flags = flags
  @location = location
end

Instance Attribute Details

#leftObject (readonly)

attr_reader left: Node?



5435
5436
5437
# File 'lib/prism/node.rb', line 5435

def left
  @left
end

#operator_locObject (readonly)

attr_reader operator_loc: Location



5441
5442
5443
# File 'lib/prism/node.rb', line 5441

def operator_loc
  @operator_loc
end

#rightObject (readonly)

attr_reader right: Node?



5438
5439
5440
# File 'lib/prism/node.rb', line 5438

def right
  @right
end

Instance Method Details

#accept(visitor) ⇒ Object

def accept: (visitor: Visitor) -> void



5456
5457
5458
# File 'lib/prism/node.rb', line 5456

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

#child_nodesObject Also known as: deconstruct

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



5461
5462
5463
# File 'lib/prism/node.rb', line 5461

def child_nodes
  [left, right]
end

#comment_targetsObject

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



5474
5475
5476
# File 'lib/prism/node.rb', line 5474

def comment_targets
  [*left, *right, operator_loc]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



5466
5467
5468
5469
5470
5471
# File 'lib/prism/node.rb', line 5466

def compact_child_nodes
  compact = []
  compact << left if left
  compact << right if right
  compact
end

#copy(**params) ⇒ Object

def copy: (**params) -> FlipFlopNode



5479
5480
5481
5482
5483
5484
5485
5486
5487
# File 'lib/prism/node.rb', line 5479

def copy(**params)
  FlipFlopNode.new(
    params.fetch(:left) { left },
    params.fetch(:right) { right },
    params.fetch(:operator_loc) { operator_loc },
    params.fetch(:flags) { flags },
    params.fetch(:location) { location },
  )
end

#deconstruct_keys(keys) ⇒ Object

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



5493
5494
5495
# File 'lib/prism/node.rb', line 5493

def deconstruct_keys(keys)
  { left: left, right: right, operator_loc: operator_loc, flags: flags, location: location }
end

#exclude_end?Boolean

def exclude_end?: () -> bool

Returns:

  • (Boolean)


5503
5504
5505
# File 'lib/prism/node.rb', line 5503

def exclude_end?
  flags.anybits?(RangeFlags::EXCLUDE_END)
end

#inspect(inspector = NodeInspector.new) ⇒ Object



5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
# File 'lib/prism/node.rb', line 5507

def inspect(inspector = NodeInspector.new)
  inspector << inspector.header(self)
  if (left = self.left).nil?
    inspector << "├── left: ∅\n"
  else
    inspector << "├── left:\n"
    inspector << left.inspect(inspector.child_inspector("")).delete_prefix(inspector.prefix)
  end
  if (right = self.right).nil?
    inspector << "├── right: ∅\n"
  else
    inspector << "├── right:\n"
    inspector << right.inspect(inspector.child_inspector("")).delete_prefix(inspector.prefix)
  end
  inspector << "├── operator_loc: #{inspector.location(operator_loc)}\n"
  flags = [("exclude_end" if exclude_end?)].compact
  inspector << "└── flags: #{flags.empty? ? "" : flags.join(", ")}\n"
  inspector.to_str
end

#operatorObject

def operator: () -> String



5498
5499
5500
# File 'lib/prism/node.rb', line 5498

def operator
  operator_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



5541
5542
5543
# File 'lib/prism/node.rb', line 5541

def type
  :flip_flop_node
end