Module: Jade::Formatter::CaseOfBranch

Extended by:
CaseOfBranch, Helper
Included in:
CaseOfBranch
Defined in:
lib/jade/formatter/case_of_branch.rb

Overview

CaseOfBranch is invoked from CaseOf with an extra ‘as_else:` flag — it doesn’t go through the generic dispatcher, so its ‘format` signature differs from siblings on purpose.

Instance Method Summary collapse

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

#format(node, indent:, source:, as_else: false) ⇒ Object

Three shapes, in order of preference:

- Single-expression body, fits on one line → `in <pat> then <expr>`
- Single-expression body, doesn't fit / nested case / has a
  leading comment → `in <pat>\n  <body>` (drop `then`).
- Multi-statement body → same as above.


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/jade/formatter/case_of_branch.rb', line 15

def format(node, indent:, source:, as_else: false)
  # `else` already conveys "wildcard inline body" — no `then` after.
  header     = as_else ? "else" : "in #{format_pattern(node.pattern)}"
  inline_sep = as_else ? "" : " then"
  body       = unwrap_grouped_case_body(node.body)
  first      = body.expressions.first
  single     = body.expressions.length == 1
  has_leading = !first.leading_comments.empty? ||
    !body.leading_comments.empty?

  multi_line = ->(child) {
    [
      header.then(&and_indent(indent)),
      format_node(child, indent: indent + 1, source:),
    ].join("\n")
  }

  return multi_line.call(first) if single && first.is_a?(AST::CaseOf)
  return multi_line.call(body) unless single && !has_leading

  first_str = format_node(first, source:)
  inline    = "#{header}#{inline_sep} #{first_str}"

  if first_str.include?("\n") || too_long?(inline, indent)
    multi_line.call(first)
  else
    inline.then(&and_indent(indent))
  end
end

#unwrap_grouped_case_body(body) ⇒ Object

Strip ‘(case … end)` parens around a branch body. With block-form `case`, the inner `end` terminates the case, so wrapping parens are redundant — drop them so reformat normalises both shapes.



48
49
50
51
52
53
54
55
56
# File 'lib/jade/formatter/case_of_branch.rb', line 48

def unwrap_grouped_case_body(body)
  return body unless body.expressions.length == 1

  first = body.expressions.first
  return body unless first.is_a?(AST::Grouping) &&
    first.expression.is_a?(AST::CaseOf)

  AST::Body.new(expressions: [first.expression], range: body.range)
end