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.
5213 5214 5215 5216 |
# File 'lib/syntax_tree/node.rb', line 5213 def initialize(keyword, node) @keyword = keyword @node = node end |
Instance Attribute Details
#keyword ⇒ Object (readonly)
- String
-
the keyword associated with this conditional
5208 5209 5210 |
# File 'lib/syntax_tree/node.rb', line 5208 def keyword @keyword end |
#node ⇒ Object (readonly)
- If | Unless
-
the node that is being formatted
5211 5212 5213 |
# File 'lib/syntax_tree/node.rb', line 5211 def node @node end |
Instance Method Details
#format(q) ⇒ Object
5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 |
# File 'lib/syntax_tree/node.rb', line 5218 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 |