Class: SyntaxTree::UntilMod

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

Overview

UntilMod represents the modifier form of a until loop.

expression until predicate

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Constructor Details

#initialize(statement:, predicate:, location:, comments: []) ⇒ UntilMod

Returns a new instance of UntilMod.



10455
10456
10457
10458
10459
10460
# File 'lib/syntax_tree/node.rb', line 10455

def initialize(statement:, predicate:, location:, comments: [])
  @statement = statement
  @predicate = predicate
  @location = location
  @comments = comments
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



10453
10454
10455
# File 'lib/syntax_tree/node.rb', line 10453

def comments
  @comments
end

#predicateObject (readonly)

untyped

the expression to be checked



10450
10451
10452
# File 'lib/syntax_tree/node.rb', line 10450

def predicate
  @predicate
end

#statementObject (readonly)

untyped

the expression to be executed



10447
10448
10449
# File 'lib/syntax_tree/node.rb', line 10447

def statement
  @statement
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



10462
10463
10464
# File 'lib/syntax_tree/node.rb', line 10462

def child_nodes
  [statement, predicate]
end

#deconstruct_keys(keys) ⇒ Object



10468
10469
10470
10471
10472
10473
10474
10475
# File 'lib/syntax_tree/node.rb', line 10468

def deconstruct_keys(keys)
  {
    statement: statement,
    predicate: predicate,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
# File 'lib/syntax_tree/node.rb', line 10477

def format(q)
  # If we're in the modifier form and we're modifying a `begin`, then this
  # is a special case where we need to explicitly use the modifier form
  # because otherwise the semantic meaning changes. This looks like:
  #
  #     begin
  #       foo
  #     end until bar
  #
  # Also, if the statement of the modifier includes an assignment, then we
  # can't know for certain that it won't impact the predicate, so we need to
  # force it to stay as it is. This looks like:
  #
  #     foo = bar until foo
  #
  if statement.is_a?(Begin) || ContainsAssignment.call(statement)
    q.format(statement)
    q.text(" until ")
    q.format(predicate)
  else
    LoopFormatter.new("until", self, statement).format(q)
  end
end

#pretty_print(q) ⇒ Object



10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
# File 'lib/syntax_tree/node.rb', line 10501

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

    q.breakable
    q.pp(statement)

    q.breakable
    q.pp(predicate)

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

#to_json(*opts) ⇒ Object



10515
10516
10517
10518
10519
10520
10521
10522
10523
# File 'lib/syntax_tree/node.rb', line 10515

def to_json(*opts)
  {
    type: :until_mod,
    stmt: statement,
    pred: predicate,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end