Module: Jade::Formatter::RecordLiteral

Extended by:
Helper, RecordLiteral
Included in:
RecordLiteral
Defined in:
lib/jade/formatter/collections.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



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/jade/formatter/collections.rb', line 39

def format(node, indent:, source:)
  node => AST::RecordLiteral(fields:, trailing_comma:)

  field_strs = fields.map { "#{it.key}: #{format_node(it.value, source:)}" }
  inline     = "{ #{field_strs.join(', ')} }"

  if trailing_comma || too_long?(inline, indent)
    inner = field_strs
      .map { "#{it.then(&and_indent(indent + 1))}," }
      .join("\n")
    "#{INDENT * indent}{\n#{inner}\n#{INDENT * indent}}"
  else
    inline.then(&and_indent(indent))
  end
end