Class: SyntaxTree::ArgParen

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

ArgParen represents wrapping arguments to a method inside a set of parentheses.

method(argument)

In the example above, there would be an ArgParen node around the Args node that represents the set of arguments being sent to the method method. The argument child node can be nil if no arguments were passed, as in:

method()

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(arguments:, location:) ⇒ ArgParen

Returns a new instance of ArgParen.



725
726
727
728
729
# File 'lib/syntax_tree/node.rb', line 725

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

Instance Attribute Details

#argumentsObject (readonly)

nil | Args | ArgsForward

the arguments inside the

parentheses



720
721
722
# File 'lib/syntax_tree/node.rb', line 720

def arguments
  @arguments
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



723
724
725
# File 'lib/syntax_tree/node.rb', line 723

def comments
  @comments
end

Instance Method Details

#===(other) ⇒ Object



774
775
776
# File 'lib/syntax_tree/node.rb', line 774

def ===(other)
  other.is_a?(ArgParen) && arguments === other.arguments
end

#accept(visitor) ⇒ Object



731
732
733
# File 'lib/syntax_tree/node.rb', line 731

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

#child_nodesObject Also known as: deconstruct



735
736
737
# File 'lib/syntax_tree/node.rb', line 735

def child_nodes
  [arguments]
end

#copy(arguments: nil, location: nil) ⇒ Object



739
740
741
742
743
744
745
746
747
748
# File 'lib/syntax_tree/node.rb', line 739

def copy(arguments: nil, location: nil)
  node =
    ArgParen.new(
      arguments: arguments || self.arguments,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end

#deconstruct_keys(_keys) ⇒ Object



752
753
754
# File 'lib/syntax_tree/node.rb', line 752

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

#format(q) ⇒ Object



756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
# File 'lib/syntax_tree/node.rb', line 756

def format(q)
  unless arguments
    q.text("()")
    return
  end

  q.text("(")
  q.group do
    q.indent do
      q.breakable_empty
      q.format(arguments)
      q.if_break { q.text(",") } if q.trailing_comma? && trailing_comma?
    end
    q.breakable_empty
  end
  q.text(")")
end