Formatters
What is StyLua? An introduction to JohnnyMorganz's Lua formatter
StyLua is a deterministic code formatter for Lua 5.1-5.4, LuaJIT, Luau and CfxLua/FiveM Lua, built using full-moon. It parses the codebase and reprints it from scratch to enforce a consistent style, similar to prettier. It mainly follows the Roblox Lua Style Guide.
- Tool
- StyLua
- Made by
- JohnnyMorganz
- Official site
- github.com/JohnnyMorganz/StyLua
- Category
- Formatters
Who makes StyLua
StyLua is made by JohnnyMorganz and developed on GitHub. The official website is linked below and leads to the project repository.
It is built using full-moon.
Official website: github.com/JohnnyMorganz/StyLua
What StyLua is for
StyLua is for enforcing a consistent style across Lua and Luau code. It is deterministic and supports several Lua versions and variants. Rather than patching existing text, it parses the codebase and reprints it from scratch, in the manner of prettier.
Its style mainly follows the Roblox Lua Style Guide. The --check flag reports files needing formatting without rewriting them, and the output can be machine-readable JSON, among other formats. A --verify flag re-parses formatted output to check it is still valid.
Who StyLua is for
The stated audience is Lua and Luau developers, and teams adopting StyLua in large codebases. For large codebases, the ability to format a specific range of lines within a file and to skip formatting for part of a file with a comment directive helps with adopting the formatter gradually.
The many distribution options make it usable in different setups, from local editors to CI.
Notable capabilities
StyLua pairs a deterministic formatter with several ways to install and run it. The capabilities below come from the verified facts.
- It supports Lua 5.1-5.4, LuaJIT, Luau and CfxLua/FiveM Lua. Installing with cargo builds for Lua 5.1 by default, with feature flags for other syntaxes.
- A comment directive lets users skip formatting for part of a file, and it can format a specific range of lines within a file.
- The --check flag reports files needing formatting without rewriting them, with output available as machine-readable JSON among other formats.
- A --verify flag re-parses formatted output to check it is still valid.
- It has built-in, opt-in support for sorting require statements.
- It can run as a language server following the Language Server Protocol.
- Editor integrations exist for Sublime, Neovim and Zed, and require the StyLua binary installed.
- Pre-built binaries are on GitHub Releases, and it is available through Homebrew on macOS, on Docker Hub, as a GitHub Action, as a WASM library for Node.js or the browser, and with three pre-commit hooks.
How StyLua fits formatters
Formatters rewrite code to a consistent layout without changing its behavior. StyLua is a single-language formatter for the Lua family, covering several Lua versions and variants with one tool.
Its approach of parsing and reprinting from scratch is the same one used by prettier, which is why the output does not depend on how the input was originally written.
For Linthelm's wider niche of linters, static analysis and code quality, StyLua handles layout for Lua and Luau code. The --check flag and the pre-commit hooks make it suitable for automated verification, so style can be checked in the same places as other quality tooling.
Frequently asked questions
What is StyLua used for?
StyLua is used to format Lua and Luau code. It parses the codebase and reprints it from scratch to enforce a consistent style, similar to prettier.
Which Lua versions does StyLua support?
It supports Lua 5.1-5.4, LuaJIT, Luau and CfxLua/FiveM Lua. A cargo install builds for Lua 5.1 by default, with feature flags for other syntaxes.
How do I check formatting with StyLua without changing files?
Use the --check flag, which reports files needing formatting without rewriting them. Output can be machine-readable JSON, among other formats.
Can StyLua skip formatting part of a file?
Yes. A comment directive lets users skip formatting for part of a file, and it can also format a specific range of lines.
Does StyLua work in editors?
Editor integrations exist for Sublime, Neovim and Zed, and they require the StyLua binary installed. It can also run as a language server following the Language Server Protocol.
Linthelm is independent and is not affiliated with JohnnyMorganz. Product names belong to their owners. Features change; confirm details on the official website.