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.



6933
6934
6935
6936
6937
6938
# File 'lib/syntax_tree/node.rb', line 6933

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

Instance Attribute Details

#comments ⇒ Object (readonly)

[Array[ Comment | EmbDoc ]] the comments attached to this node



6931
6932
6933
# File 'lib/syntax_tree/node.rb', line 6931

def comments
  @comments
end

#contents ⇒ Object (readonly)

[nil | untyped] the expression inside the parentheses



6928
6929
6930
# File 'lib/syntax_tree/node.rb', line 6928

def contents
  @contents
end

#lparen ⇒ Object (readonly)

[LParen] the left parenthesis that opened this statement



6925
6926
6927
# File 'lib/syntax_tree/node.rb', line 6925

def lparen
  @lparen
end

Instance Method Details

#accept(visitor) ⇒ Object



6940
6941
6942
# File 'lib/syntax_tree/node.rb', line 6940

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

#child_nodes ⇒ Object Also known as: deconstruct



6944
6945
6946
# File 'lib/syntax_tree/node.rb', line 6944

def child_nodes
  [lparen, contents]
end

#deconstruct_keys(_keys) ⇒ Object



6950
6951
6952
6953
6954
6955
6956
6957
# File 'lib/syntax_tree/node.rb', line 6950

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

#format(q) ⇒ Object



6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
# File 'lib/syntax_tree/node.rb', line 6959

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