class Pronto::Luacheck::Wrapper

Public Class Methods

new() click to toggle source
# File lib/pronto/luacheck/wrapper.rb, line 8
def initialize
  @luacheck_path = ENV['PRONTO_LUACHECK_PATH'] || 'luacheck'
end

Public Instance Methods

run(filepath) click to toggle source
# File lib/pronto/luacheck/wrapper.rb, line 12
def run(filepath)
  stdout, stderr, = run_luacheck(filepath)
  puts "WARN: pronto-luacheck: #{stderr}" if stderr && !stderr.empty?
  return [] if stdout.nil? || stdout == 0
  parse_output(stdout)
end

Private Instance Methods

level(failure) click to toggle source
# File lib/pronto/luacheck/wrapper.rb, line 56
def level(failure)
  type = failure.attributes['type']
  return :warning if type.start_with?('W')
  return :error if type.start_with?('E')
  :info
end
parse_output(output) click to toggle source
# File lib/pronto/luacheck/wrapper.rb, line 21
def parse_output(output)
  doc = REXML::Document.new(output)

  result = []
  REXML::XPath.match(doc, '//testcase').each do |testcase|
    file = testcase.attributes['classname']
    next if testcase.elements.size != 1

    failure = testcase.elements.first
    md = failure.attributes['message']
         .match(/\A#{Regexp.escape(file)}:(?<line>\d+):(?<column>\d+):\s+(?<message>.*)\z/)
    next unless md

    result << {
      file: file,
      line: md[:line].to_i,
      column: md[:column].to_i,
      level: level(failure),
      message: md[:message],
      rule: rule(failure)
    }
  end

  result
end
rule(failure) click to toggle source
# File lib/pronto/luacheck/wrapper.rb, line 51
def rule(failure)
  type = failure.attributes['type']
  type[1..-1]
end
run_luacheck(path) click to toggle source
# File lib/pronto/luacheck/wrapper.rb, line 47
def run_luacheck(path)
  Open3.capture3("#{@luacheck_path.shellescape} --formatter=JUnit #{path.shellescape}")
end