LuaLS / LuaLS/lua-language-server

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

Đang mở
#2,931 0 bình luận 3 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Ngôn ngữ chính
Lua
Star
4.4k
Fork
442
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu bằng cách tái hiện ví dụ DynamicDude được cung cấp trong extension Visual Studio Code và kiểm tra cách diagnostics phân loại các closures được tạo bên trong các phương thức của class. Hoàn thành khi các closures đó có thể truy cập protected hoặc private members của class mà không có cảnh báo vô hình, trong khi các lượt truy cập không liên quan vẫn được chẩn đoán.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
lua
Lĩnh vực
devtools
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
45/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.