LuaLS / LuaLS/lua-language-server
[Feature request] Add a pseudo-type for custom require-like functions
Ninguém assumiu esta issue ainda.
- Linguagem predominante
- Lua
- Estrelas
- 4.4k
- Forks
- 442
- Métricas de merge de PRs
- Nenhum PR com merge em 30d
Descrição
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.
Guia de contribuição
Primeiros passos
- Leia a issue inteira e depois o guia de contribuição do projeto.
- Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
- Faça um fork do repositório e trabalhe em uma branch.
- Abra um pull request que referencie o número da issue.
Direção de pesquisa
Comece rastreando como o servidor de linguagem lida com as dicas de tipo de require() e com a configuração Lua.runtime.special. Defina o que Require deve fazer para nomes literais de módulos, wrappers e o exemplo mostrado de checked_require; considera-se concluído quando o wrapper e o require direto são resolvidos para o mesmo tipo de módulo.
Escrita pelo modelo de indexação a partir do texto da issue.
Avaliação
- Stack de tecnologia
- lua
- Domínio
- devtools
- Tipo de issue
- Funcionalidade
- Dificuldade
- 5/5
- Tempo estimado
- Mais de uma semana
- Status de atividade
- Estagnada
- Clareza
- Razoavelmente clara
- Facilidade para iniciantes
- 35/100