diff --git a/lib/pry/pry_instance.rb b/lib/pry/pry_instance.rb index 10c28124..6e99f231 100644 --- a/lib/pry/pry_instance.rb +++ b/lib/pry/pry_instance.rb @@ -533,7 +533,8 @@ class Pry # valid_expression?("class Hello") #=> false # valid_expression?("class Hello; end") #=> true def valid_expression?(code) - RubyParser.new.parse(code) + # NOTE: we're using .dup because RubyParser mutates the input + RubyParser.new.parse(code.dup) true rescue Racc::ParseError, SyntaxError false diff --git a/test/test_pry.rb b/test/test_pry.rb index 636d32d0..5023a5be 100644 --- a/test/test_pry.rb +++ b/test/test_pry.rb @@ -207,6 +207,15 @@ describe Pry do end end + describe "valid_expression?" do + it "should not mutate the input!" do + clean = "puts <<-FOO\nhi\nFOO\n" + a = clean.dup + Pry.new.valid_expression?(a) + a.should == clean + end + end + describe "history arrays" do it 'sets _ to the last result' do res = []