Class: Tapioca::Runtime::SourceLocation

Inherits:
Object
  • Object
show all
Defined in:
lib/tapioca/runtime/source_location.rb

Constant Summary collapse

EVAL_SOURCE_FILE_PATTERN =

this looks something like: “(eval at /path/to/file.rb:123)” and we are interested in the “/path/to/file.rb” and “123” parts

/^\(eval at (?<file>.+):(?<line>\d+)\)/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file:, line:) ⇒ SourceLocation

Returns a new instance of SourceLocation.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/tapioca/runtime/source_location.rb', line 18

def initialize(file:, line:)
  # Ruby 3.3 adds automatic definition of source location for evals if
  # `file` and `line` arguments are not provided. This results in the source
  # file being something like `(eval at /path/to/file.rb:123)`. We try to parse
  # this string to get the actual source file.
  eval_pattern_match = EVAL_SOURCE_FILE_PATTERN.match(file)
  if eval_pattern_match
    file = eval_pattern_match[:file]
    line = eval_pattern_match[:line].to_i
  end

  @file = file
  @line = line
end

Instance Attribute Details

#fileObject (readonly)

: String



13
14
15
# File 'lib/tapioca/runtime/source_location.rb', line 13

def file
  @file
end

#lineObject (readonly)

: Integer



16
17
18
# File 'lib/tapioca/runtime/source_location.rb', line 16

def line
  @line
end

Class Method Details

.from_loc(loc) ⇒ Object

: ([String?, Integer?]? loc) -> SourceLocation?



38
39
40
# File 'lib/tapioca/runtime/source_location.rb', line 38

def from_loc(loc)
  new(file: loc.first, line: loc.last) if loc&.first && loc.last
end