LuaLS / LuaLS/lua-language-server

Type narrowing for unioned classes

オープン
#2,109 コメント 2 件 リアクション 6 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

enhancement
主要言語
Lua
スター
4.4k
フォーク
442
PR マージ指標
30日以内にマージされた PR はありません

説明

I have a large number of classes that look something like this:
```lua
---@class Foo
---@field type "foo"
---@field name string
---@field property string
-- more unique fields

---@class Bar
---@field type "bar"
---@field name string
---@field stuff string
-- more unique fields

-- more classes
```

I have a function that takes an array of any of these classes as a parameter, so naturally I've unioned all of the classes.
```lua
---@alias Prototypes
---|Foo
---|Bar
-- all of the other classes

---@param prototypes Prototypes[]
local function extend(prototypes)
-- stuff and things
end
```

However, when I try to make `prototypes` a table literal, I get autocomplete for the other classes because the union operator behaves like intersection instead of union.

![image](https://github.com/LuaLS/lua-language-server/assets/88168191/6ec8340a-2921-4a5d-b781-5f36094b8bdb)

Is there any way to get it to show only the fields of the class indicated by the `type` field? I can't seem to find way to do a proper union of types.
`@type` seems to only work on variables, and even if it did work on table literals, it would be cumbersome to write out the type for every entry in the array.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、この issue の注釈付き Foo/Bar Prototypes[] の例を再現し、language server の union 型の絞り込みとテーブルリテラルの補完の挙動を調査します。type フィールドによって絞り込まれた判別可能な union が、補完候補に選択されたクラスのフィールドだけを表示しつつ、他の union メンバーに対する有効な補完も維持できれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
lua
領域
developer-experience, tooling
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。