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.



6898
6899
6900
6901
6902
6903
# File 'lib/syntax_tree/node.rb', line 6898

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



6896
6897
6898
# File 'lib/syntax_tree/node.rb', line 6896

def comments
  @comments
end

#contentsObject (readonly)

nil | untyped

the expression inside the parentheses



6893
6894
6895
# File 'lib/syntax_tree/node.rb', line 6893

def contents
  @contents
end

#lparenObject (readonly)

LParen

the left parenthesis that opened this statement



6890
6891
6892
# File 'lib/syntax_tree/node.rb', line 6890

def lparen
  @lparen
end

Instance Method Details

#accept(visitor) ⇒ Object



6905
6906
6907
# File 'lib/syntax_tree/node.rb', line 6905

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

#child_nodesObject Also known as: deconstruct



6909
6910
6911
# File 'lib/syntax_tree/node.rb', line 6909

def child_nodes
  [lparen, contents]
end

#deconstruct_keys(_keys) ⇒ Object



6915
6916
6917
6918
6919
6920
6921
6922
# File 'lib/syntax_tree/node.rb', line 6915

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

#format(q) ⇒ Object



6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
# File 'lib/syntax_tree/node.rb', line 6924

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