summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorKyrylo Silin <silin@kyrylo.org>2018-11-02 02:49:11 +0800
committerKyrylo Silin <silin@kyrylo.org>2018-11-02 23:45:24 +0800
commit20fb1efde4530c78f7627d8b5b4e0b3f43cf821d (patch)
tree19a833aa40a1c7acb36b2d5659cb77d64190be72 /lib
parent39021edb3f40ecf6bc70ff9809ce93fe6e3fface (diff)
downloadmethod_source-20fb1efde4530c78f7627d8b5b4e0b3f43cf821d.tar.gz
method_source: fix broken Procs on JRuby 9.2.0.0
Fixes https://github.com/pry/pry/issues/1804 (JRuby 9.2.0.0 breaks `source_location` and therefore our test suite) JRuby 9.2.0.0 fails to fetch source code for procs because of the bug: https://github.com/jruby/jruby/pull/5262 The problem is that source_location is reported at the end of the proc, instead of the beginning. The way I fix it is rather dumb (rewinding back and checking if it's complete expression) but it's isolated only to 9.2.0.0 and likely won't be needed when another JRuby is released. However, so far it's the latest release.
Diffstat (limited to 'lib')
-rw-r--r--lib/method_source/code_helpers.rb23
1 files changed, 23 insertions, 0 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