Module:Module introspection: Difference between revisions
m adding extra modules for testing |
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) | ||
-- 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, | 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 | ||
raw_deps[var] = { | |||
dep = dep, | dep = dep, | ||
direct_func = direct_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( | 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 | 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 | |||
-- 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) | ||
-- Table headers | -- Table headers | ||
local lines = {} | local lines = {} | ||
table.insert(lines, '{| class="wikitable sortable"') | table.insert(lines, '{| class="wikitable sortable"') | ||
table.insert(lines, "|+ Lua modules used " .. string.format("(%d)", | table.insert(lines, "|+ Lua modules used " .. 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 used") | table.insert(lines, "! Functions used") | ||
-- Table rows | -- Table rows (assuming they're all alphabetized) | ||
for | for _, info in ipairs(module_deps) do | ||
local funcs_text | local funcs_text | ||
if #info | 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) | ||