Class: SyntaxTree::LambdaVar

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

Overview

LambdaVar represents the parameters being declared for a lambda. Effectively this node is everything contained within the parentheses. This includes all of the various parameter types, as well as block-local variable declarations.

-> (positional, optional = value, keyword:, █ local) do
end

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(params:, locals:, location:) ⇒ LambdaVar

Returns a new instance of LambdaVar.



7253
7254
7255
7256
7257
7258
# File 'lib/syntax_tree/node.rb', line 7253

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



7251
7252
7253
# File 'lib/syntax_tree/node.rb', line 7251

def comments
  @comments
end

#localsObject (readonly)

Array[ Ident ]

the list of block-local variable declarations



7248
7249
7250
# File 'lib/syntax_tree/node.rb', line 7248

def locals
  @locals
end

#paramsObject (readonly)

Params

the parameters being declared with the block



7245
7246
7247
# File 'lib/syntax_tree/node.rb', line 7245

def params
  @params
end

Instance Method Details

#===(other) ⇒ Object



7299
7300
7301
7302
# File 'lib/syntax_tree/node.rb', line 7299

def ===(other)
  other.is_a?(LambdaVar) && params === other.params &&
    ArrayMatch.call(locals, other.locals)
end

#accept(visitor) ⇒ Object



7260
7261
7262
# File 'lib/syntax_tree/node.rb', line 7260

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

#child_nodesObject Also known as: deconstruct



7264
7265
7266
# File 'lib/syntax_tree/node.rb', line 7264

def child_nodes
  [params, *locals]
end

#copy(params: nil, locals: nil, location: nil) ⇒ Object



7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
# File 'lib/syntax_tree/node.rb', line 7268

def copy(params: nil, locals: nil, location: nil)
  node =
    LambdaVar.new(
      params: params || self.params,
      locals: locals || self.locals,
      location: location || self.location
    )

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

#deconstruct_keys(_keys) ⇒ Object



7282
7283
7284
# File 'lib/syntax_tree/node.rb', line 7282

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

#empty?Boolean

Returns:

  • (Boolean)


7286
7287
7288
# File 'lib/syntax_tree/node.rb', line 7286

def empty?
  params.empty? && locals.empty?
end

#format(q) ⇒ Object



7290
7291
7292
7293
7294
7295
7296
7297
# File 'lib/syntax_tree/node.rb', line 7290

def format(q)
  q.format(params)

  if locals.any?
    q.text("; ")
    q.seplist(locals, BlockVar::SEPARATOR) { |local| q.format(local) }
  end
end