类 SyntaxSuggest::PathnameFromMessage

SyntaxError 消息转换为路径

处理文件名包含冒号的情况,例如在 Windows 文件系统中:github.com/ruby/syntax_suggest/issues/111

示例

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"

常量

EVAL_RE
STREAMING_RE

属性

name[R]

公共类方法

new(message, io: $stderr) 点击切换源代码
# 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

公共实例方法

call() 点击切换源代码
# 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?() 点击切换源代码
# 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?() 点击切换源代码
# 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