Module:Superclass

--- Functions and data related to superclasses of instance classes -- local class_link = require("Module:Class link") local utils = require('Module:Utils') local p = {}

--- Get the superclass of a class. -- @param {string} class_name Name of the class -- @return {?string} Name of the superclass, or nil if there is no superclass or the superclass is not known function p.get_superclass(class_name) if p.superclass_data[class_name] then return p.superclass_data[class_name] end if class_name == 'Instance' then return end local class_data = require('Module:API').get_class_data(class_name) if class_data then return class_data.Superclass end end

function p._superclass(args) return p.get_superclass(args.class) end

p.superclass = utils.make_wrapper_function(p._superclass)

function p._superclass_level(args) if args.level == "0" then return args.class else local current_superclass = args.class for i = 1, tonumber(args.level) do           current_superclass = p.get_superclass(current_superclass) end return current_superclass end end

p.superclass_level = utils.make_wrapper_function(p._superclass_level)

function p._superclass_list(args) local list = '' local current_superclass = p.get_superclass(args.class) while current_superclass do list = list .. mw.text.tag(			"li",			nil,			class_link._class_link_with_fallback_icon {class = current_superclass}		) current_superclass = p.get_superclass(current_superclass) end return mw.text.tag("ol", nil, list) end

p.superclass_list = utils.make_wrapper_function(p._superclass_list)

--- Get the subclasses of a class. -- @param {string} class_name Name of the class -- @return {table} An array of the subclasses, in alphabetical order function p.get_subclasses(class_name) local subclasses = {} for _, class in pairs(mw.loadData('Module:APIDump').Classes) do      if class.Superclass == class_name then subclasses[class.Name] = true end end for class, superclass in pairs(p.superclass_data) do       if superclass == class_name then subclasses[class] = true end end subclasses = utils.set_to_array(subclasses) table.sort(subclasses) return subclasses end

function p._subclass_tree(args) local list = '' for _, subclass in pairs(p.get_subclasses(args.class)) do list = list .. mw.text.tag(			"li",			nil,			class_link._class_link_with_fallback_icon{class = subclass} .. p._subclass_tree{class = subclass}		) .. "\n" end if list ~= '' then return mw.text.tag("ul", nil, list) end return list end

p.subclass_tree = utils.make_wrapper_function(p._subclass_tree)

-- Keys are class names; values are their superclass. -- This overrides information from the API dump. -- Information about removed classes should preferably be added to Module:RemovedAPI. p.superclass_data = { -- Removed classes SocialService = 'Instance'; WeldConstraint = 'Instance'; }

return p