《luacheck 代码检查工具的配置与使用》

《luacheck 代码检查工具的配置与使用》

luacheck 是 Lua 的静态代码检查工具,可检测未使用变量、语法错误、逻辑错误等。

1. 安装

方法一:通过 luarocks

apt-get install -y --force-yes luarocks luarocks install --deps-mode=all luacheck

方法二:通过 deb 包(Debian Jessie 等)

wget http://ftp.br.debian.org/debian/pool/main/l/lua-filesystem/lua-filesystem_1.6.2-3_amd64.deb dpkg -i lua-filesystem_1.6.2-3_amd64.deb wget http://ftp.br.debian.org/debian/pool/main/l/luacheck/lua-check_0.17.1-1_all.deb dpkg -i lua-check_0.17.1-1_all.deb

2. 配置文件 .luacheckrc

通过 .luacheckrc 控制 luacheck 行为:

-- 忽略的全局变量 ignore = { "ngx", "cf", "resty", "cjson", } -- 忽略特定文件中 `Line is too long.` 警告 files["security/rate_limit/check.lua"] = { ignore = { "631" -- 631 表示 `Line is too long` 警告 } } -- 排除的文件 exclude_files = { "baidu_error_page.lua", "china_logic.lua", "legacy/*", } -- 使用 ngx_lua 标准,避免 require/tonumber/os/type 等全局对象报警 std = "ngx_lua" color = true -- 自定义的全局变量,加入后 luacheck 不会发出警告 globals = { "g_config_redis_cname", "g_config_redis_ip", }

3. 运行

luacheck script.lua luacheck . # 检查当前目录

4. 官方文档

  • 配置说明:https://luacheck.readthedocs.io/en/stable/config.html
  • 警告编码对应表:https://luacheck.readthedocs.io/en/stable/warnings.html
阅读 — · 全站 —
🎸 我的歌单 0 首