LuaLS / LuaLS/lua-language-server
Feature Request: Assertion Functions
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 would like annotation and type checking that supports and implements TypeScript's "Assertion Functions":
https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions
It's similar to other open feature requests for type predicates / narrowing except it avoids the need for if statements and to return a boolean from the function where true means a variable is of a given type and false if it's not that type.
I mentioned this briefly in another open issue but the original issue was for implementing narrowing/type predicates:
https://github.com/LuaLS/lua-language-server/issues/704#issuecomment-1484060572
This TypeScript code illustrates the feature:
function assertIsString(val: any): asserts val is string {
if (typeof val !== "string") {
throw new AssertionError("Not a string!");
}
}
This tells the linter that the argument passed to the val parameter is a string if no assertion raised an error.
In Lua, this might look like the following:
---@param val any
---@asserts val is string
local function assertIsString(val)
assert(type(val) == "string", "Not a string!")
end
---@param str any
local function yell(str)
assertIsString(str)
-- No error was thrown! Therefore, the next line should not be marked as a warning and `str` must be a string.
return str:upper()
end
The problem at the moment is that I need to add ---@cast val string after every usage of assertIsString:
local function yell(str)
assertIsString(str)
---@cast str string
return str:upper() -- LuaLS complains that str might not be a string unless I cast it
end
There is no other way of telling LuaLS that the result has been confirmed to be a string unless the assert expression is directly in the same body of code.
I would suggest adding narrowing with type predicates first as this feature request seems like the next feature on top of that feature, but I'd like to see what people think :)
Thank you!
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia leggendo il riferimento di TypeScript 3.7 alle funzioni di asserzione e la issue 704 collegata sui predicati di tipo, quindi esamina come le annotazioni @cast esistenti eseguono il narrowing. Il lavoro è completo quando un’annotazione di asserzione Lua può restringere l’argomento dopo una chiamata di asserzione riuscita, così che l’esempio yell accetti str:upper() senza un cast separato.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- lua, typescript
- Ambito
- tooling
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 30/100