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

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

Open
nurpax opened this issue Jan 5, 2024 · 5 comments
Assignees
Labels
duplicate This issue or pull request already exists enhancement New feature or request

Comments

@nurpax
Copy link

nurpax commented Jan 5, 2024

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 nurpax added the enhancement New feature or request label Jan 5, 2024
nurpax added a commit to nurpax/luau that referenced this issue Jan 5, 2024
…tion files

Add the ability to load a type definition file before type checking.

This is useful in projects that'd like to declare their own types
(such as types declared by their game engine's Luau integration code)
and linting their scripts using 'luau-analyze'.

This is modeled to behave similar to the luau-lsp.types.definitionFiles
in the Luau LSP project (https://github.com/JohnnyMorganz/luau-lsp).

Usage example:

  luau-analyze --definitions=engine_types.d.luau some_script.luau

Note: doesn't introduce new clang-format delta, but the edited
file already deviates from what clang-format produces.

for luau-lang#1140
@nurpax
Copy link
Author

nurpax commented Jan 5, 2024

PR for the above: #1139

@JohnnyMorganz
Copy link
Contributor

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

nurpax commented Jan 6, 2024

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 vegorov-rbx self-assigned this Jan 8, 2024
@vegorov-rbx
Copy link
Collaborator

If we were to add this, it should work through a .luaurc file so that any tool can be aware of the dependency: #418

@vegorov-rbx vegorov-rbx added the duplicate This issue or pull request already exists label Jan 8, 2024
@nurpax
Copy link
Author

nurpax commented Jan 8, 2024

@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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists enhancement New feature or request
Development

No branches or pull requests

3 participants