Module: Jade::Formatter::CaseOf

Extended by:
CaseOf, Helper
Included in:
CaseOf
Defined in:
lib/jade/formatter/case_of.rb

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:) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/jade/formatter/case_of.rb', line 7

def format(node, indent:, source:)
  node => AST::CaseOf(expression:, branches:)

  branches_str = branches
    .map.with_index { |b, i| format_branch(b, branches, i, indent, source:) }
    .join("\n")

  [
    "case #{format_node(expression, source:)}".then(&and_indent(indent)),
    branches_str,
    "end".then(&and_indent(indent)),
  ].join("\n")
end

#format_branch(branch, all_branches, i, indent, source:) ⇒ Object

The last branch is rendered as ‘else` when its pattern is a wildcard — purely sugar; the AST shape is the same.



23
24
25
26
27
28
# File 'lib/jade/formatter/case_of.rb', line 23

def format_branch(branch, all_branches, i, indent, source:)
  last_wildcard = i == all_branches.length - 1 &&
    branch.pattern.is_a?(AST::Pattern::Wildcard)

  CaseOfBranch.format(branch, indent:, source:, as_else: last_wildcard)
end