Class: SyntaxTree::Args

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

Overview

Args represents a list of arguments being passed to a method call or array literal.

method(first, second, third)

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(parts:, location:) ⇒ Args

Returns a new instance of Args.



828
829
830
831
832
# File 'lib/syntax_tree/node.rb', line 828

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



826
827
828
# File 'lib/syntax_tree/node.rb', line 826

def comments
  @comments
end

#partsObject (readonly)

Array[ Node ]

the arguments that this node wraps



823
824
825
# File 'lib/syntax_tree/node.rb', line 823

def parts
  @parts
end

Instance Method Details

#===(other) ⇒ Object



863
864
865
# File 'lib/syntax_tree/node.rb', line 863

def ===(other)
  other.is_a?(Args) && ArrayMatch.call(parts, other.parts)
end

#accept(visitor) ⇒ Object



834
835
836
# File 'lib/syntax_tree/node.rb', line 834

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

#arityObject



867
868
869
870
871
872
873
874
875
876
877
878
879
880
# File 'lib/syntax_tree/node.rb', line 867

def arity
  parts.sum do |part|
    case part
    when ArgStar, ArgsForward
      Float::INFINITY
    when BareAssocHash
      part.assocs.sum do |assoc|
        assoc.is_a?(AssocSplat) ? Float::INFINITY : 1
      end
    else
      1
    end
  end
end

#child_nodesObject Also known as: deconstruct



838
839
840
# File 'lib/syntax_tree/node.rb', line 838

def child_nodes
  parts
end

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



842
843
844
845
846
847
848
849
850
851
# File 'lib/syntax_tree/node.rb', line 842

def copy(parts: nil, location: nil)
  node =
    Args.new(
      parts: parts || self.parts,
      location: location || self.location
    )

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

#deconstruct_keys(_keys) ⇒ Object



855
856
857
# File 'lib/syntax_tree/node.rb', line 855

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

#format(q) ⇒ Object



859
860
861
# File 'lib/syntax_tree/node.rb', line 859

def format(q)
  q.seplist(parts) { |part| q.format(part) }
end