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<T>-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:
--- 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<T> module -- <-- Note the use of `Require<T>`
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:
---@generic T : string
---@param module_name T
---@return Require<T> 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