Add luacheck + fix warnings detected by it

This commit is contained in:
tour 2024-09-14 20:06:27 +02:00
parent 2a90c58c50
commit 9c723301ec
5 changed files with 10 additions and 9 deletions

4
.luacheckrc Normal file
View File

@ -0,0 +1,4 @@
std = "lua51+luajit+minetest"
globals = {"test_string_to_table"}
read_globals = {"DIR_DELIM"}

View File

@ -1,9 +1,8 @@
local function stringToTable(str) local function stringToTable(str)
local parseTable -- defined later, but we need it in parseValue local parseTable -- defined later, but we need it in parseValue
local function parseValue(s, i) local function parseValue(s, idx)
s = s:match("^%s*(.-)%s*$", i) -- Trim leading/trailing spaces s = s:match("^%s*(.-)%s*$", idx) -- Trim leading/trailing spaces
i = 1
if s:sub(1, 1) == "{" then if s:sub(1, 1) == "{" then
return parseTable(s) return parseTable(s)
@ -47,7 +46,6 @@ local function stringToTable(str)
local function parseKey(s, i) local function parseKey(s, i)
s = s:match("^%s*(.-)%s*$", i) -- Trim leading/trailing spaces s = s:match("^%s*(.-)%s*$", i) -- Trim leading/trailing spaces
i = 1
if s:find("^[%a_]") then if s:find("^[%a_]") then
local key = s:match("^[%a%d_]*") local key = s:match("^[%a%d_]*")
if key ~= "true" and key ~= "false" and key ~= "nil" then if key ~= "true" and key ~= "false" and key ~= "nil" then

View File

@ -55,7 +55,6 @@ local function codedStringToTable(str)
local tbl = {} local tbl = {}
local key, value local key, value
local next_key = 1 local next_key = 1
local inKey = true
local depth = 0 local depth = 0
local buffer = "" local buffer = ""
@ -70,7 +69,7 @@ local function codedStringToTable(str)
for i = 1, #str do for i = 1, #str do
local char = str:sub(i, i) local char = str:sub(i, i)
if char == '{' then if char == '{' then
if depth > 0 then if depth > 0 then
buffer = buffer .. char buffer = buffer .. char

View File

@ -45,7 +45,7 @@ local function parse_key(str)
if not success then if not success then
return false, key return false, key
end end
if key == nil then if key == nil then
return false, "nil cannot be a key" return false, "nil cannot be a key"
end end
return true, key return true, key
@ -95,7 +95,7 @@ local function stringToTable(str)
local success = true local success = true
local tbl local tbl
local replacements = 0 local replacements
local table_count = 0 local table_count = 0
repeat repeat
str, replacements = str:gsub("({[^{}]*})", function (match) str, replacements = str:gsub("({[^{}]*})", function (match)

View File

@ -18,7 +18,7 @@ local tests = {
"{key1 = 123, key2 = true, key3 = {nestedKey1 = 'value1', nestedKey2 = false}}", "{key1 = 123, key2 = true, key3 = {nestedKey1 = 'value1', nestedKey2 = false}}",
"{}", "{ }", "{}", "{ }",
"{[{'tables can be'}] = 'keys too'}", "{[{'tables can be'}] = 'keys too'}",
'{\'{table = "that", "contains", a = "string", ["looking"] = {"like", "a"; "table"}}\'}', '{\'{table = "that", "contains", a = "string", ["looking"] = {"like", "a"; "table"}}\'}',
'{[-42.42e42] = 42.42E-42}', '{[-42.42e42] = 42.42E-42}',
'{1, 2, 3, }', '{1, 2, 3, }',
-- invalid tables, the code should return an error -- invalid tables, the code should return an error