diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2022-06-18 02:48:43 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2022-06-18 02:48:43 +0800 |
commit | f99a3cd8dea446852d8282319d620704eb0e6094 (patch) | |
tree | 6521dc0c0bec4fcdd9d060baacc27812ce2afa53 | |
parent | 256d5de895fd5f74467f2eefe35c96131c1adf15 (diff) | |
download | lua-language-server-f99a3cd8dea446852d8282319d620704eb0e6094.zip |
cleanup
-rw-r--r-- | script/config/config.lua | 25 | ||||
-rw-r--r-- | test/diagnostics/common.lua | 233 | ||||
-rw-r--r-- | test/diagnostics/init.lua | 2 | ||||
-rw-r--r-- | test/diagnostics/type-check.lua | 241 |
4 files changed, 164 insertions, 337 deletions
diff --git a/script/config/config.lua b/script/config/config.lua index 8dd7dc3e..f8d9e985 100644 --- a/script/config/config.lua +++ b/script/config/config.lua @@ -112,6 +112,31 @@ function m.add(uri, key, value) return false end +function m.remove(uri, key, value) + local unit = template[key] + if not unit then + return false + end + local list = m.getRaw(uri, key) + if type(list) ~= 'table' then + return false + end + local copyed = {} + for i, v in ipairs(list) do + if not util.equal(v, value) then + copyed[i] = v + end + end + local oldValue = m.get(uri, key) + m.set(uri, key, copyed) + local newValue = m.get(uri, key) + if not util.equal(oldValue, newValue) then + m.event(uri, key, newValue, oldValue) + return true + end + return false +end + function m.prop(uri, key, prop, value) local unit = template[key] if not unit then diff --git a/test/diagnostics/common.lua b/test/diagnostics/common.lua index 0e7d4436..32c496df 100644 --- a/test/diagnostics/common.lua +++ b/test/diagnostics/common.lua @@ -1613,236 +1613,3 @@ local function foo(x) end foo(f()) ]] - -TEST [[ ----@diagnostic disable: unused-local -local x = 0 - -<!x!> = true -]] - -TEST [[ ----@diagnostic disable: unused-local - ----@type integer -local x - -<!x!> = true -]] - -TEST [[ ----@diagnostic disable: unused-local - ----@type unknown -local x - -x = nil -]] - -TEST [[ ----@diagnostic disable: unused-local - ----@type unknown -local x - -x = 1 -]] - -TEST [[ ----@diagnostic disable: unused-local - ----@type unknown|nil -local x - -x = 1 -]] - -TEST [[ ----@diagnostic disable: unused-local - -local x = {} - -x = nil -]] - -TEST [[ ----@diagnostic disable: unused-local - ----@type string -local x - -<?x?> = nil -]] - -TEST [[ ----@diagnostic disable: unused-local - ----@type string? -local x - -x = nil -]] - -TEST [[ ----@diagnostic disable: unused-local - ----@type table -local x - -<!x!> = nil -]] - -TEST [[ ----@diagnostic disable: unused-local - -local x - -x = nil -]] - -TEST [[ ----@diagnostic disable: unused-local, undefined-global - ----@type integer -local x - ----@type number -<!x!> = f() -]] - -TEST [[ ----@diagnostic disable: unused-local, undefined-global - ----@type number -local x - ----@type integer -x = f() -]] - -TEST [[ ----@diagnostic disable: unused-local, undefined-global - ----@type number|boolean -local x - ----@type string -<!x!> = f() -]] - -TEST [[ ----@diagnostic disable: unused-local, undefined-global - ----@type number|boolean -local x - ----@type boolean -x = f() -]] - -TEST [[ ----@diagnostic disable: unused-local, undefined-global - ----@type number|boolean -local x - ----@type boolean|string -<!x!> = f() -]] - -TEST [[ ----@diagnostic disable: unused-local, undefined-global - ----@type boolean -local x - -if not x then - return -end - -x = f() -]] - -TEST [[ ----@diagnostic disable: unused-local - ----@type boolean -local x - ----@type integer -local y - -<!x!> = y -]] - -TEST [[ ----@diagnostic disable: unused-local - -local y = true - -local x -x = 1 -x = y -]] - -TEST [[ ----@diagnostic disable: unused-local - -local t = {} - -local x = 0 -x = x + #t -]] - -TEST [[ ----@diagnostic disable: unused-local - -local x = 0 - -x = 1.0 -]] - -TEST [[ ----@diagnostic disable: unused-local - ----@type integer -local x = 0 - -<!x!> = 1.0 -]] - -do return end -TEST [[ ----@diagnostic disable: unused-local - ----@class A ----@field x integer -local t - -<!t.x!> = true -]] - -TEST [[ ----@diagnostic disable: unused-local - ----@class A ----@field x integer -local t - ----@type boolean -local y - -<!t.x!> = y -]] - -TEST [[ ----@diagnostic disable: unused-local - ----@class A ----@field x integer -local t - -t = { - <!x!> = true -} -]] diff --git a/test/diagnostics/init.lua b/test/diagnostics/init.lua index 75d9da6c..2c5dba11 100644 --- a/test/diagnostics/init.lua +++ b/test/diagnostics/init.lua @@ -50,4 +50,4 @@ function TEST(script, ...) end require 'diagnostics.common' ---require 'diagnostics.type-check' +require 'diagnostics.type-check' diff --git a/test/diagnostics/type-check.lua b/test/diagnostics/type-check.lua index 9d9eb3ec..9de3e061 100644 --- a/test/diagnostics/type-check.lua +++ b/test/diagnostics/type-check.lua @@ -1,164 +1,199 @@ local config = require 'config' -config.get(nil, 'Lua.diagnostics.neededFileStatus')['unused-local'] = 'None' +config.add(nil, 'Lua.diagnostics.disable', 'unused-local') +config.add(nil, 'Lua.diagnostics.disable', 'undefined-global') + TEST [[ ----@param table table ----@param metatable table ----@return table -function Setmetatable(table, metatable) end +local x = 0 -Setmetatable(<!1!>, {}) +<!x!> = true ]] TEST [[ ----@param table table ----@param metatable table ----@return table -function Setmetatable(table, metatable) end +---@type integer +local x + +<!x!> = true +]] -Setmetatable(<!'name'!>, {}) +TEST [[ +---@type unknown +local x +x = nil ]] TEST [[ ----@param table table ----@param metatable table ----@return table -function Setmetatable(table, metatable) end +---@type unknown +local x ----@type table -local name ----@type function -local mt ----err -Setmetatable(name, <!mt!>) +x = 1 ]] TEST [[ ----@param p1 string ----@param p2 number ----@return table -local function func1(p1, p2) end +---@type unknown|nil +local x ----@type string -local s ----@type table -local t ----err -func1(s, <!t!>) +x = 1 ]] TEST [[ ----@class bird ----@field wing string +local x = {} ----@class eagle ----@field family bird +x = nil +]] ----@class chicken ----@field family bird +TEST [[ +---@type string +local x ----@param bd eagle -local function fly(bd) end +<?x?> = nil +]] ----@type chicken -local h -fly(<!h!>) +TEST [[ +---@type string? +local x + +x = nil ]] TEST [[ ----@overload fun(x: number, y: number) ----@param x boolean ----@param y boolean -local function f(x, y) end +---@type table +local x -f(true, true) -- OK -f(0, 0) -- OK +<!x!> = nil +]] + +TEST [[ +local x +x = nil ]] TEST [[ ----@class bird -local m = {} -setmetatable(m, {}) -- OK +---@type integer +local x + +---@type number +<!x!> = f() ]] TEST [[ ----@class childString: string -local s ----@param name string -local function f(name) end -f(s) +---@type number +local x + +---@type integer +x = f() ]] TEST [[ ----@class childString: string +---@type number|boolean +local x ---@type string -local s ----@param name childString -local function f(name) end -f(<!s!>) +<!x!> = f() ]] TEST [[ ----@alias searchmode '"ref"'|'"def"'|'"field"'|'"allref"'|'"alldef"'|'"allfield"' +---@type number|boolean +local x ----@param mode searchmode -local function searchRefs(mode)end -searchRefs('ref') +---@type boolean +x = f() ]] TEST [[ ----@class markdown -local mt = {} ----@param language string ----@param text string|markdown -function mt:add(language, text) - if not text then - return - end -end ----@type markdown -local desc +---@type number|boolean +local x -desc:add('md', 'hover') +---@type boolean|string +<!x!> = f() ]] ----可选参数和枚举 TEST [[ ----@param str string ----@param mode? '"left"'|'"right"' ----@return string -local function trim(str, mode) - if mode == "left" then - print(1) - end +---@type boolean +local x + +if not x then + return end -trim('str', 'left') -trim('str', nil) + +x = f() ]] -config.get(nil, 'Lua.diagnostics.neededFileStatus')['unused-local'] = 'Any' +TEST [[ +---@type boolean +local x + +---@type integer +local y + +<!x!> = y +]] ----不完整的函数参数定义,会跳过检查 TEST [[ ----@param mode string -local function status(source, field, mode) - print(source, field, mode) -end -status(1, 2, 'name') +local y = true + +local x +x = 1 +x = y ]] +TEST [[ +local t = {} + +local x = 0 +x = x + #t +]] TEST [[ ----@alias range {start: number, end: number} ----@param uri string ----@param range range -local function location(uri, range) - print(uri, range) -end ----@type range -local val = {} -location('uri', val) +local x = 0 + +x = 1.0 +]] + +TEST [[ +---@type integer +local x = 0 + +<!x!> = 1.0 +]] + +do return end +TEST [[ +---@diagnostic disable: unused-local + +---@class A +---@field x integer +local t + +<!t.x!> = true ]] + +TEST [[ +---@diagnostic disable: unused-local + +---@class A +---@field x integer +local t + +---@type boolean +local y + +<!t.x!> = y +]] + +TEST [[ +---@diagnostic disable: unused-local + +---@class A +---@field x integer +local t + +t = { + <!x!> = true +} +]] + +config.remove(nil, 'Lua.diagnostics.disable', 'unused-local') +config.remove(nil, 'Lua.diagnostics.disable', 'undefined-global') |