Skip to content

Commit

Permalink
Add a fix for Symbol#inspect on 3.0, 3.1 (#361)
Browse files Browse the repository at this point in the history
* Handle `Symbol#inspect` for Ruby `<3.2.0`
* Extract dedicated `Symbol` emitter
* Disable mutant on `Symbol#dispatch` as it depends on Ruby version and would fail on CI.
* Improve symbol detection logic
* Replace `regexp` check with `parse`
  • Loading branch information
prikha committed Feb 1, 2024
1 parent 7960ef5 commit 13419d8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
28 changes: 27 additions & 1 deletion lib/unparser/emitter/primitive.rb
Expand Up @@ -10,7 +10,7 @@ class Primitive < self
# Emitter for primitives based on Object#inspect
class Inspect < self

handle :sym, :str
handle :str

private

Expand All @@ -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

Expand Down
5 changes: 5 additions & 0 deletions spec/unit/unparser_spec.rb
Expand Up @@ -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
Expand Down

0 comments on commit 13419d8

Please sign in to comment.