diff --git a/lib/unparser/emitter/primitive.rb b/lib/unparser/emitter/primitive.rb index e173704d..ec17b16b 100644 --- a/lib/unparser/emitter/primitive.rb +++ b/lib/unparser/emitter/primitive.rb @@ -10,7 +10,7 @@ class Primitive < self # Emitter for primitives based on Object#inspect class Inspect < self - handle :sym, :str + handle :str private @@ -20,6 +20,32 @@ def dispatch end # Inspect + class Symbol < self + + handle :sym + + private + + # mutant:disable + def dispatch + if inspect_breaks_parsing? + write(":#{value.name.inspect}") + else + write(value.inspect) + end + end + + # mutant:disable + def inspect_breaks_parsing? + return false unless RUBY_VERSION < '3.2.' + + Unparser.parse(value.inspect) + false + rescue Parser::SyntaxError + true + end + end # Symbol + # Emitter for complex literals class Complex < self diff --git a/spec/unit/unparser_spec.rb b/spec/unit/unparser_spec.rb index c979d1a9..43d6867a 100644 --- a/spec/unit/unparser_spec.rb +++ b/spec/unit/unparser_spec.rb @@ -390,6 +390,11 @@ def noop "false" end RUBY + + # Test Symbol#inspect Ruby bug: https://bugs.ruby-lang.org/issues/18905 + assert_source(':"@="') + assert_source(':"$$$$="') + assert_source(':"8 >="') end describe 'corpus' do