module DeadEnd

Constants

IsProduction
SEARCH_SOURCE_ON_ERROR_DEFAULT
TIMEOUT_DEFAULT
VERSION

Public Class Methods

call(source: , filename: , terminal: false, record_dir: nil, timeout: TIMEOUT_DEFAULT, io: $stderr) click to toggle source
# File lib/dead_end/internals.rb, line 40
def self.call(source: , filename: , terminal: false, record_dir: nil, timeout: TIMEOUT_DEFAULT, io: $stderr)
  search = nil
  Timeout.timeout(timeout) do
    record_dir ||= ENV["DEBUG"] ? "tmp" : nil
    search = CodeSearch.new(source, record_dir: record_dir).call
  end

  blocks = search.invalid_blocks
  DisplayInvalidBlocks.new(
    blocks: blocks,
    filename: filename,
    terminal: terminal,
    code_lines: search.code_lines,
    invalid_obj: invalid_type(source),
    io: io
  ).call
rescue Timeout::Error => e
  io.puts "Search timed out DEAD_END_TIMEOUT=#{timeout}, run with DEBUG=1 for more info"
  io.puts e.backtrace.first(3).join($/)
end
handle_error(e, search_source_on_error: SEARCH_SOURCE_ON_ERROR_DEFAULT) click to toggle source
# File lib/dead_end/internals.rb, line 19
def self.handle_error(e, search_source_on_error: SEARCH_SOURCE_ON_ERROR_DEFAULT)
  raise e if !e.message.include?("end-of-input")

  filename = e.message.split(":").first

  $stderr.sync = true
  $stderr.puts "Run `$ dead_end #{filename}` for more options\n"

  if search_source_on_error
    self.call(
      source: Pathname(filename).read,
      filename: filename,
      terminal: true,
    )
  end

  $stderr.puts ""
  $stderr.puts ""
  raise e
end
invalid?(source) click to toggle source
# File lib/dead_end/internals.rb, line 95
def self.invalid?(source)
  source = source.join if source.is_a?(Array)
  source = source.to_s

  Ripper.new(source).tap(&:parse).error?
end
invalid_type(source) click to toggle source
# File lib/dead_end/internals.rb, line 141
def self.invalid_type(source)
  WhoDisSyntaxError.new(source).call
end
valid?(source) click to toggle source

Returns truthy if a given input source is valid syntax

DeadEnd.valid?(<<~EOM) # => true
  def foo
  end
EOM

DeadEnd.valid?(<<~EOM) # => false
  def foo
    def bar # Syntax error here
  end
EOM

You can also pass in an array of lines and they'll be joined before evaluating

DeadEnd.valid?(
  [
    "def foo\n",
    "end\n"
  ]
) # => true

DeadEnd.valid?(
  [
    "def foo\n",
    "  def bar\n", # Syntax error here
    "end\n"
  ]
) # => false

As an FYI the CodeLine class instances respond to `to_s` so passing a CodeLine in as an object or as an array will convert it to it's code representation.

# File lib/dead_end/internals.rb, line 136
def self.valid?(source)
  !invalid?(source)
end
valid_without?(without_lines: , code_lines:) click to toggle source

This will tell you if the `code_lines` would be valid if you removed the `without_lines`. In short it's a way to detect if we've found the lines with syntax errors in our document yet.

code_lines = [
  CodeLine.new(line: "def foo\n",   index: 0)
  CodeLine.new(line: "  def bar\n", index: 1)
  CodeLine.new(line: "end\n",       index: 2)
]

DeadEnd.valid_without?(
  without_lines: code_lines[1],
  code_lines: code_lines
)                                    # => true

DeadEnd.valid?(code_lines) # => false
# File lib/dead_end/internals.rb, line 85
def self.valid_without?(without_lines: , code_lines:)
  lines = code_lines - Array(without_lines).flatten

  if lines.empty?
    return true
  else
    return valid?(lines)
  end
end