Skip to content

Commit 4396730

Browse files
authored
Merge pull request #3024 from tomlau10/feat/basic_partial_class
Basic `partial` class support
2 parents 389744b + 2e06d03 commit 4396730

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed

changelog.md

+14
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,20 @@
1313
```
1414
* `NEW` Test CLI: `--name=<testname>` `-n=<testname>`: run specify unit test
1515
* `FIX` Fixed the error that the configuration file pointed to by the `--configpath` option was not read and loaded.
16+
* `NEW` `---@class` supports attribute `partial`, which will not check missing inherited fields [#3023](https://github.com/LuaLS/lua-language-server/issues/3023)
17+
```lua
18+
---@class Config
19+
---@field a number
20+
21+
---@class (partial) Config.P: Config
22+
---@field b number
23+
24+
---@type Config.P[]
25+
local cfgs = {}
26+
cfgs[1] = { b = 1 } -- no warning
27+
cfgs[2] = {} -- only warns missing `b`
28+
```
29+
This enables the previous missing field check behavior before [#2970](https://github.com/LuaLS/lua-language-server/issues/2970)
1630

1731
## 3.13.5
1832
`2024-12-20`

script/core/diagnostics/missing-fields.lua

+20-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@ return function (uri, callback)
2626
local className = def.class[1]
2727
if not sortedDefs[className] then
2828
sortedDefs[className] = {}
29+
-- check if this class is a `partial` class
30+
-- a partial class will not check missing inherited fields
31+
local class = vm.getGlobal('type', className)
32+
---@cast class -nil
33+
for _, set in ipairs(class:getSets(uri)) do
34+
if set.type == 'doc.class'
35+
and vm.docHasAttr(set, 'partial')
36+
then
37+
sortedDefs[className].isPartial = true
38+
break
39+
end
40+
end
2941
end
3042
local samedefs = sortedDefs[className]
3143
samedefs[#samedefs+1] = def
@@ -41,8 +53,8 @@ return function (uri, callback)
4153
for className, samedefs in pairs(sortedDefs) do
4254
local missedKeys = {}
4355
for _, def in ipairs(samedefs) do
44-
local fields = vm.getFields(def)
45-
if #fields == 0 then
56+
local fields = samedefs.isPartial and def.fields or vm.getFields(def)
57+
if not fields or #fields == 0 then
4658
goto continue
4759
end
4860

@@ -78,6 +90,12 @@ return function (uri, callback)
7890
end
7991
end
8092
::continue::
93+
94+
if not samedefs.isPartial then
95+
-- if not partial class, then all fields in this class have already been checked
96+
-- because in the above uses `vm.getFields` to get all fields
97+
break
98+
end
8199
end
82100

83101
if #missedKeys == 0 then

test/diagnostics/missing-fields.lua

+34
Original file line numberDiff line numberDiff line change
@@ -462,4 +462,38 @@ local function f(b) end
462462
f <!{y = 1}!>
463463
]]
464464

465+
-- partial class
466+
467+
TEST[[
468+
---@class A
469+
---@field x number
470+
471+
---@class (partial) B: A
472+
473+
---@type B
474+
local t = {}
475+
]]
476+
477+
TEST[[
478+
---@class A
479+
---@field x number
480+
481+
---@class (partial) B: A
482+
---@field y number
483+
484+
---@type B
485+
local t = <!{}!>
486+
]]
487+
488+
TEST[[
489+
---@class A
490+
---@field x number
491+
492+
---@class (partial) B: A
493+
---@field y number
494+
495+
---@type B
496+
local t = {y = 1}
497+
]]
498+
465499
--

0 commit comments

Comments
 (0)