Skip to content

Commit 510258a

Browse files
committed
Also handle string conversion in Ripper.lex
In `ripper`, both go through the same converion logic. Needed for rspec, no other failures in their own tests
1 parent 40ca8a4 commit 510258a

2 files changed

Lines changed: 25 additions & 12 deletions

File tree

lib/prism/translation/ripper.rb

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def self.parse(src, filename = "(ripper)", lineno = 1)
6969
# [[1, 13], :on_kw, "end", END ]]
7070
#
7171
def self.lex(src, filename = "-", lineno = 1, raise_errors: false)
72-
result = Prism.lex_compat(src, filepath: filename, line: lineno, version: "current")
72+
result = Prism.lex_compat(coerce_source(src), filepath: filename, line: lineno, version: "current")
7373

7474
if result.failure? && raise_errors
7575
raise SyntaxError, result.errors.first.message
@@ -91,6 +91,21 @@ def self.tokenize(...)
9191
lex(...).map { |token| token[2] }
9292
end
9393

94+
# Mirros the various lex_types that ripper supports
95+
def self.coerce_source(source) # :nodoc:
96+
if source.is_a?(IO)
97+
source.read
98+
elsif source.respond_to?(:gets)
99+
src = +""
100+
while line = source.gets
101+
src << line
102+
end
103+
src
104+
else
105+
source.to_str
106+
end
107+
end
108+
94109
# This contains a table of all of the parser events and their
95110
# corresponding arity.
96111
PARSER_EVENT_TABLE = {
@@ -480,17 +495,7 @@ def self.lex_state_name(state)
480495

481496
# Create a new Translation::Ripper object with the given source.
482497
def initialize(source, filename = "(ripper)", lineno = 1)
483-
if source.is_a?(IO)
484-
@source = source.read
485-
elsif source.respond_to?(:gets)
486-
@source = +""
487-
while line = source.gets
488-
@source << line
489-
end
490-
else
491-
@source = source.to_str
492-
end
493-
498+
@source = Ripper.coerce_source(source)
494499
@filename = filename
495500
@lineno = lineno
496501
@column = 0

test/prism/ruby/ripper_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,14 @@ def string_like.to_str
243243
end
244244
end
245245

246+
def test_lex_coersion
247+
string_like = Object.new
248+
def string_like.to_str
249+
"a"
250+
end
251+
assert_equal Ripper.lex(string_like), Translation::Ripper.lex(string_like)
252+
end
253+
246254
# Check that the hardcoded values don't change without us noticing.
247255
def test_internals
248256
actual = Translation::Ripper.constants.select { |name| name.start_with?("EXPR_") }.sort

0 commit comments

Comments
 (0)