LuaLS / LuaLS/lua-language-server

Feature Request: Propagate and Validate Array Index Annotations from Immutable Variable Indexing

未关闭
#2,972 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, 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.

  • string cannot match integer
  • Type string cannot match integer Lua 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

贡献指南

打开贡献指南

从这里开始

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

调研方向

在启用诊断的 Visual Studio Code 扩展中重现提供的 Lua 代码片段,然后跟踪负责所报告的 assign-type-mismatch 警告的数组索引类型检查入口点。当不可变的局部索引被验证为其常量值,并且示例对整数赋值不再报告 string|integer,同时直接索引仍然正确时,此更改即完成。

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

评估

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

把新 issue 发到你的邮箱

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