mpeterv / luacheck

A tool for linting and static analysis of Lua code.
MIT License
1.92k stars 322 forks source link

Allow to change the format of the output #73

Closed ogerovich closed 8 years ago

ogerovich commented 8 years ago

Hello,

We have a tool that processes output of luacheck. Currently, the format of luacheck with default or plain formatter is this: C:\some\dir\foo.lua:5:7: accessing undefined variable 'qqq'

Our tool natively works with format like this: C:\some\dir\foo.lua(5,7): accessing undefined variable 'qqq'

I can't find a way to produce this with luacheck.

mpeterv commented 8 years ago

Hello,

You can use a custom formatter to produce output in your format. See docs for --formatter. Built-in formatters are implemented in src/luacheck/format.lua, you can use that as an example. Reimplementing default formatter with your change would require copying a lot of code from there to support colors and other options, but for plain formatter it's simple:

local luacheck = require "luacheck"

return function(reports, file_names)
   local lines = {}

   for i, report in ipairs(reports) do
      if report.fatal then
         table.insert(lines, ("%s: %s error (%s)"):format(file_names[i], report.fatal, report.msg))
      else
         for _, issue in ipairs(report) do
            local message = luacheck.get_message(issue)
            table.insert(lines, ("%s(%d,%d): %s"):format(file_names[i], issue.line, issue.column, message))
         end
      end
   end

   return table.concat(lines, "\n")
end

Put this into a module and then run luacheck with --formatter=module.name.

ogerovich commented 8 years ago

Thank you! Exactly what I needed. It works perfectly.