LuaLS / LuaLS/lua-language-server
Feature Request: Propagate and Validate Array Index Annotations from Immutable Variable Indexing
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Lua
- Estrellas
- 4.4k
- Forks
- 442
- Métricas de merge de PR
- Sin PR fusionados en 30 d
Descripción
How are you using the lua-language-server?
Visual Studio Code Extension (sumneko.lua)
Which OS are you using?
Windows
What is the issue affecting?
Annotations, Type Checking, Diagnostics/Syntax Checking
Expected Behaviour
The linter should propagate and validate the array index annotations from immutable variables, recognizing that the indices are constant and understanding the types associated with these indices.
local one = 1
---@class A
---@field [1] integer
---@field [2] string
---@type A
local a = {2, 'b'}
---@type integer
local b = a[one] -- no error
Actual Behaviour
The linter assumes that the value of the indices could be anything, even when the variables are immutable and clearly annotated. This leads to incorrect type warnings and reduces the effectiveness of the type checking.
local b = a[one] -- unexpectedly produces the following error; but 'local b = a[1]' does not.
Cannot assign string|integer to integer.
stringcannot matchinteger- Type
stringcannot matchintegerLua Diagnostics. (assign-type-mismatch)
Reproduction steps
Copy source in expected behavior into VS Code with extension v3.13.1 into a file with diagnostics enabled.
Additional Notes
I use prefer arrays instead of tables in my projects to avoid the cost of hashing the key and maximize the compressibility of the Lua script with external tools. Immutable local variables, used as enums, help maintain readability and maintainability. As noted by #2721 , a table seems unnecessary.
-
Using the enum annotation doesn't change the resulting behavior.
-
Using the alias annotation also doesn't change the resulting behavior.
---@alias a_1 1
---@type a_1
local one = 1
- The const annotation is not supported and would be another alternative to using alias.
Log File
No response
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Reproduce el fragmento de Lua proporcionado en la extensión de Visual Studio Code con los diagnósticos habilitados y, a continuación, rastrea el punto de entrada de la comprobación de tipos del índice de array responsable de la advertencia assign-type-mismatch indicada. El cambio estará completo cuando un índice local inmutable se valide como su valor constante y el ejemplo deje de informar string|integer para la asignación de un entero, mientras que la indexación directa siga siendo correcta.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- lua
- Área
- devtools
- Tipo de issue
- Nueva funcionalidad
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 35/100