StyLua icon indicating copy to clipboard operation
StyLua copied to clipboard

When both lua5.2 and luau are enabled, stylua sometimes chokes on label declarations

Open ahicks92 opened this issue 1 year ago • 0 comments

Stylua 0.20.0. I wish I had a minimum repro but the very obvious repros aren't triggering it I'm afraid so the block of code is longer.

Why it matters: we can't use the official GH action to reliably format code containing labels because it enables luau, and there don't seem to be variants to let us use another asset or something like that to only have the lua variants.

Anyhow:

cargo install stylua --features lua52,luau

On both formatting and checking, we get:

error: could not format file .\scripts\memosort.lua: error parsing: error occurred while creating ast: unexpected token `end`. (starting from line 41, character 4 and ending on line 41, character 7)
additional information: expected identifier after `::`

And the code, as minimal as I could get it:

local mod = {}

---@alias fa.memosort.ScoreCallback fun(any): number

---@param  tab any[]
---@param callback fa.memosort.ScoreCallback
---@param cache table?
function mod.memosort(tab, callback, cache)
   cache = cache or {}

   for i = 1, #tab do
      if cache[tab[i]] then goto continue end
      cache[tab[i]] = callback(tab[i])

      ::continue::
   end

   table.sort(tab, function(a, b)
      assert(cache[a])
      assert(cache[b])
      return cache[a] < cache[b]
   end)
end

return mod

This is the Lua "continue idiom": ::continue:: as a label and goto continue. I did think okay it's a keyword so let's rename it. ::cont::, but that didn't work either. And I'm sure it doesn't trigger on all labels--it certainly didn't on small test cases.

Note that Luau itself dropped goto. Not sure if that's relevant to the problem but I did also check that.

ahicks92 avatar Sep 12 '24 04:09 ahicks92