From 63b63c5d844e8702fce2ae0181ee4397b8954d92 Mon Sep 17 00:00:00 2001 From: x12xx12x <44411062+12xx12@users.noreply.github.com> Date: Sat, 7 May 2022 01:26:13 +0200 Subject: Added check for unexported but documented API (#5418) Also fixed the errors --- Server/Plugins/APIDump/main_APIDump.lua | 62 +++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) (limited to 'Server/Plugins/APIDump/main_APIDump.lua') diff --git a/Server/Plugins/APIDump/main_APIDump.lua b/Server/Plugins/APIDump/main_APIDump.lua index b1277ca0b..735a6ec05 100644 --- a/Server/Plugins/APIDump/main_APIDump.lua +++ b/Server/Plugins/APIDump/main_APIDump.lua @@ -2041,6 +2041,56 @@ end +--- Checks if any functions that are documented are present in the API +-- Returns an array-table of strings representing the unexported symbol names +-- If no unexported are found, returns no value. +-- If an error occurs, returns true and error message. +local function CheckUnexportedFunctions() + local res = {} + + local API, Globals, Desc = PrepareApi() + for clsname, cls in pairs(Desc.Classes) do + if not(cls.IsExported) then + -- The whole class is not exported + table.insert(res, "class\t" .. clsname .. "\n"); + else + if (cls.Functions ~= nil) then + for fnname, fnapi in pairs(cls.Functions) do + if not(fnapi.IsExported) then + table.insert(res, "func\t" .. clsname .. "." .. fnname); + end + end -- for j, fn - cls.Functions[] + end + if (cls.Constants ~= nil) then + for cnname, cnapi in pairs(cls.Constants) do + if not(cnapi.IsExported) then + table.insert(res, "const\t" .. clsname .. "." .. cnname); + end + end -- for j, fn - cls.Functions[] + end + end + end -- for i, cls - a_APIDesc.Classes[] + + table.sort(res) + + -- Bail out if no items found: + if not(res[1]) then + return + end + + -- Save any found items to a file: + local f = io.open("Unexported.lua", "w") + f:write(table.concat(res, "\n")) + f:write("\n") + f:close() + + return res +end + + + + + local function HandleWebAdminDump(a_Request) if (a_Request.PostParams["Dump"] ~= nil) then @@ -2101,6 +2151,18 @@ local function HandleCmdApiCheck(a_Split, a_EntireCmd) end end + LOG("Checking for unexported Objects...") + local unexported, msg = CheckUnexportedFunctions() + if (unexported) then + if (unexported == true) then + LOGERROR("Cannot check for unexported symbols: " .. (msg or "")) + return true + else + LOGERROR("Found unexported symbols:\n" .. table.concat(unexported, "\n")) + return true + end + end + -- The check completed successfully, remove the "test failed" flag from the filesystem: cFile:DeleteFile("apiCheckFailed.flag") -- cgit v1.2.3