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



1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
# File 'lib/syntax_tree/node.rb', line 1764

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