Class: Prism::KeywordParameterNode

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

Overview

Represents a keyword parameter to a method, block, or lambda definition.

def a(b:)
      ^^
end

def a(b: 1)
      ^^^^
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, name_loc, value, location) ⇒ KeywordParameterNode

def initialize: (name: Symbol, name_loc: Location, value: Node?, location: Location) -> void



8493
8494
8495
8496
8497
8498
# File 'lib/prism/node.rb', line 8493

def initialize(name, name_loc, value, location)
  @name = name
  @name_loc = name_loc
  @value = value
  @location = location
end

Instance Attribute Details

#nameObject (readonly)

attr_reader name: Symbol



8484
8485
8486
# File 'lib/prism/node.rb', line 8484

def name
  @name
end

#name_locObject (readonly)

attr_reader name_loc: Location



8487
8488
8489
# File 'lib/prism/node.rb', line 8487

def name_loc
  @name_loc
end

#valueObject (readonly)

attr_reader value: Node?



8490
8491
8492
# File 'lib/prism/node.rb', line 8490

def value
  @value
end

Instance Method Details

#accept(visitor) ⇒ Object

def accept: (visitor: Visitor) -> void



8501
8502
8503
# File 'lib/prism/node.rb', line 8501

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

#child_nodesObject Also known as: deconstruct

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



8506
8507
8508
# File 'lib/prism/node.rb', line 8506

def child_nodes
  [value]
end

#comment_targetsObject

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



8518
8519
8520
# File 'lib/prism/node.rb', line 8518

def comment_targets
  [name_loc, *value]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



8511
8512
8513
8514
8515
# File 'lib/prism/node.rb', line 8511

def compact_child_nodes
  compact = []
  compact << value if value
  compact
end

#copy(**params) ⇒ Object

def copy: (**params) -> KeywordParameterNode



8523
8524
8525
8526
8527
8528
8529
8530
# File 'lib/prism/node.rb', line 8523

def copy(**params)
  KeywordParameterNode.new(
    params.fetch(:name) { name },
    params.fetch(:name_loc) { name_loc },
    params.fetch(:value) { value },
    params.fetch(:location) { location },
  )
end

#deconstruct_keys(keys) ⇒ Object

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



8536
8537
8538
# File 'lib/prism/node.rb', line 8536

def deconstruct_keys(keys)
  { name: name, name_loc: name_loc, value: value, location: location }
end

#inspect(inspector = NodeInspector.new) ⇒ Object



8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
# File 'lib/prism/node.rb', line 8540

def inspect(inspector = NodeInspector.new)
  inspector << inspector.header(self)
  inspector << "├── name: #{name.inspect}\n"
  inspector << "├── name_loc: #{inspector.location(name_loc)}\n"
  if (value = self.value).nil?
    inspector << "└── value: ∅\n"
  else
    inspector << "└── value:\n"
    inspector << value.inspect(inspector.child_inspector("    ")).delete_prefix(inspector.prefix)
  end
  inspector.to_str
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



8567
8568
8569
# File 'lib/prism/node.rb', line 8567

def type
  :keyword_parameter_node
end