Sha256: e59ab53a61d0a6d33ce9593597bd9309ca76f25c7b25bd195dc371145e6c1d58

Contents?: true

Size: 1.05 KB

Versions: 3

Compression:

Stored size: 1.05 KB

Contents

# frozen_string_literal: true

module DeadEnd
  # 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: https://github.com/zombocom/dead_end/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"
  #
  class PathnameFromMessage
    attr_reader :name

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

    def call
      until stop?
        @guess << @parts.shift
        @name = Pathname(@guess.join(":"))
      end

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

      self
    end

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

      @name&.exist?
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
dead_end-3.1.2 lib/dead_end/pathname_from_message.rb
dead_end-3.1.1 lib/dead_end/pathname_from_message.rb
dead_end-3.1.0 lib/dead_end/pathname_from_message.rb