Add luacheck and fix reported warnings

This commit is contained in:
luk3yx 2020-11-26 16:20:50 +13:00
parent 5545c25ea7
commit a3c1a490e0
5 changed files with 48 additions and 15 deletions

11
.github/workflows/build.yml vendored Normal file
View File

@ -0,0 +1,11 @@
on: [push, pull_request]
name: build
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- name: lint
uses: Roang-zero1/factorio-mod-luacheck@master
with:
luacheckrc_url: ""

23
.luacheckrc Normal file
View File

@ -0,0 +1,23 @@
max_line_length = 80
globals = {
'formspec_ast',
'minetest',
}
read_globals = {
string = {fields = {'split', 'trim'}},
table = {fields = {'copy'}}
}
-- The elements.lua file is auto-generated and has a hideously long line which
-- luacheck complains about.
files["elements.lua"].ignore = {""}
-- Because formspec_ast supports running outside of MT, some string and table
-- functions are added in init.lua (unless running in MT).
files['init.lua'].globals = read_globals
-- This error is thrown for methods that don't use the implicit "self"
-- parameter.
ignore = {"212/self"}

View File

@ -83,7 +83,6 @@ local function raw_parse(spec)
if #inner > 0 then
table.insert(inner, i)
table.insert(elem, inner)
inner = {}
else
table.insert(elem, i)
end
@ -204,7 +203,7 @@ types['...'] = function(elems, obj, res)
local template = {obj}
local val = {}
local is_string = type(obj[2]) == 'string'
for i, elem in ipairs(elems) do
for _, elem in ipairs(elems) do
local n = parse_value({elem}, template)
if is_string then
n = n[obj[1]]
@ -251,7 +250,7 @@ local function parse_elem(elem, custom_handlers)
end
local good, ast_elem
for i, template in ipairs(data) do
for _, template in ipairs(data) do
if type(template) == 'function' then
good, ast_elem = pcall(template, elem)
if good and (not ast_elem or not ast_elem.type) then
@ -351,7 +350,7 @@ local function unparse_ellipsis(elem, obj1, res, inner)
elseif type(obj1[2]) == 'string' then
local value = elem[obj1[1]]
if value == nil then return end
for k, v in ipairs(value) do
for _, v in ipairs(value) do
table.insert(res, tostring(v))
end
else
@ -359,7 +358,7 @@ local function unparse_ellipsis(elem, obj1, res, inner)
local data = elem[elem.type or 'data'] or elem
for _, elem2 in ipairs(data) do
local r = {}
for i, obj2 in ipairs(obj1) do
for _, obj2 in ipairs(obj1) do
if obj2[2] == '...' then
unparse_ellipsis(elem2, obj2[1], r, true)
elseif type(obj2[2]) == 'string' then
@ -421,7 +420,7 @@ local function unparse_elem(elem, res, force)
local err = unparse_elem(elem, res, true)
if err then return err end
for _, e in ipairs(elem) do
local err = unparse_elem(e, res)
err = unparse_elem(e, res)
if err then return err end
end
return unparse_elem({type=elem.type .. '_end'}, res, true)
@ -434,7 +433,7 @@ local function unparse_elem(elem, res, force)
local good, raw_elem
local possible_elems = {}
for i, template in ipairs(data) do
for _, template in ipairs(data) do
if type(template) == 'function' then
good, raw_elem = false, 'Unknown element.'
else
@ -520,7 +519,7 @@ function parse_mt:__index(key)
return func(obj or '')
end
else
return function(obj)
return function(_)
error('Unknown element type: ' .. tostring(key))
end
end

View File

@ -137,9 +137,9 @@ function formspec_ast.show_formspec(player, formname, formspec)
return 'No such player!'
end
local formspec, err = formspec_ast.interpret(formspec)
if formspec then
minetest.show_formspec(player, formname, formspec)
local new_fs, err = formspec_ast.interpret(formspec)
if new_fs then
minetest.show_formspec(player, formname, new_fs)
else
minetest.log('warning', 'formspec_ast.show_formspec(): ' ..
tostring(err))
@ -166,7 +166,7 @@ formspec_ast.register_element('formspec_ast:centered_label', function(raw,
y = 0,
w = parse.number(raw[2][1]),
h = parse.number(raw[2][2]),
texture_name = '',
texture_name = 'blank.png',
name = '',
label = parse.string(raw[3]),
noclip = true,
@ -193,7 +193,7 @@ formspec_ast.register_element('formspec_ast:centered_label', function(raw,
end)
-- Add a formspec element to crash clients
formspec_ast.register_element('formspec_ast:crash', function(raw, parse)
formspec_ast.register_element('formspec_ast:crash', function(_, _)
return {
type = 'list',
inventory_location = '___die',

View File

@ -186,8 +186,8 @@ function formspec_ast.safe_parse(tree, custom_handlers)
-- Iterate over the tree and add valid elements to a new table.
local res = {formspec_version = tree.formspec_version}
for i, elem in ipairs(tree) do
local good, msg = pcall(validate_elem, elem)
for _, elem in ipairs(tree) do
local good, _ = pcall(validate_elem, elem)
if good then
res[#res + 1] = elem
end