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.
5429 5430 5431 5432 |
# File 'lib/syntax_tree/node.rb', line 5429 def initialize(keyword, node) @keyword = keyword @node = node end |
Instance Attribute Details
#keyword ⇒ Object (readonly)
- String
-
the keyword associated with this conditional
5424 5425 5426 |
# File 'lib/syntax_tree/node.rb', line 5424 def keyword @keyword end |
#node ⇒ Object (readonly)
- If | Unless
-
the node that is being formatted
5427 5428 5429 |
# File 'lib/syntax_tree/node.rb', line 5427 def node @node end |
Instance Method Details
#format(q) ⇒ Object
5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 |
# File 'lib/syntax_tree/node.rb', line 5434 def format(q) # 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 |