Class: SyntaxTree::HashKeyFormatter::Labels

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

Formats the keys of a hash literal using labels.

Constant Summary collapse

LABEL =
/\A[A-Za-z_](\w*[\w!?])?\z/.freeze

Instance Method Summary collapse

Instance Method Details

#format_key(q, key) ⇒ Object



1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
# File 'lib/syntax_tree/node.rb', line 1729

def format_key(q, key)
  case key
  when Label
    q.format(key)
  when SymbolLiteral
    q.format(key.value)
    q.text(":")
  when DynaSymbol
    parts = key.parts

    if parts.length == 1 && (part = parts.first) &&
         part.is_a?(TStringContent) && part.value.match?(LABEL)
      q.format(part)
      q.text(":")
    else
      q.format(key)
      q.text(":")
    end
  end
end