Class: SyntaxSuggest::PathnameFromMessage

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_suggest/pathname_from_message.rb

Overview

Converts a SyntaxError message to a path

Handles the case where the filename has a colon in it such as on a windows file system: github.com/ruby/syntax_suggest/issues/111

Example:

message = "/tmp/scratch:2:in `require_relative': /private/tmp/bad.rb:1: syntax error, unexpected `end' (SyntaxError)"
puts PathnameFromMessage.new(message).call.name
# => "/tmp/scratch.rb"

Constant Summary collapse

EVAL_RE =
/^\(eval.*\):\d+/
STREAMING_RE =
/^-:\d+/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message, io: $stderr) ⇒ PathnameFromMessage

Returns a new instance of PathnameFromMessage.



20
21
22
23
24
25
26
# File 'lib/syntax_suggest/pathname_from_message.rb', line 20

def initialize(message, io: $stderr)
  @line = message.lines.first
  @parts = @line.split(":")
  @guess = []
  @name = nil
  @io = io
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



18
19
20
# File 'lib/syntax_suggest/pathname_from_message.rb', line 18

def name
  @name
end

Instance Method Details

#callObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/syntax_suggest/pathname_from_message.rb', line 28

def call
  if skip_missing_file_name?
    if ENV["SYNTAX_SUGGEST_DEBUG"]
      @io.puts "SyntaxSuggest: Could not find filename from #{@line.inspect}"
    end
  else
    until stop?
      @guess << @parts.shift
      @name = Pathname(@guess.join(":"))
    end

    if @parts.empty?
      @io.puts "SyntaxSuggest: Could not find filename from #{@line.inspect}"
      @name = nil
    end
  end

  self
end

#skip_missing_file_name?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/syntax_suggest/pathname_from_message.rb', line 55

def skip_missing_file_name?
  @line.match?(EVAL_RE) || @line.match?(STREAMING_RE)
end

#stop?Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
# File 'lib/syntax_suggest/pathname_from_message.rb', line 48

def stop?
  return true if @parts.empty?
  return false if @guess.empty?

  @name&.exist?
end