From 3d29424fb9960085f09f7e322abf204eab9287b9 Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Mon, 3 Apr 2023 12:01:23 +0100 Subject: refactor(unit): add type annotations --- test/unit/set.lua | 44 +++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) (limited to 'test/unit/set.lua') diff --git a/test/unit/set.lua b/test/unit/set.lua index f3d68c3042..91b7e2dd70 100644 --- a/test/unit/set.lua +++ b/test/unit/set.lua @@ -4,10 +4,15 @@ -- other: -- 1) index => item -- 2) item => index +--- @class Set +--- @field nelem integer +--- @field items string[] +--- @field tbl table local Set = {} +--- @param items? string[] function Set:new(items) - local obj = {} + local obj = {} --- @ type Set setmetatable(obj, self) self.__index = self @@ -26,8 +31,9 @@ function Set:new(items) return obj end +--- @return Set function Set:copy() - local obj = {} + local obj = {} --- @ type Set obj.nelem = self.nelem obj.tbl = {} obj.items = {} @@ -43,6 +49,7 @@ function Set:copy() end -- adds the argument Set to this Set +--- @param other Set function Set:union(other) for e in other:iterator() do self:add(e) @@ -57,6 +64,7 @@ function Set:union_table(t) end -- subtracts the argument Set from this Set +--- @param other Set function Set:diff(other) if other:size() > self:size() then -- this set is smaller than the other set @@ -75,6 +83,7 @@ function Set:diff(other) end end +--- @param it string function Set:add(it) if not self:contains(it) then local idx = #self.tbl + 1 @@ -84,6 +93,7 @@ function Set:add(it) end end +--- @param it string function Set:remove(it) if self:contains(it) then local idx = self.items[it] @@ -93,10 +103,13 @@ function Set:remove(it) end end +--- @param it string +--- @return boolean function Set:contains(it) return self.items[it] or false end +--- @return integer function Set:size() return self.nelem end @@ -113,29 +126,18 @@ function Set:iterator() return pairs(self.items) end +--- @return string[] function Set:to_table() -- there might be gaps in @tbl, so we have to be careful and sort first - local keys - do - local _accum_0 = { } - local _len_0 = 1 - for idx, _ in pairs(self.tbl) do - _accum_0[_len_0] = idx - _len_0 = _len_0 + 1 - end - keys = _accum_0 + local keys = {} --- @type string[] + for idx, _ in pairs(self.tbl) do + keys[#keys+1] = idx end + table.sort(keys) - local copy - do - local _accum_0 = { } - local _len_0 = 1 - for _index_0 = 1, #keys do - local idx = keys[_index_0] - _accum_0[_len_0] = self.tbl[idx] - _len_0 = _len_0 + 1 - end - copy = _accum_0 + local copy = {} --- @type string[] + for _, idx in ipairs(keys) do + copy[#copy+1] = self.tbl[idx] end return copy end -- cgit From c43c745a14dced87a23227d7be4f1c33d4455193 Mon Sep 17 00:00:00 2001 From: Christian Clason Date: Wed, 9 Aug 2023 11:06:13 +0200 Subject: fix(lua): improve annotations for stricter luals diagnostics (#24609) Problem: luals returns stricter diagnostics with bundled luarc.json Solution: Improve some function and type annotations: * use recognized uv.* types * disable diagnostic for global `vim` in shared.lua * docs: don't start comment lines with taglink (otherwise LuaLS will interpret it as a type) * add type alias for lpeg pattern * fix return annotation for `vim.secure.trust` * rename local Range object in vim.version (shadows `Range` in vim.treesitter) * fix some "missing fields" warnings * add missing required fields for test functions in eval.lua * rename lsp meta files for consistency --- test/unit/set.lua | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) (limited to 'test/unit/set.lua') diff --git a/test/unit/set.lua b/test/unit/set.lua index 91b7e2dd70..7c30be32aa 100644 --- a/test/unit/set.lua +++ b/test/unit/set.lua @@ -12,7 +12,7 @@ local Set = {} --- @param items? string[] function Set:new(items) - local obj = {} --- @ type Set + local obj = {} --- @type Set setmetatable(obj, self) self.__index = self @@ -33,10 +33,7 @@ end --- @return Set function Set:copy() - local obj = {} --- @ type Set - obj.nelem = self.nelem - obj.tbl = {} - obj.items = {} + local obj = {nelem = self.nelem, tbl = {}, items = {}} --- @type Set for k, v in pairs(self.tbl) do obj.tbl[k] = v end -- cgit