LuaLS / LuaLS/lua-language-server
Feature Request: Propagate and Validate Array Index Annotations from Immutable Variable Indexing
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?
Annotations, Type Checking, Diagnostics/Syntax Checking
Expected Behaviour
The linter should propagate and validate the array index annotations from immutable variables, recognizing that the indices are constant and understanding the types associated with these indices.
local one = 1
---@class A
---@field [1] integer
---@field [2] string
---@type A
local a = {2, 'b'}
---@type integer
local b = a[one] -- no error
Actual Behaviour
The linter assumes that the value of the indices could be anything, even when the variables are immutable and clearly annotated. This leads to incorrect type warnings and reduces the effectiveness of the type checking.
local b = a[one] -- unexpectedly produces the following error; but 'local b = a[1]' does not.
Cannot assign string|integer to integer.
stringcannot matchinteger- Type
stringcannot matchintegerLua Diagnostics. (assign-type-mismatch)
Reproduction steps
Copy source in expected behavior into VS Code with extension v3.13.1 into a file with diagnostics enabled.
Additional Notes
I use prefer arrays instead of tables in my projects to avoid the cost of hashing the key and maximize the compressibility of the Lua script with external tools. Immutable local variables, used as enums, help maintain readability and maintainability. As noted by #2721 , a table seems unnecessary.
-
Using the enum annotation doesn't change the resulting behavior.
-
Using the alias annotation also doesn't change the resulting behavior.
---@alias a_1 1
---@type a_1
local one = 1
- The const annotation is not supported and would be another alternative to using alias.
Log File
No response
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Tái hiện đoạn mã Lua được cung cấp trong extension Visual Studio Code với diagnostics được bật, sau đó lần theo entry point kiểm tra kiểu của chỉ mục mảng chịu trách nhiệm về cảnh báo assign-type-mismatch đã được báo cáo. Thay đổi được hoàn tất khi một chỉ mục cục bộ bất biến được xác thực là giá trị hằng của nó và ví dụ không còn báo cáo string|integer đối với phép gán số nguyên, trong khi việc lập chỉ mục trực tiếp vẫn đúng.
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
- Tính năng
- Độ 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
- 35/100