From 6cff2bfa4640cf49c62d127ddaba551ea0462f55 Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Tue, 11 Oct 2011 23:19:37 -0700 Subject: [PATCH] .dup code before letting RubyParser mangle it --- lib/pry/pry_instance.rb | 3 ++- test/test_pry.rb | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) 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 = []