LuaLS / LuaLS/lua-language-server

Feature Request: Propagate and Validate Array Index Annotations from Immutable Variable Indexing

Ouverte
#2,972 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Langage dominant
Lua
Étoiles
4.4k
Forks
442
Métriques de merge des PR
Aucune PR mergée en 30 j

Description

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.

  • string cannot match integer
  • Type string cannot match integer Lua 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

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Reproduisez l’extrait Lua fourni dans l’extension Visual Studio Code avec les diagnostics activés, puis suivez le point d’entrée de la vérification de type de l’index de tableau responsable de l’avertissement assign-type-mismatch signalé. La modification est terminée lorsqu’un index local immuable est validé comme sa valeur constante et que l’exemple ne signale plus string|integer pour l’affectation d’un entier, tandis que l’indexation directe reste correcte.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
lua
Domaine
devtools
Type d'issue
Fonctionnalité
Difficulté
4/5
Temps estimé
3-5 jours
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
35/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.