luau-lang / luau

A fast, small, safe, gradually typed embeddable scripting language derived from Lua
https://luau.org
MIT License
3.93k stars 370 forks source link

luau-analyze should support loading in type definition files through a command line switch #1140

Open nurpax opened 8 months ago

nurpax commented 8 months ago

Currently luau-analyze does not support loading in a type definition file.

Type definitions are supported and work well with f.ex. the luau-lsp VSCode extension. Alas, should a user want to perform the same static type checking at the command line (f.ex to easily integrate this into build scripts), the same seems to be impossible with luau-analyze.

I propose that luau-analyze is extended with a --definitions=<typedef_file> CLI flag, that will load type definitions into the global scope before type checking.

Why are type definition files useful? For example, in a sitation where a game engine integrates Luau, the engine may expose its own functions, types and classes into the Lua global namespace that the game scripts then. Any Luau scripts written against this engine could then be static type checked on the command line with luau-analyze to ensure that the engine's types are correctly used in scripts.

(Note: I happened to also author a PR that implements the --definitions flag but noticed in the contributor guidelines that I should file an issue first. So here's the issue!)

nurpax commented 8 months ago

PR for the above: https://github.com/luau-lang/luau/pull/1139

JohnnyMorganz commented 8 months ago

should a user want to perform the same static type checking at the command line (f.ex to easily integrate this into build scripts), the same seems to be impossible with luau-analyze.

FYI in case you weren't aware, luau-lsp releases a binary that you can use like luau-analyze as well, with --definitions= support: luau-lsp analyze --definitions=path (https://github.com/JohnnyMorganz/luau-lsp?tab=readme-ov-file#standalone). Just in case your PR doesn't work out :)

But still +1 for first class support in the Luau CLI tool. Although I think definitions file syntax is currently not formalised so that may prevent this from being supported until then

nurpax commented 8 months ago

Although I think definitions file syntax is currently not formalised so that may prevent this from being supported until then

Yeah, I kind of gathered that from various github issues but thought to file a PR anyway as it a) makes forward progress on exposing extremely useful functionality, and b) it doesn't touch the Luau core so it doesn't feel like the risk of breaking anything is high.

Thanks for @JohnnyMorganz for luau-lsp btw, it's great. The ability to declare these type files is one of the main reasons why I'm gravitating towards Luau instead of Lua, the experience in VSCode is just so much better with types.

vegorov-rbx commented 8 months ago

If we were to add this, it should work through a .luaurc file so that any tool can be aware of the dependency: https://github.com/luau-lang/luau/issues/418

nurpax commented 8 months ago

@vegorov-rbx Looks like .luaurc PR got dropped in #418. Would you be willing to accept a PR for that updates my PR to input definitions from .luaurc instead of command line arguments?