Module talk:Template invocation

Latest comment: 3 months ago by Mikhail Ryazanov in topic Editor-hostile wikitext

Lua error: attempt to compare string with number. edit

At Template:Toolbar/testcases and Template:3x/testcases test cases fail with this backtrace:

Backtrace:

    [C]: in function "sort"
    Module:Template_invocation:107: ?
    (tail call): ?
    Module:Template_test_case:140: in function "getInvocation"
    Module:Template_test_case:642: in function "?"
    Module:Template_test_case:663: ?
    [C]: in function "tostring"
    Module:Template_test_case:786: ?
    (tail call): ?
    mw.lua:518: ?
    [C]: ?

The issue seems to be caused by template invocation, in which a positional parameter is {{=}} and at least one named parameter is present. I've added test14_equals_sign_parameter to Module:Template test case/testcases to demonstrate the issue. —⁠andrybak (talk) 14:25, 19 May 2020 (UTC)Reply

I haven't looked at what this module does or how it's used but the cause of the error is simple. A loop at line 104 uses pairs(invArgs) to put each key (parameter name) into the invArgs_list array. That will include numbers like 1 if the first unnamed parameter is entered, and strings like the names of any named parameters. Line 107 then fails because it cannot sort a mixture of numbers and strings. Someone would have to work out what the module wants to do at that point (why is it sorting the parameters names?) and decide what should happen to numbered parameters. Perhaps they should be omitted, or converted to strings. Or, add a custom sort routine to put the numbers before the strings. Ping me if help is wanted with implementing any of this after deciding what is wanted. Johnuniq (talk) 23:39, 19 May 2020 (UTC)Reply
Johnuniq, at line 95, the module tries to guess if something that looks like 1=foo=bar was passed to the template:
::		if type(v) == 'string' and v:find('=', 1, true) then
::			-- Likely something like 1=foo=bar, we need to do it as a named arg
::			break
::		end
::
My guess is that this logic breaks when characters on the left of equals sign are not a valid parameter name or index. For example, two opening curly braces, as is the case with transclusion of {{=}} as a template. —⁠andrybak (talk) 00:13, 20 May 2020 (UTC)Reply
No, {{=}} would be expanded before the module sees it. I was trying to avoid taking the time to work out how it's used but I'll look later. The questions I posed above are what need to considered. Regardless of how the template should be used, it should not break with this error if someone uses a mixture of unnamed and named parameters. Johnuniq (talk) 00:31, 20 May 2020 (UTC)Reply
Johnuniq, sorry for the confusion, I should have been more clear. What I'm trying to say is that {{=}} is a corner case. If the module had issues with just a mixture of unnamed and named parameters, every other /testcases page would have been broken. —⁠andrybak (talk) 00:51, 20 May 2020 (UTC)Reply
What I said is correct. If you have an example of a template that appears to contradict my assessment, please identify it. The explanation would be that the code I looked at (just a couple of lines around line 95) does not receive the numbered parameters. My first attempt to work out what this module is for failed. How does this module end up in lots of articles, for example Alan Garner? Is there a particular template that is responsible for most of those? {{Toolbar}}? Very briefly, why is this module needed for toolbar? Johnuniq (talk) 01:48, 20 May 2020 (UTC)Reply

The code seems somewhat broken to me. I would fix it but omg it's used in so many places and I haven't got a clue why it is invoked in articles. For a lol, see Module talk:Template invocation/testcases which apparently has been displaying "Error: attempt to compare string with number" for a long time. Johnuniq (talk) 03:42, 20 May 2020 (UTC)Reply

@Andrybak: OK I got courageous and fixed the module anyway. Please check it out. Johnuniq (talk) 04:05, 20 May 2020 (UTC)Reply
A mystery explained with a mystery: the reason the module is used at Alan Garner is that it uses {{postnominals}} and even "{{postnominals}}" (with no parameters) calls Module:Template invocation. Johnuniq (talk) 04:50, 20 May 2020 (UTC)Reply

Editor-hostile wikitext edit

The module is currently producing "editor-hostile wikitext" by not adding spaces between the parameters. Please add a space at line Module:Template_invocation#L-116:

ret[#ret + 1] = ' ' .. seps.pipe

(an alternative solution by changing pipe = '|', to pipe = ' |', at Module:Template_invocation#L-81 would probably be less robust, since pipe is used for unnamed parameters as well, but some such templates don't trim whitespace from their arguments). — Mikhail Ryazanov (talk) 20:56, 9 February 2024 (UTC)Reply

  Done * Pppery * it has begun... 23:23, 9 February 2024 (UTC)Reply
  Thank you, seems to work better now. I've updated the test cases accordingly. — Mikhail Ryazanov (talk) 00:25, 11 February 2024 (UTC)Reply