Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a fix for Symbol#inspect on 3.0, 3.1 #361

Merged
merged 6 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion lib/unparser/emitter/primitive.rb
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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