LuaLS / LuaLS/lua-language-server

Over-zealous 'Field `_field` is protected' warning in class-created closure functions

未關閉
#2,931 0 則留言 3 個 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?

Diagnostics/Syntax Checking

Expected Behaviour

Functions created from within class methods should be allowed to access protected (and private) members of that class.

Essentially, any functions inside a class method should inheret that method's access- this is a simple pattern that maintains encapsulation of exactly those private/protected fields, as the code is still clearly "owned" by the encapsulating class.

Actual Behaviour

In some scenarios, functions within class methods that access protected members of that class are being marked with the 'Field _field is protected' warning. See Repro Steps for code sample.

Reproduction steps
---@class DynamicDude
---@field _field string?
local DynamicDude = {}

function DynamicDude:demo()
    self:setup(function(self)
        -- This disable shouldn't be necessary
        ---@diagnostic disable-next-line: invisible
        self._field = 'test'
    end)
    ---@param self DynamicDude
    local function closure(self)
        -- This disable shouldn't be necessary
        ---@diagnostic disable-next-line: invisible
        self._field = 'test'
    end
    self.dynamic_fn = function(self)
        self._field = 'test'
    end
end

---@param fn fun(self:DynamicDude)
function DynamicDude:setup(fn)
    self.dynamic_fn = fn
end
Additional Notes

FEATURE REQUEST: I'd actually love to have a setting that just said "all variables named self are assumed to indicate valid access to private/protected members", to cover all my bases. Happy to dig up some examples / explain myself more if that's useful.

Log File

No response

貢獻指南

開啟貢獻指南

從這裡開始

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

研究方向

首先在 Visual Studio Code 擴充功能中重現所提供的 DynamicDude 範例,並檢查 diagnostics 如何分類在類別方法內建立的 closures。當這些 closures 能夠存取類別的 protected 或 private members 而不會出現不可見的警告,同時不相關的存取仍會被診斷時,即表示完成。

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

評估

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

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

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