JohnnyMorganz / StyLua

An opinionated Lua code formatter
Mozilla Public License 2.0
1.6k stars 72 forks source link

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

Closed ahicks92 closed 2 weeks ago

ahicks92 commented 2 months ago

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.

JohnnyMorganz commented 2 weeks ago

This is a duplicate of #407. It will be fixed by #854 which will introduce a way to configure the style of syntax used at runtime in the stylua.toml file

ahicks92 commented 2 weeks ago

Hmm thanks. I did look before posting this. Sorry about that. Looking forward to the changes.