diff --git a/lib/prism/translation/ripper.rb b/lib/prism/translation/ripper.rb index f179a149a1..826a4e3f87 100644 --- a/lib/prism/translation/ripper.rb +++ b/lib/prism/translation/ripper.rb @@ -648,6 +648,16 @@ def parse end end + # Return encoding of the source. + def encoding + result.source.encoding + end + + # Return true if parsed source ended by `__END__`. + def end_seen? + !!result.data_loc + end + ########################################################################## # Visitor methods ########################################################################## diff --git a/test/prism/ruby/ripper_test.rb b/test/prism/ruby/ripper_test.rb index 4fff630561..c5aa63aab2 100644 --- a/test/prism/ruby/ripper_test.rb +++ b/test/prism/ruby/ripper_test.rb @@ -230,6 +230,28 @@ def test_encoding assert_equal(Ripper.sexp(source), Translation::Ripper.sexp(source)) end + def test_encoding_method + source = "foo" + assert_equal(Ripper.new(source).tap(&:parse).encoding, Prism::Translation::Ripper.new(source).tap(&:parse).encoding) + + source = "foo".b + assert_equal(Ripper.new(source).tap(&:parse).encoding, Prism::Translation::Ripper.new(source).tap(&:parse).encoding) + + source = "# encoding: shift_jis" + assert_equal(Ripper.new(source).tap(&:parse).encoding, Prism::Translation::Ripper.new(source).tap(&:parse).encoding) + + source = "# encoding: shift_jis".b + assert_equal(Ripper.new(source).tap(&:parse).encoding, Prism::Translation::Ripper.new(source).tap(&:parse).encoding) + end + + def test_end_seen + source = "" + assert_equal(Ripper.new(source).tap(&:parse).end_seen?, Prism::Translation::Ripper.new(source).tap(&:parse).end_seen?) + + source = "__END__" + assert_equal(Ripper.new(source).tap(&:parse).end_seen?, Prism::Translation::Ripper.new(source).tap(&:parse).end_seen?) + end + def test_sexp_coercion string_like = Object.new def string_like.to_str