LuaLS / LuaLS/lua-language-server
[Feature request] Add a pseudo-type for custom require-like functions
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Lua
- Star
- 4.4k
- Fork
- 442
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Mô tả
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.
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu bằng cách lần theo cách language server xử lý các gợi ý kiểu của require() và thiết lập Lua.runtime.special. Xác định Require nên hoạt động như thế nào đối với tên module dạng literal, các wrapper và ví dụ checked_require được hiển thị; hoàn thành có nghĩa là wrapper và require trực tiếp được phân giải thành cùng một kiểu module.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- lua
- Lĩnh vực
- devtools
- Loại issue
- Tính năng
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 35/100