LuaLS / LuaLS/lua-language-server
[Feature request] Add a pseudo-type for custom require-like functions
還沒有人認領這個 Issue。
- 主要語言
- 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.
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
首先追蹤語言伺服器如何處理 require() 的型別提示以及 Lua.runtime.special 設定。定義 Require 對字面值模組名稱、包裝器和所示 checked_require 範例應執行的操作;完成的標準是包裝器和直接的 require 解析為相同的模組型別。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- lua
- 領域
- devtools
- Issue 類型
- 功能
- 難度
- 5/5
- 預估耗時
- 一週以上
- 活躍度
- 停滯
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100