Class: SyntaxTree::Kw

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

Kw represents the use of a keyword. It can be almost anywhere in the syntax tree, so you end up seeing it quite a lot.

if value
end

In the above example, there would be two Kw nodes: one for the if and one for the end. Note that anything that matches the list of keywords in Ruby will use a Kw, so if you use a keyword in a symbol literal for instance:

:if

then the contents of the symbol node will contain a Kw node.

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid

Constructor Details

#initialize(value:, location:) ⇒ Kw

Returns a new instance of Kw.



6948
6949
6950
6951
6952
6953
# File 'lib/syntax_tree/node.rb', line 6948

def initialize(value:, location:)
  @value = value
  @name = value.to_sym
  @location = location
  @comments = []
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



6946
6947
6948
# File 'lib/syntax_tree/node.rb', line 6946

def comments
  @comments
end

#nameObject (readonly)

Symbol

the symbol version of the value



6943
6944
6945
# File 'lib/syntax_tree/node.rb', line 6943

def name
  @name
end

#valueObject (readonly)

String

the value of the keyword



6940
6941
6942
# File 'lib/syntax_tree/node.rb', line 6940

def value
  @value
end

Instance Method Details

#===(other) ⇒ Object



6981
6982
6983
# File 'lib/syntax_tree/node.rb', line 6981

def ===(other)
  other.is_a?(Kw) && value === other.value
end

#accept(visitor) ⇒ Object



6955
6956
6957
# File 'lib/syntax_tree/node.rb', line 6955

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

#child_nodesObject Also known as: deconstruct



6959
6960
6961
# File 'lib/syntax_tree/node.rb', line 6959

def child_nodes
  []
end

#copy(value: nil, location: nil) ⇒ Object



6963
6964
6965
6966
6967
6968
6969
# File 'lib/syntax_tree/node.rb', line 6963

def copy(value: nil, location: nil)
  node =
    Kw.new(value: value || self.value, location: location || self.location)

  node.comments.concat(comments.map(&:copy))
  node
end

#deconstruct_keys(_keys) ⇒ Object



6973
6974
6975
# File 'lib/syntax_tree/node.rb', line 6973

def deconstruct_keys(_keys)
  { value: value, location: location, comments: comments }
end

#format(q) ⇒ Object



6977
6978
6979
# File 'lib/syntax_tree/node.rb', line 6977

def format(q)
  q.text(value)
end