Skip to content

Basic partial class support #3024

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@
```
* `NEW` Test CLI: `--name=<testname>` `-n=<testname>`: run specify unit test
* `FIX` Fixed the error that the configuration file pointed to by the `--configpath` option was not read and loaded.
* `NEW` `---@class` supports attribute `partial`, which will not check missing inherited fields [#3023](https://github.com/LuaLS/lua-language-server/issues/3023)
```lua
---@class Config
---@field a number

---@class (partial) Config.P: Config
---@field b number

---@type Config.P[]
local cfgs = {}
cfgs[1] = { b = 1 } -- no warning
cfgs[2] = {} -- only warns missing `b`
```
This enables the previous missing field check behavior before [#2970](https://github.com/LuaLS/lua-language-server/issues/2970)

## 3.13.5
`2024-12-20`
Expand Down
22 changes: 20 additions & 2 deletions script/core/diagnostics/missing-fields.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ return function (uri, callback)
local className = def.class[1]
if not sortedDefs[className] then
sortedDefs[className] = {}
-- check if this class is a `partial` class
-- a partial class will not check missing inherited fields
local class = vm.getGlobal('type', className)
---@cast class -nil
for _, set in ipairs(class:getSets(uri)) do
if set.type == 'doc.class'
and vm.docHasAttr(set, 'partial')
then
sortedDefs[className].isPartial = true
break
end
end
end
local samedefs = sortedDefs[className]
samedefs[#samedefs+1] = def
Expand All @@ -41,8 +53,8 @@ return function (uri, callback)
for className, samedefs in pairs(sortedDefs) do
local missedKeys = {}
for _, def in ipairs(samedefs) do
local fields = vm.getFields(def)
if #fields == 0 then
local fields = samedefs.isPartial and def.fields or vm.getFields(def)
if not fields or #fields == 0 then
goto continue
end

Expand Down Expand Up @@ -78,6 +90,12 @@ return function (uri, callback)
end
end
::continue::

if not samedefs.isPartial then
-- if not partial class, then all fields in this class have already been checked
-- because in the above uses `vm.getFields` to get all fields
break
end
end

if #missedKeys == 0 then
Expand Down
34 changes: 34 additions & 0 deletions test/diagnostics/missing-fields.lua
Original file line number Diff line number Diff line change
Expand Up @@ -462,4 +462,38 @@ local function f(b) end
f <!{y = 1}!>
]]

-- partial class

TEST[[
---@class A
---@field x number

---@class (partial) B: A

---@type B
local t = {}
]]

TEST[[
---@class A
---@field x number

---@class (partial) B: A
---@field y number

---@type B
local t = <!{}!>
]]

TEST[[
---@class A
---@field x number

---@class (partial) B: A
---@field y number

---@type B
local t = {y = 1}
]]

--
Loading