Module: Jade::Formatter::Type
- Extended by:
- Helper, Type
- Included in:
- Type
- Defined in:
- lib/jade/formatter/type.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
#breakable_record?(type) ⇒ Boolean
52
53
54
|
# File 'lib/jade/formatter/type.rb', line 52
def breakable_record?(type)
type.is_a?(AST::TypeRecord) && type.fields.size > 1
end
|
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
|
# File 'lib/jade/formatter/type.rb', line 7
def format(node)
case node
in AST::TypeName(type:)
type
in AST::QualifiedTypeName(path:)
path.join(".")
in AST::TypeVar(type:)
type
in AST::TypeApplication(constructor:, args:)
if args.empty?
format(constructor)
else
args
.map { format(it) }
.join(', ')
.then { "#{format(constructor)}(#{it})" }
end
in AST::TypeFunction(params:, return_type:)
params_str = params.empty? ?
"()" :
params.map { format_atom(it) }.join(', ')
"#{params_str} -> #{format_atom(return_type)}"
in AST::TypeRecord(fields:, row_var:)
fields_str = fields.map { |k, v| "#{k}: #{format(v)}" }.join(", ")
row_prefix = row_var ? "#{row_var.name} | " : ""
"{ #{row_prefix}#{fields_str} }"
in AST::TypeTuple(items:)
items.map { format(it) }.join(', ').then { "(#{it})" }
end
end
|
Wrap a function type in ‘(…)` so nested arrows stay unambiguous —the type parser only accepts a `type_atom` on either side of `->`.
48
49
50
|
# File 'lib/jade/formatter/type.rb', line 48
def format_atom(node)
node.is_a?(AST::TypeFunction) ? "(#{format(node)})" : format(node)
end
|
56
57
58
59
60
61
62
63
64
|
# File 'lib/jade/formatter/type.rb', line 56
def format_record_multiline(type, indent)
type => AST::TypeRecord(fields:, row_var:)
open = row_var ? "{ #{row_var.name} |" : "{"
fields_str = fields
.map { |k, v| "#{k}: #{format(v)},".then(&and_indent(indent + 1)) }
.join("\n")
"#{open}\n#{fields_str}\n#{INDENT * indent}}"
end
|