LuaLS / LuaLS/lua-language-server

@overload not as expected check param in class instance

未关闭
#2,576 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

主要语言
Lua
星标
4.4k
派生
442
PR 合并指标
30 天内没有已合并 PR

描述

### 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

### Expected Behaviour

当一个类通过@overload定义了__call元方法。
当它的一个实例以__call的形式被调用,应该检测参数是否正确。
When a class defines a __call metamethod via @overload.
When one of its instances is called as __call, the argument should be checked to see if it is correct.

### Actual Behaviour

被正确地识别为了一个函数,但是没有检测其参数是否正确。
Is correctly identified as a function, but its arguments are not checked for correctness.

### Reproduction steps

```lua
---@class Callable
---@overload fun(tbl:table)
local Callable = {}
Callable.__index = Callable

---@return Callable
function Callable:extend()
return setmetatable({ __index = self, __call = self.__call }, self)
return

function Callable:__call(tbl)
for key, value in pairs(tbl) do
print(key)
end
end

-- other lua file
local instance = { }
instance.callable = Callable:extend()

instance:callable { a=1, b=2 } -- A warning should be thrown here because `instance` is passed in as the first argument (self).
instance.callable { a=1, b=2 } -- Correct call.
```

### Additional Notes
一个临时的解决方法:再次进行类型的声明。
Temporary workaround: Type declaration again.
```lua
local instance = { }
---@overload fun(tbl:table)
instance.callable = Callable:extend() --[[@as function]]

instance:callable { a=1, b=2 } -- A warning.
instance.callable { a=1, b=2 } -- Correct call.
```

我是在实现一个C#风格的Seter和Geter时发现的,我定义了一个Property类,子类会继承父类的Property。
为了使代码更加简洁,我定义了Property类的__call元方法,使得我可以以这样的风格进行声明:
I discovered this when implementing a C# style Seter and Geter, and I defined a Property class where subclasses inherit the Property of the parent class.
To make the code more concise, I define the __call metamethod of the Property class so that I can declare it in this style:
```lua
MyClass.property {
name = {
get = function(self)
return self._name
end,
set = function(self, value)
self._name = value
end,
}
}
```
### Log File

_No response_

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从提供的 Lua 复现开始,检查语言服务器如何解析 __call 元方法上的 @overload,以及冒号调用与点号调用。完成标准是:冒号调用报告额外的 self 参数,而点号调用仍然有效,并覆盖这两种形式。

由索引模型根据 Issue 内容生成。

评估

技术栈
lua
领域
devtools
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
42/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。