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

Allow to enhance luacheck with custom checks #37

Open
ligurio opened this issue Nov 20, 2021 · 1 comment
Open

Allow to enhance luacheck with custom checks #37

ligurio opened this issue Nov 20, 2021 · 1 comment
Labels
enhancement New feature or request

Comments

@ligurio
Copy link

ligurio commented Nov 20, 2021

Problem description

Luacheck allows finding coding errors in Lua source code. I found it quite useful for Lua development. However, coding errors is only a part of possible bugs that can be found in a real projects. It would be nice to find other types of bugs automatically: suspicious constructions in code, using incorrect methods Lua, warnings specific for Lua interpreter (for example highlight things that not implemented in LuaJIT for performance purpose). Unfortunately, Luacheck doesn't allow adding custom checks or rules and limited by a constant number of warnings and errors.

Possible solutions

Use semgrep. There is a static analysis tool named semgrep that allows to describe your own rules in declarative format and among other supported programming languages semgrep has Lua support too (in experimental mode). Seems semgrep uses tree-sitter parser for Lua.

Luacheck already has a Lua API, but it is lack of methods to connect custom checkers. I think it is worth to enhance API that allow to add custom checks.

Additional information

Similar requests in Peter's Luacheck repository- How do add Custom Rules and Q: Can I add custom operators? (adjust syntax).

Requests for new checkers: #19, mpeterv#190, mpeterv#167, mpeterv#64, mpeterv#63, mpeterv#128, mpeterv#129.

@ligurio ligurio changed the title Allow to enhance luacheck with cutom checks Allow to enhance luacheck with custom checks Nov 20, 2021
@arichard4
Copy link

I think that if the "use semgrep" approach is used, that wouldn't be part of luacheck.

Can you elaborate on what you are looking for a bit? Luacheck goes through a number of stages, which you can view in src/luacheck/stages/init.lua. The way to extend luacheck in practice would be to add additional modules in there. There are potentially two places people might want to add modules:
(1) After "unwrap_parens". At this point, the starting lua code has been parsed into an AST and any simplifications have been applied to the AST.
(2) After "resolve_locals". (At any point, possibly including at the absolute end.) At this point, luacheck has "linearized" the AST, i.e. converted into a linear representation of items, and analyzed out scope and variable information.

Seems like the natural way to handle this is to expose callbacks in the Lua API to register additional callbacks to run after those two stages. However, I'm not sure if that actually makes any sense, presumably it's about as easy to just add the stage manually and fork luacheck?

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

No branches or pull requests

3 participants