Skip to content

Commit

Permalink
don't check duplicate-set-field in different if
Browse files Browse the repository at this point in the history
  • Loading branch information
sumneko committed Jan 17, 2023
1 parent 6c89b0a commit 505f8de
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
25 changes: 25 additions & 0 deletions script/core/diagnostics/duplicate-set-field.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,33 @@ local sourceTypes = {
'setindex',
}

---@param source parser.object
---@return parser.object?
local function getTopFunctionOfIf(source)
while true do
if source.type == 'ifblock'
or source.type == 'elseifblock'
or source.type == 'elseblock'
or source.type == 'function'
or source.type == 'main' then
return source
end
source = source.parent
end
return nil
end

---@async
return function (uri, callback)
local state = files.getState(uri)
if not state then
return
end

if vm.isMetaFile(uri) then
return
end

---@async
guide.eachSourceTypes(state.ast, sourceTypes, function (src)
await.delay()
Expand All @@ -29,6 +49,7 @@ return function (uri, callback)
if not value or value.type ~= 'function' then
return
end
local myTopBlock = getTopFunctionOfIf(src)
local defs = vm.getDefs(src)
for _, def in ipairs(defs) do
if def == src then
Expand All @@ -39,6 +60,10 @@ return function (uri, callback)
and def.type ~= 'setindex' then
goto CONTINUE
end
local defTopBlock = getTopFunctionOfIf(def)
if uri == guide.getUri(def) and myTopBlock ~= defTopBlock then
goto CONTINUE
end
local defValue = vm.getObjectValue(def)
if not defValue or defValue.type ~= 'function' then
goto CONTINUE
Expand Down
31 changes: 31 additions & 0 deletions test/diagnostics/common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2231,3 +2231,34 @@ mt.init = function ()
obj.x = 1
end
]]

TEST [[
---@class A
X = {}
function <!X.f!>() end
function <!X.f!>() end
]]

TEST [[
---@meta
---@class A
X = {}
function X.f() end
function X.f() end
]]

TEST [[
---@class A
X = {}
if true then
function X.f() end
else
function X.f() end
end
]]

0 comments on commit 505f8de

Please sign in to comment.