summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2023-01-30 20:04:02 +0800
committer最萌小汐 <sumneko@hotmail.com>2023-01-30 20:04:02 +0800
commit76b8cf3236db2efc7f73f3508212d99199b73b52 (patch)
tree25c028edc02fd1b1732c64ed0d39b13cc8ec1fa6 /test
parent4a9ab5b1ae9889e9732b53ad8cdf3b6db5394c3b (diff)
downloadlua-language-server-76b8cf3236db2efc7f73f3508212d99199b73b52.zip
limit error message for type dismatch
resolve #1838
Diffstat (limited to 'test')
-rw-r--r--test/diagnostics/init.lua4
-rw-r--r--test/diagnostics/type-check.lua52
2 files changed, 56 insertions, 0 deletions
diff --git a/test/diagnostics/init.lua b/test/diagnostics/init.lua
index 2d2374a5..3c19e58d 100644
--- a/test/diagnostics/init.lua
+++ b/test/diagnostics/init.lua
@@ -47,6 +47,10 @@ function TEST(script, ...)
end
files.remove(TESTURI)
+
+ return function (callback)
+ callback(origins)
+ end
end
require 'diagnostics.common'
diff --git a/test/diagnostics/type-check.lua b/test/diagnostics/type-check.lua
index ba0c78b4..7882df69 100644
--- a/test/diagnostics/type-check.lua
+++ b/test/diagnostics/type-check.lua
@@ -1128,6 +1128,58 @@ local t = {
}
]]
+TEST [[
+local x
+
+if X then
+ x = 'A'
+elseif X then
+ x = 'B'
+else
+ x = 'C'
+end
+
+local y = x
+
+<!y!> = nil
+]]
+(function (diags)
+ local diag = diags[1]
+ assert(diag.message == [[
+已显式定义变量的类型为 `string` ,不能再将其类型转换为 `nil`。
+- `nil` 无法匹配 `string`
+- 类型 `nil` 无法匹配 `string`]])
+end)
+
+
+TEST [[
+---@type 'A'|'B'|'C'|'D'|'E'|'F'|'G'|'H'|'I'|'J'|'K'|'L'|'M'|'N'|'O'|'P'|'Q'|'R'|'S'|'T'|'U'|'V'|'W'|'X'|'Y'|'Z'
+local x
+
+<!x!> = nil
+]]
+(function (diags)
+ local diag = diags[1]
+ assert(diag.message == [[
+已显式定义变量的类型为 `'A'|'B'|'C'|'D'|'E'...(+21)` ,不能再将其类型转换为 `nil`。
+- `nil` 无法匹配 `'A'|'B'|'C'|'D'|'E'...(+21)`
+- `nil` 无法匹配 `'A'|'B'|'C'|'D'|'E'...(+21)` 中的任何子类
+- 类型 `nil` 无法匹配 `'Z'`
+- 类型 `nil` 无法匹配 `'Y'`
+- 类型 `nil` 无法匹配 `'X'`
+- 类型 `nil` 无法匹配 `'W'`
+- 类型 `nil` 无法匹配 `'V'`
+- 类型 `nil` 无法匹配 `'U'`
+- 类型 `nil` 无法匹配 `'T'`
+- 类型 `nil` 无法匹配 `'S'`
+- 类型 `nil` 无法匹配 `'R'`
+- 类型 `nil` 无法匹配 `'Q'`
+...(+13)
+- 类型 `nil` 无法匹配 `'C'`
+- 类型 `nil` 无法匹配 `'B'`
+- 类型 `nil` 无法匹配 `'A'`]])
+end)
+
config.remove(nil, 'Lua.diagnostics.disable', 'unused-local')
config.remove(nil, 'Lua.diagnostics.disable', 'unused-function')
config.remove(nil, 'Lua.diagnostics.disable', 'undefined-global')