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, #pretty_print, #to_json

Constructor Details

#initialize(lparen:, contents:, location:) ⇒ Paren

Returns a new instance of Paren.



8378
8379
8380
8381
8382
8383
# File 'lib/syntax_tree/node.rb', line 8378

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



8376
8377
8378
# File 'lib/syntax_tree/node.rb', line 8376

def comments
  @comments
end

#contentsObject (readonly)

nil | untyped

the expression inside the parentheses



8373
8374
8375
# File 'lib/syntax_tree/node.rb', line 8373

def contents
  @contents
end

#lparenObject (readonly)

LParen

the left parenthesis that opened this statement



8370
8371
8372
# File 'lib/syntax_tree/node.rb', line 8370

def lparen
  @lparen
end

Instance Method Details

#===(other) ⇒ Object



8432
8433
8434
8435
# File 'lib/syntax_tree/node.rb', line 8432

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

#accept(visitor) ⇒ Object



8385
8386
8387
# File 'lib/syntax_tree/node.rb', line 8385

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

#child_nodesObject Also known as: deconstruct



8389
8390
8391
# File 'lib/syntax_tree/node.rb', line 8389

def child_nodes
  [lparen, contents]
end

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



8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
# File 'lib/syntax_tree/node.rb', line 8393

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



8407
8408
8409
8410
8411
8412
8413
8414
# File 'lib/syntax_tree/node.rb', line 8407

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

#format(q) ⇒ Object



8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
# File 'lib/syntax_tree/node.rb', line 8416

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