LuaLS / LuaLS/lua-language-server

Properties (getter/setter) support

Open
#2,874 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Lua
Stars
4.4k
Forks
442
PR merge metrics
No merged PRs in 30d

Description

Lua's variables can act as C#-like properties by setting `__newindex` appropriately.

Getters and setters occasionally have different type signatures. However, the `@field` annotation can only represent one type.

```lua
-- hoge.meta.lua

---@meta

---@class Point
---@field x number
---@field y number
Point = {}

---@class Hoge
---@field position Point ⚠️ actually a property
Hoge = {}

---@return Hoge
function new_hoge() end
```

```lua
-- main.lua

local hoge = new_hoge()
hoge.position = { 3, 7 } -- emits "Missing required fields in type `Point`: `x`, `y`"
print(hoge.position.x) -- 3.0
print(hoge.position.y) -- 7.0
```

(Full sample program is [here](https://codeberg.org/haruomaki/luaproperty))

It should be annotated as "`position`'s setter receives a `table`, and its getter returns a `Point`."

How about adding a new syntax to describe this?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the annotation examples in hoge.meta.lua and main.lua, then reproduce the diagnostic for assigning a table to hoge.position. The issue names no repository files or tests, so the relevant annotation and type-checking entry points must be located. Done means the annotation syntax can express distinct setter and getter types and the sample assignment no longer reports missing Point fields.

Written by the indexing model from the issue text.

Assessment

Tech stack
lua
Domain
devtools
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.