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.



6986
6987
6988
6989
6990
6991
# File 'lib/syntax_tree/node.rb', line 6986

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



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

def comments
  @comments
end

#contentsObject (readonly)

nil | untyped

the expression inside the parentheses



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

def contents
  @contents
end

#lparenObject (readonly)

LParen

the left parenthesis that opened this statement



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

def lparen
  @lparen
end

Instance Method Details

#accept(visitor) ⇒ Object



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

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

#child_nodesObject Also known as: deconstruct



6997
6998
6999
# File 'lib/syntax_tree/node.rb', line 6997

def child_nodes
  [lparen, contents]
end

#deconstruct_keys(_keys) ⇒ Object



7003
7004
7005
7006
7007
7008
7009
7010
# File 'lib/syntax_tree/node.rb', line 7003

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

#format(q) ⇒ Object



7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
# File 'lib/syntax_tree/node.rb', line 7012

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