Module: Jade::Formatter::IfThenElse
- Extended by:
- Helper, IfThenElse
- Included in:
- IfThenElse
- Defined in:
- lib/jade/formatter/if_then_else.rb
Instance Method Summary collapse
-
#block_form?(if_branch, else_branch) ⇒ Boolean
Block form needed whenever a branch has more than one expression or a leading comment that the ternary form would drop.
- #format(node, indent:, source:) ⇒ Object
- #format_block(cond_str, if_branch, else_branch, indent, source:) ⇒ Object
- #multi?(body) ⇒ Boolean
-
#single_branch_expr(body, source:) ⇒ Object
Format a body known to hold a single expression with no leading comments.
-
#try_inline_ternary(cond_str, if_branch, else_branch, indent, source:) ⇒ Object
Returns nil when block form is needed: either branch is multi- statement / has a leading comment, the combined line is too long, or either single-expression branch already formats across multiple lines (e.g. a ‘|>` ladder).
Methods included from Helper
and_indent, dispatch_for, format_delimited, format_exposing, format_leading_comments, format_node, format_pattern, format_trailing_comment, format_type, format_type_atom, too_long?
Instance Method Details
#block_form?(if_branch, else_branch) ⇒ Boolean
Block form needed whenever a branch has more than one expression or a leading comment that the ternary form would drop.
44 45 46 |
# File 'lib/jade/formatter/if_then_else.rb', line 44 def block_form?(if_branch, else_branch) multi?(if_branch) || multi?(else_branch) end |
#format(node, indent:, source:) ⇒ Object
7 8 9 10 11 12 13 |
# File 'lib/jade/formatter/if_then_else.rb', line 7 def format(node, indent:, source:) node => AST::IfThenElse(condition:, if_branch:, else_branch:) cond_str = format_node(condition, source:) try_inline_ternary(cond_str, if_branch, else_branch, indent, source:) || format_block(cond_str, if_branch, else_branch, indent, source:) end |
#format_block(cond_str, if_branch, else_branch, indent, source:) ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/jade/formatter/if_then_else.rb', line 32 def format_block(cond_str, if_branch, else_branch, indent, source:) [ "if #{cond_str} then".then(&and_indent(indent)), format_node(if_branch, indent: indent + 1, source:), "else".then(&and_indent(indent)), format_node(else_branch, indent: indent + 1, source:), "end".then(&and_indent(indent)), ].join("\n") end |
#multi?(body) ⇒ Boolean
48 49 50 51 52 |
# File 'lib/jade/formatter/if_then_else.rb', line 48 def multi?(body) body.expressions.length > 1 || !body.leading_comments.empty? || !body.expressions.first.leading_comments.empty? end |
#single_branch_expr(body, source:) ⇒ Object
Format a body known to hold a single expression with no leading comments. Called only after ‘block_form?` is false — the raise is an invariant guard, not a user-facing error.
57 58 59 60 61 |
# File 'lib/jade/formatter/if_then_else.rb', line 57 def single_branch_expr(body, source:) raise "formatter invariant: single-expr body expected" if multi?(body) format_node(body.expressions.first, source:) end |
#try_inline_ternary(cond_str, if_branch, else_branch, indent, source:) ⇒ Object
Returns nil when block form is needed: either branch is multi- statement / has a leading comment, the combined line is too long, or either single-expression branch already formats across multiple lines (e.g. a ‘|>` ladder).
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/jade/formatter/if_then_else.rb', line 19 def try_inline_ternary(cond_str, if_branch, else_branch, indent, source:) return nil if block_form?(if_branch, else_branch) if_str = single_branch_expr(if_branch, source:) else_str = single_branch_expr(else_branch, source:) inline = "#{cond_str} ? #{if_str} : #{else_str}" return nil if if_str.include?("\n") || else_str.include?("\n") return nil if too_long?(inline, indent) inline.then(&and_indent(indent)) end |