Module: Jade::Formatter::Pattern

Extended by:
Helper, Pattern
Included in:
Pattern
Defined in:
lib/jade/formatter/pattern.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, source: nil) ⇒ Object



7
8
9
10
11
12
13
14
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/jade/formatter/pattern.rb', line 7

def format(node, source: nil)
  case node
  in AST::Pattern::Wildcard
    "_"

  in AST::Pattern::Literal(literal:)
    format_node(literal, source:)

  in AST::Pattern::Binding(name:)
    name

  in AST::Pattern::Constructor(constructor:, patterns:)
    name = format_node(constructor, source:)

    if patterns.nil? || patterns.empty?
      name
    else
      patterns
        .map { format(it) }
        .join(', ')
        .then { "#{name}(#{it})" }
    end

  in AST::Pattern::Record(fields:)
    fields
      .map { format(it) }
      .join(", ")
      .then { "{ #{it} }" }

  in AST::Pattern::RecordField(name:, pattern: AST::Pattern::Binding(name: ^name))
    "#{name}:"

  in AST::Pattern::RecordField(name:, pattern:)
    "#{name}: #{format(pattern)}"

  in AST::Pattern::Tuple(patterns:)
    patterns
      .map { format(it) }
      .join(', ')
      .then { "(#{it})" }

  in AST::Pattern::List(patterns:, rest:)
    heads = patterns.map { format(it) }.join(', ')
    tail = case rest
      in AST::Pattern::Binding(name:) then " | #{name}"
      in AST::Pattern::Wildcard       then " | _"
      in nil                          then ""
      end

    "[#{heads}#{tail}]"
  end
end