stylua
StyLua is an opinionated code formatter for Lua that automatically formats Lua code to enforce consistent style. It removes the need for debates about formatting by providing sensible defaults while still allowing some configuration.
Key Features:
- Opinionated formatting with sensible defaults for consistent Lua code style
- Automatic indentation and spacing management
- Line break optimization for improved code readability
- Configurable options via
stylua.toml
for project-specific needs - Lua 5.1-5.4 support covering all modern Lua versions
- LuaJIT compatibility for Just-In-Time compiled Lua code
- Roblox Luau support with specialized formatting rules
- Fast performance for formatting large codebases quickly
- Ignore comments to exclude specific code sections from formatting
- Range formatting to format only selected portions of files
stylua documentation
- Version in MegaLinter: 2.0.0
- Visit Official Web Site
- See How to configure stylua rules
- See How to disable stylua rules in files
- See Index of problems detected by stylua
Configuration in MegaLinter
- Enable stylua by adding
LUA_STYLUA
in ENABLE_LINTERS variable - Disable stylua by adding
LUA_STYLUA
in DISABLE_LINTERS variable
Variable | Description | Default value |
---|---|---|
LUA_STYLUA_ARGUMENTS | User custom arguments to add in linter CLI call Ex: -s --foo "bar" |
|
LUA_STYLUA_COMMAND_REMOVE_ARGUMENTS | User custom arguments to remove from command line before calling the linter Ex: -s --foo "bar" |
|
LUA_STYLUA_FILTER_REGEX_INCLUDE | Custom regex including filter Ex: (src\|lib) |
Include every file |
LUA_STYLUA_FILTER_REGEX_EXCLUDE | Custom regex excluding filter Ex: (test\|examples) |
Exclude no file |
LUA_STYLUA_CLI_LINT_MODE | Override default CLI lint mode - file : Calls the linter for each file- project : Call the linter from the root of the project |
file |
LUA_STYLUA_FILE_EXTENSIONS | Allowed file extensions. "*" matches any extension, "" matches empty extension. Empty list excludes all filesEx: [".py", ""] |
[".lua"] |
LUA_STYLUA_FILE_NAMES_REGEX | File name regex filters. Regular expression list for filtering files by their base names using regex full match. Empty list includes all files Ex: ["Dockerfile(-.+)?", "Jenkinsfile"] |
Include every file |
LUA_STYLUA_PRE_COMMANDS | List of bash commands to run before the linter | None |
LUA_STYLUA_POST_COMMANDS | List of bash commands to run after the linter | None |
LUA_STYLUA_UNSECURED_ENV_VARIABLES | List of env variables explicitly not filtered before calling LUA_STYLUA and its pre/post commands | None |
LUA_STYLUA_CONFIG_FILE | stylua configuration file nameUse LINTER_DEFAULT to let the linter find it |
stylua.toml |
LUA_STYLUA_RULES_PATH | Path where to find linter configuration file | Workspace folder, then MegaLinter default rules |
LUA_STYLUA_DISABLE_ERRORS | Run linter but consider errors as warnings | false |
LUA_STYLUA_DISABLE_ERRORS_IF_LESS_THAN | Maximum number of errors allowed | 0 |
LUA_STYLUA_CLI_EXECUTABLE | Override CLI executable | ['stylua'] |
IDE Integration
Use stylua in your favorite IDE to catch errors before MegaLinter !
IDE | Extension Name | Install | |
---|---|---|---|
neovim | ['nvim-lint', 'none-ls', 'stylua-nvim', 'stylua.nvim'] | Visit Web Site | |
Visual Studio Code | Stylua | ![]() |
|
Sublime Text | Sublime-Pretty-Lua | Visit Web Site |
MegaLinter Flavors
This linter is available in the following flavors
Flavor | Description | Embedded linters | Info | |
---|---|---|---|---|
![]() |
all | Default MegaLinter Flavor | 127 |
Behind the scenes
How are identified applicable files
- File extensions:
.lua
How the linting is performed
- stylua is called one time by identified file (
file
CLI lint mode)
Example calls
stylua --check myfile.lua
stylua --config-path chktexrc.toml --check myfile.lua
Help content
stylua 2.0.0
A utility to format Lua code
USAGE:
stylua [OPTIONS] [FILES]...
ARGS:
<FILES>... A list of files to format
OPTIONS:
-a, --allow-hidden
Whether to traverse hidden files/directories
-c, --check
Runs in 'check' mode
--color <COLOR>
Use colored output [default: Auto] [possible values: Always, Auto, Never]
-f, --config-path <CONFIG_PATH>
Specify path to stylua.toml configuration file
-g, --glob <GLOB>
Glob patterns to test against which files to check
-h, --help
Print help information
--no-editorconfig
Disables the EditorConfig feature
--num-threads <NUM_THREADS>
The number of threads to use to format files in parallel [default: 4]
--output-format <OUTPUT_FORMAT>
Configures the diff output when using 'check' mode [default: Standard] [possible values:
Standard, Unified, Json, Summary]
--range-end <RANGE_END>
An ending range to format files, given as a byte offset from the beginning of the file
--range-start <RANGE_START>
A starting range to format files, given as a byte offset from the beginning of the file
--respect-ignores
Respect .styluaignore and glob matching for file paths provided directly to the tool
-s, --search-parent-directories
Search parent directories for stylua.toml, if not found in current directory. Ignored if
config_path is provided
--stdin-filepath <STDIN_FILEPATH>
Specify the location of the file that is being passed into stdin. Ignored if not taking
in input from stdin
-v, --verbose
Enables verbose output
-V, --version
Print version information
--verify
Verifies the output correctness after formatting
FORMATTING OPTIONS:
--call-parentheses <CALL_PARENTHESES>
Specify whether to apply parentheses on function calls with single string or table arg
[possible values: Always, NoSingleString, NoSingleTable, None, Input]
--collapse-simple-statement <COLLAPSE_SIMPLE_STATEMENT>
Specify whether to collapse simple statements [possible values: Never, FunctionOnly,
ConditionalOnly, Always]
--column-width <COLUMN_WIDTH>
The column width to use to attempt to wrap lines
--indent-type <INDENT_TYPE>
The type of indents to use [possible values: Tabs, Spaces]
--indent-width <INDENT_WIDTH>
The width of a single indentation level
--line-endings <LINE_ENDINGS>
The type of line endings to use [possible values: Unix, Windows]
--quote-style <QUOTE_STYLE>
The style of quotes to use in string literals [possible values: AutoPreferDouble,
AutoPreferSingle, ForceDouble, ForceSingle]
--sort-requires
Enable requires sorting
--space-after-function-names <SPACE_AFTER_FUNCTION_NAMES>
[possible values: Never, Definitions, Calls, Always]
--syntax <SYNTAX>
The type of Lua syntax to parse [possible values: All, Lua51]
Installation on mega-linter Docker image
- Dockerfile commands :
# Parent descriptor install
RUN wget --tries=5 https://www.lua.org/ftp/lua-5.3.5.tar.gz -O - -q | tar -xzf - \
&& cd lua-5.3.5 \
&& make linux \
&& make install \
&& cd .. && rm -r lua-5.3.5/
# Linter install
# renovate: datasource=crate depName=stylua
ARG CARGO_STYLUA_VERSION=2.0.0
- Cargo packages (Rust):