Class: SyntaxTree::LBrace

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

Overview

LBrace represents the use of a left brace, i.e., {.

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #pretty_print, #to_json

Constructor Details

#initialize(value:, location:) ⇒ LBrace

Returns a new instance of LBrace.



7219
7220
7221
7222
7223
# File 'lib/syntax_tree/node.rb', line 7219

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



7217
7218
7219
# File 'lib/syntax_tree/node.rb', line 7217

def comments
  @comments
end

#valueObject (readonly)

String

the left brace



7214
7215
7216
# File 'lib/syntax_tree/node.rb', line 7214

def value
  @value
end

Class Method Details

.defaultObject

Because some nodes keep around a { token so that comments can be attached to it if they occur in the source, oftentimes an LBrace is a child of another node. This means it’s required at initialization time. To make it easier to create LBrace nodes without any specific value, this method provides a default node.



7263
7264
7265
# File 'lib/syntax_tree/node.rb', line 7263

def self.default
  new(value: "{", location: Location.default)
end

Instance Method Details

#===(other) ⇒ Object



7254
7255
7256
# File 'lib/syntax_tree/node.rb', line 7254

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

#accept(visitor) ⇒ Object



7225
7226
7227
# File 'lib/syntax_tree/node.rb', line 7225

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

#child_nodesObject Also known as: deconstruct



7229
7230
7231
# File 'lib/syntax_tree/node.rb', line 7229

def child_nodes
  []
end

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



7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
# File 'lib/syntax_tree/node.rb', line 7233

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

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

#deconstruct_keys(_keys) ⇒ Object



7246
7247
7248
# File 'lib/syntax_tree/node.rb', line 7246

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

#format(q) ⇒ Object



7250
7251
7252
# File 'lib/syntax_tree/node.rb', line 7250

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