Module:Module introspection: Difference between revisions

Ganaram inukshuk (talk | contribs)
m adding extra modules for testing
Ganaram inukshuk (talk | contribs)
alphabetize dependencies
Line 75: Line 75:
-- that information to find every function call for each dependency.
-- that information to find every function call for each dependency.
function p.find_dependencies(code)
function p.find_dependencies(code)
local deps = {} -- Dependencies used
-- STEP 1
-- STEP 1
Line 82: Line 81:
-- that dependency.
-- that dependency.
-- Only consider dependencies that have "Module:" in its name
-- Only consider dependencies that have "Module:" in its name
for var, dep, func in code:gmatch([[local%s+([%w_]+)%s*=%s*require%(%s*["']([^"']+)["']%s*%)%.?([%w_%.]*)]]) do
local raw_deps = {} -- Dependencies used; unsorted and to be processed in step 2
for var, dep, direct_func in code:gmatch([[local%s+([%w_]+)%s*=%s*require%(%s*["']([^"']+)["']%s*%)%.?([%w_%.]*)]]) do
if dep:match("^Module:") then
if dep:match("^Module:") then
if func == "" then func = nil end
if func == "" then func = nil end
deps[var] = {  
raw_deps[var] = {  
dep = dep,
dep = dep,
funcs = {},
direct_func = direct_func  
direct_func = func
}
}
end
end
Line 97: Line 96:
-- dependency, as var.func(), or var(), without duplicates. If at least one
-- dependency, as var.func(), or var(), without duplicates. If at least one
-- function call was found, then that module is used.
-- function call was found, then that module is used.
for var, info in pairs(deps) do
local deps = {}  -- Final array result; to be sorted
for var, info in pairs(raw_deps) do
local seen  = {}  -- For detecting whether a function call was already found
local seen  = {}  -- For detecting whether a function call was already found
local funcs = {}  -- For tracking all found function calls
local funcs = {}  -- For tracking all found function calls
 
if info["direct_func"] then
if info.direct_func then
-- SPECIAL CASE 1: only one function imported from a package
-- SPECIAL CASE 1: only one function imported from a package
if code:match(var .. "%s*%(") then
if code:match(var .. "%s*%(") then
Line 114: Line 114:
end
end
end
end
 
-- SPECIAL CASE 2: module returns a function and is callable
-- SPECIAL CASE 2: module returns a function and is callable
if #funcs == 0 and code:match(var .. "%s*%(") then
if #funcs == 0 and code:match(var .. "%s*%(") then
Line 120: Line 120:
end
end
end
end
-- Add data to table
table.insert(deps, {
var  = var,
dep  = info.dep,
funcs = funcs
})
end
info.funcs = funcs
-- STEP 3: Sort alphabetically by dependency name
end
table.sort(deps, function(a, b)
return a.dep:lower() < b.dep:lower()
end)


return deps
return deps
Line 167: Line 177:
-- Given the output of the above function, create a mediawiki table
-- Given the output of the above function, create a mediawiki table
function p.make_dependency_table(module_deps)
function p.make_dependency_table(module_deps)
-- Count the number of dependencies
local num_deps = 0
for k, v in pairs(module_deps) do
num_deps = num_deps + 1
end


-- Table headers
-- Table headers
local lines = {}
local lines = {}
table.insert(lines, '{| class="wikitable sortable"')
table.insert(lines, '{| class="wikitable sortable"')
table.insert(lines, "|+ Lua&nbsp;modules&nbsp;used&nbsp;" .. string.format("(%d)", num_deps))
table.insert(lines, "|+ Lua&nbsp;modules&nbsp;used&nbsp;" .. string.format("(%d)", #module_deps))
table.insert(lines, "! Variable")
table.insert(lines, "! Variable")
table.insert(lines, "! Module")
table.insert(lines, "! Module")
table.insert(lines, "! Functions&nbsp;used")
table.insert(lines, "! Functions&nbsp;used")


-- Table rows
-- Table rows (assuming they're all alphabetized)
for var, info in pairs(module_deps) do
for _, info in ipairs(module_deps) do
local funcs_text
local funcs_text
if #info["funcs"] == 0 then
if #info.funcs == 0 then
funcs_text = "''dependency not used''"
funcs_text = "''dependency not used''"
else
else
Line 192: Line 196:


table.insert(lines, "|-")
table.insert(lines, "|-")
table.insert(lines, "| " .. var)
table.insert(lines, "| " .. info.var)
table.insert(lines, "| [[" .. info.dep .. "]]")
table.insert(lines, "| [[" .. info.dep .. "]]")
table.insert(lines, "| " .. funcs_text)
table.insert(lines, "| " .. funcs_text)