summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Mair <jrmair@gmail.com>2018-11-02 16:46:26 +0100
committerGitHub <noreply@github.com>2018-11-02 16:46:26 +0100
commit64fff2206c38fe0cf883017bceac56250d444c5b (patch)
tree19a833aa40a1c7acb36b2d5659cb77d64190be72
parent39021edb3f40ecf6bc70ff9809ce93fe6e3fface (diff)
parent20fb1efde4530c78f7627d8b5b4e0b3f43cf821d (diff)
downloadmethod_source-64fff2206c38fe0cf883017bceac56250d444c5b.tar.gz
Merge pull request #51 from kyrylo/jruby-9200-fix
method_source: fix broken Procs on JRuby 9.2.0.0
-rw-r--r--lib/method_source/code_helpers.rb23
-rw-r--r--spec/method_source_spec.rb2
-rw-r--r--spec/spec_helper.rb5
3 files changed, 27 insertions, 3 deletions
diff --git a/lib/method_source/code_helpers.rb b/lib/method_source/code_helpers.rb
index 9d9da55..e431742 100644
--- a/lib/method_source/code_helpers.rb
+++ b/lib/method_source/code_helpers.rb
@@ -1,6 +1,9 @@
module MethodSource
module CodeHelpers
+ # @return [Boolean]
+ JRUBY_9200 = (defined?(JRUBY_VERSION) || false) && JRUBY_VERSION == '9.2.0.0'
+
# Retrieve the first expression starting on the given line of the given file.
#
# This is useful to get module or method source code.
@@ -29,6 +32,26 @@ module MethodSource
extract_first_expression(relevant_lines, options[:consume])
rescue SyntaxError => e
+ # JRuby 9.2.0.0 breaks #source_location for Procs (it reports line number
+ # as the last line of the Proc). This raises SyntaxError.
+ # See https://github.com/pry/pry/issues/1804 for details.
+ #
+ # To fix this, this hack rewinds source location one step at a time and
+ # tries to see if the new location is a complete expression.
+ #
+ # TODO: delete this once latest JRuby version is bumped.
+ # See https://github.com/banister/method_source/issues/52
+ if JRUBY_9200 && line_number > 0
+ loop do
+ line_number -= 1
+
+ # Skip empty lines since they are not real expressions.
+ break unless lines[line_number - 1] == "\n"
+ end
+
+ retry
+ end
+
raise if options[:strict]
begin
diff --git a/spec/method_source_spec.rb b/spec/method_source_spec.rb
index 088c21f..a609d49 100644
--- a/spec/method_source_spec.rb
+++ b/spec/method_source_spec.rb
@@ -31,7 +31,7 @@ describe MethodSource do
@hello_comment = "# A comment for hello\n# It spans two lines and is indented by 2 spaces\n"
@lambda_comment = "# This is a comment for MyLambda\n"
@lambda_source = "MyLambda = lambda { :lambda }\n"
- @proc_source = "MyProc = Proc.new { :proc }\n"
+ @proc_source = "MyProc = Proc.new do\n\n\nend\n"
@hello_instance_evaled_source = " def hello_\#{name}(*args)\n send_mesg(:\#{name}, *args)\n end\n"
@hello_instance_evaled_source_2 = " def \#{name}_two()\n if 44\n 45\n end\n end\n"
@hello_class_evaled_source = " def hello_\#{name}(*args)\n send_mesg(:\#{name}, *args)\n end\n"
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index 0e5de61..3b5201a 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -49,9 +49,11 @@ def comment_test5; end
# This is a comment for MyLambda
MyLambda = lambda { :lambda }
-MyProc = Proc.new { :proc }
+MyProc = Proc.new do
+end
+
name = "name"
M.instance_eval <<-METHOD, __FILE__, __LINE__ + 1
@@ -98,4 +100,3 @@ EOF
# class_eval without filename and lineno + 1 parameter
M.class_eval "def #{name}_three; @tempfile.#{name}; end"
-