Skip to content

Commit

Permalink
fix wrong missing-fields with union types
Browse files Browse the repository at this point in the history
fix #2252
  • Loading branch information
sumneko committed Aug 22, 2023
1 parent a5c3b64 commit cb16010
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 9 deletions.
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@
* `FIX` wrong hover and signature for method with varargs and overloads
* `FIX` [#2155]
* `FIX` [#2224]
* `FIX` [#2252]
* `FIX` [#2267]

[#2155]: https://github.com/LuaLS/lua-language-server/issues/2155
[#2224]: https://github.com/LuaLS/lua-language-server/issues/2224
[#2252]: https://github.com/LuaLS/lua-language-server/issues/2252
[#2267]: https://github.com/LuaLS/lua-language-server/issues/2267

## 3.6.25
Expand Down
2 changes: 1 addition & 1 deletion locale/en-us/script.lua
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ DIAG_INVISIBLE_PACKAGE =
DIAG_GLOBAL_ELEMENT =
'Element is global.'
DIAG_MISSING_FIELDS =
'Missing fields: {}'
'Missing required fields in type `{1}`: {2}'
DIAG_INJECT_FIELD =
'Fields cannot be injected into the reference of `{class}` for `{field}`. {fix}'
DIAG_INJECT_FIELD_FIX_CLASS =
Expand Down
2 changes: 1 addition & 1 deletion locale/pt-br/script.lua
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ DIAG_INVISIBLE_PACKAGE = -- TODO: need translate!
DIAG_GLOBAL_ELEMENT = -- TODO: need translate!
'Element is global.'
DIAG_MISSING_FIELDS = -- TODO: need translate!
'Missing fields: {}'
'Missing required fields in type `{1}`: {2}'
DIAG_INJECT_FIELD = -- TODO: need translate!
'Fields cannot be injected into the reference of `{class}` for `{field}`. {fix}'
DIAG_INJECT_FIELD_FIX_CLASS = -- TODO: need translate!
Expand Down
2 changes: 1 addition & 1 deletion locale/zh-cn/script.lua
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ DIAG_INVISIBLE_PACKAGE =
DIAG_GLOBAL_ELEMENT =
'全局变量。'
DIAG_MISSING_FIELDS =
'缺少字段: {}'
'缺少类型 `{1}` 的必要字段: {2}'
DIAG_INJECT_FIELD =
'不能在 `{class}` 的引用中注入字段 `{field}` 。{fix}'
DIAG_INJECT_FIELD_FIX_CLASS =
Expand Down
2 changes: 1 addition & 1 deletion locale/zh-tw/script.lua
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ DIAG_INVISIBLE_PACKAGE = -- TODO: need translate!
DIAG_GLOBAL_ELEMENT = -- TODO: need translate!
'Element is global.'
DIAG_MISSING_FIELDS = -- TODO: need translate!
'Missing fields: {}'
'Missing required fields in type `{1}`: {2}'
DIAG_INJECT_FIELD = -- TODO: need translate!
'Fields cannot be injected into the reference of `{class}` for `{field}`. {fix}'
DIAG_INJECT_FIELD_FIX_CLASS = -- TODO: need translate!
Expand Down
16 changes: 11 additions & 5 deletions script/core/diagnostics/missing-fields.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ return function (uri, callback)
return
end
end
local warnings = {}
for _, def in ipairs(defs) do
if def.type == 'doc.class' then
if not def.fields then
Expand Down Expand Up @@ -67,12 +68,17 @@ return function (uri, callback)
return
end

callback {
start = src.start,
finish = src.finish,
message = lang.script('DIAG_MISSING_FIELDS', table.concat(missedKeys, ', ')),
}
warnings[#warnings+1] = lang.script('DIAG_MISSING_FIELDS', def.class[1], table.concat(missedKeys, ', '))
end
end

if #warnings == 0 then
return
end
callback {
start = src.start,
finish = src.finish,
message = table.concat(warnings, '\n')
}
end)
end
26 changes: 26 additions & 0 deletions test/diagnostics/missing-fields.lua
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,29 @@ TEST [[
---@type A
return <!{}!>
]]

TEST [[
---@class A
---@field x number
---@class B
---@field y number
---@type A|B
local t = <!{
z = 1,
}!>
]]

TEST [[
---@class A
---@field x number
---@class B
---@field y number
---@type A|B
local t = {
y = 1,
}
]]

0 comments on commit cb16010

Please sign in to comment.