module SyntaxSuggest

Constants

VERSION

Public Class Methods

Takes a ‘SyntaxError` exception, uses the error message to locate the file. Then the file will be analyzed to find the location of the syntax error and emit that location to stderr.

Example:

begin
  require 'bad_file'
rescue => e
  SyntaxSuggest.handle_error(e)
end

By default it will re-raise the exception unless ‘re_raise: false`. The message output location can be configured using the `io: $stderr` input.

If a valid filename cannot be determined, the original exception will be re-raised (even with ‘re_raise: false`).

# File lib/syntax_suggest/api.rb, line 43
def self.handle_error(e, re_raise: true, io: $stderr)
  unless e.is_a?(SyntaxError)
    io.puts("SyntaxSuggest: Must pass a SyntaxError, got: #{e.class}")
    raise e
  end

  file = PathnameFromMessage.new(e.message, io: io).call.name
  raise e unless file

  io.sync = true

  call(
    io: io,
    source: file.read,
    filename: file
  )

  raise e if re_raise
end

Public Instance Methods

Calls superclass method
# File lib/syntax_suggest/core_ext.rb, line 7
def detailed_message(highlight: true, syntax_suggest: true, **kwargs)
  return super unless syntax_suggest

  require "syntax_suggest/api" unless defined?(SyntaxSuggest::DEFAULT_VALUE)

  message = super

  if path
    file = Pathname.new(path)
    io = SyntaxSuggest::MiniStringIO.new

    SyntaxSuggest.call(
      io: io,
      source: file.read,
      filename: file,
      terminal: highlight
    )
    annotation = io.string

    annotation += "\n" unless annotation.end_with?("\n")

    annotation + message
  else
    message
  end
rescue => e
  if ENV["SYNTAX_SUGGEST_DEBUG"]
    warn(e.message)
    warn(e.backtrace)
  end

  # Ignore internal errors
  message
end