Module: Jade::Formatter::TypeDeclaration

Extended by:
Helper, TypeDeclaration
Included in:
TypeDeclaration
Defined in:
lib/jade/formatter/declarations.rb

Overview

Top-level declaration nodes other than ‘def` (which lives in its own file because of the signature break logic).

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



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/jade/formatter/declarations.rb', line 10

def format(node, indent:, source:)
  node => AST::TypeDeclaration(name:, type_params:, variants:)

  params_str = type_params.empty? ?
    "" :
    "(#{type_params.map(&:name).join(', ')})"
  header = "type #{name}#{params_str}"

  if variants.size == 1
    "#{header} = #{format_node(variants.first, source:)}"
      .then(&and_indent(indent))
  else
    inner = INDENT * (indent + 1)
    variants_str = variants
      .map { format_node(it, source:) }
      .map.with_index { |v, i| "#{inner}#{i == 0 ? '=' : '|'} #{v}" }
      .join("\n")

    "#{and_indent(indent).call(header)}\n#{variants_str}"
  end
end