Class: Prism::CallNode

Inherits:
PrismNode
  • Object
show all
Defined in:
lib/prism/node.rb,
ext/prism/api_node.c

Overview

Represents a method call, in all of the various forms that can take.

foo
^^^

foo()
^^^^^

+foo
^^^^

foo + bar
^^^^^^^^^

foo.bar
^^^^^^^

foo&.bar
^^^^^^^^

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(receiver, call_operator_loc, message_loc, opening_loc, arguments, closing_loc, block, flags, name, location) ⇒ CallNode

def initialize: (receiver: Node?, call_operator_loc: Location?, message_loc: Location?, opening_loc: Location?, arguments: ArgumentsNode?, closing_loc: Location?, block: Node?, flags: Integer, name: String, location: Location) -> void



1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
# File 'lib/prism/node.rb', line 1966

def initialize(receiver, call_operator_loc, message_loc, opening_loc, arguments, closing_loc, block, flags, name, location)
  @receiver = receiver
  @call_operator_loc = call_operator_loc
  @message_loc = message_loc
  @opening_loc = opening_loc
  @arguments = arguments
  @closing_loc = closing_loc
  @block = block
  @flags = flags
  @name = name
  @location = location
end

Instance Attribute Details

#argumentsObject (readonly)

attr_reader arguments: ArgumentsNode?



1951
1952
1953
# File 'lib/prism/node.rb', line 1951

def arguments
  @arguments
end

#blockObject (readonly)

attr_reader block: Node?



1957
1958
1959
# File 'lib/prism/node.rb', line 1957

def block
  @block
end

#call_operator_locObject (readonly)

attr_reader call_operator_loc: Location?



1942
1943
1944
# File 'lib/prism/node.rb', line 1942

def call_operator_loc
  @call_operator_loc
end

#closing_locObject (readonly)

attr_reader closing_loc: Location?



1954
1955
1956
# File 'lib/prism/node.rb', line 1954

def closing_loc
  @closing_loc
end

#message_locObject (readonly)

attr_reader message_loc: Location?



1945
1946
1947
# File 'lib/prism/node.rb', line 1945

def message_loc
  @message_loc
end

#nameObject (readonly)

attr_reader name: String



1963
1964
1965
# File 'lib/prism/node.rb', line 1963

def name
  @name
end

#opening_locObject (readonly)

attr_reader opening_loc: Location?



1948
1949
1950
# File 'lib/prism/node.rb', line 1948

def opening_loc
  @opening_loc
end

#receiverObject (readonly)

attr_reader receiver: Node?



1939
1940
1941
# File 'lib/prism/node.rb', line 1939

def receiver
  @receiver
end

Instance Method Details

#accept(visitor) ⇒ Object

def accept: (visitor: Visitor) -> void



1980
1981
1982
# File 'lib/prism/node.rb', line 1980

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

#call_operatorObject

def call_operator: () -> String?



2028
2029
2030
# File 'lib/prism/node.rb', line 2028

def call_operator
  call_operator_loc&.slice
end

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array[nil | Node]



1985
1986
1987
# File 'lib/prism/node.rb', line 1985

def child_nodes
  [receiver, arguments, block]
end

#closingObject

def closing: () -> String?



2043
2044
2045
# File 'lib/prism/node.rb', line 2043

def closing
  closing_loc&.slice
end

#comment_targetsObject

def comment_targets: () -> Array[Node | Location]



1999
2000
2001
# File 'lib/prism/node.rb', line 1999

def comment_targets
  [*receiver, *call_operator_loc, *message_loc, *opening_loc, *arguments, *closing_loc, *block]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



1990
1991
1992
1993
1994
1995
1996
# File 'lib/prism/node.rb', line 1990

def compact_child_nodes
  compact = []
  compact << receiver if receiver
  compact << arguments if arguments
  compact << block if block
  compact
end

#copy(**params) ⇒ Object

def copy: (**params) -> CallNode



2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
# File 'lib/prism/node.rb', line 2004

