Skip to content

Commit 505f8de

Browse files
committed
don't check duplicate-set-field in different if
1 parent 6c89b0a commit 505f8de

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

script/core/diagnostics/duplicate-set-field.lua

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,33 @@ local sourceTypes = {
1111
'setindex',
1212
}
1313

14+
---@param source parser.object
15+
---@return parser.object?
16+
local function getTopFunctionOfIf(source)
17+
while true do
18+
if source.type == 'ifblock'
19+
or source.type == 'elseifblock'
20+
or source.type == 'elseblock'
21+
or source.type == 'function'
22+
or source.type == 'main' then
23+
return source
24+
end
25+
source = source.parent
26+
end
27+
return nil
28+
end
29+
1430
---@async
1531
return function (uri, callback)
1632
local state = files.getState(uri)
1733
if not state then
1834
return
1935
end
2036

37+
if vm.isMetaFile(uri) then
38+
return
39+
end
40+
2141
---@async
2242
guide.eachSourceTypes(state.ast, sourceTypes, function (src)
2343
await.delay()
@@ -29,6 +49,7 @@ return function (uri, callback)
2949
if not value or value.type ~= 'function' then
3050
return
3151
end
52+
local myTopBlock = getTopFunctionOfIf(src)
3253
local defs = vm.getDefs(src)
3354
for _, def in ipairs(defs) do
3455
if def == src then
@@ -39,6 +60,10 @@ return function (uri, callback)
3960
and def.type ~= 'setindex' then
4061
goto CONTINUE
4162
end
63+
local defTopBlock = getTopFunctionOfIf(def)
64+
if uri == guide.getUri(def) and myTopBlock ~= defTopBlock then
65+
goto CONTINUE
66+
end
4267
local defValue = vm.getObjectValue(def)
4368
if not defValue or defValue.type ~= 'function' then
4469
goto CONTINUE

test/diagnostics/common.lua

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2231,3 +2231,34 @@ mt.init = function ()
22312231
obj.x = 1
22322232
end
22332233
]]
2234+
2235+
TEST [[
2236+
---@class A
2237+
X = {}
2238+
2239+
function <!X.f!>() end
2240+
2241+
function <!X.f!>() end
2242+
]]
2243+
2244+
TEST [[
2245+
---@meta
2246+
2247+
---@class A
2248+
X = {}
2249+
2250+
function X.f() end
2251+
2252+
function X.f() end
2253+
]]
2254+
2255+
TEST [[
2256+
---@class A
2257+
X = {}
2258+
2259+
if true then
2260+
function X.f() end
2261+
else
2262+
function X.f() end
2263+
end
2264+
]]

0 commit comments

Comments
 (0)