Module:Module introspection: Difference between revisions
bugfix non-modules being tracked as dependencies |
bugfix false positives from arguments |
||
| Line 130: | Line 130: | ||
-- Find functions provided by a module, ignoring nested/local functions | -- Find functions provided by a module, ignoring nested/local functions | ||
function p.find_functions(code) | function p.find_functions(code) | ||
local funcs = {} | |||
if not code then return funcs end | |||
local line_num = 0 | |||
local module_name = nil | |||
-- Functions of the form module_name.func are considered. | |||
-- module_name is usually p, but could be something else, such as arguments | |||
-- so detect that alternate name, or fall back to p. | |||
for var in code:gmatch([[local%s+([%w_]+)%s*=%s*{}]]) do | |||
module_name = var | |||
break | |||
end | |||
module_name = module_name or "p" | |||
for line in code:gmatch("([^\n]*)\n?") do | |||
line_num = line_num + 1 | |||
-- Match functions defined as: function p.name( | |||
local name = line:match("function%s+" .. module_name .. "%.([%w_]+)%s*%(") | |||
if name then | |||
table.insert(funcs, { name = name, line = line_num }) | |||
end | |||
-- Match functions defined as: p.name = function( | |||
name = line:match(module_name .. "%.([%w_]+)%s*=%s*function%s*%(") | |||
if name then | |||
table.insert(funcs, { name = name, line = line_num }) | |||
end | end | ||
end | end | ||