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:, comments: []) ⇒ Paren

Returns a new instance of Paren.



6983
6984
6985
6986
6987
6988
# File 'lib/syntax_tree/node.rb', line 6983

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



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

def comments
  @comments
end

#contentsObject (readonly)

nil | untyped

the expression inside the parentheses



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

def contents
  @contents
end

#lparenObject (readonly)

LParen

the left parenthesis that opened this statement



6975
6976
6977
# File 'lib/syntax_tree/node.rb', line 6975

def lparen
  @lparen
end

Instance Method Details

#accept(visitor) ⇒ Object



6990
6991
6992
# File 'lib/syntax_tree/node.rb', line 6990

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

#child_nodesObject Also known as: deconstruct



6994
6995
6996
# File 'lib/syntax_tree/node.rb', line 6994

def child_nodes
  [lparen, contents]
end

#deconstruct_keys(_keys) ⇒ Object



7000
7001
7002
7003
7004
7005
7006
7007
# File 'lib/syntax_tree/node.rb', line 7000

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

#format(q) ⇒ Object



7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
# File 'lib/syntax_tree/node.rb', line 7009

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

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

    q.breakable("")
    q.text(")")
  end
end