luacheck:忽略文件中定义的全球群 我有一个模块常数。lua,其定义了许多全球群体。我如何在卢阿切克中忽略所有这些? 我假设我可以在.luacheckrc中构建逻辑来执行此操作:load startants.lua,看看什么...

问题描述 投票:0回答:1
read_globals

.

。 我使用lua5.4.

	
如果您在常数中具有

lua global lua-5.4 luacheck
1个回答
0
投票
everepect上处理此文件。

从您的.luacheckrc加载文件: -- Your normal luacheck config here. We'll modify this table later. read_globals = { -- Can manually define globals too. } -- Below code will load your constants files and append them to -- read_globals. -- Load script with constants and add to populate dest_globals. local function append_globals(dest_globals, script) -- Create a fallback env so lua stdlib is available, but we -- have a clean list of globals. local env = setmetatable({}, {__index = _G}) local fn = loadfile(script, "t", env) fn() for key,val in pairs(env) do table.insert(dest_globals, key) end end -- Pass a global table created above to populate with globals: -- read_globals, files["*.lua"].read_globals, etc. xpcall(append_globals, print, read_globals, "absolute/path/to/constants.lua") 如果常数显示为缺失变量,请在命令行上运行Luacheck进行调试(您应该获取输出)。

渗透与相对路径

不幸的是,它使用了绝对路径,因为

debug.getInfo
和其他技巧

在Luacheckrc中不工作 - 它们返回“块”而不是文件名。 wowever
,如果您的所有LUA代码都在一个通用目录中,则可以尝试从当前工作目录中获取绝对路径,因为Luacheck可能与您的LUA代码相同的文件夹运行:

local cwd = io.popen("cd"):read('*all') -- All code lives in the script folder: c:/proj/script/*.lua local root_dir = cwd:match("(.*[/\\])script[/\\]") xpcall(append_globals, print, read_globals, root_dir .."script/constants.lua")

支持

如果您嵌入了LUA,并且主机程序设置了软件包路径,则需要将该路径设置为支持

require。您可以在运行时使用require或修改它。将其放在任何电话之前: $LUA_PATH_5_4

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.