fix: type warnings in shared.lua

This commit is contained in:
Lewis Russell
2024-02-15 10:53:51 +00:00
committed by Lewis Russell
parent c6f7419420
commit 35f453f65d
5 changed files with 72 additions and 21 deletions

View File

@@ -893,6 +893,9 @@ vim.empty_dict() *vim.empty_dict()*
Note: If numeric keys are present in the table, Nvim ignores the metatable Note: If numeric keys are present in the table, Nvim ignores the metatable
marker and converts the dict to a list/array anyway. marker and converts the dict to a list/array anyway.
Return: ~
(`table`)
vim.iconv({str}, {from}, {to}, {opts}) *vim.iconv()* vim.iconv({str}, {from}, {to}, {opts}) *vim.iconv()*
The result is a String, which is the text {str} converted from encoding The result is a String, which is the text {str} converted from encoding
{from} to encoding {to}. When the conversion fails `nil` is returned. When {from} to encoding {to}. When the conversion fails `nil` is returned. When

View File

@@ -86,6 +86,7 @@ setmetatable(vim, {
--- <Docs described in |vim.empty_dict()| > --- <Docs described in |vim.empty_dict()| >
---@private ---@private
--- TODO: should be in vim.shared when vim.shared always uses nvim-lua --- TODO: should be in vim.shared when vim.shared always uses nvim-lua
--- @diagnostic disable-next-line:duplicate-set-field
function vim.empty_dict() function vim.empty_dict()
return setmetatable({}, vim._empty_dict_mt) return setmetatable({}, vim._empty_dict_mt)
end end

View File

@@ -83,6 +83,7 @@ function vim.in_fast_event() end
--- ---
--- Note: If numeric keys are present in the table, Nvim ignores the metatable --- Note: If numeric keys are present in the table, Nvim ignores the metatable
--- marker and converts the dict to a list/array anyway. --- marker and converts the dict to a list/array anyway.
--- @return table
function vim.empty_dict() end function vim.empty_dict() end
--- Sends {event} to {channel} via |RPC| and returns immediately. If {channel} --- Sends {event} to {channel} via |RPC| and returns immediately. If {channel}

View File

@@ -10,6 +10,7 @@
--- vim.fn[func]({...}) --- vim.fn[func]({...})
--- ``` --- ```
--- ---
--- @param func fun() --- @param func string
--- @param ... any --- @param ... any
--- @return any
function vim.call(func, ...) end function vim.call(func, ...) end

View File

@@ -63,6 +63,10 @@ function vim.deepcopy(orig, noref)
return deepcopy(orig, not noref and {} or nil) return deepcopy(orig, not noref and {} or nil)
end end
--- @class vim.gsplit.Opts
--- @field plain? boolean Use `sep` literally (as in string.find).
--- @field trimempty? boolean Discard empty segments at start and end of the sequence.
--- Gets an |iterator| that splits a string at each instance of a separator, in "lazy" fashion --- Gets an |iterator| that splits a string at each instance of a separator, in "lazy" fashion
--- (as opposed to |vim.split()| which is "eager"). --- (as opposed to |vim.split()| which is "eager").
--- ---
@@ -91,12 +95,12 @@ end
--- ---
--- @param s string String to split --- @param s string String to split
--- @param sep string Separator or pattern --- @param sep string Separator or pattern
--- @param opts (table|nil) Keyword arguments |kwargs|: --- @param opts? vim.gsplit.Opts (table) Keyword arguments |kwargs|:
--- - plain: (boolean) Use `sep` literally (as in string.find). --- - plain: (boolean) Use `sep` literally (as in string.find).
--- - trimempty: (boolean) Discard empty segments at start and end of the sequence. --- - trimempty: (boolean) Discard empty segments at start and end of the sequence.
---@return fun():string|nil (function) Iterator over the split components ---@return fun():string|nil (function) Iterator over the split components
function vim.gsplit(s, sep, opts) function vim.gsplit(s, sep, opts)
local plain local plain --- @type boolean?
local trimempty = false local trimempty = false
if type(opts) == 'boolean' then if type(opts) == 'boolean' then
plain = opts -- For backwards compatibility. plain = opts -- For backwards compatibility.
@@ -113,6 +117,11 @@ function vim.gsplit(s, sep, opts)
local segs = {} local segs = {}
local empty_start = true -- Only empty segments seen so far. local empty_start = true -- Only empty segments seen so far.
--- @param i integer?
--- @param j integer
--- @param ... unknown
--- @return string
--- @return ...
local function _pass(i, j, ...) local function _pass(i, j, ...)
if i then if i then
assert(j + 1 > start, 'Infinite loop detected') assert(j + 1 > start, 'Infinite loop detected')
@@ -201,10 +210,11 @@ end
---@param t table<T, any> (table) Table ---@param t table<T, any> (table) Table
---@return T[] (list) List of keys ---@return T[] (list) List of keys
function vim.tbl_keys(t) function vim.tbl_keys(t)
assert(type(t) == 'table', string.format('Expected table, got %s', type(t))) vim.validate({ t = { t, 't' } })
--- @cast t table<any,any>
local keys = {} local keys = {}
for k, _ in pairs(t) do for k in pairs(t) do
table.insert(keys, k) table.insert(keys, k)
end end
return keys return keys
@@ -217,10 +227,12 @@ end
---@param t table<any, T> (table) Table ---@param t table<any, T> (table) Table
---@return T[] (list) List of values ---@return T[] (list) List of values
function vim.tbl_values(t) function vim.tbl_values(t)
assert(type(t) == 'table', string.format('Expected table, got %s', type(t))) vim.validate({ t = { t, 't' } })
local values = {} local values = {}
for _, v in pairs(t) do for _, v in
pairs(t --[[@as table<any,any>]])
do
table.insert(values, v) table.insert(values, v)
end end
return values return values
@@ -234,8 +246,9 @@ end
---@return table Table of transformed values ---@return table Table of transformed values
function vim.tbl_map(func, t) function vim.tbl_map(func, t)
vim.validate({ func = { func, 'c' }, t = { t, 't' } }) vim.validate({ func = { func, 'c' }, t = { t, 't' } })
--- @cast t table<any,any>
local rettab = {} local rettab = {} --- @type table<any,any>
for k, v in pairs(t) do for k, v in pairs(t) do
rettab[k] = func(v) rettab[k] = func(v)
end end
@@ -250,16 +263,20 @@ end
---@return T[] (table) Table of filtered values ---@return T[] (table) Table of filtered values
function vim.tbl_filter(func, t) function vim.tbl_filter(func, t)
vim.validate({ func = { func, 'c' }, t = { t, 't' } }) vim.validate({ func = { func, 'c' }, t = { t, 't' } })
--- @cast t table<any,any>
local rettab = {} local rettab = {} --- @type table<any,any>
for _, entry in pairs(t) do for _, entry in pairs(t) do
if func(entry) then if func(entry) then
table.insert(rettab, entry) rettab[#rettab + 1] = entry
end end
end end
return rettab return rettab
end end
--- @class vim.tbl_contains.Opts
--- @field predicate? boolean
--- Checks if a table contains a given value, specified either directly or via --- Checks if a table contains a given value, specified either directly or via
--- a predicate that is checked for each value. --- a predicate that is checked for each value.
--- ---
@@ -276,13 +293,14 @@ end
--- ---
---@param t table Table to check ---@param t table Table to check
---@param value any Value to compare or predicate function reference ---@param value any Value to compare or predicate function reference
---@param opts (table|nil) Keyword arguments |kwargs|: ---@param opts? vim.tbl_contains.Opts (table) Keyword arguments |kwargs|:
--- - predicate: (boolean) `value` is a function reference to be checked (default false) --- - predicate: (boolean) `value` is a function reference to be checked (default false)
---@return boolean `true` if `t` contains `value` ---@return boolean `true` if `t` contains `value`
function vim.tbl_contains(t, value, opts) function vim.tbl_contains(t, value, opts)
vim.validate({ t = { t, 't' }, opts = { opts, 't', true } }) vim.validate({ t = { t, 't' }, opts = { opts, 't', true } })
--- @cast t table<any,any>
local pred local pred --- @type fun(v: any): boolean?
if opts and opts.predicate then if opts and opts.predicate then
vim.validate({ value = { value, 'c' } }) vim.validate({ value = { value, 'c' } })
pred = value pred = value
@@ -309,6 +327,7 @@ end
---@return boolean `true` if `t` contains `value` ---@return boolean `true` if `t` contains `value`
function vim.list_contains(t, value) function vim.list_contains(t, value)
vim.validate({ t = { t, 't' } }) vim.validate({ t = { t, 't' } })
--- @cast t table<any,any>
for _, v in ipairs(t) do for _, v in ipairs(t) do
if v == value then if v == value then
@@ -325,7 +344,7 @@ end
---@param t table Table to check ---@param t table Table to check
---@return boolean `true` if `t` is empty ---@return boolean `true` if `t` is empty
function vim.tbl_isempty(t) function vim.tbl_isempty(t)
assert(type(t) == 'table', string.format('Expected table, got %s', type(t))) vim.validate({ t = { t, 't' } })
return next(t) == nil return next(t) == nil
end end
@@ -347,7 +366,7 @@ local function tbl_extend(behavior, deep_extend, ...)
) )
end end
local ret = {} local ret = {} --- @type table<any,any>
if vim._empty_dict_mt ~= nil and getmetatable(select(1, ...)) == vim._empty_dict_mt then if vim._empty_dict_mt ~= nil and getmetatable(select(1, ...)) == vim._empty_dict_mt then
ret = vim.empty_dict() ret = vim.empty_dict()
end end
@@ -355,6 +374,7 @@ local function tbl_extend(behavior, deep_extend, ...)
for i = 1, select('#', ...) do for i = 1, select('#', ...) do
local tbl = select(i, ...) local tbl = select(i, ...)
vim.validate({ ['after the second argument'] = { tbl, 't' } }) vim.validate({ ['after the second argument'] = { tbl, 't' } })
--- @cast tbl table<any,any>
if tbl then if tbl then
for k, v in pairs(tbl) do for k, v in pairs(tbl) do
if deep_extend and can_merge(v) and can_merge(ret[k]) then if deep_extend and can_merge(v) and can_merge(ret[k]) then
@@ -417,12 +437,14 @@ function vim.deep_equal(a, b)
return false return false
end end
if type(a) == 'table' then if type(a) == 'table' then
--- @cast a table<any,any>
--- @cast b table<any,any>
for k, v in pairs(a) do for k, v in pairs(a) do
if not vim.deep_equal(v, b[k]) then if not vim.deep_equal(v, b[k]) then
return false return false
end end
end end
for k, _ in pairs(b) do for k in pairs(b) do
if a[k] == nil then if a[k] == nil then
return false return false
end end
@@ -440,6 +462,8 @@ end
---@param o table Table to add the reverse to ---@param o table Table to add the reverse to
---@return table o ---@return table o
function vim.tbl_add_reverse_lookup(o) function vim.tbl_add_reverse_lookup(o)
--- @cast o table<any,any>
--- @type any[]
local keys = vim.tbl_keys(o) local keys = vim.tbl_keys(o)
for _, k in ipairs(keys) do for _, k in ipairs(keys) do
local v = o[k] local v = o[k]
@@ -469,7 +493,6 @@ end
--- ---
---@param o table Table to index ---@param o table Table to index
---@param ... any Optional keys (0 or more, variadic) via which to index the table ---@param ... any Optional keys (0 or more, variadic) via which to index the table
---
---@return any Nested value indexed by key (if it exists), else nil ---@return any Nested value indexed by key (if it exists), else nil
function vim.tbl_get(o, ...) function vim.tbl_get(o, ...)
local keys = { ... } local keys = { ... }
@@ -477,7 +500,7 @@ function vim.tbl_get(o, ...)
return nil return nil
end end
for i, k in ipairs(keys) do for i, k in ipairs(keys) do
o = o[k] o = o[k] --- @type any
if o == nil then if o == nil then
return nil return nil
elseif type(o) ~= 'table' and next(keys, i) then elseif type(o) ~= 'table' and next(keys, i) then
@@ -521,6 +544,7 @@ end
---@return table Flattened copy of the given list-like table ---@return table Flattened copy of the given list-like table
function vim.tbl_flatten(t) function vim.tbl_flatten(t)
local result = {} local result = {}
--- @param _t table<any,any>
local function _tbl_flatten(_t) local function _tbl_flatten(_t)
local n = #_t local n = #_t
for i = 1, n do for i = 1, n do
@@ -543,7 +567,8 @@ end
---@param t table Dict-like table ---@param t table Dict-like table
---@return function # |for-in| iterator over sorted keys and their values ---@return function # |for-in| iterator over sorted keys and their values
function vim.spairs(t) function vim.spairs(t)
assert(type(t) == 'table', string.format('Expected table, got %s', type(t))) vim.validate({ t = { t, 't' } })
--- @cast t table<any,any>
-- collect the keys -- collect the keys
local keys = {} local keys = {}
@@ -578,6 +603,8 @@ function vim.tbl_isarray(t)
return false return false
end end
--- @cast t table<any,any>
local count = 0 local count = 0
for k, _ in pairs(t) do for k, _ in pairs(t) do
@@ -642,6 +669,7 @@ end
---@return integer Number of non-nil values in table ---@return integer Number of non-nil values in table
function vim.tbl_count(t) function vim.tbl_count(t)
vim.validate({ t = { t, 't' } }) vim.validate({ t = { t, 't' } })
--- @cast t table<any,any>
local count = 0 local count = 0
for _ in pairs(t) do for _ in pairs(t) do
@@ -658,7 +686,7 @@ end
---@param finish integer|nil End range of slice ---@param finish integer|nil End range of slice
---@return T[] (list) Copy of table sliced from start to finish (inclusive) ---@return T[] (list) Copy of table sliced from start to finish (inclusive)
function vim.list_slice(list, start, finish) function vim.list_slice(list, start, finish)
local new_list = {} local new_list = {} --- @type `T`[]
for i = start or 1, finish or #list do for i = start or 1, finish or #list do
new_list[#new_list + 1] = list[i] new_list[#new_list + 1] = list[i]
end end
@@ -707,6 +735,16 @@ function vim.endswith(s, suffix)
end end
do do
--- @alias vim.validate.Type
--- | 't' | 'table'
--- | 's' | 'string'
--- | 'n' | 'number'
--- | 'f' | 'function'
--- | 'c' | 'callable'
--- | 'nil'
--- | 'thread'
--- | 'userdata
local type_names = { local type_names = {
['table'] = 'table', ['table'] = 'table',
t = 'table', t = 'table',
@@ -725,10 +763,17 @@ do
['userdata'] = 'userdata', ['userdata'] = 'userdata',
} }
--- @class vim.validate.Spec {[1]: any, [2]: string|string[], [3]: boolean }
--- @field [1] any Argument value
--- @field [2] string|string[]|fun(v:any):boolean, string? Type name, or callable
--- @field [3]? boolean
local function _is_type(val, t) local function _is_type(val, t)
return type(val) == t or (t == 'callable' and vim.is_callable(val)) return type(val) == t or (t == 'callable' and vim.is_callable(val))
end end
--- @param opt table<vim.validate.Type,vim.validate.Spec>
--- @return boolean, string?
local function is_valid(opt) local function is_valid(opt)
if type(opt) ~= 'table' then if type(opt) ~= 'table' then
return false, string.format('opt: expected table, got %s', type(opt)) return false, string.format('opt: expected table, got %s', type(opt))
@@ -787,7 +832,7 @@ do
end end
end end
return true, nil return true
end end
--- Validates a parameter specification (types and values). --- Validates a parameter specification (types and values).
@@ -829,7 +874,7 @@ do
--- ---
--- ``` --- ```
--- ---
---@param opt table Names of parameters to validate. Each key is a parameter ---@param opt table<vim.validate.Type,vim.validate.Spec> (table) Names of parameters to validate. Each key is a parameter
--- name; each value is a tuple in one of these forms: --- name; each value is a tuple in one of these forms:
--- 1. (arg_value, type_name, optional) --- 1. (arg_value, type_name, optional)
--- - arg_value: argument value --- - arg_value: argument value