From a9ffc8f0e596e61c38d9db6d3007a9a322f1c1d0 Mon Sep 17 00:00:00 2001 From: Eloy Duran Date: Sun, 30 May 2010 11:18:50 +0200 Subject: [PATCH] Fix syntax error messages so that it never shows the underscore variable. --- TODO | 2 -- lib/irb/context.rb | 8 +++++++- spec/regression/context_spec.rb | 16 ++++++++++++++++ 3 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 spec/regression/context_spec.rb diff --git a/TODO b/TODO index 57826ae..5ed1784 100644 --- a/TODO +++ b/TODO @@ -1,5 +1,3 @@ -* Make stdin work -* Fix error message resulting of syntax like: require 'date; * Make sure the following formatters work: hirb, awesome_print, and looksee * Make sure the majority of the utils in utility_belt work * Possibly add copy-paste support as an ext diff --git a/lib/irb/context.rb b/lib/irb/context.rb index 8854300..358c78b 100644 --- a/lib/irb/context.rb +++ b/lib/irb/context.rb @@ -34,6 +34,7 @@ def initialize(object, explicit_binding = nil) @line = 1 clear_buffer + @underscore_assigner = __evaluate__("_ = nil; proc { |val| _ = val }") @processors = self.class.processors.map { |processor| processor.new(self) } end @@ -42,7 +43,8 @@ def __evaluate__(source, file = __FILE__, line = __LINE__) end def evaluate(source) - result = __evaluate__("_ = (#{source})", '(irb)', @line - @source.buffer.size + 1) + result = __evaluate__(source.to_s, '(irb)', @line - @source.buffer.size + 1) + store_result(result) puts formatter.result(result) result rescue Exception => e @@ -108,6 +110,10 @@ def formatter def clear_buffer @source = Source.new end + + def store_result(result) + @underscore_assigner.call(result) + end end end diff --git a/spec/regression/context_spec.rb b/spec/regression/context_spec.rb new file mode 100644 index 0000000..2862724 --- /dev/null +++ b/spec/regression/context_spec.rb @@ -0,0 +1,16 @@ +require File.expand_path('../../spec_helper', __FILE__) + +main = self + +describe "IRB::Context, when evaluating source" do + before do + @context = IRB::Context.new(main) + def @context.printed; @printed ||= '' end + def @context.puts(string); printed << "#{string}\n" end + end + + it "does not assign the result to the `_' variable in one go, so it doesn't show up in a syntax error" do + @context.evaluate("'banana;") + @context.printed.should.not.include "_ = ('banana;)" + end +end \ No newline at end of file