Module:Module introspection: Difference between revisions

Ganaram inukshuk (talk | contribs)
bugfix non-modules being tracked as dependencies
Ganaram inukshuk (talk | contribs)
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"


-- Iterate through file and find each function and the line found at
for line in code:gmatch("([^\n]*)\n?") do
local funcs = {}
line_num = line_num + 1
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(
-- Match functions defined as: function p.name(
local name = line:match("function%s+[%w_]+%.([%w_]+)%s*%(")
local name = line:match("function%s+" .. module_name .. "%.([%w_]+)%s*%(")
if name then
if name then
table.insert(funcs, {name = name, line = line_num})
table.insert(funcs, { name = name, line = line_num })
end
end


-- Match functions defined as p.name = function(
-- Match functions defined as: p.name = function(
name = line:match("[%w_]+%.([%w_]+)%s*=%s*function%s*%(")
name = line:match(module_name .. "%.([%w_]+)%s*=%s*function%s*%(")
if name then
if name then
table.insert(funcs, {name = name, line = line_num})
table.insert(funcs, { name = name, line = line_num })
end
end
end
end
end