def copy(**params)
  CallNode.new(
    params.fetch(:receiver) { receiver },
    params.fetch(:call_operator_loc) { call_operator_loc },
    params.fetch(:message_loc) { message_loc },
    params.fetch(:opening_loc) { opening_loc },
    params.fetch(:arguments) { arguments },
    params.fetch(:closing_loc) { closing_loc },
    params.fetch(:block) { block },
    params.fetch(:flags) { flags },
    params.fetch(:name) { name },
    params.fetch(:location) { location },
  )
end

#deconstruct_keys(keys) ⇒ Object

def deconstruct_keys: (keys: Array) -> Hash[Symbol, nil | Node | Array | String | Token | Array | Location]



2023
2024
2025
# File 'lib/prism/node.rb', line 2023

def deconstruct_keys(keys)
  { receiver: receiver, call_operator_loc: call_operator_loc, message_loc: message_loc, opening_loc: opening_loc, arguments: arguments, closing_loc: closing_loc, block: block, flags: flags, name: name, location: location }
end

#inspect(inspector = NodeInspector.new) ⇒ Object



2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
# File 'lib/prism/node.rb', line 2057

def inspect(inspector = NodeInspector.new)
  inspector << inspector.header(self)
  if (receiver = self.receiver).nil?
    inspector << "├── receiver: ∅\n"
  else
    inspector << "├── receiver:\n"
    inspector << receiver.inspect(inspector.child_inspector("")).delete_prefix(inspector.prefix)
  end
  inspector << "├── call_operator_loc: #{inspector.location(call_operator_loc)}\n"
  inspector << "├── message_loc: #{inspector.location(message_loc)}\n"
  inspector << "├── opening_loc: #{inspector.location(opening_loc)}\n"
  if (arguments = self.arguments).nil?
    inspector << "├── arguments: ∅\n"
  else
    inspector << "├── arguments:\n"
    inspector << arguments.inspect(inspector.child_inspector("")).delete_prefix(inspector.prefix)
  end
  inspector << "├── closing_loc: #{inspector.location(closing_loc)}\n"
  if (block = self.block).nil?
    inspector << "├── block: ∅\n"
  else
    inspector << "├── block:\n"
    inspector << block.inspect(inspector.child_inspector("")).delete_prefix(inspector.prefix)
  end
  flags = [("safe_navigation" if safe_navigation?), ("variable_call" if variable_call?)].compact
  inspector << "├── flags: #{flags.empty? ? "" : flags.join(", ")}\n"
  inspector << "└── name: #{name.inspect}\n"
  inspector.to_str
end

#messageObject

def message: () -> String?



2033
2034
2035
# File 'lib/prism/node.rb', line 2033

def message
  message_loc&.slice
end

#openingObject

def opening: () -> String?



2038
2039
2040
# File 'lib/prism/node.rb', line 2038

def opening
  opening_loc&.slice
end

#safe_navigation?Boolean

def safe_navigation?: () -> bool

Returns:

  • (Boolean)


2048
2049
2050
# File 'lib/prism/node.rb', line 2048

def safe_navigation?
  flags.anybits?(CallNodeFlags::SAFE_NAVIGATION)
end

#typeObject

Sometimes you want to check an instance of a node against a list of classes to see what kind of behavior to perform. Usually this is done by calling ‘[cls1, cls2].include?(node.class)` or putting the node into a case statement and doing `case node; when cls1; when cls2; end`. Both of these approaches are relatively slow because of the constant lookups, method calls, and/or array allocations.

Instead, you can call #type, which will return to you a symbol that you can use for comparison. This is faster than the other approaches because it uses a single integer comparison, but also because if you’re on CRuby you can take advantage of the fact that case statements with all symbol keys will use a jump table.

def type: () -> Symbol



2101
2102
2103
# File 'lib/prism/node.rb', line 2101

def type
  :call_node
end

#variable_call?Boolean

def variable_call?: () -> bool

Returns:

  • (Boolean)


2053
2054
2055
# File 'lib/prism/node.rb', line 2053

def variable_call?
  flags.anybits?(CallNodeFlags::VARIABLE_CALL)
end