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.
5996 5997 5998 5999 |
# File 'lib/syntax_tree/node.rb', line 5996 def initialize(keyword, node) @keyword = keyword @node = node end |
Instance Attribute Details
#keyword ⇒ Object (readonly)
- String
-
the keyword associated with this conditional
5991 5992 5993 |
# File 'lib/syntax_tree/node.rb', line 5991 def keyword @keyword end |
#node ⇒ Object (readonly)
- If | Unless
-
the node that is being formatted
5994 5995 5996 |
# File 'lib/syntax_tree/node.rb', line 5994 def node @node end |
Instance Method Details
#format(q) ⇒ Object
6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 |
# File 'lib/syntax_tree/node.rb', line 6001 def format(q) # 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? 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 |