Module:Module introspection: Difference between revisions

Ganaram inukshuk (talk | contribs)
testing...
Ganaram inukshuk (talk | contribs)
introspection should now ignore strings, on the offchance that strings contain code
Line 18: Line 18:
--  generally provides functionality to other modules.
--  generally provides functionality to other modules.


-- TODO: bugfix dependency usage
-- The following snippet should detect use of any dependency, but it should be
-- cross-referenced with the found dependencies
--[[
for match in code:gmatch("([_%a][_%w%.]*)%s*%(") do
    print(match)
end
]]--
-- PROPOSED ALGORITHM FOR FINDING FUNCTIONS OF DEPENDENCIES
-- - Find every unique function call in the template, filtering out all function
--  calls that do not correspond to a dependency. For a dependency included as
--  local dep = require("Module:Dependency"), the module is called "Dependency"
--  and is called "dep" throughout the code. Function calls involving this
--  dependency will have "dep" as part of the name, as dep(), dep.func(), or
--  with any number of dots (dep.utils.formatter.func()), but zero or one dots
--  is typical.
-- - For each dependency name, place each function call with that dependency's
--  name in a table of function calls.
-- - Inspect each dependency's table. If there is at least one function call in
--  that table, then that dependency is used. If not, then that module is not
--  used.
-- - If function calls match the name of the dependency "dep", then either the
--  module returned a function or the code require("Module:Dep").func selected
--  only one function. If it's the former, then the module IS the function. If
--  it's the latter, then func from require("Module:Dep").func is the function.
--  In either case, if no function calls called "dep" are found, then the
--  dependency "dep" was not used.


-- Inspects a module for its functions, its dependencies, and the functions used
-- Inspects a module for its functions, its dependencies, and the functions used
Line 95: Line 67:
return table.concat(lines, "\n")
return table.concat(lines, "\n")
end
end
-- Helper function
-- Blanks strings while preserving line numbers
function p.strip_strings(code)
-- Blank double-quoted strings
code = code:gsub('"(.-)"', function(s)
return string.rep(" ", #s + 2)  -- +2 for the quotes
end)
-- Blank single-quoted strings
code = code:gsub("'(.-)'", function(s)
return string.rep(" ", #s + 2)
end)
-- Blank long bracketed strings [[...]]
code = code:gsub("%[%[(.-)%]%]", function(s)
-- Preserve line breaks
local blanks = s:gsub(".", " ")
local newlines = s:gsub("[^\n]", " ")
return blanks .. "]" .. "]"
end)
return code
end


-- Helper function
-- Helper function
Line 261: Line 258:
local code = title:getContent()
local code = title:getContent()
code = p.strip_comments(code) -- Blank-out comments
code = p.strip_comments(code) -- Blank-out comments
code = p.strip_strings(code) -- Blank out strings
-- Get dependencies and their functions used, then build a table
-- Get dependencies and their functions used, then build a table