Class: SyntaxTree::BlockNode

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

Overview

Block represents passing a block to a method call using the do and end keywords or the { and } operators.

method do |value|
end

method { |value| }

Defined Under Namespace

Classes: BlockOpenFormatter

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #pretty_print, #to_json

Constructor Details

#initialize(opening:, block_var:, bodystmt:, location:) ⇒ BlockNode

Returns a new instance of BlockNode.



4274
4275
4276
4277
4278
4279
4280
# File 'lib/syntax_tree/node.rb', line 4274

def initialize(opening:, block_var:, bodystmt:, location:)
  @opening = opening
  @block_var = block_var
  @bodystmt = bodystmt
  @location = location
  @comments = []
end

Instance Attribute Details

#block_varObject (readonly)

nil | BlockVar

the optional variable declaration within this block



4266
4267
4268
# File 'lib/syntax_tree/node.rb', line 4266

def block_var
  @block_var
end

#bodystmtObject (readonly)

BodyStmt | Statements

the expressions to be executed within this block



4269
4270
4271
# File 'lib/syntax_tree/node.rb', line 4269

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



4272
4273
4274
# File 'lib/syntax_tree/node.rb', line 4272

def comments
  @comments
end

#openingObject (readonly)

LBrace | Kw

the left brace or the do keyword that opens this block



4263
4264
4265
# File 'lib/syntax_tree/node.rb', line 4263

def opening
  @opening
end

Instance Method Details

#===(other) ⇒ Object



4347
4348
4349
4350
# File 'lib/syntax_tree/node.rb', line 4347

def ===(other)
  other.is_a?(BlockNode) && opening === other.opening &&
    block_var === other.block_var && bodystmt === other.bodystmt
end

#accept(visitor) ⇒ Object



4282
4283
4284
# File 'lib/syntax_tree/node.rb', line 4282

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

#child_nodesObject Also known as: deconstruct



4286
4287
4288
# File 'lib/syntax_tree/node.rb', line 4286

def child_nodes
  [opening, block_var, bodystmt]
end

#copy(opening: nil, block_var: nil, bodystmt: nil, location: nil) ⇒ Object



4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
# File 'lib/syntax_tree/node.rb', line 4290

def copy(opening: nil, block_var: nil, bodystmt: nil, location: nil)
  node =
    BlockNode.new(
      opening: opening || self.opening,
      block_var: block_var || self.block_var,
      bodystmt: bodystmt || self.bodystmt,
      location: location || self.location
    )

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

#deconstruct_keys(_keys) ⇒ Object



4305
4306
4307
4308
4309
4310
4311
4312
4313
# File 'lib/syntax_tree/node.rb', line 4305

def deconstruct_keys(_keys)
  {
    opening: opening,
    block_var: block_var,
    bodystmt: bodystmt,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
# File 'lib/syntax_tree/node.rb', line 4315

def format(q)
  # If this is nested anywhere inside of a Command or CommandCall node, then
  # we can't change which operators we're using for the bounds of the block.
  break_opening, break_closing, flat_opening, flat_closing =
    if unchangeable_bounds?(q)
      block_close = keywords? ? "end" : "}"
      [opening.value, block_close, opening.value, block_close]
    elsif forced_do_end_bounds?(q)
      %w[do end do end]
    elsif forced_brace_bounds?(q)
      %w[{ } { }]
    else
      %w[do end { }]
    end

  # If the receiver of this block a Command or CommandCall node, then there
  # are no parentheses around the arguments to that command, so we need to
  # break the block.
  case q.parent
  when Command, CommandCall
    q.break_parent
    format_break(q, break_opening, break_closing)
    return
  end

  q.group do
    q
      .if_break { format_break(q, break_opening, break_closing) }
      .if_flat { format_flat(q, flat_opening, flat_closing) }
  end
end

#keywords?Boolean

Returns:

  • (Boolean)


4352
4353
4354
# File 'lib/syntax_tree/node.rb', line 4352

def keywords?
  opening.is_a?(Kw)
end