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.



8343
8344
8345
8346
8347
8348
# File 'lib/syntax_tree/node.rb', line 8343

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



8341
8342
8343
# File 'lib/syntax_tree/node.rb', line 8341

def comments
  @comments
end

#contentsObject (readonly)

nil | untyped

the expression inside the parentheses



8338
8339
8340
# File 'lib/syntax_tree/node.rb', line 8338

def contents
  @contents
end

#lparenObject (readonly)

LParen

the left parenthesis that opened this statement



8335
8336
8337
# File 'lib/syntax_tree/node.rb', line 8335

def lparen
  @lparen
end

Instance Method Details

#===(other) ⇒ Object



8397
8398
8399
8400
# File 'lib/syntax_tree/node.rb', line 8397

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

#accept(visitor) ⇒ Object



8350
8351
8352
# File 'lib/syntax_tree/node.rb', line 8350

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

#child_nodesObject Also known as: deconstruct



8354
8355
8356
# File 'lib/syntax_tree/node.rb', line 8354

def child_nodes
  [lparen, contents]
end

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



8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
# File 'lib/syntax_tree/node.rb', line 8358

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



8372
8373
8374
8375
8376
8377
8378
8379
# File 'lib/syntax_tree/node.rb', line 8372

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

#format(q) ⇒ Object



8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
# File 'lib/syntax_tree/node.rb', line 8381

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