如何将 luacheck 中的警告转化为错误?

我想让 luacheck 将“访问未定义变量 'variable'”的警告视为错误,这对我来说总是有意义的。 那么我应该如何配置特定的警告,比如将 W113 视为错误,这样我就可以一眼找到它们。

点赞
用户5704342
用户5704342

你可以使用 Lua 脚本通过命令行运行 luacheck,使用模式匹配来查找错误:

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.")

当然,这意味着你不能直接运行 Luacheck,而是需要执行脚本,因此它可能并不适用于所有情况。此外,如果日志输出在某些时候发生改变,脚本也需要相应更新,否则模式匹配将会失败。

示例:runLuacheck.lua . 在工作目录上运行 Luacheck。

2022-11-18 17:35:31