summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2022-11-05 01:01:56 +0800
committer最萌小汐 <sumneko@hotmail.com>2022-11-05 01:01:56 +0800
commit82ccd9baf64bf739e0bf7f8251075acb9e286356 (patch)
treed2d65db2c4280f6b5f66aa30d437c0c202a8ff70
parent167a17c541a08b3dc5b3588dbb617d2ebf61f343 (diff)
downloadlua-language-server-82ccd9baf64bf739e0bf7f8251075acb9e286356.zip
only ignore empty functions in meta files
#1457
-rw-r--r--script/core/diagnostics/missing-return.lua15
-rw-r--r--script/core/diagnostics/unused-local.lua17
-rw-r--r--script/vm/function.lua14
-rw-r--r--test/diagnostics/common.lua7
-rw-r--r--test/diagnostics/type-check.lua2
5 files changed, 29 insertions, 26 deletions
diff --git a/script/core/diagnostics/missing-return.lua b/script/core/diagnostics/missing-return.lua
index d90311d7..42ccaa9f 100644
--- a/script/core/diagnostics/missing-return.lua
+++ b/script/core/diagnostics/missing-return.lua
@@ -39,17 +39,6 @@ local function hasReturn(block)
end
---@param func parser.object
----@return boolean
-local function isEmptyFunction(func)
- if #func > 0 then
- return false
- end
- local startRow = guide.rowColOf(func.start)
- local finishRow = guide.rowColOf(func.finish)
- return finishRow - startRow <= 1
-end
-
----@param func parser.object
---@return integer
local function getReturnsMin(func)
local min = vm.countReturnsOfFunction(func, true)
@@ -77,10 +66,12 @@ return function (uri, callback)
return
end
+ local isMeta = vm.isMetaFile(uri)
+
---@async
guide.eachSourceType(state.ast, 'function', function (source)
-- check declare only
- if isEmptyFunction(source) then
+ if isMeta and vm.isEmptyFunction(source) then
return
end
await.delay()
diff --git a/script/core/diagnostics/unused-local.lua b/script/core/diagnostics/unused-local.lua
index 8f2ee217..52622eb2 100644
--- a/script/core/diagnostics/unused-local.lua
+++ b/script/core/diagnostics/unused-local.lua
@@ -65,24 +65,13 @@ local function isDocClass(source)
return false
end
----@param func parser.object
----@return boolean
-local function isEmptyFunction(func)
- if #func > 0 then
- return false
- end
- local startRow = guide.rowColOf(func.start)
- local finishRow = guide.rowColOf(func.finish)
- return finishRow - startRow <= 1
-end
-
---@param source parser.object
local function isDeclareFunctionParam(source)
if source.parent.type ~= 'funcargs' then
return false
end
local func = source.parent.parent
- return isEmptyFunction(func)
+ return vm.isEmptyFunction(func)
end
return function (uri, callback)
@@ -90,6 +79,8 @@ return function (uri, callback)
if not ast then
return
end
+
+ local isMeta = vm.isMetaFile(uri)
local ignorePatterns = config.get(uri, 'Lua.diagnostics.unusedLocalExclude')
local ignore = glob.glob(ignorePatterns)
guide.eachSourceType(ast.ast, 'local', function (source)
@@ -107,7 +98,7 @@ return function (uri, callback)
if isDocClass(source) then
return
end
- if isDeclareFunctionParam(source) then
+ if isMeta and isDeclareFunctionParam(source) then
return
end
local data = hasGet(source)
diff --git a/script/vm/function.lua b/script/vm/function.lua
index 8b996721..5ad12acf 100644
--- a/script/vm/function.lua
+++ b/script/vm/function.lua
@@ -1,5 +1,6 @@
---@class vm
-local vm = require 'vm.vm'
+local vm = require 'vm.vm'
+local guide = require 'parser.guide'
---@param arg parser.object
---@return parser.object?
@@ -266,3 +267,14 @@ function vm.isVarargFunctionWithOverloads(func)
end
return false
end
+
+---@param func parser.object
+---@return boolean
+function vm.isEmptyFunction(func)
+ if #func > 0 then
+ return false
+ end
+ local startRow = guide.rowColOf(func.start)
+ local finishRow = guide.rowColOf(func.finish)
+ return finishRow - startRow <= 1
+end
diff --git a/test/diagnostics/common.lua b/test/diagnostics/common.lua
index e899c0c4..7c2ffb60 100644
--- a/test/diagnostics/common.lua
+++ b/test/diagnostics/common.lua
@@ -1149,6 +1149,8 @@ return m
]]
TEST [[
+---@meta
+
---@class A
---@field a boolean
@@ -1769,6 +1771,8 @@ function t:init() end
]]
TEST [[
+---@meta
+
return function f(x, y, z) end
]]
@@ -1816,6 +1820,8 @@ end
]]
TEST [[
+---@meta
+
---@return number, number
local function r2() end
@@ -2057,6 +2063,7 @@ local _ <close> = t
]]
TEST [[
+---@meta
---@diagnostic disable: duplicate-set-field
---@class A
local m = {}
diff --git a/test/diagnostics/type-check.lua b/test/diagnostics/type-check.lua
index 6455cd66..f0133501 100644
--- a/test/diagnostics/type-check.lua
+++ b/test/diagnostics/type-check.lua
@@ -3,6 +3,7 @@ local config = require 'config'
config.add(nil, 'Lua.diagnostics.disable', 'unused-local')
config.add(nil, 'Lua.diagnostics.disable', 'unused-function')
config.add(nil, 'Lua.diagnostics.disable', 'undefined-global')
+config.add(nil, 'Lua.diagnostics.disable', 'missing-return')
config.set(nil, 'Lua.type.castNumberToInteger', false)
TEST [[
@@ -867,4 +868,5 @@ t.x = 1
config.remove(nil, 'Lua.diagnostics.disable', 'unused-local')
config.remove(nil, 'Lua.diagnostics.disable', 'unused-function')
config.remove(nil, 'Lua.diagnostics.disable', 'undefined-global')
+config.remove(nil, 'Lua.diagnostics.disable', 'missing-return')
config.set(nil, 'Lua.type.castNumberToInteger', true)