LuaLS / LuaLS/lua-language-server

Nested Table Type Narrowing

未关闭
#2,218 4 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

enhancement
主要语言
Lua
星标
4.4k
派生
442
PR 合并指标
30 天内没有已合并 PR

描述

It would be super nice to be able to get field suggestions when narrowing an indice of a nested table.

Currently this:

---@class TestClass
---@field test_group_a TestGroupA
---@field test_group_b TestGroupB

---@class TestGroupA : TestGroupCommon
---@field test_field_1 string
---@field test_field_2 string

---@class TestGroupB : TestGroupCommon
---@field test_field_1 string
---@field test_field_3 string

---@class TestGroupCommon
---@field test_field_common string

---@type TestClass
local test_class

---@type 'test_group_a' | 'test_group_b'
local test_group

local test1 = test_class[test_group].test_field_1


---------------------------------------

---@alias TestTable {test_group_a: {test_field_1: string, test_field_2: string}, test_group_b: {test_field_1: string, test_field_3: string}}

---@type TestTable
local test_table

---@type "test_group_a" | "test_group_b"
local test_table_group

local test2 = test_table[test_table_group].test_field_1

The inferred type for test1 and test2 are both unknown. It would be nice if for test1 and test2 the inferred type would be string (as test_field_1 is a string for both, if test_field_1 and test_field_2 differed the inferred the type should be test_field_1_type | test_field_2_type). It would also be nice if on local test1 = test_class[test_group]. there were field suggestions for test_field_1 and test_field_common which are on both TestGroupA and TestGroupB.

Note: to add this for classes would also require doing: https://github.com/LuaLS/lua-language-server/issues/2217

贡献指南

打开贡献指南

从这里开始

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

调研方向

首先复现 issue 中的两个 Lua 示例,并检查处理索引访问、类型收窄和字段补全的 language-server 路径。完成的标准是:两个示例都能为 test_field_1 推断出 string,并提供可能的嵌套表类型共有的字段,同时考虑相关的类收窄问题 2217。

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

评估

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

把新 issue 发到你的邮箱

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