Class: SyntaxTree::ConditionalFormatter
- Inherits:
-
Object
- Object
- SyntaxTree::ConditionalFormatter
- Defined in:
- lib/syntax_tree/node.rb
Overview
Formats an If or Unless node.
Instance Attribute Summary collapse
-
#keyword ⇒ Object
readonly
- String
-
the keyword associated with this conditional.
-
#node ⇒ Object
readonly
- If | Unless
-
the node that is being formatted.
Instance Method Summary collapse
- #format(q) ⇒ Object
-
#initialize(keyword, node) ⇒ ConditionalFormatter
constructor
A new instance of ConditionalFormatter.
Constructor Details
#initialize(keyword, node) ⇒ ConditionalFormatter
Returns a new instance of ConditionalFormatter.
6300 6301 6302 6303 |
# File 'lib/syntax_tree/node.rb', line 6300 def initialize(keyword, node) @keyword = keyword @node = node end |
Instance Attribute Details
#keyword ⇒ Object (readonly)
- String
-
the keyword associated with this conditional
6295 6296 6297 |
# File 'lib/syntax_tree/node.rb', line 6295 def keyword @keyword end |
#node ⇒ Object (readonly)
- If | Unless
-
the node that is being formatted
6298 6299 6300 |
# File 'lib/syntax_tree/node.rb', line 6298 def node @node end |
Instance Method Details
#format(q) ⇒ Object
6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 |
# File 'lib/syntax_tree/node.rb', line 6305 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 |