LuaLS / LuaLS/lua-language-server

Returning a value as a class doesnt set the fields

Open
#2,321 0 comments 0 reactions 0 assignees View on GitHub
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

![grafik](https://github.com/LuaLS/lua-language-server/assets/15840247/d0ede492-0113-4d95-beb5-391a77508f8f)
`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

Open the contributing guide

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.