From a21cc74540008bd237c3e681e9743fe3dc9be223 Mon Sep 17 00:00:00 2001 From: Rikard Blixt Date: Wed, 11 Sep 2024 15:30:57 +0200 Subject: [PATCH 1/7] refactor(widget): improve building part 4 - `Html[]` into `Html` --- components/widget/widget.lua | 4 +-- components/widget/widget_breakdown.lua | 4 +-- components/widget/widget_cell.lua | 6 ++-- components/widget/widget_center.lua | 4 +-- components/widget/widget_chronology.lua | 12 ++++---- components/widget/widget_factory.lua | 31 +++++++++------------ components/widget/widget_header.lua | 6 +++- components/widget/widget_highlights.lua | 4 +-- components/widget/widget_links.lua | 6 ++-- components/widget/widget_table.lua | 4 +-- components/widget/widget_table_cell.lua | 4 +-- components/widget/widget_table_cell_new.lua | 4 +-- components/widget/widget_table_new.lua | 4 +-- components/widget/widget_table_row.lua | 4 +-- components/widget/widget_table_row_new.lua | 4 +-- components/widget/widget_title.lua | 4 +-- 16 files changed, 50 insertions(+), 55 deletions(-) diff --git a/components/widget/widget.lua b/components/widget/widget.lua index e6f35872d54..9d8be39fe26 100644 --- a/components/widget/widget.lua +++ b/components/widget/widget.lua @@ -22,13 +22,13 @@ function Widget:assertExistsAndCopy(value) end ---@param injector WidgetInjector? ----@return Widget[]|Html[]|nil +---@return Widget[]|Html|nil function Widget:make(injector) error('A Widget must override the make() function!') end ---@param injector WidgetInjector? ----@return Widget[]|Html[]|nil +---@return Widget[]|Html|nil function Widget:tryMake(injector) return Logic.tryOrElseLog( function() return self:make(injector) end, diff --git a/components/widget/widget_breakdown.lua b/components/widget/widget_breakdown.lua index 15623203c0e..07178309d76 100644 --- a/components/widget/widget_breakdown.lua +++ b/components/widget/widget_breakdown.lua @@ -26,9 +26,9 @@ local Breakdown = Class.new( ) ---@param injector WidgetInjector? ----@return {[1]: Html?} +---@return Html? function Breakdown:make(injector) - return {Breakdown:_breakdown(self.contents, self.classes, self.contentClasses)} + return Breakdown:_breakdown(self.contents, self.classes, self.contentClasses) end ---@param contents (string|number)[] diff --git a/components/widget/widget_cell.lua b/components/widget/widget_cell.lua index 36e7e6d651c..83e15578ba3 100644 --- a/components/widget/widget_cell.lua +++ b/components/widget/widget_cell.lua @@ -90,7 +90,7 @@ function Cell:_content(...) end ---@param injector WidgetInjector? ----@return {[1]: Html?} +---@return Html? function Cell:make(injector) self:_new(self.name) self:_class(unpack(self.classes or {})) @@ -102,9 +102,7 @@ function Cell:make(injector) self.root :node(self.description) :node(self.contentDiv) - return { - self.root - } + return self.root end return Cell diff --git a/components/widget/widget_center.lua b/components/widget/widget_center.lua index 3122a9a9f12..7b81449d8c0 100644 --- a/components/widget/widget_center.lua +++ b/components/widget/widget_center.lua @@ -25,9 +25,9 @@ local Center = Class.new( ) ---@param injector WidgetInjector? ----@return {[1]: Html?} +---@return Html? function Center:make(injector) - return {Center:_create(self.content, self.classes)} + return Center:_create(self.content, self.classes) end ---@param content (string|number)[] diff --git a/components/widget/widget_chronology.lua b/components/widget/widget_chronology.lua index e3d50e0cf0e..3f99dc57939 100644 --- a/components/widget/widget_chronology.lua +++ b/components/widget/widget_chronology.lua @@ -23,26 +23,26 @@ local Chronology = Class.new( ) ---@param injector WidgetInjector? ----@return Html[] +---@return Html? function Chronology:make(injector) return Chronology:_chronology(self.links) end ---@param links table ----@return Html[] +---@return Html? function Chronology:_chronology(links) if links == nil or Table.size(links) == 0 then - return self + return end - local chronologyContent = {} - chronologyContent[1] = self:_createChronologyRow(links['previous'], links['next']) + local chronologyContent = mw.html.create() + chronologyContent:node(self:_createChronologyRow(links['previous'], links['next'])) local index = 2 local previous = links['previous' .. index] local next = links['next' .. index] while (previous ~= nil or next ~= nil) do - chronologyContent[index] = self:_createChronologyRow(previous, next) + chronologyContent:node(self:_createChronologyRow(previous, next)) index = index + 1 previous = links['previous' .. index] diff --git a/components/widget/widget_factory.lua b/components/widget/widget_factory.lua index 18d4312debc..3f8f4e39df4 100644 --- a/components/widget/widget_factory.lua +++ b/components/widget/widget_factory.lua @@ -8,34 +8,29 @@ local Array = require('Module:Array') local Class = require('Module:Class') -local Lua = require('Module:Lua') - -local Widget = Lua.import('Module:Widget') ---@class WidgetFactory local WidgetFactory = Class.new() ---@param widget Widget ---@param injector WidgetInjector? ----@return Html[] +---@return Html function WidgetFactory.work(widget, injector) - local convertedWidgets = {} ---@type Html[] - - if widget == nil then - return {} + local children = widget:tryMake(injector) + if not children then + return mw.html.create() end - - for _, child in ipairs(widget:tryMake(injector) or {}) do - if type(child) == 'table' and type(child['is_a']) == 'function' and child:is_a(Widget) then - ---@cast child Widget - Array.extendWith(convertedWidgets, WidgetFactory.work(child, injector)) - else - ---@cast child Html - table.insert(convertedWidgets, child) - end + if not Array.isArray(children) then + ---@cast children Html + return children end - return convertedWidgets + ---@cast children Widget[] + local wrapper = mw.html.create() + Array.forEach(children, function(child) + wrapper:node(WidgetFactory.work(child, injector)) + end) + return wrapper end return WidgetFactory diff --git a/components/widget/widget_header.lua b/components/widget/widget_header.lua index a232ea4f105..bd3959fc269 100644 --- a/components/widget/widget_header.lua +++ b/components/widget/widget_header.lua @@ -59,7 +59,11 @@ function Header:make(injector) table.insert(header, 2, subHeader) end - return header + local wrapper = mw.html.create() + for _, element in ipairs(header) do + wrapper:node(element) + end + return wrapper end ---@param name string? diff --git a/components/widget/widget_highlights.lua b/components/widget/widget_highlights.lua index 3d431c56862..ce50100e0fa 100644 --- a/components/widget/widget_highlights.lua +++ b/components/widget/widget_highlights.lua @@ -23,7 +23,7 @@ local Highlights = Class.new( ) ---@param injector WidgetInjector? ----@return {[1]: Html}? +---@return Html? function Highlights:make(injector) return Highlights:_highlights(self.list) end @@ -44,7 +44,7 @@ function Highlights:_highlights(list) div:node(highlights) - return {div} + return div end return Highlights diff --git a/components/widget/widget_links.lua b/components/widget/widget_links.lua index ba2a1157c9f..7541947e4bf 100644 --- a/components/widget/widget_links.lua +++ b/components/widget/widget_links.lua @@ -28,7 +28,7 @@ local Links = Class.new( local PRIORITY_GROUPS = Lua.import('Module:Links/PriorityGroups', {loadData = true}) ---@param injector WidgetInjector? ----@return {[1]: Html} +---@return Html? function Links:make(injector) local infoboxLinks = mw.html.create('div') infoboxLinks :addClass('infobox-center') @@ -56,9 +56,7 @@ function Links:make(injector) infoboxLinks:wikitext(' ' .. self:_makeLink(key, value)) end - return { - mw.html.create('div'):node(infoboxLinks) - } + return mw.html.create('div'):node(infoboxLinks) end ---@param key string diff --git a/components/widget/widget_table.lua b/components/widget/widget_table.lua index d5c6dc3783c..ee8fc666b0d 100644 --- a/components/widget/widget_table.lua +++ b/components/widget/widget_table.lua @@ -50,7 +50,7 @@ function Table:addClass(class) end ---@param injector WidgetInjector? ----@return {[1]: Html} +---@return Html? function Table:make(injector) local displayTable = mw.html.create('div'):addClass('csstable-widget') displayTable:css{ @@ -69,7 +69,7 @@ function Table:make(injector) end end - return {displayTable} + return displayTable end ---@return integer? diff --git a/components/widget/widget_table_cell.lua b/components/widget/widget_table_cell.lua index 1b81ffe0c60..d3b410349d4 100644 --- a/components/widget/widget_table_cell.lua +++ b/components/widget/widget_table_cell.lua @@ -57,7 +57,7 @@ function TableCell:addCss(key, value) end ---@param injector WidgetInjector? ----@return {[1]: Html} +---@return Html? function TableCell:make(injector) local cell = mw.html.create('div'):addClass('csstable-widget-cell') cell:css{ @@ -73,7 +73,7 @@ function TableCell:make(injector) cell:node(self:_concatContent()) - return {cell} + return cell end ---@return string diff --git a/components/widget/widget_table_cell_new.lua b/components/widget/widget_table_cell_new.lua index 8ac97cb1ea5..16206fd3517 100644 --- a/components/widget/widget_table_cell_new.lua +++ b/components/widget/widget_table_cell_new.lua @@ -43,7 +43,7 @@ local TableCell = Class.new( ) ---@param injector WidgetInjector? ----@return {[1]: Html} +---@return Html? function TableCell:make(injector) local cell = mw.html.create(self.isHeader and 'th' or 'td') cell:attr('colspan', self.colSpan) @@ -55,7 +55,7 @@ function TableCell:make(injector) cell:node(self:_content()) - return {cell} + return cell end ---@return string diff --git a/components/widget/widget_table_new.lua b/components/widget/widget_table_new.lua index 631643d0f4a..6a45ec235d4 100644 --- a/components/widget/widget_table_new.lua +++ b/components/widget/widget_table_new.lua @@ -34,7 +34,7 @@ local Table = Class.new( ) ---@param injector WidgetInjector? ----@return {[1]: Html} +---@return Html? function Table:make(injector) local wrapper = mw.html.create('div'):addClass('table-responsive') local output = mw.html.create('table'):addClass('wikitable') @@ -48,7 +48,7 @@ function Table:make(injector) end) wrapper:node(output) - return {wrapper} + return wrapper end return Table diff --git a/components/widget/widget_table_row.lua b/components/widget/widget_table_row.lua index 24486f59bf1..8ef7e3e2675 100644 --- a/components/widget/widget_table_row.lua +++ b/components/widget/widget_table_row.lua @@ -59,7 +59,7 @@ function TableRow:getCellCount() end ---@param injector WidgetInjector? ----@return {[1]: Html} +---@return Html? function TableRow:make(injector) local row = mw.html.create('div'):addClass('csstable-widget-row') @@ -75,7 +75,7 @@ function TableRow:make(injector) end end - return {row} + return row end return TableRow diff --git a/components/widget/widget_table_row_new.lua b/components/widget/widget_table_row_new.lua index ca022a80aa9..8f8a9d2a0f3 100644 --- a/components/widget/widget_table_row_new.lua +++ b/components/widget/widget_table_row_new.lua @@ -34,7 +34,7 @@ local TableRow = Class.new( ) ---@param injector WidgetInjector? ----@return {[1]: Html} +---@return Html? function TableRow:make(injector) local row = mw.html.create('tr') @@ -46,7 +46,7 @@ function TableRow:make(injector) Array.forEach(WidgetFactory.work(child, injector), FnUtil.curry(row.node, row)) end) - return {row} + return row end return TableRow diff --git a/components/widget/widget_title.lua b/components/widget/widget_title.lua index 06ca2ef6287..3454a083fd6 100644 --- a/components/widget/widget_title.lua +++ b/components/widget/widget_title.lua @@ -22,9 +22,9 @@ local Title = Class.new( ) ---@param injector WidgetInjector? ----@return {[1]: Html} +---@return Html? function Title:make(injector) - return {Title:_create(self.content)} + return Title:_create(self.content) end ---@param infoDescription string|number|nil From aa15ff1acacd7e932d573184aa53e9864cc37615 Mon Sep 17 00:00:00 2001 From: Rikard Blixt Date: Wed, 11 Sep 2024 15:44:46 +0200 Subject: [PATCH 2/7] reorder --- components/widget/widget_factory.lua | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/components/widget/widget_factory.lua b/components/widget/widget_factory.lua index 3f8f4e39df4..807275fcf8d 100644 --- a/components/widget/widget_factory.lua +++ b/components/widget/widget_factory.lua @@ -17,20 +17,22 @@ local WidgetFactory = Class.new() ---@return Html function WidgetFactory.work(widget, injector) local children = widget:tryMake(injector) + if not children then return mw.html.create() end - if not Array.isArray(children) then - ---@cast children Html - return children + + if Array.isArray(children) then + ---@cast children Widget[] + local wrapper = mw.html.create() + Array.forEach(children, function(child) + wrapper:node(WidgetFactory.work(child, injector)) + end) + return wrapper end - ---@cast children Widget[] - local wrapper = mw.html.create() - Array.forEach(children, function(child) - wrapper:node(WidgetFactory.work(child, injector)) - end) - return wrapper + ---@cast children Html + return children end return WidgetFactory From 84e8423abc6c333e61da7d53f77c2bccb11e325d Mon Sep 17 00:00:00 2001 From: Rikard Blixt Date: Wed, 11 Sep 2024 15:56:40 +0200 Subject: [PATCH 3/7] update usages --- components/infobox/commons/infobox.lua | 6 +----- components/prize_pool/commons/prize_pool_base.lua | 4 +--- components/squad/commons/squad.lua | 2 +- components/widget/widget_builder.lua | 8 ++++---- components/widget/widget_table.lua | 4 +--- components/widget/widget_table_new.lua | 2 +- components/widget/widget_table_row.lua | 4 +--- components/widget/widget_table_row_new.lua | 2 +- 8 files changed, 11 insertions(+), 21 deletions(-) diff --git a/components/infobox/commons/infobox.lua b/components/infobox/commons/infobox.lua index dfe0da3fb6d..95bfe45fc15 100644 --- a/components/infobox/commons/infobox.lua +++ b/components/infobox/commons/infobox.lua @@ -86,11 +86,7 @@ function Infobox:build(widgets) error('Infobox:build can only accept Widgets') end - local contentItems = WidgetFactory.work(widget, self.injector) - - for _, node in ipairs(contentItems or {}) do - self.content:node(node) - end + self.content:node(WidgetFactory.work(widget, self.injector)) end self.root:node(self.content) diff --git a/components/prize_pool/commons/prize_pool_base.lua b/components/prize_pool/commons/prize_pool_base.lua index c9e9643b7e0..7e930d1efe7 100644 --- a/components/prize_pool/commons/prize_pool_base.lua +++ b/components/prize_pool/commons/prize_pool_base.lua @@ -608,9 +608,7 @@ function BasePrizePool:_buildTable(isAward) end local tableNode = mw.html.create('div'):css('overflow-x', 'auto') - for _, node in ipairs(WidgetFactory.work(tbl, self._widgetInjector)) do - tableNode:node(node) - end + tableNode:node(WidgetFactory.work(tbl, self._widgetInjector)) return tableNode end diff --git a/components/squad/commons/squad.lua b/components/squad/commons/squad.lua index c0229610aa3..4870bb038f9 100644 --- a/components/squad/commons/squad.lua +++ b/components/squad/commons/squad.lua @@ -110,7 +110,7 @@ function Squad:create() children = self.rows, } local wrapper = mw.html.create() - Array.forEach(WidgetFactory.work(dataTable, self.injector), FnUtil.curry(wrapper.node, wrapper)) + wrapper:node(WidgetFactory.work(dataTable, self.injector)) return wrapper end diff --git a/components/widget/widget_builder.lua b/components/widget/widget_builder.lua index 767f3ff6212..466a0249d7e 100644 --- a/components/widget/widget_builder.lua +++ b/components/widget/widget_builder.lua @@ -24,14 +24,14 @@ local Builder = Class.new( ) ---@param injector WidgetInjector? ----@return Widget[] +---@return Html function Builder:make(injector) local children = self.builder() - local widgets = {} + local builtChildren = mw.html.create() for _, child in ipairs(children or {}) do - Array.extendWith(widgets, WidgetFactory.work(child, injector)) + builtChildren:node(WidgetFactory.work(child, injector)) end - return widgets + return builtChildren end return Builder diff --git a/components/widget/widget_table.lua b/components/widget/widget_table.lua index ee8fc666b0d..ca982ae542e 100644 --- a/components/widget/widget_table.lua +++ b/components/widget/widget_table.lua @@ -64,9 +64,7 @@ function Table:make(injector) displayTable:css(self.css) for _, row in ipairs(self.rows) do - for _, node in ipairs(WidgetFactory.work(row, injector)) do - displayTable:node(node) - end + displayTable:node(WidgetFactory.work(row, injector)) end return displayTable diff --git a/components/widget/widget_table_new.lua b/components/widget/widget_table_new.lua index 6a45ec235d4..23dbcb3ec6a 100644 --- a/components/widget/widget_table_new.lua +++ b/components/widget/widget_table_new.lua @@ -44,7 +44,7 @@ function Table:make(injector) output:css(self.css) Array.forEach(self.children, function(child) - Array.forEach(WidgetFactory.work(child, injector), FnUtil.curry(output.node, output)) + output:node(WidgetFactory.work(child, injector)) end) wrapper:node(output) diff --git a/components/widget/widget_table_row.lua b/components/widget/widget_table_row.lua index 8ef7e3e2675..dbafaddb562 100644 --- a/components/widget/widget_table_row.lua +++ b/components/widget/widget_table_row.lua @@ -70,9 +70,7 @@ function TableRow:make(injector) row:css(self.css) for _, cell in ipairs(self.cells) do - for _, node in ipairs(WidgetFactory.work(cell, injector)) do - row:node(node) - end + row:node(WidgetFactory.work(cell, injector)) end return row diff --git a/components/widget/widget_table_row_new.lua b/components/widget/widget_table_row_new.lua index 8f8a9d2a0f3..1c5ea278187 100644 --- a/components/widget/widget_table_row_new.lua +++ b/components/widget/widget_table_row_new.lua @@ -43,7 +43,7 @@ function TableRow:make(injector) row:css(self.css) Array.forEach(self.children, function(child) - Array.forEach(WidgetFactory.work(child, injector), FnUtil.curry(row.node, row)) + row:node(WidgetFactory.work(child, injector)) end) return row From 4d66dc41bc8af6030598d44b2391ad58da09edd2 Mon Sep 17 00:00:00 2001 From: Rikard Blixt Date: Wed, 11 Sep 2024 15:59:40 +0200 Subject: [PATCH 4/7] unused code --- components/squad/commons/squad.lua | 5 +---- components/widget/widget_builder.lua | 1 - 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/components/squad/commons/squad.lua b/components/squad/commons/squad.lua index 4870bb038f9..ff138ec10b8 100644 --- a/components/squad/commons/squad.lua +++ b/components/squad/commons/squad.lua @@ -9,7 +9,6 @@ local Arguments = require('Module:Arguments') local Array = require('Module:Array') local Class = require('Module:Class') -local FnUtil = require('Module:FnUtil') local Logic = require('Module:Logic') local Lua = require('Module:Lua') local String = require('Module:StringUtils') @@ -109,9 +108,7 @@ function Squad:create() classes = {'wikitable-striped', 'roster-card'}, children = self.rows, } - local wrapper = mw.html.create() - wrapper:node(WidgetFactory.work(dataTable, self.injector)) - return wrapper + return WidgetFactory.work(dataTable, self.injector) end return Squad diff --git a/components/widget/widget_builder.lua b/components/widget/widget_builder.lua index 466a0249d7e..6ea20ddb472 100644 --- a/components/widget/widget_builder.lua +++ b/components/widget/widget_builder.lua @@ -6,7 +6,6 @@ -- Please see https://github.com/Liquipedia/Lua-Modules to contribute -- -local Array = require('Module:Array') local Class = require('Module:Class') local Lua = require('Module:Lua') From e7225f60c791a94c76b08693910597acf4711f6a Mon Sep 17 00:00:00 2001 From: Rikard Blixt Date: Wed, 11 Sep 2024 17:34:57 +0200 Subject: [PATCH 5/7] change from `Html` to `string` --- components/widget/widget.lua | 4 ++-- components/widget/widget_breakdown.lua | 4 ++-- components/widget/widget_builder.lua | 4 ++-- components/widget/widget_cell.lua | 6 +++--- components/widget/widget_center.lua | 4 ++-- components/widget/widget_chronology.lua | 4 ++-- components/widget/widget_header.lua | 4 ++-- components/widget/widget_highlights.lua | 4 ++-- components/widget/widget_links.lua | 4 ++-- components/widget/widget_table.lua | 4 ++-- components/widget/widget_table_cell.lua | 4 ++-- components/widget/widget_table_cell_new.lua | 4 ++-- components/widget/widget_table_new.lua | 4 ++-- components/widget/widget_table_row.lua | 4 ++-- components/widget/widget_table_row_new.lua | 4 ++-- components/widget/widget_title.lua | 4 ++-- 16 files changed, 33 insertions(+), 33 deletions(-) diff --git a/components/widget/widget.lua b/components/widget/widget.lua index 9d8be39fe26..146f41315dc 100644 --- a/components/widget/widget.lua +++ b/components/widget/widget.lua @@ -22,13 +22,13 @@ function Widget:assertExistsAndCopy(value) end ---@param injector WidgetInjector? ----@return Widget[]|Html|nil +---@return Widget[]|string|nil function Widget:make(injector) error('A Widget must override the make() function!') end ---@param injector WidgetInjector? ----@return Widget[]|Html|nil +---@return Widget[]|string|nil function Widget:tryMake(injector) return Logic.tryOrElseLog( function() return self:make(injector) end, diff --git a/components/widget/widget_breakdown.lua b/components/widget/widget_breakdown.lua index 07178309d76..4d4b8874666 100644 --- a/components/widget/widget_breakdown.lua +++ b/components/widget/widget_breakdown.lua @@ -26,9 +26,9 @@ local Breakdown = Class.new( ) ---@param injector WidgetInjector? ----@return Html? +---@return string? function Breakdown:make(injector) - return Breakdown:_breakdown(self.contents, self.classes, self.contentClasses) + return tostring(Breakdown:_breakdown(self.contents, self.classes, self.contentClasses)) end ---@param contents (string|number)[] diff --git a/components/widget/widget_builder.lua b/components/widget/widget_builder.lua index 6ea20ddb472..0fb0f8d8fa2 100644 --- a/components/widget/widget_builder.lua +++ b/components/widget/widget_builder.lua @@ -23,14 +23,14 @@ local Builder = Class.new( ) ---@param injector WidgetInjector? ----@return Html +---@return string function Builder:make(injector) local children = self.builder() local builtChildren = mw.html.create() for _, child in ipairs(children or {}) do builtChildren:node(WidgetFactory.work(child, injector)) end - return builtChildren + return tostring(builtChildren) end return Builder diff --git a/components/widget/widget_cell.lua b/components/widget/widget_cell.lua index 83e15578ba3..f598100bf79 100644 --- a/components/widget/widget_cell.lua +++ b/components/widget/widget_cell.lua @@ -90,19 +90,19 @@ function Cell:_content(...) end ---@param injector WidgetInjector? ----@return Html? +---@return string? function Cell:make(injector) self:_new(self.name) self:_class(unpack(self.classes or {})) self:_content(unpack(self.content)) if self.contentDiv == nil then - return {} + return end self.root :node(self.description) :node(self.contentDiv) - return self.root + return tostring(self.root) end return Cell diff --git a/components/widget/widget_center.lua b/components/widget/widget_center.lua index 7b81449d8c0..6247b1d5af0 100644 --- a/components/widget/widget_center.lua +++ b/components/widget/widget_center.lua @@ -25,9 +25,9 @@ local Center = Class.new( ) ---@param injector WidgetInjector? ----@return Html? +---@return string? function Center:make(injector) - return Center:_create(self.content, self.classes) + return tostring(Center:_create(self.content, self.classes)) end ---@param content (string|number)[] diff --git a/components/widget/widget_chronology.lua b/components/widget/widget_chronology.lua index 3f99dc57939..0fd0e436def 100644 --- a/components/widget/widget_chronology.lua +++ b/components/widget/widget_chronology.lua @@ -23,9 +23,9 @@ local Chronology = Class.new( ) ---@param injector WidgetInjector? ----@return Html? +---@return string? function Chronology:make(injector) - return Chronology:_chronology(self.links) + return tostring(Chronology:_chronology(self.links)) end ---@param links table diff --git a/components/widget/widget_header.lua b/components/widget/widget_header.lua index bd3959fc269..4a0dfb73432 100644 --- a/components/widget/widget_header.lua +++ b/components/widget/widget_header.lua @@ -36,7 +36,7 @@ local Header = Class.new( ) ---@param injector WidgetInjector? ----@return Html[] +---@return string function Header:make(injector) local header = { Header:_name(self.name), @@ -63,7 +63,7 @@ function Header:make(injector) for _, element in ipairs(header) do wrapper:node(element) end - return wrapper + return tostring(wrapper) end ---@param name string? diff --git a/components/widget/widget_highlights.lua b/components/widget/widget_highlights.lua index ce50100e0fa..89dc7a51e68 100644 --- a/components/widget/widget_highlights.lua +++ b/components/widget/widget_highlights.lua @@ -23,9 +23,9 @@ local Highlights = Class.new( ) ---@param injector WidgetInjector? ----@return Html? +---@return string? function Highlights:make(injector) - return Highlights:_highlights(self.list) + return tostring(Highlights:_highlights(self.list)) end ---@param list (string|number)[]? diff --git a/components/widget/widget_links.lua b/components/widget/widget_links.lua index 7541947e4bf..80cb8f14a93 100644 --- a/components/widget/widget_links.lua +++ b/components/widget/widget_links.lua @@ -28,7 +28,7 @@ local Links = Class.new( local PRIORITY_GROUPS = Lua.import('Module:Links/PriorityGroups', {loadData = true}) ---@param injector WidgetInjector? ----@return Html? +---@return string? function Links:make(injector) local infoboxLinks = mw.html.create('div') infoboxLinks :addClass('infobox-center') @@ -56,7 +56,7 @@ function Links:make(injector) infoboxLinks:wikitext(' ' .. self:_makeLink(key, value)) end - return mw.html.create('div'):node(infoboxLinks) + return tostring(mw.html.create('div'):node(infoboxLinks)) end ---@param key string diff --git a/components/widget/widget_table.lua b/components/widget/widget_table.lua index ca982ae542e..832d2b50b2a 100644 --- a/components/widget/widget_table.lua +++ b/components/widget/widget_table.lua @@ -50,7 +50,7 @@ function Table:addClass(class) end ---@param injector WidgetInjector? ----@return Html? +---@return string? function Table:make(injector) local displayTable = mw.html.create('div'):addClass('csstable-widget') displayTable:css{ @@ -67,7 +67,7 @@ function Table:make(injector) displayTable:node(WidgetFactory.work(row, injector)) end - return displayTable + return tostring(displayTable) end ---@return integer? diff --git a/components/widget/widget_table_cell.lua b/components/widget/widget_table_cell.lua index d3b410349d4..dcec7ccbc9a 100644 --- a/components/widget/widget_table_cell.lua +++ b/components/widget/widget_table_cell.lua @@ -57,7 +57,7 @@ function TableCell:addCss(key, value) end ---@param injector WidgetInjector? ----@return Html? +---@return string? function TableCell:make(injector) local cell = mw.html.create('div'):addClass('csstable-widget-cell') cell:css{ @@ -73,7 +73,7 @@ function TableCell:make(injector) cell:node(self:_concatContent()) - return cell + return tostring(cell) end ---@return string diff --git a/components/widget/widget_table_cell_new.lua b/components/widget/widget_table_cell_new.lua index 16206fd3517..809dbac43e1 100644 --- a/components/widget/widget_table_cell_new.lua +++ b/components/widget/widget_table_cell_new.lua @@ -43,7 +43,7 @@ local TableCell = Class.new( ) ---@param injector WidgetInjector? ----@return Html? +---@return string? function TableCell:make(injector) local cell = mw.html.create(self.isHeader and 'th' or 'td') cell:attr('colspan', self.colSpan) @@ -55,7 +55,7 @@ function TableCell:make(injector) cell:node(self:_content()) - return cell + return tostring(cell) end ---@return string diff --git a/components/widget/widget_table_new.lua b/components/widget/widget_table_new.lua index 23dbcb3ec6a..3e8bca8e6c8 100644 --- a/components/widget/widget_table_new.lua +++ b/components/widget/widget_table_new.lua @@ -34,7 +34,7 @@ local Table = Class.new( ) ---@param injector WidgetInjector? ----@return Html? +---@return string? function Table:make(injector) local wrapper = mw.html.create('div'):addClass('table-responsive') local output = mw.html.create('table'):addClass('wikitable') @@ -48,7 +48,7 @@ function Table:make(injector) end) wrapper:node(output) - return wrapper + return tostring(wrapper) end return Table diff --git a/components/widget/widget_table_row.lua b/components/widget/widget_table_row.lua index dbafaddb562..fc194699d53 100644 --- a/components/widget/widget_table_row.lua +++ b/components/widget/widget_table_row.lua @@ -59,7 +59,7 @@ function TableRow:getCellCount() end ---@param injector WidgetInjector? ----@return Html? +---@return string? function TableRow:make(injector) local row = mw.html.create('div'):addClass('csstable-widget-row') @@ -73,7 +73,7 @@ function TableRow:make(injector) row:node(WidgetFactory.work(cell, injector)) end - return row + return tostring(row) end return TableRow diff --git a/components/widget/widget_table_row_new.lua b/components/widget/widget_table_row_new.lua index 1c5ea278187..e8f015d9e38 100644 --- a/components/widget/widget_table_row_new.lua +++ b/components/widget/widget_table_row_new.lua @@ -34,7 +34,7 @@ local TableRow = Class.new( ) ---@param injector WidgetInjector? ----@return Html? +---@return string? function TableRow:make(injector) local row = mw.html.create('tr') @@ -46,7 +46,7 @@ function TableRow:make(injector) row:node(WidgetFactory.work(child, injector)) end) - return row + return tostring(row) end return TableRow diff --git a/components/widget/widget_title.lua b/components/widget/widget_title.lua index 3454a083fd6..048368d0acd 100644 --- a/components/widget/widget_title.lua +++ b/components/widget/widget_title.lua @@ -22,9 +22,9 @@ local Title = Class.new( ) ---@param injector WidgetInjector? ----@return Html? +---@return string? function Title:make(injector) - return Title:_create(self.content) + return tostring(Title:_create(self.content)) end ---@param infoDescription string|number|nil From fecd7a480350842952ed41629917927ec0f734d6 Mon Sep 17 00:00:00 2001 From: Rikard Blixt Date: Wed, 11 Sep 2024 18:01:20 +0200 Subject: [PATCH 6/7] fix annos --- components/squad/commons/squad.lua | 2 +- components/squad/commons/squad_utils.lua | 4 ++-- components/squad/wikis/dota2/squad_custom.lua | 2 +- .../squad/wikis/overwatch/squad_custom.lua | 4 ++-- components/squad/wikis/smash/squad_custom.lua | 2 +- .../squad/wikis/starcraft/squad_custom.lua | 2 +- .../squad/wikis/starcraft2/squad_custom.lua | 4 ++-- .../squad/wikis/stormgate/squad_custom.lua | 4 ++-- components/widget/widget_factory.lua | 18 +++++++----------- 9 files changed, 19 insertions(+), 23 deletions(-) diff --git a/components/squad/commons/squad.lua b/components/squad/commons/squad.lua index ff138ec10b8..a52215bc08d 100644 --- a/components/squad/commons/squad.lua +++ b/components/squad/commons/squad.lua @@ -101,7 +101,7 @@ function Squad:row(row) return self end ----@return Html +---@return string function Squad:create() local dataTable = Widget.TableNew{ css = {['margin-bottom'] = '10px'}, diff --git a/components/squad/commons/squad_utils.lua b/components/squad/commons/squad_utils.lua index 2f18e6a085e..dd690f5012f 100644 --- a/components/squad/commons/squad_utils.lua +++ b/components/squad/commons/squad_utils.lua @@ -181,7 +181,7 @@ end ---@param squadClass Squad ---@param personFunction fun(player: table, squadType: integer):WidgetTableRowNew ---@param injector WidgetInjector? ----@return Html +---@return string function SquadUtils.defaultRunManual(frame, squadClass, personFunction, injector) local args = Arguments.getArgs(frame) local injectorInstance = (injector and injector()) or @@ -210,7 +210,7 @@ end ---@param customTitle string? ---@param injector? WidgetInjector ---@param personMapper? fun(person: table): table ----@return Html? +---@return string? function SquadUtils.defaultRunAuto(players, squadType, squadClass, rowCreator, customTitle, injector, personMapper) local args = {type = squadType, title = customTitle} local injectorInstance = (injector and injector()) or diff --git a/components/squad/wikis/dota2/squad_custom.lua b/components/squad/wikis/dota2/squad_custom.lua index c20db55a4d6..2cb2554f6bf 100644 --- a/components/squad/wikis/dota2/squad_custom.lua +++ b/components/squad/wikis/dota2/squad_custom.lua @@ -55,7 +55,7 @@ function ExtendedSquadRow:activeteam() end ---@param frame Frame ----@return Html +---@return string function CustomSquad.run(frame) return SquadUtils.defaultRunManual(frame, Squad, CustomSquad._playerRow, CustomInjector) end diff --git a/components/squad/wikis/overwatch/squad_custom.lua b/components/squad/wikis/overwatch/squad_custom.lua index 1ba4ba70881..2fea3de24c4 100644 --- a/components/squad/wikis/overwatch/squad_custom.lua +++ b/components/squad/wikis/overwatch/squad_custom.lua @@ -48,7 +48,7 @@ function ExtendedSquadRow:number() end ---@param frame Frame ----@return Html +---@return string function CustomSquad.run(frame) local args = Arguments.getArgs(frame) local squad = Squad(args, CustomInjector()):title() @@ -69,7 +69,7 @@ end ---@param playerList table[] ---@param squadType integer ---@param customTitle string? ----@return Html? +---@return string? function CustomSquad.runAuto(playerList, squadType, customTitle) return SquadUtils.defaultRunAuto(playerList, squadType, Squad, SquadUtils.defaultRow(SquadRow), customTitle) end diff --git a/components/squad/wikis/smash/squad_custom.lua b/components/squad/wikis/smash/squad_custom.lua index 4c587b89fc8..552c59d4e7d 100644 --- a/components/squad/wikis/smash/squad_custom.lua +++ b/components/squad/wikis/smash/squad_custom.lua @@ -54,7 +54,7 @@ function ExtendedSquadRow:mains() end ---@param frame Frame ----@return Html +---@return string function CustomSquad.run(frame) local args = Arguments.getArgs(frame) local squad = Squad(args, CustomInjector()):title():header() diff --git a/components/squad/wikis/starcraft/squad_custom.lua b/components/squad/wikis/starcraft/squad_custom.lua index 596488a7182..aa7d2e33e4b 100644 --- a/components/squad/wikis/starcraft/squad_custom.lua +++ b/components/squad/wikis/starcraft/squad_custom.lua @@ -59,7 +59,7 @@ function ExtendedSquadRow:elo() end ---@param frame Frame ----@return Html +---@return string function CustomSquad.run(frame) local args = Arguments.getArgs(frame) local tlpd = Logic.readBool(args.tlpd) diff --git a/components/squad/wikis/starcraft2/squad_custom.lua b/components/squad/wikis/starcraft2/squad_custom.lua index 7659410be8e..048db1bcab1 100644 --- a/components/squad/wikis/starcraft2/squad_custom.lua +++ b/components/squad/wikis/starcraft2/squad_custom.lua @@ -18,7 +18,7 @@ local SquadUtils = Lua.import('Module:Squad/Utils') local CustomSquad = {} ---@param frame Frame ----@return Html +---@return string function CustomSquad.run(frame) return SquadUtils.defaultRunManual(frame, Squad, CustomSquad._playerRow) end @@ -26,7 +26,7 @@ end ---@param playerList table[] ---@param squadType integer ---@param customTitle string? ----@return Html? +---@return string? function CustomSquad.runAuto(playerList, squadType, customTitle) return SquadUtils.defaultRunAuto( playerList, diff --git a/components/squad/wikis/stormgate/squad_custom.lua b/components/squad/wikis/stormgate/squad_custom.lua index e0de2461311..44337dfe8ef 100644 --- a/components/squad/wikis/stormgate/squad_custom.lua +++ b/components/squad/wikis/stormgate/squad_custom.lua @@ -17,7 +17,7 @@ local SquadUtils = Lua.import('Module:Squad/Utils') local CustomSquad = {} ---@param frame Frame ----@return Html +---@return string function CustomSquad.run(frame) return SquadUtils.defaultRunManual(frame, Squad, CustomSquad._playerRow) end @@ -25,7 +25,7 @@ end ---@param playerList table[] ---@param squadType integer ---@param customTitle string? ----@return Html? +---@return string? function CustomSquad.runAuto(playerList, squadType, customTitle) return SquadUtils.defaultRunAuto( playerList, diff --git a/components/widget/widget_factory.lua b/components/widget/widget_factory.lua index 807275fcf8d..bf5074b64dc 100644 --- a/components/widget/widget_factory.lua +++ b/components/widget/widget_factory.lua @@ -14,25 +14,21 @@ local WidgetFactory = Class.new() ---@param widget Widget ---@param injector WidgetInjector? ----@return Html +---@return string function WidgetFactory.work(widget, injector) local children = widget:tryMake(injector) if not children then - return mw.html.create() + return '' end - if Array.isArray(children) then - ---@cast children Widget[] - local wrapper = mw.html.create() - Array.forEach(children, function(child) - wrapper:node(WidgetFactory.work(child, injector)) - end) - return wrapper + if type(children) == 'string' then + return children end - ---@cast children Html - return children + return table.concat(Array.map(children, function(child) + return WidgetFactory.work(child, injector) + end)) end return WidgetFactory From da3c4ec33c325b89ee50dbcde4efac9469be9045 Mon Sep 17 00:00:00 2001 From: Rikard Blixt Date: Thu, 12 Sep 2024 10:51:19 +0200 Subject: [PATCH 7/7] ensure we don't tostring nils --- components/widget/widget_breakdown.lua | 6 +++--- components/widget/widget_center.lua | 6 +++--- components/widget/widget_chronology.lua | 6 +++--- components/widget/widget_highlights.lua | 6 +++--- components/widget/widget_title.lua | 6 +++--- 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/components/widget/widget_breakdown.lua b/components/widget/widget_breakdown.lua index 4d4b8874666..019ed3baff2 100644 --- a/components/widget/widget_breakdown.lua +++ b/components/widget/widget_breakdown.lua @@ -28,13 +28,13 @@ local Breakdown = Class.new( ---@param injector WidgetInjector? ---@return string? function Breakdown:make(injector) - return tostring(Breakdown:_breakdown(self.contents, self.classes, self.contentClasses)) + return Breakdown:_breakdown(self.contents, self.classes, self.contentClasses) end ---@param contents (string|number)[] ---@param classes string[] ---@param contentClasses table --can have gaps in the outer table ----@return Html? +---@return string? function Breakdown:_breakdown(contents, classes, contentClasses) if type(contents) ~= 'table' or contents == {} then return nil @@ -54,7 +54,7 @@ function Breakdown:_breakdown(contents, classes, contentClasses) div:node(infoboxCustomCell) end - return div + return tostring(div) end return Breakdown diff --git a/components/widget/widget_center.lua b/components/widget/widget_center.lua index 6247b1d5af0..42551a99ffa 100644 --- a/components/widget/widget_center.lua +++ b/components/widget/widget_center.lua @@ -27,12 +27,12 @@ local Center = Class.new( ---@param injector WidgetInjector? ---@return string? function Center:make(injector) - return tostring(Center:_create(self.content, self.classes)) + return Center:_create(self.content, self.classes) end ---@param content (string|number)[] ---@param classes string[] ----@return Html? +---@return string? function Center:_create(content, classes) if Table.isEmpty(content) then return nil @@ -47,7 +47,7 @@ function Center:_create(content, classes) centered:wikitext(item) end - return mw.html.create('div'):node(centered) + return tostring(mw.html.create('div'):node(centered)) end return Center diff --git a/components/widget/widget_chronology.lua b/components/widget/widget_chronology.lua index 0fd0e436def..e7236007fd1 100644 --- a/components/widget/widget_chronology.lua +++ b/components/widget/widget_chronology.lua @@ -25,11 +25,11 @@ local Chronology = Class.new( ---@param injector WidgetInjector? ---@return string? function Chronology:make(injector) - return tostring(Chronology:_chronology(self.links)) + return Chronology:_chronology(self.links) end ---@param links table ----@return Html? +---@return string? function Chronology:_chronology(links) if links == nil or Table.size(links) == 0 then return @@ -49,7 +49,7 @@ function Chronology:_chronology(links) next = links['next' .. index] end - return chronologyContent + return tostring(chronologyContent) end ---@param previous string|number|nil diff --git a/components/widget/widget_highlights.lua b/components/widget/widget_highlights.lua index 89dc7a51e68..152b63e2f58 100644 --- a/components/widget/widget_highlights.lua +++ b/components/widget/widget_highlights.lua @@ -25,11 +25,11 @@ local Highlights = Class.new( ---@param injector WidgetInjector? ---@return string? function Highlights:make(injector) - return tostring(Highlights:_highlights(self.list)) + return Highlights:_highlights(self.list) end ---@param list (string|number)[]? ----@return Html? +---@return string? function Highlights:_highlights(list) if list == nil or Table.size(list) == 0 then return nil @@ -44,7 +44,7 @@ function Highlights:_highlights(list) div:node(highlights) - return div + return tostring(div) end return Highlights diff --git a/components/widget/widget_title.lua b/components/widget/widget_title.lua index 048368d0acd..62b4c9320cd 100644 --- a/components/widget/widget_title.lua +++ b/components/widget/widget_title.lua @@ -24,18 +24,18 @@ local Title = Class.new( ---@param injector WidgetInjector? ---@return string? function Title:make(injector) - return tostring(Title:_create(self.content)) + return Title:_create(self.content) end ---@param infoDescription string|number|nil ----@return Html +---@return string function Title:_create(infoDescription) local header = mw.html.create('div') header :addClass('infobox-header') :addClass('wiki-backgroundcolor-light') :addClass('infobox-header-2') :wikitext(infoDescription) - return mw.html.create('div'):node(header) + return tostring(mw.html.create('div'):node(header)) end return Title