Class: Prism::SymbolNode

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

Overview

Represents a symbol literal or a symbol contained within a ‘%i` list.

:foo
^^^^

%i[foo]
   ^^^

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opening_loc, value_loc, closing_loc, unescaped, location) ⇒ SymbolNode

def initialize: (opening_loc: Location?, value_loc: Location?, closing_loc: Location?, unescaped: String, location: Location) -> void



13515
13516
13517
13518
13519
13520
13521
# File 'lib/prism/node.rb', line 13515

def initialize(opening_loc, value_loc, closing_loc, unescaped, location)
  @opening_loc = opening_loc
  @value_loc = value_loc
  @closing_loc = closing_loc
  @unescaped = unescaped
  @location = location
end

Instance Attribute Details

#closing_locObject (readonly)

attr_reader closing_loc: Location?



13509
13510
13511
# File 'lib/prism/node.rb', line 13509

def closing_loc
  @closing_loc
end

#opening_locObject (readonly)

attr_reader opening_loc: Location?



13503
13504
13505
# File 'lib/prism/node.rb', line 13503

def opening_loc
  @opening_loc
end

#unescapedObject (readonly)

attr_reader unescaped: String



13512
13513
13514
# File 'lib/prism/node.rb', line 13512

def unescaped
  @unescaped
end

#value_locObject (readonly)

attr_reader value_loc: Location?



13506
13507
13508
# File 'lib/prism/node.rb', line 13506

def value_loc
  @value_loc
end

Instance Method Details

#accept(visitor) ⇒ Object

def accept: (visitor: Visitor) -> void



13524
13525
13526
# File 'lib/prism/node.rb', line 13524

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

#child_nodesObject Also known as: deconstruct

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



13529
13530
13531
# File 'lib/prism/node.rb', line 13529

def child_nodes
  []
end

#closingObject

def closing: () -> String?



13573
13574
13575
# File 'lib/prism/node.rb', line 13573

def closing
  closing_loc&.slice
end

#comment_targetsObject

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



13539
13540
13541
# File 'lib/prism/node.rb', line 13539

def comment_targets
  [*opening_loc, *value_loc, *closing_loc]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



13534
13535
13536
# File 'lib/prism/node.rb', line 13534

def compact_child_nodes
  []
end

#copy(**params) ⇒ Object

def copy: (**params) -> SymbolNode



13544
13545
13546
13547
13548
13549
13550
13551
13552
# File 'lib/prism/node.rb', line 13544

def copy(**params)
  SymbolNode.new(
    params.fetch(:opening_loc) { opening_loc },
    params.fetch(:value_loc) { value_loc },
    params.fetch(:closing_loc) { closing_loc },
    params.fetch(:unescaped) { unescaped },
    params.fetch(:location) { location },
  )
end

#deconstruct_keys(keys) ⇒ Object

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



13558
13559
13560
# File 'lib/prism/node.rb', line 13558

def deconstruct_keys(keys)
  { opening_loc: opening_loc, value_loc: value_loc, closing_loc: closing_loc, unescaped: unescaped, location: location }
end

#inspect(inspector = NodeInspector.new) ⇒ Object



13577
13578
13579
13580
13581
13582
13583
13584
# File 'lib/prism/node.rb', line 13577

def inspect(inspector = NodeInspector.new)
  inspector << inspector.header(self)
  inspector << "├── opening_loc: #{inspector.location(opening_loc)}\n"
  inspector << "├── value_loc: #{inspector.location(value_loc)}\n"
  inspector << "├── closing_loc: #{inspector.location(closing_loc)}\n"
  inspector << "└── unescaped: #{unescaped.inspect}\n"
  inspector.to_str
end

#openingObject

def opening: () -> String?



13563
13564
13565
# File 'lib/prism/node.rb', line 13563

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



13600
13601
13602
# File 'lib/prism/node.rb', line 13600

def type
  :symbol_node
end

#valueObject

def value: () -> String?



13568
13569
13570
# File 'lib/prism/node.rb', line 13568

def value
  value_loc&.slice
end