Liu Hao
Liu Hao

Reputation: 512

How to make warning in luacheck as error?

I want luacheck treat "accessing undefined variable 'variable' " warning as error, which ALWAYS make sense for me. so how can I config specific warning , let's say W113 as errors, so I can find them at a glance

Upvotes: 3

Views: 270

Answers (1)

Davide Magni
Davide Magni

Reputation: 11

You could use a Lua script to run luacheck via command-line, and use pattern matching to error out if needed:

local dir = assert(arg[1])
local f = assert(io.popen('luacheck --no-color ' .. dir, 'r')) -- no-color flag allows numbers represented as strings to be tonumber'd
local s = assert(f:read('*a'))
print(s)
f:close()

local errors = tonumber(s:match("Total:.+/ (.+) error"))

assert(errors == 0, "Luacheck: found " .. errors .. " critical error(s) in Lua code.")

Of course, this implies that you can't run Luacheck directly — you have to execute the script instead, so it might not be applicable to all scenarios. Also if the log output changes at some point, the script will need updating accordingly, as the pattern matching would fail.

Example: runLuacheck.lua . runs Luacheck on the working directory.

Upvotes: 0

Related Questions