minetest_modding_book/_en/quality/luacheck.md

108 lines
3.1 KiB
Markdown
Raw Normal View History

2018-02-25 02:56:45 +03:00
---
title: Automatic Error Checking
layout: default
2018-07-15 21:36:35 +03:00
root: ../..
2019-08-14 16:45:42 +03:00
idx: 8.2
2018-07-15 17:28:10 +03:00
description: Use LuaCheck to find errors
2018-07-15 21:13:16 +03:00
redirect_from: /en/chapters/luacheck.html
2018-02-25 02:56:45 +03:00
---
## Introduction <!-- omit in toc -->
2018-02-25 02:56:45 +03:00
In this chapter, you will learn how to use a tool called LuaCheck to automatically
2018-02-25 02:56:45 +03:00
scan your mod for any mistakes. This tool can be used in combination with your
editor to provide alerts to any mistakes.
- [Installing LuaCheck](#installing-luacheck)
- [Windows](#windows)
- [Linux](#linux)
- [Running LuaCheck](#running-luacheck)
- [Configuring LuaCheck](#configuring-luacheck)
- [Troubleshooting](#troubleshooting)
- [Using with editor](#using-with-editor)
2018-02-25 02:56:45 +03:00
## Installing LuaCheck
### Windows
Simply download luacheck.exe from
[the Github Releases page](https://github.com/mpeterv/luacheck/releases).
### Linux
First, you'll need to install LuaRocks:
2018-02-25 02:56:45 +03:00
sudo apt install luarocks
You can then install LuaCheck globally:
sudo luarocks install luacheck
Check that it's installed with the following command:
luacheck -v
## Running LuaCheck
The first time you run LuaCheck, it will probably pick up a lot of false
errors. This is because it still needs to be configured.
On Windows, open powershell or bash in the root folder of your project
and run `path\to\luacheck.exe .`
2018-02-25 02:56:45 +03:00
On Linux, run `luacheck .` whilst in the root folder of your project.
## Configuring LuaCheck
2018-04-12 03:02:22 +03:00
Create a file called .luacheckrc in the root of your project. This could be the
2018-02-25 02:56:45 +03:00
root of your game, modpack, or mod.
Put the following contents in it:
```lua
2018-02-25 02:56:45 +03:00
unused_args = false
allow_defined_top = true
globals = {
2018-04-12 03:02:22 +03:00
"minetest",
2018-02-25 02:56:45 +03:00
}
read_globals = {
2018-04-12 03:02:22 +03:00
string = {fields = {"split"}},
table = {fields = {"copy", "getn"}},
-- Builtin
"vector", "ItemStack",
2018-04-12 03:02:22 +03:00
"dump", "DIR_DELIM", "VoxelArea", "Settings",
-- MTG
"default", "sfinv", "creative",
2018-02-25 02:56:45 +03:00
}
```
2018-02-25 02:56:45 +03:00
Next, you'll need to test that it works by running LuaCheck. You should get a lot
fewer errors this time. Starting at the first error you get, modify the code to
remove the issue, or modify the configuration if the code is correct. See the list
below.
2018-02-25 02:56:45 +03:00
### Troubleshooting
2018-02-25 03:00:10 +03:00
* **accessing undefined variable foobar** - If `foobar` is meant to be a global,
2018-10-27 05:10:37 +03:00
add it to `read_globals`. Otherwise, add any missing `local`s to the mod.
2018-02-25 03:00:10 +03:00
* **setting non-standard global variable foobar** - If `foobar` is meant to be a global,
2018-10-27 05:10:37 +03:00
add it to `globals`. Remove from `read_globals` if present.
Otherwise, add any missing `local`s to the mod.
2018-02-25 03:00:10 +03:00
* **mutating read-only global variable 'foobar'** - Move `foobar` from `read_globals` to
`globals`, or stop writing to foobar.
2018-02-25 02:56:45 +03:00
## Using with editor
It is highly recommended that you find and install a plugin for your editor of choice
to show you errors without running a command. Most editors will likely have a plugin
available.
* **VSCode** - Ctrl+P, then paste: `ext install dwenegar.vscode-luacheck`
* **Sublime** - Install using package-control:
[SublimeLinter](https://github.com/SublimeLinter/SublimeLinter),
2018-10-27 05:10:37 +03:00
[SublimeLinter-luacheck](https://github.com/SublimeLinter/SublimeLinter-luacheck).