summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDouglas Eichelberger <dduugg@gmail.com>2022-08-15 12:38:45 -0700
committerDouglas Eichelberger <dduugg@gmail.com>2022-08-17 07:59:35 -0700
commite0ecbb3b28e4183ec0546762144704b086feb468 (patch)
tree88126158a288763226034257367864e46d4f0a78
parent0633835d5de65f8099b0905e5aef34be4cae4238 (diff)
downloadpry-e0ecbb3b28e4183ec0546762144704b086feb468.tar.gz
Short circuit eval regexes in finding module definition
-rw-r--r--lib/pry/wrapped_module/candidate.rb11
1 files changed, 6 insertions, 5 deletions
diff --git a/lib/pry/wrapped_module/candidate.rb b/lib/pry/wrapped_module/candidate.rb
index f48f8751..7a0c3d66 100644
--- a/lib/pry/wrapped_module/candidate.rb
+++ b/lib/pry/wrapped_module/candidate.rb
@@ -98,14 +98,15 @@ class Pry
# line number is one-indexed.
def first_line_of_module_definition(file, line)
searchable_lines = lines_for_file(file)[0..(line - 2)]
- searchable_lines.rindex { |v| class_regexes.any? { |r| r =~ v } } + 1
+ searchable_lines.rindex { |v| module_definition_first_line?(v) } + 1
end
- def class_regexes
+ def module_definition_first_line?(line)
mod_type_string = wrapped.class.to_s.downcase
- [/(^|=)\s*#{mod_type_string}\s+(?:(?:\w*)::)*?#{wrapped.name.split(/::/).last}/,
- /^\s*(::)?#{wrapped.name.split(/::/).last}\s*?=\s*?#{wrapped.class}/,
- /^\s*(::)?#{wrapped.name.split(/::/).last}\.(class|instance)_eval/]
+ wrapped_name_last = wrapped.name.split(/::/).last
+ /(^|=)\s*#{mod_type_string}\s+(?:(?:\w*)::)*?#{wrapped_name_last}/ =~ line ||
+ /^\s*(::)?#{wrapped_name_last}\s*?=\s*?#{wrapped.class}/ =~ line ||
+ /^\s*(::)?#{wrapped_name_last}\.(class|instance)_eval/ =~ line
end
# This method is used by `Candidate#source_location` as a