Module:DependencyList: Difference between revisions

osrsw>CephHunter
Trim whitespace in function getInvokeCallList
osrsw>CephHunter
Fix bug when the require contains a string.format expression
Line 13: Line 13:


--- Used in case 'require( varName )' is found. Attempts to find a string value stored in 'varName'.
--- Used in case 'require( varName )' is found. Attempts to find a string value stored in 'varName'.
---@param content string    @The content of the module to look in as a string
---@param content string    The content of the module to search in
---@param varName string
---@param varName string
---@return string
---@return string
local function substVarValue( content, varName )
local function substVarValue( content, varName )
     if varName:find( "%b''" ) or varName:find( '%b""' ) then -- Look for balanced quotes
     local res = content:match( varName .. '%s*=%s*(%b""%s-%.*)' ) or content:match( varName .. "%s*=%s*(%b''%s-%.*)" ) or ''
         return varName -- Is already a string
    if res:find( '^(["\'])[Mm]odule:[%S]+%1' ) and not res:find( '%.%.' ) and not res:find( '%%%a' ) then
         return mw.text.trim( res )
     else
     else
         local res = content:match( varName .. '%s*=%s*(%b""%s-%.*)' ) or content:match( varName .. "%s*=%s*(%b''%s-%.*)" ) or ''
         return ''
        if res:find( '^(["\'])[Mm]odule:[%S]+%1' ) and not res:find( '%.%.' ) then
    end
            return mw.text.trim( res )
end
        else
 
            return ''
---@param capture string
        end
---@param content string    The content of the module to search in
---@return string
local function extractModuleName( capture, content )
    capture = capture:gsub( '^%(%s*(.-)%s*%)$', '%1' )
 
    if capture:find( '^(["\']).-%1$' ) then -- Check if it is already a pure string
        return capture
    elseif capture:find( '^[%a_][%w_]*$' ) then -- Check if if is a single variable
        return substVarValue( content, capture )
     end
     end
    return capture
end
end


Line 33: Line 44:
local function formatModuleName( str )
local function formatModuleName( str )
     return (str:gsub( '^([\'\"])(.-)%1$', function(_, x) return x end ) -- Only remove quotes at start and end of string if both are the same type
     return (str:gsub( '^([\'\"])(.-)%1$', function(_, x) return x end ) -- Only remove quotes at start and end of string if both are the same type
    :gsub( '_', ' ' )
        :gsub( '_', ' ' )
    :gsub( '^.', string.upper )
        :gsub( '^.', string.upper )
    :gsub( ':(.)', function(x) return ':'..x:upper() end ))
        :gsub( ':(.)', function(x) return ':'..x:upper() end ))
end
end


Line 41: Line 52:
--- Will return a list of pages which satisfy this pattern where 'isTheBest' can take any value.
--- Will return a list of pages which satisfy this pattern where 'isTheBest' can take any value.
---@param query string
---@param query string
---@return string[]    @Sequence of strings
---@return string[]    Sequence of strings
local function getDynamicRequireList( query )
local function getDynamicRequireList( query )
     query = mw.text.split( query, '..', true )
     if query:find( '%.%.' ) then
    query = enum.map( query, function(x) return mw.text.trim(x) end )
        query = mw.text.split( query, '..', true )
    query = enum.map( query, function(x) return (x:match('^[\'\"]([^\'\"]+)[\'\"]$') or '%') end )
        query = enum.map( query, function(x) return mw.text.trim(x) end )
    query = table.concat( query )
        query = enum.map( query, function(x) return (x:match('^[\'\"](.-)[\'\"]$') or '%') end )
        query = table.concat( query )
    else
        _, query = query:match( '(["\'])(.-)%1' )
        query = query:gsub( '%%%a', '%%' )
    end
     query = query:gsub( '^[Mm]odule:', '' )
     query = query:gsub( '^[Mm]odule:', '' )


Line 103: Line 119:
     end
     end


     for match in dualGmatch( content, 'require%s*%(([^%)]+)', 'require%s*((["\'])%s*[Mm]odule:.-%2)' ) do
     for match in dualGmatch( content, 'require%s*(%b())', 'require%s*((["\'])%s*[Mm]odule:.-%2)' ) do
         match = mw.text.trim( match )
         match = mw.text.trim( match )
         match = substVarValue( content, match )
         match = extractModuleName( match, content )


         if match:find( '%.%.' ) then
         if match:find( '%.%.' ) or match:find( '%%%a' ) then
             for _, x in ipairs( getDynamicRequireList( match ) ) do
             for _, x in ipairs( getDynamicRequireList( match ) ) do
                 table.insert( dynamicRequirelist, x )
                 table.insert( dynamicRequirelist, x )
Line 122: Line 138:
     end
     end


     for match in dualGmatch( content, 'mw%.loadData%s*%(([^%)]+)', 'mw%.loadData%s*((["\'])%s*[Mm]odule:.-%2)' ) do
     for match in dualGmatch( content, 'mw%.loadData%s*(%b())', 'mw%.loadData%s*((["\'])%s*[Mm]odule:.-%2)' ) do
         match = mw.text.trim( match )
         match = mw.text.trim( match )
         match = substVarValue( content, match )
         match = extractModuleName( match, content )


         if match:find( '%.%.' ) then
         if match:find( '%.%.' ) or match:find( '%%%a' ) then
             for _, x in ipairs( getDynamicRequireList( match ) ) do
             for _, x in ipairs( getDynamicRequireList( match ) ) do
                 table.insert( dynamicLoadDataList, x )
                 table.insert( dynamicLoadDataList, x )