Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ahicks92 opened this issue Sep 12, 2024 · 2 comments

Comments

@ahicks92
Copy link

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
Copy link
Owner

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

@JohnnyMorganz JohnnyMorganz closed this as not planned Won't fix, can't repro, duplicate, stale Oct 26, 2024
@ahicks92
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants