Skip to content

Commit 4ddf771

Browse files
committed
fix: return single definition for anonymous-function field assignment
1 parent 7a73c78 commit 4ddf771

4 files changed

Lines changed: 33 additions & 1 deletion

File tree

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Unreleased
44
<!-- Add all new changes here. They will be moved under a version at release -->
55
* `FIX` `need-check-nil` diagnostic is no longer reported on safe navigation access (e.g. `x?.field`, `f?.()`, `t?.[key]`), since the optional access itself already handles the nil check. Note that a non-safe access chained after a safe one (e.g. `x.upper()?.field`) still reports, because the safe access only protects its own result.
6+
* `FIX` Go to definition on a table field assigned an anonymous function (e.g. `A.c = function() end`) now returns a single definition at the field name, instead of two candidates (the field name and the function value) [#2451](https://github.com/LuaLS/lua-language-server/issues/2451)
67

78
## 3.19.1
89
`2026-08-14`

script/core/definition.lua

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,25 @@ return function (uri, offset)
231231
return nil
232232
end
233233

234+
-- Drop a redundant function-value target when the owning assignment's name node is also a target.
235+
-- Collapses `A.c = function() end` to a single definition at the name `c`.
236+
local targetMark = {}
237+
for _, res in ipairs(results) do
238+
targetMark[res.target] = true
239+
end
240+
for i = #results, 1, -1 do
241+
local target = results[i].target
242+
if target.type == 'function' then
243+
local parent = target.parent
244+
if parent and guide.isAssign(parent) then
245+
local owner = parent.field or parent.method or parent.index or parent.variable or parent
246+
if targetMark[owner] then
247+
table.remove(results, i)
248+
end
249+
end
250+
end
251+
end
252+
234253
sortResults(results)
235254
jumpSource(results)
236255

test/definition/field.lua

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,15 @@ local t = X
2121
2222
print(t.x.<?y?>)
2323
]]
24+
25+
TEST [[
26+
A = {}
27+
A.<!c!> = function() end
28+
A.<?c?>()
29+
]]
30+
31+
TEST [[
32+
local A = {}
33+
A.<!c!> = function() end
34+
A.<?c?>()
35+
]]

test/definition/function.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ end
2424
]]
2525

2626
TEST [[
27-
local <!f!> = <!function () end!>
27+
local <!f!> = function () end
2828
<?f?>()
2929
]]

0 commit comments

Comments
 (0)