LuaLS / LuaLS/lua-language-server
Returning a value as a class doesnt set the fields
Nobody has claimed this yet.
- Dominant language
- Lua
- Stars
- 4.4k
- Forks
- 442
- PR merge metrics
- No merged PRs in 30d
Description
### How are you using the lua-language-server?
Visual Studio Code Extension (sumneko.lua)
### Which OS are you using?
Windows
### What is the issue affecting?
Annotations, Type Checking
### Expected Behaviour
`value` be member of `MyClass`
### Actual Behaviour

`value` is not a member
### Reproduction steps
```lua
local function myConstructor()
---@class MyClass
return { value = true }
end
local o = myConstructor()
print(o.value)
```
### Additional Notes
This should also work with a `setmetatable` variant:
```lua
---@class MyClass
local MyClass = {}
MyClass.__index = MyClass
local function myConstructor()
---@class MyClass
return setmetatable({ value = true }, MyClass)
end
local o = myConstructor()
print(o.value)
```
It does work with this workarround:
```lua
local function myConstructor()
---@class MyClass
local o = setmetatable({ value = true }, MyClass)
return o
end
```
### Log File
_No response_
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the two constructor examples from the issue and trace how the language server interprets the @class annotation on a returned table. Compare this with the working local-variable workaround and add a regression test covering both direct returns and setmetatable returns. Done means value is recognized as a member of MyClass during type checking.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- lua
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100