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.
5169 5170 5171 5172 |
# File 'lib/syntax_tree/node.rb', line 5169 def initialize(keyword, node) @keyword = keyword @node = node end |
Instance Attribute Details
#keyword ⇒ Object (readonly)
- String
-
the keyword associated with this conditional
5164 5165 5166 |
# File 'lib/syntax_tree/node.rb', line 5164 def keyword @keyword end |
#node ⇒ Object (readonly)
- If | Unless
-
the node that is being formatted
5167 5168 5169 |
# File 'lib/syntax_tree/node.rb', line 5167 def node @node end |
Instance Method Details
#format(q) ⇒ Object
5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 |
# File 'lib/syntax_tree/node.rb', line 5174 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 |