Skip to content

Commit 6e1d9e9

Browse files
committed
Handle String-like objects for Ripper.sexp
* RSpec relies on this in https://github.com/rspec/rspec/blob/rspec-support-v3.13.6/rspec-support/lib/rspec/support/source.rb#L57 which is given an RSpec::Support::EncodedString. * CI failure caused by this on truffleruby: https://github.com/sporkmonger/addressable/actions/runs/21457707372/job/61802608154#step:7:14
1 parent e8c6b49 commit 6e1d9e9

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

lib/prism/translation/ripper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ def self.lex_state_name(state)
480480

481481
# Create a new Translation::Ripper object with the given source.
482482
def initialize(source, filename = "(ripper)", lineno = 1)
483-
@source = source
483+
@source = source.to_str
484484
@filename = filename
485485
@lineno = lineno
486486
@column = 0

test/prism/ruby/ripper_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,14 @@ def test_tokenize
145145
assert_equal(Ripper.tokenize(source), Translation::Ripper.tokenize(source))
146146
end
147147

148+
def test_sexp_coercion
149+
string_like = Object.new
150+
def string_like.to_str
151+
"a"
152+
end
153+
assert_equal [:program, [[:vcall, [:@ident, "a", [1, 0]]]]], Translation::Ripper.sexp(string_like)
154+
end
155+
148156
# Check that the hardcoded values don't change without us noticing.
149157
def test_internals
150158
actual = Translation::Ripper.constants.select { |name| name.start_with?("EXPR_") }.sort

0 commit comments

Comments
 (0)