Module: InternalApi::FullMethodSourceLocation

Extended by:
FullMethodSourceLocation
Included in:
FullMethodSourceLocation
Defined in:
lib/internal_api/full_method_source_location.rb

Overview

Ruby gives us the start location of a method but not the end. We need to know the full range so we can verify any calls made anywhere in a method have originated from that method.

We lean on the MethodSource gem (the library underneath Pry’s method inspection) for this as it does some wild trickery with attempting to parse raw Ruby code without actually using a parser.

Instance Method Summary collapse

Instance Method Details

#public_method_source_ranges(mod) ⇒ Object

Find the file path and the start and end line numbers of all public class methods and public instance methods



27
28
29
30
31
32
33
34
35
# File 'lib/internal_api/full_method_source_location.rb', line 27

def public_method_source_ranges(mod)
  class_ranges = mod.public_methods(false).map do |m|
    FullMethodSourceLocation.range(mod.method(m))
  end.compact
  instance_ranges = mod.public_instance_methods(false).map do |m|
    FullMethodSourceLocation.range(mod.instance_method(m))
  end.compact
  class_ranges + instance_ranges
end

#range(method) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/internal_api/full_method_source_location.rb', line 15

def range(method)
  source_location = method.source_location
  return unless source_location

  path, start = source_location

  source = MethodSource.source_helper(source_location)
  [path, (start..(start + source.lines.size - 1))]
end