LuaLS / LuaLS/lua-language-server

[Feature request] Add a pseudo-type for custom require-like functions

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

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

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

説明

I think it would be useful to have something like a `Require`-type. This type would take a literal string type `T` and evaluate as the same type that calling `require(modname: T)` would. This way, a wrapper around `require` could easily be built like this:

```lua
--- Require a module, throwing an error if it returned `true` (meaning that you
--- probably forgot to actually return something from it).
---@generic T : string
---@param module_name T
---@return Require module -- <-- Note the use of `Require`
local function checked_require(module_name)
assert(type(module_name) == "string", "The `module_name` must be a string!")

local module = require(module_name)

assert(
module ~= true,
string.format(
"Could not import module: `require(%q)` returned `true`!",
module_name
)
)

return module
end

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

local socket = checked_require("socket")
local socket_require = require("socket")

assert(socket == socket_require)
```

The type-hint of `require()` could then be changed to the following:

```lua
---@generic T : string
---@param module_name T
---@return Require module
function require(module_name) end
```

The language server already has a way to make global functions behave like `require` built-in, in the form of the `"Lua.runtime.special"` setting. This type would essentially do the same, except that it would be more powerful / broadly usable, since it could be applied to any variable. It would also be easier to use, since it's just another type in your code instead of something hiding in a config file.

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

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

はじめの一歩

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

調査の方向性

まず、言語サーバーが require() の型ヒントと Lua.runtime.special 設定をどのように処理するかを追跡します。リテラルなモジュール名、ラッパー、および示されている checked_require の例に対して Require がどう動作すべきかを定義します。完了の条件は、ラッパーと直接の require が同じモジュール型に解決されることです。

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

評価

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

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

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