Class: Prism::ParametersNode

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

Overview

Represents the list of parameters on a method, block, or lambda definition.

def a(b, c, d)
      ^^^^^^^
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, node_id, location, flags, requireds, optionals, rest, posts, keywords, keyword_rest, block) ⇒ ParametersNode

Initialize a new ParametersNode node.



12770
12771
12772
12773
12774
12775
12776
12777
12778
12779
12780
12781
12782
# File 'lib/prism/node.rb', line 12770

def initialize(source, node_id, location, flags, requireds, optionals, rest, posts, keywords, keyword_rest, block)
  @source = source
  @node_id = node_id
  @location = location
  @flags = flags
  @requireds = requireds
  @optionals = optionals
  @rest = rest
  @posts = posts
  @keywords = keywords
  @keyword_rest = keyword_rest
  @block = block
end

Instance Attribute Details

#blockObject (readonly)

attr_reader block: BlockParameterNode?



12844
12845
12846
# File 'lib/prism/node.rb', line 12844

def block
  @block
end

#keyword_restObject (readonly)

attr_reader keyword_rest: KeywordRestParameterNode | ForwardingParameterNode | NoKeywordsParameterNode | nil



12841
12842
12843
# File 'lib/prism/node.rb', line 12841

def keyword_rest
  @keyword_rest
end

#keywordsObject (readonly)

attr_reader keywords: Array[RequiredKeywordParameterNode | OptionalKeywordParameterNode]



12838
12839
12840
# File 'lib/prism/node.rb', line 12838

def keywords
  @keywords
end

#optionalsObject (readonly)

attr_reader optionals: Array



12829
12830
12831
# File 'lib/prism/node.rb', line 12829

def optionals
  @optionals
end

#postsObject (readonly)

attr_reader posts: Array[RequiredParameterNode | MultiTargetNode | KeywordRestParameterNode | NoKeywordsParameterNode | ForwardingParameterNode]



12835
12836
12837
# File 'lib/prism/node.rb', line 12835

def posts
  @posts
end

#requiredsObject (readonly)

attr_reader requireds: Array[RequiredParameterNode | MultiTargetNode]



12826
12827
12828
# File 'lib/prism/node.rb', line 12826

def requireds
  @requireds
end

#restObject (readonly)

attr_reader rest: RestParameterNode | ImplicitRestNode | nil



12832
12833
12834
# File 'lib/prism/node.rb', line 12832

def rest
  @rest
end

Class Method Details

.typeObject

