Skip to content

Commit

Permalink
Add luacheck to check builtin (#7895)
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenwardy committed Aug 6, 2019
1 parent 8da35c2 commit 8e75785
Show file tree
Hide file tree
Showing 24 changed files with 201 additions and 139 deletions.
74 changes: 74 additions & 0 deletions .luacheckrc
@@ -0,0 +1,74 @@
unused_args = false
allow_defined_top = true

ignore = {
"131", -- Unused global variable
"431", -- Shadowing an upvalue
"432", -- Shadowing an upvalue argument
}

read_globals = {
"ItemStack",
"INIT",
"DIR_DELIM",
"dump", "dump2",
"fgettext", "fgettext_ne",
"vector",
"VoxelArea",
"profiler",
"Settings",

string = {fields = {"split"}},
table = {fields = {"copy", "getn", "indexof", "insert_all"}},
math = {fields = {"hypot"}},
}

globals = {
"core",
"gamedata",
os = { fields = { "tempfolder" } },
"_",
}

files["builtin/client/register.lua"] = {
globals = {
debug = {fields={"getinfo"}},
}
}

files["builtin/common/misc_helpers.lua"] = {
globals = {
"dump", "dump2", "table", "math", "string",
"fgettext", "fgettext_ne", "basic_dump", "game", -- ???
"file_exists", "get_last_folder", "cleanup_path", -- ???
},
}

files["builtin/common/vector.lua"] = {
globals = { "vector" },
}

files["builtin/game/voxelarea.lua"] = {
globals = { "VoxelArea" },
}

files["builtin/game/init.lua"] = {
globals = { "profiler" },
}

files["builtin/common/filterlist.lua"] = {
globals = {
"filterlist",
"compare_worlds", "sort_worlds_alphabetic", "sort_mod_list", -- ???
},
}

files["builtin/mainmenu"] = {
globals = {
"gamedata",
},

read_globals = {
"PLATFORM",
},
}
8 changes: 4 additions & 4 deletions builtin/client/chatcommands.lua
Expand Up @@ -16,7 +16,7 @@ core.register_on_sending_chat_message(function(message)
end

local cmd, param = string.match(message, "^%.([^ ]+) *(.*)")
param = param or ""
param = param or ""

if not cmd then
core.display_chat_message(core.gettext("-!- Empty command"))
Expand All @@ -26,9 +26,9 @@ core.register_on_sending_chat_message(function(message)
local cmd_def = core.registered_chatcommands[cmd]
if cmd_def then
core.set_last_run_mod(cmd_def.mod_origin)
local _, message = cmd_def.func(param)
if message then
core.display_chat_message(message)
local _, result = cmd_def.func(param)
if result then
core.display_chat_message(result)
end
else
core.display_chat_message(core.gettext("-!- Invalid command: ") .. cmd)
Expand Down
1 change: 0 additions & 1 deletion builtin/common/filterlist.lua
Expand Up @@ -250,7 +250,6 @@ end

--------------------------------------------------------------------------------
function compare_worlds(world1,world2)

if world1.path ~= world2.path then
return false
end
Expand Down
7 changes: 3 additions & 4 deletions builtin/common/information_formspecs.lua
Expand Up @@ -31,7 +31,6 @@ local mod_cmds = {}
local function load_mod_command_tree()
mod_cmds = {}

local check_player_privs = core.check_player_privs
for name, def in pairs(core.registered_chatcommands) do
mod_cmds[def.mod_origin] = mod_cmds[def.mod_origin] or {}
local cmds = mod_cmds[def.mod_origin]
Expand Down Expand Up @@ -86,8 +85,8 @@ end

local function build_privs_formspec(name)
local privs = {}
for name, def in pairs(core.registered_privileges) do
privs[#privs + 1] = { name, def }
for priv_name, def in pairs(core.registered_privileges) do
privs[#privs + 1] = { priv_name, def }
end
table.sort(privs, function(a, b) return a[1] < b[1] end)

Expand Down Expand Up @@ -137,7 +136,7 @@ help_command.func = function(name, param)
if param == "" or param == "all" then
core.show_formspec(name, "__builtin:help_cmds",
build_chatcommands_formspec(name))
return
return
end

return old_help_func(name, param)
Expand Down
17 changes: 9 additions & 8 deletions builtin/common/misc_helpers.lua
Expand Up @@ -128,6 +128,7 @@ function dump(o, indent, nested, level)
if t ~= "table" then
return basic_dump(o)
end

-- Contains table -> true/nil of currently nested tables
nested = nested or {}
if nested[o] then
Expand All @@ -136,10 +137,11 @@ function dump(o, indent, nested, level)
nested[o] = true
indent = indent or "\t"
level = level or 1
local t = {}

local ret = {}
local dumped_indexes = {}
for i, v in ipairs(o) do
t[#t + 1] = dump(v, indent, nested, level + 1)
ret[#ret + 1] = dump(v, indent, nested, level + 1)
dumped_indexes[i] = true
end
for k, v in pairs(o) do
Expand All @@ -148,7 +150,7 @@ function dump(o, indent, nested, level)
k = "["..dump(k, indent, nested, level + 1).."]"
end
v = dump(v, indent, nested, level + 1)
t[#t + 1] = k.." = "..v
ret[#ret + 1] = k.." = "..v
end
end
nested[o] = nil
Expand All @@ -157,10 +159,10 @@ function dump(o, indent, nested, level)
local end_indent_str = "\n"..string.rep(indent, level - 1)
return string.format("{%s%s%s}",
indent_str,
table.concat(t, ","..indent_str),
table.concat(ret, ","..indent_str),
end_indent_str)
end
return "{"..table.concat(t, ", ").."}"
return "{"..table.concat(ret, ", ").."}"
end

--------------------------------------------------------------------------------
Expand Down Expand Up @@ -407,9 +409,8 @@ if INIT == "game" then
end

local old_itemstack = ItemStack(itemstack)
local new_itemstack, removed = core.item_place_node(
itemstack, placer, pointed_thing, param2, prevent_after_place
)
local new_itemstack = core.item_place_node(itemstack, placer,
pointed_thing, param2, prevent_after_place)
return infinitestacks and old_itemstack or new_itemstack
end

Expand Down
1 change: 0 additions & 1 deletion builtin/common/serialize.lua
Expand Up @@ -218,4 +218,3 @@ test_in = {escape_chars="\n\r\t\v\\\"\'", non_european="θשׁ٩∂"}
test_out = core.deserialize(core.serialize(test_in))
assert(test_in.escape_chars == test_out.escape_chars)
assert(test_in.non_european == test_out.non_european)

57 changes: 30 additions & 27 deletions builtin/game/chatcommands.lua
Expand Up @@ -27,8 +27,8 @@ core.register_on_chat_message(function(name, message)
local has_privs, missing_privs = core.check_player_privs(name, cmd_def.privs)
if has_privs then
core.set_last_run_mod(cmd_def.mod_origin)
local success, message = cmd_def.func(name, param)
if message then
local _, result = cmd_def.func(name, param)
if result then
core.chat_send_player(name, message)
end
else
Expand Down Expand Up @@ -125,10 +125,10 @@ core.register_chatcommand("haspriv", {
if core.check_player_privs(player_name, privs) then
table.insert(players_with_priv, player_name)
end
end
end
return true, "Players online with the \"" .. param .. "\" privilege: " ..
table.concat(players_with_priv, ", ")
end
end
})

local function handle_grant_command(caller, grantname, grantprivstr)
Expand Down Expand Up @@ -261,11 +261,12 @@ core.register_chatcommand("setpassword", {
toname = param:match("^([^ ]+) *$")
raw_password = nil
end

if not toname then
return false, "Name field required"
end
local act_str_past = "?"
local act_str_pres = "?"

local act_str_past, act_str_pres
if not raw_password then
core.set_player_password(toname, "")
act_str_past = "cleared"
Expand All @@ -277,13 +278,14 @@ core.register_chatcommand("setpassword", {
act_str_past = "set"
act_str_pres = "sets"
end

if toname ~= name then
core.chat_send_player(toname, "Your password was "
.. act_str_past .. " by " .. name)
end

core.log("action", name .. " " .. act_str_pres
.. " password of " .. toname .. ".")
core.log("action", name .. " " .. act_str_pres ..
" password of " .. toname .. ".")

return true, "Password of player \"" .. toname .. "\" " .. act_str_past
end,
Expand Down Expand Up @@ -367,35 +369,35 @@ core.register_chatcommand("teleport", {
return pos, false
end

local teleportee = nil
local p = {}
p.x, p.y, p.z = string.match(param, "^([%d.-]+)[, ] *([%d.-]+)[, ] *([%d.-]+)$")
p.x = tonumber(p.x)
p.y = tonumber(p.y)
p.z = tonumber(p.z)
if p.x and p.y and p.z then

local lm = 31000
if p.x < -lm or p.x > lm or p.y < -lm or p.y > lm or p.z < -lm or p.z > lm then
return false, "Cannot teleport out of map bounds!"
end
teleportee = core.get_player_by_name(name)
local teleportee = core.get_player_by_name(name)
if teleportee then
teleportee:set_pos(p)
return true, "Teleporting to "..core.pos_to_string(p)
end
end

local teleportee = nil
local p = nil
local target_name = nil
target_name = param:match("^([^ ]+)$")
teleportee = core.get_player_by_name(name)
local target_name = param:match("^([^ ]+)$")
local teleportee = core.get_player_by_name(name)

p = nil
if target_name then
local target = core.get_player_by_name(target_name)
if target then
p = target:get_pos()
end
end

if teleportee and p then
p = find_free_position_near(p)
teleportee:set_pos(p)
Expand All @@ -407,9 +409,9 @@ core.register_chatcommand("teleport", {
return false, "You don't have permission to teleport other players (missing bring privilege)"
end

local teleportee = nil
local p = {}
local teleportee_name = nil
teleportee = nil
p = {}
local teleportee_name
teleportee_name, p.x, p.y, p.z = param:match(
"^([^ ]+) +([%d.-]+)[, ] *([%d.-]+)[, ] *([%d.-]+)$")
p.x, p.y, p.z = tonumber(p.x), tonumber(p.y), tonumber(p.z)
Expand All @@ -422,10 +424,8 @@ core.register_chatcommand("teleport", {
.. " to " .. core.pos_to_string(p)
end

local teleportee = nil
local p = nil
local teleportee_name = nil
local target_name = nil
teleportee = nil
p = nil
teleportee_name, target_name = string.match(param, "^([^ ]+) +([^ ]+)$")
if teleportee_name then
teleportee = core.get_player_by_name(teleportee_name)
Expand Down Expand Up @@ -459,22 +459,25 @@ core.register_chatcommand("set", {
core.settings:set(setname, setvalue)
return true, setname .. " = " .. setvalue
end
local setname, setvalue = string.match(param, "([^ ]+) (.+)")

setname, setvalue = string.match(param, "([^ ]+) (.+)")
if setname and setvalue then
if not core.settings:get(setname) then
return false, "Failed. Use '/set -n <name> <value>' to create a new setting."
end
core.settings:set(setname, setvalue)
return true, setname .. " = " .. setvalue
end
local setname = string.match(param, "([^ ]+)")

setname = string.match(param, "([^ ]+)")
if setname then
local setvalue = core.settings:get(setname)
setvalue = core.settings:get(setname)
if not setvalue then
setvalue = "<not set>"
end
return true, setname .. " = " .. setvalue
end

return false, "Invalid parameters (see /help set)."
end,
})
Expand Down Expand Up @@ -692,7 +695,7 @@ core.register_chatcommand("pulverize", {
end
core.log("action", name .. " pulverized \"" ..
wielded_item:get_name() .. " " .. wielded_item:get_count() .. "\"")
player:set_wielded_item(nil)
player:set_wielded_item(nil)
return true, "An item was pulverized."
end,
})
Expand Down Expand Up @@ -771,7 +774,7 @@ core.register_chatcommand("rollback", {
end
local target_name, seconds = string.match(param, ":([^ ]+) *(%d*)")
if not target_name then
local player_name = nil
local player_name
player_name, seconds = string.match(param, "([^ ]+) *(%d*)")
if not player_name then
return false, "Invalid parameters. See /help rollback"
Expand Down
2 changes: 1 addition & 1 deletion builtin/game/forceloading.lua
Expand Up @@ -111,7 +111,7 @@ end

-- periodical forceload persistence
local function periodically_persist_forceloaded_blocks()

-- only persist if the blocks actually changed
if forceload_blocks_changed then
persist_forceloaded_blocks()
Expand Down
6 changes: 0 additions & 6 deletions builtin/game/item.lua
Expand Up @@ -206,7 +206,6 @@ function core.get_node_drops(node, toolname)
-- Extended drop table
local got_items = {}
local got_count = 0
local _, item, tool
for _, item in ipairs(drop.items) do
local good_rarity = true
local good_tool = true
Expand Down Expand Up @@ -614,15 +613,10 @@ function core.node_dig(pos, node, digger)
end

-- Run script hook
local _, callback
for _, callback in ipairs(core.registered_on_dignodes) do
local origin = core.callback_origins[callback]
if origin then
core.set_last_run_mod(origin.mod)
--print("Running " .. tostring(callback) ..
-- " (a " .. origin.name .. " callback in " .. origin.mod .. ")")
else
--print("No data associated with callback")
end

-- Copy pos and node because callback can modify them
Expand Down

0 comments on commit 8e75785

Please sign in to comment.