darklua icon indicating copy to clipboard operation
darklua copied to clipboard

Convert between LuaLs annotation comments and Luau type annotations

Open itsfrank opened this issue 10 months ago • 2 comments

Hi, first off, I recognize this is quite the undertaking, so I don't really expect any work to be done until enough people have real use cases for it and someone feels passionate about building it.

But I was curious if it was ever considered to convert between the two different type systems, and wanted to start a conversation / create an issue documenting the topic that others can find in the future.

My current scenario

I want to write a Lua/Luau library that is not coupled Roblox but can be used in Roblox. I would like to make it available in both Lua 5.1 and Luau. In either case, I would love if my users could benefit from the appropriate LSP (lua-ls for Lua, and luau-lsp for Luau).

My plan is to develop the library using tests that are executed by either Luvit or Lune depending on which flavor I choose to develop in (to maximize compatibility, it is probably better to develop in Lua 5.1).

Then, to "release" I would use this wonderful project to convert from whichever dialect I use for development to the other; e.g. convert the Lua 5.1 library to Luau with Roblox-style requires.

However, critically, regardless of which way I convert, the non-development dialect will lose typing information. Say I use LuaLs annotations, my Luau code will have no typing, or if I use Luau, the Lua 5.1 code will have no typing.

Hence, why typing conversion would be very useful in my case.

LuaLs vs Luau type annotations

I suspect users/maintainers of this project are quite familiar with Luau typing, but some may not be familiar with LuaLs.

LuaLs uses doc-comment like type annotation to provide LSP features that are mostly on-par with Luau typing LSP features. The LuaLs annotation have been embraced by large Lua communities like the Neovim plugin community.

LuaLs annotation documentation

For example, this Luau code:

function string_coord(x: number, y: number): string
    return tostring(x) .. "," .. tostring(y)
end

Has equivalent typing information to the following Lua 5.1 code when using LuaLs:

---@param x number
---@param y number
---@return string
function string_coord(x, y)
    return tostring(x) .. "," .. tostring(y)
end

It would be great for the larger Lua ecosystem to have a tool that can generate the annotations of one dialect by parsing the other and darklua seems like the best project for this functionality!

itsfrank avatar Mar 27 '24 17:03 itsfrank