Class: SyntaxTree::Paren

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

Overview

Paren represents using balanced parentheses in a couple places in a Ruby program. In general parentheses can be used anywhere a Ruby expression can be used.

(1 + 2)

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(lparen:, contents:, location:) ⇒ Paren



8443
8444
8445
8446
8447
8448
# File 'lib/syntax_tree/node.rb', line 8443

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



8441
8442
8443
# File 'lib/syntax_tree/node.rb', line 8441

def comments
  @comments
end

#contentsObject (readonly)

nil | Node

the expression inside the parentheses



8438
8439
8440
# File 'lib/syntax_tree/node.rb', line 8438

def contents
  @contents
end

#lparenObject (readonly)

LParen

the left parenthesis that opened this statement



8435
8436
8437
# File 'lib/syntax_tree/node.rb', line 8435

def lparen
  @lparen
end

Instance Method Details

#===(other) ⇒ Object



8497
8498
8499
8500
# File 'lib/syntax_tree/node.rb', line 8497

def ===(other)
  other.is_a?(Paren) && lparen === other.lparen &&
    contents === other.contents
end

#accept(visitor) ⇒ Object



8450
8451
8452
# File 'lib/syntax_tree/node.rb', line 8450

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

#child_nodesObject Also known as: deconstruct



8454
8455
8456
# File 'lib/syntax_tree/node.rb', line 8454

def child_nodes
  [lparen, contents]
end

#copy(lparen: nil, contents: nil, location: nil) ⇒ Object



8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
# File 'lib/syntax_tree/node.rb', line 8458

def copy(lparen: nil, contents: nil, location: nil)
  node =
    Paren.new(
      lparen: lparen || self.lparen,
      contents: contents || self.contents,
      location: location || self.location
    )

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

#deconstruct_keys(_keys) ⇒ Object



8472
8473
8474
8475
8476
8477
8478
8479
# File 'lib/syntax_tree/node.rb', line 8472

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

#format(q) ⇒ Object



8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
# File 'lib/syntax_tree/node.rb', line 8481

def format(q)
  q.group do
    q.format(lparen)

    if contents && (!contents.is_a?(Params) || !contents.empty?)
      q.indent do
        q.breakable_empty
        q.format(contents)
      end
    end

    q.breakable_empty
    q.text(")")
  end
end