Return a symbol representation of this node type. See ‘Node::type`.



12857
12858
12859
# File 'lib/prism/node.rb', line 12857

def self.type
  :parameters_node
end

Instance Method Details

#===(other) ⇒ Object

Implements case-equality for the node. This is effectively == but without comparing the value of locations. Locations are checked only for presence.



12863
12864
12865
12866
12867
12868
12869
12870
12871
12872
12873
12874
12875
12876
# File 'lib/prism/node.rb', line 12863

def ===(other)
  other.is_a?(ParametersNode) &&
    (requireds.length == other.requireds.length) &&
    requireds.zip(other.requireds).all? { |left, right| left === right } &&
    (optionals.length == other.optionals.length) &&
    optionals.zip(other.optionals).all? { |left, right| left === right } &&
    (rest === other.rest) &&
    (posts.length == other.posts.length) &&
    posts.zip(other.posts).all? { |left, right| left === right } &&
    (keywords.length == other.keywords.length) &&
    keywords.zip(other.keywords).all? { |left, right| left === right } &&
    (keyword_rest === other.keyword_rest) &&
    (block === other.block)
end

#accept(visitor) ⇒ Object

def accept: (Visitor visitor) -> void



12785
12786
12787
# File 'lib/prism/node.rb', line 12785

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

#child_nodesObject Also known as: deconstruct

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



12790
12791
12792
# File 'lib/prism/node.rb', line 12790

def child_nodes
  [*requireds, *optionals, rest, *posts, *keywords, keyword_rest, block]
end

#comment_targetsObject

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



12808
12809
12810
# File 'lib/prism/node.rb', line 12808

def comment_targets
  [*requireds, *optionals, *rest, *posts, *keywords, *keyword_rest, *block] #: Array[Prism::node | Location]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



12795
12796
12797
12798
12799
12800
12801
12802
12803
12804
12805
# File 'lib/prism/node.rb', line 12795

def compact_child_nodes
  compact = [] #: Array[Prism::node]
  compact.concat(requireds)
  compact.concat(optionals)
  compact << rest if rest
  compact.concat(posts)
  compact.concat(keywords)
  compact << keyword_rest if keyword_rest
  compact << block if block
  compact
end

#copy(node_id: self.node_id, location: self.location, flags: self.flags, requireds: self.requireds, optionals: self.optionals, rest: self.rest, posts: self.posts, keywords: self.keywords, keyword_rest: self.keyword_rest, block: self.block) ⇒ Object

def copy: (?node_id: Integer, ?location: Location, ?flags: Integer, ?requireds: Array[RequiredParameterNode | MultiTargetNode], ?optionals: Array, ?rest: RestParameterNode | ImplicitRestNode | nil, ?posts: Array[RequiredParameterNode | MultiTargetNode | KeywordRestParameterNode | NoKeywordsParameterNode | ForwardingParameterNode], ?keywords: Array[RequiredKeywordParameterNode | OptionalKeywordParameterNode], ?keyword_rest: KeywordRestParameterNode | ForwardingParameterNode | NoKeywordsParameterNode | nil, ?block: BlockParameterNode?) -> ParametersNode



12813
12814
12815
# File 'lib/prism/node.rb', line 12813

def copy(node_id: self.node_id, location: self.location, flags: self.flags, requireds: self.requireds, optionals: self.optionals, rest: self.rest, posts: self.posts, keywords: self.keywords, keyword_rest: self.keyword_rest, block: self.block)
  ParametersNode.new(source, node_id, location, flags, requireds, optionals, rest, posts, keywords, keyword_rest, block)
end

#deconstruct_keys(keys) ⇒ Object

def deconstruct_keys: (Array keys) -> { node_id: Integer, location: Location, requireds: Array[RequiredParameterNode | MultiTargetNode], optionals: Array, rest: RestParameterNode | ImplicitRestNode | nil, posts: Array[RequiredParameterNode | MultiTargetNode | KeywordRestParameterNode | NoKeywordsParameterNode | ForwardingParameterNode], keywords: Array[RequiredKeywordParameterNode | OptionalKeywordParameterNode], keyword_rest: KeywordRestParameterNode | ForwardingParameterNode | NoKeywordsParameterNode | nil, block: BlockParameterNode? }



12821
12822
12823
# File 'lib/prism/node.rb', line 12821

def deconstruct_keys(keys)
  { node_id: node_id, location: location, requireds: requireds, optionals: optionals, rest: rest, posts: posts, keywords: keywords, keyword_rest: keyword_rest, block: block }
end

#inspectObject

def inspect -> String



12847
12848
12849
# File 'lib/prism/node.rb', line 12847

def inspect
  InspectVisitor.compose(self)
end

#signatureObject

Mirrors the Method#parameters method.



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/prism/node_ext.rb', line 269

def signature
  names = [] #: Array[[Symbol, Symbol] | [Symbol]]

  requireds.each do |param|
    names << (param.is_a?(MultiTargetNode) ? [:req] : [:req, param.name])
  end

  optionals.each { |param| names << [:opt, param.name] }

  if rest && rest.is_a?(RestParameterNode)
    names << [:rest, rest.name || :*]
  end

  posts.each do |param|
    case param
    when MultiTargetNode
      names << [:req]
    when NoKeywordsParameterNode, KeywordRestParameterNode, ForwardingParameterNode
      # Invalid syntax, e.g. "def f(**nil, ...)" moves the NoKeywordsParameterNode to posts
      raise "Invalid syntax"
    else
      names << [:req, param.name]
    end
  end

  # Regardless of the order in which the keywords were defined, the required
  # keywords always come first followed by the optional keywords.
  keyopt = [] #: Array[OptionalKeywordParameterNode]
  keywords.each do |param|
    if param.is_a?(OptionalKeywordParameterNode)
      keyopt << param
    else
      names << [:keyreq, param.name]
    end
  end

  keyopt.each { |param| names << [:key, param.name] }

  case keyword_rest
  when ForwardingParameterNode
    names.concat([[:rest, :*], [:keyrest, :**], [:block, :&]])
  when KeywordRestParameterNode
    names << [:keyrest, keyword_rest.name || :**]
  when NoKeywordsParameterNode
    names << [:nokey]
  end

  names << [:block, block.name || :&] if block
  names
end

#typeObject

Return a symbol representation of this node type. See ‘Node#type`.



12852
12853
12854
# File 'lib/prism/node.rb', line 12852

def type
  :parameters_node
end