LuaLS / LuaLS/lua-language-server

Table type annotations

未關閉
#3,295 1 則留言 2 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

主要語言
Lua
星號
4.4k
分支
442
PR 合併指標
30 天內沒有已合併 PR

描述

Hello everyone.

I've defined a field as table<MyStates, BaseState>, but the analyzer doesn't complain when I assign a table with a completely different structure to it.

---@class BaseState
local BaseState = {}

---@enum MyStates
local States = {
    GAME = 1,
    GAME_OVER = 2,
}

---@class MyStateMachine
---@field private _states table<MyStates, BaseState>
local MyStateMachine = {}
MyStateMachine.__index = MyStateMachine

---@param states table<MyStates, BaseState>
function MyStateMachine:setStates(states)
    self._states = states
end

function tmp()
    -- Assigning a table with a different annotation directly. The analyzer doesn't complain.
    MyStateMachine._states = {
        ['kek'] = 69
    }
    -- Passing a table with a different annotation to a method. The analyzer doesn't complain.
    MyStateMachine:setStates({
        ['kek'] = 69,
    })
    -- The analyzer correctly complains about a number assignment.
    -- MyStateMachine._states = 69
    -- The analyzer correctly complains about passing a number.
    -- MyStateMachine:setStates(2)
end

As you can see, the type checker only seems to validate that the value is a table, but doesn't validate the internal key and value types for table<MyStates, BaseState>. Is this the expected behavior, or am I missing something? Is there a way to enforce stricter type checking for the contents of generic tables?

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

使用提供的 Lua 範例重現該報告,並檢查 language server 的 generic-table 型別檢查進入點。驗證指派和方法引數是否同時驗證鍵的型別和值的型別,然後新增或更新涵蓋範圍,使不相容的表格內容產生診斷,同時繼續接受有效的表格。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
lua
領域
devtools
Issue 類型
缺陷
難度
3/5
預估耗時
1-2 天
活躍度
停滯
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。