build: enable lintlua for test/unit/ dir #26396

Problem:
Not all Lua code is checked by stylua. Automating code-style is an
important mechanism for reducing time spent on accidental
(non-essential) complexity.

Solution:
- Enable lintlua for `test/unit/` directory.
- TODO: only `test/functional/` remains unchecked.

previous: 45fe4d11ad
previous: 517f0cc634
This commit is contained in:
Justin M. Keyes
2023-12-04 14:32:39 -08:00
committed by GitHub
parent 45fe4d11ad
commit c3836e40a2
51 changed files with 4067 additions and 2764 deletions

View File

@@ -6,21 +6,25 @@ local to_cstr = helpers.to_cstr
local ffi = helpers.ffi
local eq = helpers.eq
local eval = cimport('./src/nvim/eval.h', './src/nvim/eval/typval.h',
'./src/nvim/hashtab.h', './src/nvim/memory.h')
local eval = cimport(
'./src/nvim/eval.h',
'./src/nvim/eval/typval.h',
'./src/nvim/hashtab.h',
'./src/nvim/memory.h'
)
local null_string = {[true]='NULL string'}
local null_list = {[true]='NULL list'}
local null_dict = {[true]='NULL dict'}
local type_key = {[true]='type key'}
local locks_key = {[true]='locks key'}
local list_type = {[true]='list type'}
local dict_type = {[true]='dict type'}
local func_type = {[true]='func type'}
local int_type = {[true]='int type'}
local flt_type = {[true]='flt type'}
local null_string = { [true] = 'NULL string' }
local null_list = { [true] = 'NULL list' }
local null_dict = { [true] = 'NULL dict' }
local type_key = { [true] = 'type key' }
local locks_key = { [true] = 'locks key' }
local list_type = { [true] = 'list type' }
local dict_type = { [true] = 'dict type' }
local func_type = { [true] = 'func type' }
local int_type = { [true] = 'int type' }
local flt_type = { [true] = 'flt type' }
local nil_value = {[true]='nil'}
local nil_value = { [true] = 'nil' }
local lua2typvalt
@@ -35,11 +39,13 @@ end
local function li_alloc(nogc)
local gcfunc = tv_list_item_free
if nogc then gcfunc = nil end
if nogc then
gcfunc = nil
end
local li = ffi.gc(tv_list_item_alloc(), gcfunc)
li.li_next = nil
li.li_prev = nil
li.li_tv = {v_type=eval.VAR_UNKNOWN, v_lock=eval.VAR_UNLOCKED}
li.li_tv = { v_type = eval.VAR_UNKNOWN, v_lock = eval.VAR_UNLOCKED }
return li
end
@@ -121,11 +127,11 @@ local function partial2lua(pt, processed)
end
end
return {
[type_key]=func_type,
value=value,
auto=auto,
args=argv,
dict=dict,
[type_key] = func_type,
value = value,
auto = auto,
args = argv,
dict = dict,
}
end
@@ -148,7 +154,7 @@ local function typvalt2lua_tab_init()
})[tonumber(t.vval.v_special)]
end,
[tonumber(eval.VAR_NUMBER)] = function(t)
return {[type_key]=int_type, value=tonumber(t.vval.v_number)}
return { [type_key] = int_type, value = tonumber(t.vval.v_number) }
end,
[tonumber(eval.VAR_FLOAT)] = function(t)
return tonumber(t.vval.v_float)
@@ -168,7 +174,7 @@ local function typvalt2lua_tab_init()
return dct2tbl(t.vval.v_dict, processed)
end,
[tonumber(eval.VAR_FUNC)] = function(t, processed)
return {[type_key]=func_type, value=typvalt2lua_tab[eval.VAR_STRING](t, processed or {})}
return { [type_key] = func_type, value = typvalt2lua_tab[eval.VAR_STRING](t, processed or {}) }
end,
[tonumber(eval.VAR_PARTIAL)] = function(t, processed)
local p_key = ptr2key(t)
@@ -182,15 +188,17 @@ end
typvalt2lua = function(t, processed)
typvalt2lua_tab_init()
return ((typvalt2lua_tab[tonumber(t.v_type)] or function(t_inner)
assert(false, 'Converting ' .. tonumber(t_inner.v_type) .. ' was not implemented yet')
end)(t, processed or {}))
return (
(typvalt2lua_tab[tonumber(t.v_type)] or function(t_inner)
assert(false, 'Converting ' .. tonumber(t_inner.v_type) .. ' was not implemented yet')
end)(t, processed or {})
)
end
local function list_iter(l)
local init_s = {
idx=0,
li=l.lv_first,
idx = 0,
li = l.lv_first,
}
local function f(s, _)
-- (listitem_T *) NULL is equal to nil, but yet it is not false.
@@ -222,7 +230,7 @@ lst2tbl = function(l, processed)
if processed[p_key] then
return processed[p_key]
end
local ret = {[type_key]=list_type}
local ret = { [type_key] = list_type }
processed[p_key] = ret
for i, li in list_iter(l) do
ret[i] = typvalt2lua(li.li_tv, processed)
@@ -238,11 +246,13 @@ local hi_key_removed = nil
local function dict_iter(d, return_hi)
hi_key_removed = hi_key_removed or eval._hash_key_removed()
local init_s = {
todo=d.dv_hashtab.ht_used,
hi=d.dv_hashtab.ht_array,
todo = d.dv_hashtab.ht_used,
hi = d.dv_hashtab.ht_array,
}
local function f(s, _)
if s.todo == 0 then return nil end
if s.todo == 0 then
return nil
end
while s.todo > 0 do
if s.hi.hi_key ~= nil and s.hi.hi_key ~= hi_key_removed then
local key = ffi.string(s.hi.hi_key)
@@ -250,8 +260,7 @@ local function dict_iter(d, return_hi)
if return_hi then
ret = s.hi
else
ret = ffi.cast('dictitem_T*',
s.hi.hi_key - ffi.offsetof('dictitem_T', 'di_key'))
ret = ffi.cast('dictitem_T*', s.hi.hi_key - ffi.offsetof('dictitem_T', 'di_key'))
end
s.todo = s.todo - 1
s.hi = s.hi + 1
@@ -269,7 +278,7 @@ local function first_di(d)
end
local function dict_items(d)
local ret = {[0]=0}
local ret = { [0] = 0 }
for k, hi in dict_iter(d) do
ret[k] = hi
ret[0] = ret[0] + 1
@@ -301,12 +310,12 @@ local typvalt = function(typ, vval)
elseif type(typ) == 'string' then
typ = eval[typ]
end
return ffi.gc(ffi.new('typval_T', {v_type=typ, vval=vval}), eval.tv_clear)
return ffi.gc(ffi.new('typval_T', { v_type = typ, vval = vval }), eval.tv_clear)
end
local lua2typvalt_type_tab = {
[int_type] = function(l, _)
return typvalt(eval.VAR_NUMBER, {v_number=l.value})
return typvalt(eval.VAR_NUMBER, { v_number = l.value })
end,
[flt_type] = function(l, processed)
return lua2typvalt(l.value, processed)
@@ -314,31 +323,34 @@ local lua2typvalt_type_tab = {
[list_type] = function(l, processed)
if processed[l] then
processed[l].lv_refcount = processed[l].lv_refcount + 1
return typvalt(eval.VAR_LIST, {v_list=processed[l]})
return typvalt(eval.VAR_LIST, { v_list = processed[l] })
end
local lst = populate_list(eval.tv_list_alloc(#l), l, processed)
return typvalt(eval.VAR_LIST, {v_list=lst})
return typvalt(eval.VAR_LIST, { v_list = lst })
end,
[dict_type] = function(l, processed)
if processed[l] then
processed[l].dv_refcount = processed[l].dv_refcount + 1
return typvalt(eval.VAR_DICT, {v_dict=processed[l]})
return typvalt(eval.VAR_DICT, { v_dict = processed[l] })
end
local dct = populate_dict(eval.tv_dict_alloc(), l, processed)
return typvalt(eval.VAR_DICT, {v_dict=dct})
return typvalt(eval.VAR_DICT, { v_dict = dct })
end,
[func_type] = function(l, processed)
if processed[l] then
processed[l].pt_refcount = processed[l].pt_refcount + 1
return typvalt(eval.VAR_PARTIAL, {v_partial=processed[l]})
return typvalt(eval.VAR_PARTIAL, { v_partial = processed[l] })
end
if l.args or l.dict then
local pt = populate_partial(ffi.gc(ffi.cast('partial_T*',
eval.xcalloc(1, ffi.sizeof('partial_T'))), nil), l, processed)
return typvalt(eval.VAR_PARTIAL, {v_partial=pt})
local pt = populate_partial(
ffi.gc(ffi.cast('partial_T*', eval.xcalloc(1, ffi.sizeof('partial_T'))), nil),
l,
processed
)
return typvalt(eval.VAR_PARTIAL, { v_partial = pt })
else
return typvalt(eval.VAR_FUNC, {
v_string=eval.xmemdupz(to_cstr(l.value), #l.value)
v_string = eval.xmemdupz(to_cstr(l.value), #l.value),
})
end
end,
@@ -349,12 +361,12 @@ local special_vals = nil
lua2typvalt = function(l, processed)
if not special_vals then
special_vals = {
[null_string] = {'VAR_STRING', {v_string=ffi.cast('char*', nil)}},
[null_list] = {'VAR_LIST', {v_list=ffi.cast('list_T*', nil)}},
[null_dict] = {'VAR_DICT', {v_dict=ffi.cast('dict_T*', nil)}},
[nil_value] = {'VAR_SPECIAL', {v_special=eval.kSpecialVarNull}},
[true] = {'VAR_BOOL', {v_bool=eval.kBoolVarTrue}},
[false] = {'VAR_BOOL', {v_bool=eval.kBoolVarFalse}},
[null_string] = { 'VAR_STRING', { v_string = ffi.cast('char*', nil) } },
[null_list] = { 'VAR_LIST', { v_list = ffi.cast('list_T*', nil) } },
[null_dict] = { 'VAR_DICT', { v_dict = ffi.cast('dict_T*', nil) } },
[nil_value] = { 'VAR_SPECIAL', { v_special = eval.kSpecialVarNull } },
[true] = { 'VAR_BOOL', { v_bool = eval.kBoolVarTrue } },
[false] = { 'VAR_BOOL', { v_bool = eval.kBoolVarFalse } },
}
for k, v in pairs(special_vals) do
@@ -382,9 +394,9 @@ lua2typvalt = function(l, processed)
end
end
elseif type(l) == 'number' then
return typvalt(eval.VAR_FLOAT, {v_float=l})
return typvalt(eval.VAR_FLOAT, { v_float = l })
elseif type(l) == 'string' then
return typvalt(eval.VAR_STRING, {v_string=eval.xmemdupz(to_cstr(l), #l)})
return typvalt(eval.VAR_STRING, { v_string = eval.xmemdupz(to_cstr(l), #l) })
elseif type(l) == 'cdata' then
local tv = typvalt(eval.VAR_UNKNOWN)
eval.tv_copy(l, tv)
@@ -408,43 +420,64 @@ local function alloc_len(len, get_ptr)
end
local alloc_logging_helpers = {
list = function(l) return {func='calloc', args={1, ffi.sizeof('list_T')}, ret=void(l)} end,
li = function(li) return {func='malloc', args={ffi.sizeof('listitem_T')}, ret=void(li)} end,
dict = function(d) return {func='calloc', args={1, ffi.sizeof('dict_T')}, ret=void(d)} end,
list = function(l)
return { func = 'calloc', args = { 1, ffi.sizeof('list_T') }, ret = void(l) }
end,
li = function(li)
return { func = 'malloc', args = { ffi.sizeof('listitem_T') }, ret = void(li) }
end,
dict = function(d)
return { func = 'calloc', args = { 1, ffi.sizeof('dict_T') }, ret = void(d) }
end,
di = function(di, size)
size = alloc_len(size, function() return di.di_key end)
return {func='malloc', args={ffi.offsetof('dictitem_T', 'di_key') + size + 1}, ret=void(di)}
size = alloc_len(size, function()
return di.di_key
end)
return {
func = 'malloc',
args = { ffi.offsetof('dictitem_T', 'di_key') + size + 1 },
ret = void(di),
}
end,
str = function(s, size)
size = alloc_len(size, function() return s end)
return {func='malloc', args={size + 1}, ret=void(s)}
size = alloc_len(size, function()
return s
end)
return { func = 'malloc', args = { size + 1 }, ret = void(s) }
end,
dwatcher = function(w) return {func='malloc', args={ffi.sizeof('DictWatcher')}, ret=void(w)} end,
dwatcher = function(w)
return { func = 'malloc', args = { ffi.sizeof('DictWatcher') }, ret = void(w) }
end,
freed = function(p) return {func='free', args={type(p) == 'table' and p or void(p)}} end,
freed = function(p)
return { func = 'free', args = { type(p) == 'table' and p or void(p) } }
end,
-- lua_…: allocated by this file, not by some Neovim function
lua_pt = function(pt) return {func='calloc', args={1, ffi.sizeof('partial_T')}, ret=void(pt)} end,
lua_pt = function(pt)
return { func = 'calloc', args = { 1, ffi.sizeof('partial_T') }, ret = void(pt) }
end,
lua_tvs = function(argv, argc)
argc = alloc_len(argc)
return {func='malloc', args={ffi.sizeof('typval_T')*argc}, ret=void(argv)}
return { func = 'malloc', args = { ffi.sizeof('typval_T') * argc }, ret = void(argv) }
end,
}
local function int(n)
return {[type_key]=int_type, value=n}
return { [type_key] = int_type, value = n }
end
local function list(...)
return populate_list(ffi.gc(eval.tv_list_alloc(select('#', ...)),
eval.tv_list_unref),
{...}, {})
return populate_list(
ffi.gc(eval.tv_list_alloc(select('#', ...)), eval.tv_list_unref),
{ ... },
{}
)
end
local function dict(d)
return populate_dict(ffi.gc(eval.tv_dict_alloc(), eval.tv_dict_free),
d or {}, {})
return populate_dict(ffi.gc(eval.tv_dict_alloc(), eval.tv_dict_free), d or {}, {})
end
local callback2tbl_type_tab = nil
@@ -454,14 +487,16 @@ local function init_callback2tbl_type_tab()
return
end
callback2tbl_type_tab = {
[tonumber(eval.kCallbackNone)] = function(_) return {type='none'} end,
[tonumber(eval.kCallbackNone)] = function(_)
return { type = 'none' }
end,
[tonumber(eval.kCallbackFuncref)] = function(cb)
return {type='fref', fref=ffi.string(cb.data.funcref)}
return { type = 'fref', fref = ffi.string(cb.data.funcref) }
end,
[tonumber(eval.kCallbackPartial)] = function(cb)
local lua_pt = partial2lua(cb.data.partial)
return {type='pt', fref=ffi.string(lua_pt.value), pt=lua_pt}
end
return { type = 'pt', fref = ffi.string(lua_pt.value), pt = lua_pt }
end,
}
end
@@ -473,15 +508,18 @@ end
local function tbl2callback(tbl)
local ret = nil
if tbl.type == 'none' then
ret = ffi.new('Callback[1]', {{type=eval.kCallbackNone}})
ret = ffi.new('Callback[1]', { { type = eval.kCallbackNone } })
elseif tbl.type == 'fref' then
ret = ffi.new('Callback[1]', {{type=eval.kCallbackFuncref,
data={funcref=eval.xstrdup(tbl.fref)}}})
ret = ffi.new(
'Callback[1]',
{ { type = eval.kCallbackFuncref, data = { funcref = eval.xstrdup(tbl.fref) } } }
)
elseif tbl.type == 'pt' then
local pt = ffi.gc(ffi.cast('partial_T*',
eval.xcalloc(1, ffi.sizeof('partial_T'))), nil)
ret = ffi.new('Callback[1]', {{type=eval.kCallbackPartial,
data={partial=populate_partial(pt, tbl.pt, {})}}})
local pt = ffi.gc(ffi.cast('partial_T*', eval.xcalloc(1, ffi.sizeof('partial_T'))), nil)
ret = ffi.new(
'Callback[1]',
{ { type = eval.kCallbackPartial, data = { partial = populate_partial(pt, tbl.pt, {}) } } }
)
else
assert(false)
end
@@ -495,24 +533,23 @@ local function dict_watchers(d)
local qs = {}
local key_patterns = {}
while q ~= h do
local qitem = ffi.cast('DictWatcher *',
ffi.cast('char *', q) - ffi.offsetof('DictWatcher', 'node'))
local qitem =
ffi.cast('DictWatcher *', ffi.cast('char *', q) - ffi.offsetof('DictWatcher', 'node'))
ret[#ret + 1] = {
cb=callback2tbl(qitem.callback),
pat=ffi.string(qitem.key_pattern, qitem.key_pattern_len),
busy=qitem.busy,
cb = callback2tbl(qitem.callback),
pat = ffi.string(qitem.key_pattern, qitem.key_pattern_len),
busy = qitem.busy,
}
qs[#qs + 1] = qitem
key_patterns[#key_patterns + 1] = {qitem.key_pattern, qitem.key_pattern_len}
key_patterns[#key_patterns + 1] = { qitem.key_pattern, qitem.key_pattern_len }
q = q.next
end
return ret, qs, key_patterns
end
local function eval0(expr)
local tv = ffi.gc(ffi.new('typval_T', {v_type=eval.VAR_UNKNOWN}),
eval.tv_clear)
local evalarg = ffi.new('evalarg_T', {eval_flags = eval.EVAL_EVALUATE})
local tv = ffi.gc(ffi.new('typval_T', { v_type = eval.VAR_UNKNOWN }), eval.tv_clear)
local evalarg = ffi.new('evalarg_T', { eval_flags = eval.EVAL_EVALUATE })
if eval.eval0(to_cstr(expr), tv, nil, evalarg) == 0 then
return nil
else
@@ -521,49 +558,49 @@ local function eval0(expr)
end
return {
int=int,
int = int,
null_string=null_string,
null_list=null_list,
null_dict=null_dict,
list_type=list_type,
dict_type=dict_type,
func_type=func_type,
int_type=int_type,
flt_type=flt_type,
null_string = null_string,
null_list = null_list,
null_dict = null_dict,
list_type = list_type,
dict_type = dict_type,
func_type = func_type,
int_type = int_type,
flt_type = flt_type,
nil_value=nil_value,
nil_value = nil_value,
type_key=type_key,
locks_key=locks_key,
type_key = type_key,
locks_key = locks_key,
list=list,
dict=dict,
lst2tbl=lst2tbl,
dct2tbl=dct2tbl,
list = list,
dict = dict,
lst2tbl = lst2tbl,
dct2tbl = dct2tbl,
lua2typvalt=lua2typvalt,
typvalt2lua=typvalt2lua,
lua2typvalt = lua2typvalt,
typvalt2lua = typvalt2lua,
typvalt=typvalt,
typvalt = typvalt,
li_alloc=li_alloc,
tv_list_item_free=tv_list_item_free,
li_alloc = li_alloc,
tv_list_item_free = tv_list_item_free,
dict_iter=dict_iter,
list_iter=list_iter,
first_di=first_di,
dict_iter = dict_iter,
list_iter = list_iter,
first_di = first_di,
alloc_logging_helpers=alloc_logging_helpers,
alloc_logging_helpers = alloc_logging_helpers,
list_items=list_items,
dict_items=dict_items,
list_items = list_items,
dict_items = dict_items,
dict_watchers=dict_watchers,
tbl2callback=tbl2callback,
callback2tbl=callback2tbl,
dict_watchers = dict_watchers,
tbl2callback = tbl2callback,
callback2tbl = callback2tbl,
eval0=eval0,
eval0 = eval0,
empty_list = {[type_key]=list_type},
empty_list = { [type_key] = list_type },
}