Class: SyntaxTree::WhileMod

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

Overview

WhileMod represents the modifier form of a while loop.

expression while predicate

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of WhileMod.



11056
11057
11058
11059
11060
11061
# File 'lib/syntax_tree/node.rb', line 11056

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



11054
11055
11056
# File 'lib/syntax_tree/node.rb', line 11054

def comments
  @comments
end

#predicateObject (readonly)

untyped

the expression to be checked



11051
11052
11053
# File 'lib/syntax_tree/node.rb', line 11051

def predicate
  @predicate
end

#statementObject (readonly)

untyped

the expression to be executed



11048
11049
11050
# File 'lib/syntax_tree/node.rb', line 11048

def statement
  @statement
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



11063
11064
11065
# File 'lib/syntax_tree/node.rb', line 11063

def child_nodes
  [statement, predicate]
end

#deconstruct_keys(keys) ⇒ Object



11069
11070
11071
11072
11073
11074
11075
11076
# File 'lib/syntax_tree/node.rb', line 11069

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

#format(q) ⇒ Object



11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
# File 'lib/syntax_tree/node.rb', line 11078

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 while 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 while foo
  #
  if statement.is_a?(Begin) || ContainsAssignment.call(statement)
    q.format(statement)
    q.text(" while ")
    q.format(predicate)
  else
    LoopFormatter.new("while", self, statement).format(q)
  end
end

#pretty_print(q) ⇒ Object



11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
# File 'lib/syntax_tree/node.rb', line 11102

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

    q.breakable
    q.pp(statement)

    q.breakable
    q.pp(predicate)

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

#to_json(*opts) ⇒ Object



11116
11117
11118
11119
11120
11121
11122
11123
11124
# File 'lib/syntax_tree/node.rb', line 11116

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