From 99ab54da0ae06a6664594fca04255760eba536a8 Mon Sep 17 00:00:00 2001 From: Cooldude2606 <25043174+Cooldude2606@users.noreply.github.com> Date: Thu, 21 Nov 2024 00:03:19 +0000 Subject: [PATCH] Enable stricter type checks --- .luarc.json | 52 +++++++++++-------- exp_commands/module/module_exports.lua | 3 +- exp_groups/module/module_exports.lua | 3 +- .../module/modules/addons/discord-alerts.lua | 2 +- .../module/modules/addons/scorched-earth.lua | 4 +- .../module/modules/addons/spawn-area.lua | 3 ++ .../module/modules/data/personal-logistic.lua | 18 ++++--- exp_legacy/module/modules/gui/module.lua | 12 +++-- .../module/modules/gui/science-info.lua | 1 + exp_util/module/include/table.lua | 4 ++ exp_util/module/module_exports.lua | 2 + 11 files changed, 67 insertions(+), 37 deletions(-) diff --git a/.luarc.json b/.luarc.json index 2fe3d68b..b23e62cc 100644 --- a/.luarc.json +++ b/.luarc.json @@ -1,33 +1,41 @@ { "$schema": "https://raw.githubusercontent.com/LuaLS/vscode-lua/master/setting/schema.json", "completion.requireSeparator": "/", - "doc.regengine": "glob", - "doc.privateName": [ "__*" ], - "doc.packageName": [ "_*" ], "runtime.pluginArgs": [ "--clusterio-modules" ], - "diagnostics.unusedLocalExclude": [ "_*", "i", "j", "k", "v" ], - "diagnostics.groupFileStatus": { - "ambiguity": "Any", - "await": "None", - "codestyle": "Opened", - "conventions": "Any", - "duplicate": "Any", - "global": "Any", - "luadoc": "Opened", - "redefined": "Opened", - "strict": "Any", - "strong": "Any", - "type-check": "Any", - "unbalanced": "Opened", - "unused": "Opened" + "doc": { + "regengine": "glob", + "privateName": [ "__*" ], + "packageName": [ "_*" ] + }, + "type": { + "checkTableShape": true, + "inferParamType": true }, "$comment-name-style-check": "Disabled below until config issue fixed: https://github.com/LuaLS/lua-language-server/issues/2643", - "diagnostics.neededFileStatus": { - "no-unknown": "None!", - "spell-check": "None!", - "name-style-check": "None!" + "diagnostics": { + "unusedLocalExclude": [ "_*", "i", "j", "k", "v" ], + "groupFileStatus": { + "ambiguity": "Any", + "await": "None", + "codestyle": "Opened", + "conventions": "Any", + "duplicate": "Any", + "global": "Any", + "luadoc": "Opened", + "redefined": "Opened", + "strict": "Any", + "strong": "Any", + "type-check": "Any", + "unbalanced": "Opened", + "unused": "Opened" + }, + "neededFileStatus": { + "no-unknown": "None!", + "spell-check": "None!", + "name-style-check": "None!" + } }, "nameStyle.config": { "local_name_style": [{ diff --git a/exp_commands/module/module_exports.lua b/exp_commands/module/module_exports.lua index 6064c3ee..206d1e5c 100644 --- a/exp_commands/module/module_exports.lua +++ b/exp_commands/module/module_exports.lua @@ -125,7 +125,6 @@ Commands._metatable = { __class = "ExpCommand", } ---- @type LuaPlayer Commands.server = setmetatable({ index = 0, color = ExpUtil.color.white, @@ -154,7 +153,7 @@ Commands.server = setmetatable({ Commands.error("Command does not support rcon usage, requires LuaPlayer." .. key) error("Command does not support rcon usage, requires LuaPlayer." .. key) end, -}) +}) --[[ @as LuaPlayer ]] --- Status Returns. -- Return values used by command callbacks diff --git a/exp_groups/module/module_exports.lua b/exp_groups/module/module_exports.lua index 037e851c..6f84f4da 100644 --- a/exp_groups/module/module_exports.lua +++ b/exp_groups/module/module_exports.lua @@ -6,12 +6,13 @@ local json_to_table = helpers.json_to_table --- Top level module table, contains event handlers and public methods local Groups = {} ---- @class ExpGroup +--- @class ExpGroup : LuaPermissionGroup --- @field group LuaPermissionGroup The permission group for this group proxy Groups._prototype = {} Groups._metatable = { __index = setmetatable(Groups._prototype, { + --- @type any Annotation required because otherwise it is typed as 'table' __index = function(self, key) return self.group[key] end, diff --git a/exp_legacy/module/modules/addons/discord-alerts.lua b/exp_legacy/module/modules/addons/discord-alerts.lua index 23bde648..9491c455 100644 --- a/exp_legacy/module/modules/addons/discord-alerts.lua +++ b/exp_legacy/module/modules/addons/discord-alerts.lua @@ -42,7 +42,7 @@ end local function emit_event(args) local title = args.title or "" - local color = args.color or "0x0" + local color = args.color or "0x0" --- @type string | Color local description = args.description or "" if type(color) == "table" then diff --git a/exp_legacy/module/modules/addons/scorched-earth.lua b/exp_legacy/module/modules/addons/scorched-earth.lua index bc143d9b..5ef34e22 100644 --- a/exp_legacy/module/modules/addons/scorched-earth.lua +++ b/exp_legacy/module/modules/addons/scorched-earth.lua @@ -14,6 +14,7 @@ end -- Will degrade a tile down to the next tile when called local function degrade(surface, position) + --- @diagnostic disable-next-line Incorrect Api Type: https://forums.factorio.com/viewtopic.php?f=233&t=109145&p=593761&hilit=get_tile#p593761 local tile = surface.get_tile(position) local tile_name = tile.name local degrade_tile_name = config.degrade_order[tile_name] @@ -55,6 +56,7 @@ end -- Gets the mean of the strengths around a tile to give the strength at that position local function get_tile_strength(surface, position) + --- @diagnostic disable-next-line Incorrect Api Type: https://forums.factorio.com/viewtopic.php?f=233&t=109145&p=593761&hilit=get_tile#p593761 local tile = surface.get_tile(position) local tile_name = tile.name local strength = config.strengths[tile_name] @@ -62,7 +64,7 @@ local function get_tile_strength(surface, position) for x = -1, 1 do -- x loop local px = position.x + x for y = -1, 1 do -- y loop - local check_tile = surface.get_tile{ x = px, y = position.y + y } + local check_tile = surface.get_tile(px, position.y + y) local check_tile_name = check_tile.name local check_strength = config.strengths[check_tile_name] or 0 strength = strength + check_strength diff --git a/exp_legacy/module/modules/addons/spawn-area.lua b/exp_legacy/module/modules/addons/spawn-area.lua index b92187bc..568fa0af 100644 --- a/exp_legacy/module/modules/addons/spawn-area.lua +++ b/exp_legacy/module/modules/addons/spawn-area.lua @@ -144,9 +144,11 @@ local function spawn_area(surface, position) local fr = config.spawn_area.landfill_radius local fr2 = fr ^ 2 + --- @diagnostic disable-next-line Incorrect Api Type: https://forums.factorio.com/viewtopic.php?f=233&t=109145&p=593761&hilit=get_tile#p593761 local fill_tile = surface.get_tile(position).name -- Make sure a non water tile is used for each tile + --- @diagnostic disable-next-line Incorrect Api Type: https://forums.factorio.com/viewtopic.php?f=233&t=109145&p=593761&hilit=get_tile#p593761 if surface.get_tile(position).collides_with("player") then fill_tile = "landfill" end if decon_tile == nil then decon_tile = fill_tile end @@ -160,6 +162,7 @@ local function spawn_area(surface, position) if dst < tr2 then -- If it is inside the decon radius always set the tile table.insert(tiles_to_make, { name = decon_tile, position = pos }) + --- @diagnostic disable-next-line Incorrect Api Type: https://forums.factorio.com/viewtopic.php?f=233&t=109145&p=593761&hilit=get_tile#p593761 elseif dst < fr2 and surface.get_tile(pos).collides_with("player") then -- If it is inside the fill radius only set the tile if it is water table.insert(tiles_to_make, { name = fill_tile, position = pos }) diff --git a/exp_legacy/module/modules/data/personal-logistic.lua b/exp_legacy/module/modules/data/personal-logistic.lua index 731578c5..4d5abaa5 100644 --- a/exp_legacy/module/modules/data/personal-logistic.lua +++ b/exp_legacy/module/modules/data/personal-logistic.lua @@ -1,18 +1,21 @@ local Commands = require("modules/exp_commands") local config = require("modules.exp_legacy.config.personal_logistic") --- @dep config.personal-logistic -local function pl(type, target, amount) +---@param target LuaEntity | LuaPlayer +---@param amount number +local function pl(target, amount) local c local s - if type == "p" then + --- @cast target any Remove cast once implemented + error("Needs updating to use 2.0 logistics") + + if target.object_name == "LuaPlayer" then c = target.clear_personal_logistic_slot s = target.set_personal_logistic_slot - elseif type == "s" then + else c = target.clear_vehicle_logistic_slot s = target.set_vehicle_logistic_slot - else - return end for _, v in pairs(config.request) do @@ -69,15 +72,16 @@ end Commands.new("personal-logistic", "Set Personal Logistic (-1 to cancel all) (Select spidertron to edit spidertron)") :argument("amount", "", Commands.types.integer_range(-1, 10)) :add_aliases{ "pl" } + :add_flags{ "disabled" } -- Remove once implemented above :register(function(player, amount) --- @cast amount number if player.force.technologies["logistic-robotics"].researched then if player.selected ~= nil then if player.selected.name == "spidertron" then - pl("s", player.selected, amount / 10) + pl(player.selected, amount / 10) end else - pl("p", player, amount / 10) + pl(player, amount / 10) end else return Commands.status.error("Personal Logistic not researched") diff --git a/exp_legacy/module/modules/gui/module.lua b/exp_legacy/module/modules/gui/module.lua index b9f09594..47834af0 100644 --- a/exp_legacy/module/modules/gui/module.lua +++ b/exp_legacy/module/modules/gui/module.lua @@ -75,8 +75,14 @@ local function clear_module(player, area, machine) local m_current_module_content = m_current_module.get_contents() if m_current_module_content then - for k, m in pairs(m_current_module_content) do - surface.spill_item_stack(entity.bounding_box.left_top, { name = k, count = m }, true, force, false) + for _, item in pairs(m_current_module_content) do + surface.spill_item_stack{ + position = entity.bounding_box.left_top, + stack = item --[[ @as ItemStackDefinition https://forums.factorio.com/viewtopic.php?f=233&t=122323]], + enable_looted = true, + force = force, + allow_belts = false + } end end @@ -90,7 +96,7 @@ local function apply_module(player, area, machine, modules) for _, entity in pairs(player.surface.find_entities_filtered{ area = area, name = machine, force = player.force }) do local m_current_recipe - if entity.prototype.crafting_speed then + if entity.prototype.get_crafting_speed() then m_current_recipe = entity.get_recipe() end diff --git a/exp_legacy/module/modules/gui/science-info.lua b/exp_legacy/module/modules/gui/science-info.lua index 807f39ac..e751a258 100644 --- a/exp_legacy/module/modules/gui/science-info.lua +++ b/exp_legacy/module/modules/gui/science-info.lua @@ -193,6 +193,7 @@ local function update_science_pack(pack_table, science_pack_data) pack_table.parent.non_made.visible = false -- Update the icon + --- @type LuaGuiElement local pack_icon = pack_table["icon-" .. science_pack] or science_pack_base(pack_table, science_pack_data) local icon_style = science_pack_data.icon_style pack_icon.style = icon_style diff --git a/exp_util/module/include/table.lua b/exp_util/module/include/table.lua index dbff8b8d..3b719654 100644 --- a/exp_util/module/include/table.lua +++ b/exp_util/module/include/table.lua @@ -9,6 +9,10 @@ local tonumber = tonumber local pairs = pairs local table_size = table_size +--- @generic T: table +--- @type fun(table: T, metatable?: table|metatable): T +setmetatable = setmetatable + --- Adds all keys of the source table to destination table as a shallow copy --- @generic K, V --- @param dst table Table to insert into diff --git a/exp_util/module/module_exports.lua b/exp_util/module/module_exports.lua index cc749eca..159ccaad 100644 --- a/exp_util/module/module_exports.lua +++ b/exp_util/module/module_exports.lua @@ -394,6 +394,8 @@ end --- @field surface LuaSurface The surface to search for targets on --- @field allow_creation boolean? If new entities can be create to store the items --- @field cache Common.get_or_create_storage_cache? Internal search cache passed between subsequent calls +--- @field name? EntityID Entity to be created if allow_creation is true +--- @field force? ForceID Force of the created entity, defaults to 'neutral' --- Find, or optionally create, a storage entity which a stack can be inserted into --- @param options Common.get_or_create_storage_param