summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--changelog.md10
-rw-r--r--script/vm/compiler.lua21
-rw-r--r--test/diagnostics/type-check.lua14
3 files changed, 45 insertions, 0 deletions
diff --git a/changelog.md b/changelog.md
index fe1d16bf..2329cdca 100644
--- a/changelog.md
+++ b/changelog.md
@@ -37,6 +37,15 @@ server will generate `doc.json` and `doc.md` in `LOGPATH`.
* `return-type-mismatch`
* `CHG` workspace-symbol: supports chain fields based on global variables and types. try `io.open` or `iolib.open`
* `CHG` [#1641] if a function only has varargs and has `---@overload`, the varargs will be ignored
+* `CHG` [#1575] search definitions by first argument of `setmetatable`
+ ```lua
+ ---@class Object
+ local obj = setmetatable({
+ initValue = 1,
+ }, mt)
+
+ print(obj.initValue) --> `obj.initValue` is integer
+ ```
* `FIX` [#1567]
* `FIX` [#1593]
* `FIX` [#1595]
@@ -53,6 +62,7 @@ server will generate `doc.json` and `doc.md` in `LOGPATH`.
[#1558]: https://github.com/sumneko/lua-language-server/issues/1558
[#1561]: https://github.com/sumneko/lua-language-server/issues/1561
[#1567]: https://github.com/sumneko/lua-language-server/issues/1567
+[#1575]: https://github.com/sumneko/lua-language-server/issues/1575
[#1582]: https://github.com/sumneko/lua-language-server/issues/1582
[#1593]: https://github.com/sumneko/lua-language-server/issues/1593
[#1595]: https://github.com/sumneko/lua-language-server/issues/1595
diff --git a/script/vm/compiler.lua b/script/vm/compiler.lua
index 61838775..c9008f5f 100644
--- a/script/vm/compiler.lua
+++ b/script/vm/compiler.lua
@@ -368,6 +368,27 @@ function vm.getClassFields(suri, object, key, ref, pushResult)
end
end)
end
+ if src.value
+ and src.value.type == 'select'
+ and src.value.vararg.type == 'call' then
+ local func = src.value.vararg.node
+ local args = src.value.vararg.args
+ if func.special == 'setmetatable'
+ and args
+ and args[1]
+ and args[1].type == 'table' then
+ searchFieldSwitch('table', suri, args[1], key, ref, function (field)
+ local fieldKey = guide.getKeyName(field)
+ if fieldKey then
+ if not searchedFields[fieldKey]
+ and guide.isSet(field) then
+ hasFounded[fieldKey] = true
+ --pushResult(field, true)
+ end
+ end
+ end)
+ end
+ end
copyToSearched()
searchFieldSwitch(src.type, suri, src, key, ref, function (field)
local fieldKey = guide.getKeyName(field)
diff --git a/test/diagnostics/type-check.lua b/test/diagnostics/type-check.lua
index bdba855b..a4ee1d65 100644
--- a/test/diagnostics/type-check.lua
+++ b/test/diagnostics/type-check.lua
@@ -841,6 +841,20 @@ TEST [[
local <!test!> = 4
]]
+TEST [[
+---@class MyClass
+local MyClass = {}
+
+function MyClass:new()
+ ---@class MyClass
+ local myObject = setmetatable({
+ initialField = true
+ }, self)
+
+ print(myObject.initialField)
+end
+]]
+
config.remove(nil, 'Lua.diagnostics.disable', 'unused-local')
config.remove(nil, 'Lua.diagnostics.disable', 'unused-function')
config.remove(nil, 'Lua.diagnostics.disable', 'undefined-global')