Class: SyntaxTree::When

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

Overview

When represents a when clause in a case chain.

case value
when predicate
end

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#pretty_print, #to_json

Constructor Details

#initialize(arguments:, statements:, consequent:, location:, comments: []) ⇒ When

Returns a new instance of When.



9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
# File 'lib/syntax_tree/node.rb', line 9420

def initialize(
  arguments:,
  statements:,
  consequent:,
  location:,
  comments: []
)
  @arguments = arguments
  @statements = statements
  @consequent = consequent
  @location = location
  @comments = comments
end

Instance Attribute Details

#argumentsObject (readonly)

Args

the arguments to the when clause



9409
9410
9411
# File 'lib/syntax_tree/node.rb', line 9409

def arguments
  @arguments
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



9418
9419
9420
# File 'lib/syntax_tree/node.rb', line 9418

def comments
  @comments
end

#consequentObject (readonly)

nil | Else | When

the next clause in the chain



9415
9416
9417
# File 'lib/syntax_tree/node.rb', line 9415

def consequent
  @consequent
end

#statementsObject (readonly)

Statements

the expressions to be executed



9412
9413
9414
# File 'lib/syntax_tree/node.rb', line 9412

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



9434
9435
9436
# File 'lib/syntax_tree/node.rb', line 9434

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

#child_nodesObject Also known as: deconstruct



9438
9439
9440
# File 'lib/syntax_tree/node.rb', line 9438

def child_nodes
  [arguments, statements, consequent]
end

#deconstruct_keys(keys) ⇒ Object



9444
9445
9446
9447
9448
9449
9450
9451
9452
# File 'lib/syntax_tree/node.rb', line 9444

def deconstruct_keys(keys)
  {
    arguments: arguments,
    statements: statements,
    consequent: consequent,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
# File 'lib/syntax_tree/node.rb', line 9454

def format(q)
  keyword = "when "

  q.group do
    q.group do
      q.text(keyword)
      q.nest(keyword.length) do
        if arguments.comments.any?
          q.format(arguments)
        else
          separator = -> { q.group { q.comma_breakable } }
          q.seplist(arguments.parts, separator) { |part| q.format(part) }
        end

        # Very special case here. If you're inside of a when clause and the
        # last argument to the predicate is and endless range, then you are
        # forced to use the "then" keyword to make it parse properly.
        last = arguments.parts.last
        if (last.is_a?(Dot2) || last.is_a?(Dot3)) && !last.right
          q.text(" then")
        end
      end
    end

    unless statements.empty?
      q.indent do
        q.breakable(force: true)
        q.format(statements)
      end
    end

    if consequent
      q.breakable(force: true)
      q.format(consequent)
    end
  end
end