Skip to content

Commit e5b926f

Browse files
Earlopainkddnewton
authored andcommitted
Add ripper encoding/end_seen?
Ripper documentation is broken (only shows methods defined in ruby), these are public API.
1 parent aa27d61 commit e5b926f

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

lib/prism/translation/ripper.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,16 @@ def parse
648648
end
649649
end
650650

651+
# Return encoding of the source.
652+
def encoding
653+
result.source.encoding
654+
end
655+
656+
# Return true if parsed source ended by `__END__`.
657+
def end_seen?
658+
!!result.data_loc
659+
end
660+
651661
##########################################################################
652662
# Visitor methods
653663
##########################################################################

test/prism/ruby/ripper_test.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,28 @@ def test_encoding
230230
assert_equal(Ripper.sexp(source), Translation::Ripper.sexp(source))
231231
end
232232

233+
def test_encoding_method
234+
source = "foo"
235+
assert_equal(Ripper.new(source).tap(&:parse).encoding, Prism::Translation::Ripper.new(source).tap(&:parse).encoding)
236+
237+
source = "foo".b
238+
assert_equal(Ripper.new(source).tap(&:parse).encoding, Prism::Translation::Ripper.new(source).tap(&:parse).encoding)
239+
240+
source = "# encoding: shift_jis"
241+
assert_equal(Ripper.new(source).tap(&:parse).encoding, Prism::Translation::Ripper.new(source).tap(&:parse).encoding)
242+
243+
source = "# encoding: shift_jis".b
244+
assert_equal(Ripper.new(source).tap(&:parse).encoding, Prism::Translation::Ripper.new(source).tap(&:parse).encoding)
245+
end
246+
247+
def test_end_seen
248+
source = ""
249+
assert_equal(Ripper.new(source).tap(&:parse).end_seen?, Prism::Translation::Ripper.new(source).tap(&:parse).end_seen?)
250+
251+
source = "__END__"
252+
assert_equal(Ripper.new(source).tap(&:parse).end_seen?, Prism::Translation::Ripper.new(source).tap(&:parse).end_seen?)
253+
end
254+
233255
def test_sexp_coercion
234256
string_like = Object.new
235257
def string_like.to_str

0 commit comments

Comments
 (0)