diff options
author | Kyrylo Silin <silin@kyrylo.org> | 2018-10-04 02:51:40 +0800 |
---|---|---|
committer | Kyrylo Silin <silin@kyrylo.org> | 2018-10-04 03:46:33 +0800 |
commit | e252aacf9161e114bd6a36e049e3a11060af337e (patch) | |
tree | 02886d7510c9bcbc3f955361464e962227c6f3d8 | |
parent | 943a964c648b9343f4755142ca42fcf34174e928 (diff) | |
download | pry-1452-show-source-fix.tar.gz |
commands/show_source: handle when source is nil but comment exists1452-show-source-fix
Fixes https://github.com/pry/pry/issues/1452.
($ RuntimeError.exception fails)
Alternative to https://github.com/pry/pry/pull/1453.
-rw-r--r-- | CHANGELOG.md | 2 | ||||
-rw-r--r-- | lib/pry/commands/show_info.rb | 10 | ||||
-rw-r--r-- | lib/pry/commands/show_source.rb | 7 | ||||
-rw-r--r-- | spec/commands/show_source_spec.rb | 26 |
4 files changed, 41 insertions, 4 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index dae08c9f..d93f1b77 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,6 +44,8 @@ See pull request [#1723](https://github.com/pry/pry/pull/1723) `require`](https://bugs.ruby-lang.org/issues/10222) ([#1762](https://github.com/pry/pry/pull/1762), [#1774](https://github.com/pry/pry/pull/1762)) +* Fixed `NoMethodError` on code objects that have a comment but no source when + invoking `show-source` ([#1779](https://github.com/pry/pry/pull/1779)) #### Pry developers diff --git a/lib/pry/commands/show_info.rb b/lib/pry/commands/show_info.rb index 37b36371..5119f84f 100644 --- a/lib/pry/commands/show_info.rb +++ b/lib/pry/commands/show_info.rb @@ -100,11 +100,17 @@ class Pry # object types: methods, modules, commands, procs... def header(code_object) file_name, line_num = file_and_line_for(code_object) + content = content_for(code_object) + h = "\n#{bold('From:')} #{file_name} " h << code_object_header(code_object, line_num) - h << "\n#{bold('Number of lines:')} " << - "#{content_for(code_object).lines.count}\n\n" + h << "\n#{bold('Number of lines:')} " << "#{content.lines.count}\n\n" h << bold('** Warning:') << " Cannot find code for #{@original_code_object.nonblank_name}. Showing superclass #{code_object.nonblank_name} instead. **\n\n" if @used_super + + if content.lines.none? + h << bold('** Warning:') << " Cannot find code for '#{code_object.name}' (source_location is nil)" + end + h end diff --git a/lib/pry/commands/show_source.rb b/lib/pry/commands/show_source.rb index f8e3a5b0..ea53e12a 100644 --- a/lib/pry/commands/show_source.rb +++ b/lib/pry/commands/show_source.rb @@ -39,8 +39,11 @@ class Pry # The source for code_object prepared for display. def content_for(code_object) - Code.new(code_object.source, start_line_for(code_object)). - with_line_numbers(use_line_numbers?).highlighted + code = Code.new( + code_object.source || [], + start_line_for(code_object) + ) + code.with_line_numbers(use_line_numbers?).highlighted end end diff --git a/spec/commands/show_source_spec.rb b/spec/commands/show_source_spec.rb index 495caabf..26f6b4f4 100644 --- a/spec/commands/show_source_spec.rb +++ b/spec/commands/show_source_spec.rb @@ -184,6 +184,32 @@ describe "show-source" do Pry.config.commands.delete "hubba-hubba" end + context "when there's no source code but the comment exists" do + before do + class Foo + # Bingo. + def bar; end + end + + allow_any_instance_of(Pry::Method).to receive(:source).and_return(nil) + end + + after do + Object.remove_const(:Foo) + end + + it "outputs zero line numbers" do + out = pry_eval('show-source Foo#bar') + expect(out).to match(/ + Owner:\sFoo + .+ + Number\sof\slines:\s0 + .+ + \*\*\sWarning:\sCannot\sfind\scode\sfor\s'bar'\s\(source_location\sis\snil\) + /mx) + end + end + describe "finding super methods with help of `--super` switch" do before do class Foo |