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, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid

Constructor Details

#initialize(arguments:, location:) ⇒ ArgParen

Returns a new instance of ArgParen.



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

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

Instance Attribute Details

#argumentsObject (readonly)

nil | Args | ArgsForward

the arguments inside the

parentheses



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

def arguments
  @arguments
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



733
734
735
# File 'lib/syntax_tree/node.rb', line 733

def comments
  @comments
end

Instance Method Details

#===(other) ⇒ Object



784
785
786
# File 'lib/syntax_tree/node.rb', line 784

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

#accept(visitor) ⇒ Object



741
742
743
# File 'lib/syntax_tree/node.rb', line 741

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

#arityObject



788
789
790
# File 'lib/syntax_tree/node.rb', line 788

def arity
  arguments&.arity || 0
end

#child_nodesObject Also known as: deconstruct



745
746
747
# File 'lib/syntax_tree/node.rb', line 745

def child_nodes
  [arguments]
end

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



749
750
751
752
753
754
755
756
757
758
# File 'lib/syntax_tree/node.rb', line 749

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



762
763
764
# File 'lib/syntax_tree/node.rb', line 762

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

#format(q) ⇒ Object



766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
# File 'lib/syntax_tree/node.rb', line 766

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