Sha256: 4bf642c54223c4d4a2c17fe423233ab2907f944e21652839a8ce06b7fb97224a

Contents?: true

Size: 1.6 KB

Versions: 2

Compression:

Stored size: 1.6 KB

Contents

require 'open3'
require 'rexml/document'
require 'shellwords'

module Pronto
  module Luacheck
    class Wrapper
      def initialize
        @luacheck_path = ENV['PRONTO_LUACHECK_PATH'] || 'luacheck'
      end

      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

      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

      def run_luacheck(path)
        Open3.capture3("#{@luacheck_path.shellescape} --formatter=JUnit #{path.shellescape}")
      end

      def rule(failure)
        type = failure.attributes['type']
        type[1..-1]
      end

      def level(failure)
        type = failure.attributes['type']
        return :warning if type.start_with?('W')
        return :error if type.start_with?('E')
        :info
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pronto-luacheck-1.0.1 lib/pronto/luacheck/wrapper.rb
pronto-luacheck-1.0.0 lib/pronto/luacheck/wrapper.rb