Module:Module introspection: Difference between revisions

Ganaram inukshuk (talk | contribs)
m add module name to table
Ganaram inukshuk (talk | contribs)
bugfix non-modules being tracked as dependencies
Line 81: Line 81:
-- for that dependency (var), and, if applicable, the function used from
-- for that dependency (var), and, if applicable, the function used from
-- that dependency.
-- that dependency.
-- A require line looks like: local var = require("Module:Dependency").func,
-- Only consider dependencies that have "Module:" in its name
-- where ".func" is optional.
for var, dep, func in code:gmatch([[local%s+([%w_]+)%s*=%s*require%(%s*["']([^"']+)["']%s*%)%.?([%w_%.]*)]]) do
for var, dep, func in code:gmatch([[local%s+([%w_]+)%s*=%s*require%(%s*["']([^"']+)["']%s*%)%.?([%w_%.]*)]]) do
if func == "" then func = nil end -- If func was blank, replace with nil instead
if dep:match("^Module:") then
deps[var] = {  
if func == "" then func = nil end
dep = dep,
deps[var] = {  
funcs = { },
dep = dep,
direct_func = func -- For detecting whether one function out of a package was used
funcs = {},
}
direct_func = func
}
end
end
end
Line 124: Line 125:


return deps
return deps
end
-- Helper function
-- Find functions provided by a module, ignoring nested/local functions
function p.find_functions(code)
-- Iterate through file and find each function and the line found at
local funcs = {}
if code then
local line_num = 0
for line in code:gmatch("([^\n]*)\n?") do -- Make sure gmatch does not skip blank lines
line_num = line_num + 1
-- Match functions defined as function p.name(
local name = line:match("function%s+[%w_]+%.([%w_]+)%s*%(")
if name then
table.insert(funcs, {name = name, line = line_num})
end
-- Match functions defined as p.name = function(
name = line:match("[%w_]+%.([%w_]+)%s*=%s*function%s*%(")
if name then
table.insert(funcs, {name = name, line = line_num})
end
end
end
return funcs
end
end


Line 164: Line 193:
-- Join all lines into a single string
-- Join all lines into a single string
return table.concat(lines, "\n")
return table.concat(lines, "\n")
end
-- Helper function
-- Find functions provided by a module, ignoring nested/local functions
function p.find_functions(code)
-- Iterate through file and find each function and the line found at
local funcs = {}
if code then
local line_num = 0
for line in code:gmatch("([^\n]*)\n?") do -- Make sure gmatch does not skip blank lines
line_num = line_num + 1
-- Match functions defined as function p.name(
local name = line:match("function%s+[%w_]+%.([%w_]+)%s*%(")
if name then
table.insert(funcs, {name = name, line = line_num})
end
-- Match functions defined as p.name = function(
name = line:match("[%w_]+%.([%w_]+)%s*=%s*function%s*%(")
if name then
table.insert(funcs, {name = name, line = line_num})
end
end
end
return funcs
end
end