class SyntaxTree::ConditionalFormatter

Formats an If or Unless node.

Attributes

keyword[R]
String

the keyword associated with this conditional

node[R]
If | Unless

the node that is being formatted

Public Class Methods

new(keyword, node) click to toggle source
# File lib/syntax_tree/node.rb, line 6324
def initialize(keyword, node)
  @keyword = keyword
  @node = node
end

Public Instance Methods

format(q) click to toggle source
# File lib/syntax_tree/node.rb, line 6329
def format(q)
  if node.modifier?
    statement = node.statements.body[0]

    if ContainsAssignment.call(statement) || q.parent.is_a?(In)
      q.group { format_flat(q) }
    else
      q.group do
        q
          .if_break { format_break(q, force: false) }
          .if_flat { format_flat(q) }
      end
    end
  else
    # If we can transform this node into a ternary, then we're going to
    # print a special version that uses the ternary operator if it fits on
    # one line.
    if Ternaryable.call(q, node)
      format_ternary(q)
      return
    end

    # If the predicate of the conditional contains an assignment (in which
    # case we can't know for certain that that assignment doesn't impact the
    # statements inside the conditional) then we can't use the modifier form
    # and we must use the block form.
    if ContainsAssignment.call(node.predicate)
      format_break(q, force: true)
      return
    end

    if node.consequent || node.statements.empty? || contains_conditional?
      q.group { format_break(q, force: true) }
    else
      q.group do
        q
          .if_break { format_break(q, force: false) }
          .if_flat do
            Parentheses.flat(q) do
              q.format(node.statements)
              q.text(" #{keyword} ")
              q.format(node.predicate)
            end
          end
      end
    end
  end
end