Class: SyntaxTree::When

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

Overview

When represents a when clause in a case chain.

case value
when predicate
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of When.



12466
12467
12468
12469
12470
12471
12472
12473
12474
12475
12476
12477
12478
# File 'lib/syntax_tree.rb', line 12466

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



12452
12453
12454
# File 'lib/syntax_tree.rb', line 12452

def arguments
  @arguments
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



12464
12465
12466
# File 'lib/syntax_tree.rb', line 12464

def comments
  @comments
end

#consequentObject (readonly)

nil | Else | When

the next clause in the chain



12458
12459
12460
# File 'lib/syntax_tree.rb', line 12458

def consequent
  @consequent
end

#locationObject (readonly)

Location

the location of this node



12461
12462
12463
# File 'lib/syntax_tree.rb', line 12461

def location
  @location
end

#statementsObject (readonly)

Statements

the expressions to be executed



12455
12456
12457
# File 'lib/syntax_tree.rb', line 12455

def statements
  @statements
end

Instance Method Details

#child_nodesObject



12480
12481
12482
# File 'lib/syntax_tree.rb', line 12480

def child_nodes
  [arguments, statements, consequent]
end

#format(q) ⇒ Object



12484
12485
12486
12487
12488
12489
12490
12491
12492
12493
12494
12495
12496
12497
12498
12499
12500
12501
12502
12503
12504
12505
12506
12507
12508
12509
12510
12511
12512
# File 'lib/syntax_tree.rb', line 12484

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
      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

#pretty_print(q) ⇒ Object



12514
12515
12516
12517
12518
12519
12520
12521
12522
12523
12524
12525
12526
12527
12528
12529
12530
12531
# File 'lib/syntax_tree.rb', line 12514

def pretty_print(q)
  q.group(2, "(", ")") do
    q.text("when")

    q.breakable
    q.pp(arguments)

    q.breakable
    q.pp(statements)

    if consequent
      q.breakable
      q.pp(consequent)
    end

    q.pp(Comment::List.new(comments))
  end
end

#to_json(*opts) ⇒ Object



12533
12534
12535
12536
12537
12538
12539
12540
12541
12542
# File 'lib/syntax_tree.rb', line 12533

def to_json(*opts)
  {
    type: :when,
    args: arguments,
    stmts: statements,
    cons: consequent,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end