LuaLS / LuaLS/lua-language-server

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

Aperta
#3,003 0 commenti 2 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Lingua principale
Lua
Stelle
4.4k
Fork
442
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia tracciando il modo in cui il language server gestisce gli hint di tipo di require() e l’impostazione Lua.runtime.special. Definisci cosa dovrebbe fare Require per i nomi di modulo letterali, i wrapper e l’esempio mostrato di checked_require; il lavoro è completo quando il wrapper e il require diretto vengono risolti nello stesso tipo di modulo.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
lua
Ambito
devtools
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.