summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2022-06-25 03:48:33 +0800
committer最萌小汐 <sumneko@hotmail.com>2022-06-25 03:48:33 +0800
commitfa7375775e97d68ce7507b0c7e50eced84bab2ac (patch)
tree6e54e79cc4f8b4b1cdf6e97c8bad09db09673902
parent711c5d44484366ac80d02ff42fc8fab908b10739 (diff)
downloadlua-language-server-fa7375775e97d68ce7507b0c7e50eced84bab2ac.zip
add 2 type related settings
`type.castNumberToInteger` and `type.weakUnionCheck`
-rw-r--r--changelog.md5
-rw-r--r--script/config/template.lua4
-rw-r--r--script/vm/type.lua37
-rw-r--r--test/diagnostics/type-check.lua42
4 files changed, 78 insertions, 10 deletions
diff --git a/changelog.md b/changelog.md
index ff65b5d8..c9b64e0f 100644
--- a/changelog.md
+++ b/changelog.md
@@ -7,6 +7,11 @@
* `param-type-mismatch`
* `unknown-cast-variable`
* `cast-type-mismatch`
+* `NEW` settings:
+ * `diagnostics.groupSeverity`
+ * `diagnostics.groupFileStatus`
+ * `type.castNumberToInteger`
+ * `type.weakUnionCheck`
* `CHG` infer `nil` as redundant return value
```lua
local function f() end
diff --git a/script/config/template.lua b/script/config/template.lua
index 672bad21..9ede5e96 100644
--- a/script/config/template.lua
+++ b/script/config/template.lua
@@ -330,7 +330,7 @@ local template = {
['Lua.hover.viewString'] = Type.Boolean >> true,
['Lua.hover.viewStringMax'] = Type.Integer >> 1000,
['Lua.hover.viewNumber'] = Type.Boolean >> true,
- ['Lua.hover.previewFields'] = Type.Integer >> 30,
+ ['Lua.hover.previewFields'] = Type.Integer >> 50,
['Lua.hover.enumsLimit'] = Type.Integer >> 5,
['Lua.hover.expandAlias'] = Type.Boolean >> true,
['Lua.semantic.enable'] = Type.Boolean >> true,
@@ -359,6 +359,8 @@ local template = {
['Lua.spell.dict'] = Type.Array(Type.String),
['Lua.telemetry.enable'] = Type.Or(Type.Boolean >> false, Type.Nil) >> nil,
['Lua.misc.parameters'] = Type.Array(Type.String),
+ ['Lua.type.castNumberToInteger'] = Type.Boolean >> false,
+ ['Lua.type.weakUnionCheck'] = Type.Boolean >> false,
-- VSCode
['files.associations'] = Type.Hash(Type.String, Type.String),
diff --git a/script/vm/type.lua b/script/vm/type.lua
index 5211c3cb..d132619c 100644
--- a/script/vm/type.lua
+++ b/script/vm/type.lua
@@ -1,6 +1,7 @@
---@class vm
local vm = require 'vm.vm'
local guide = require 'parser.guide'
+local config = require 'config.config'
---@param object vm.node.object
---@return string?
@@ -53,18 +54,33 @@ function vm.isSubType(uri, child, parent, mark)
end
child = global
elseif child.type == 'vm.node' then
- for n in child:eachObject() do
- if getNodeName(n)
- and not vm.isSubType(uri, n, parent, mark) then
- return false
+ if config.get(uri, 'Lua.type.weakUnionCheck') then
+ for n in child:eachObject() do
+ if getNodeName(n)
+ and vm.isSubType(uri, n, parent, mark) then
+ return true
+ end
end
- end
- if child:isOptional() then
- if not vm.isSubType(uri, 'nil', parent, mark) then
- return false
+ if child:isOptional() then
+ if vm.isSubType(uri, 'nil', parent, mark) then
+ return true
+ end
end
+ return false
+ else
+ for n in child:eachObject() do
+ if getNodeName(n)
+ and not vm.isSubType(uri, n, parent, mark) then
+ return false
+ end
+ end
+ if child:isOptional() then
+ if not vm.isSubType(uri, 'nil', parent, mark) then
+ return false
+ end
+ end
+ return true
end
- return true
end
if type(parent) == 'string' then
@@ -110,6 +126,9 @@ function vm.isSubType(uri, child, parent, mark)
end
if parentName == 'integer' and childName == 'number' then
+ if config.get(uri, 'Lua.type.castNumberToInteger') then
+ return true
+ end
if child.type == 'number'
and child[1]
and not math.tointeger(child[1]) then
diff --git a/test/diagnostics/type-check.lua b/test/diagnostics/type-check.lua
index 036456fe..711211e8 100644
--- a/test/diagnostics/type-check.lua
+++ b/test/diagnostics/type-check.lua
@@ -437,5 +437,47 @@ local x
t[#t+1] = x
]]
+TEST [[
+---@type number
+local n
+---@type integer
+local i
+
+<?i?> = n
+]]
+
+config.set(nil, 'Lua.type.castNumberToInteger', true)
+TEST [[
+---@type number
+local n
+---@type integer
+local i
+
+i = n
+]]
+config.set(nil, 'Lua.type.castNumberToInteger', false)
+
+TEST [[
+---@type number|boolean
+local nb
+
+---@type number
+local n
+
+<?n?> = nb
+]]
+
+config.set(nil, 'Lua.type.weakUnionCheck', true)
+TEST [[
+---@type number|boolean
+local nb
+
+---@type number
+local n
+
+n = nb
+]]
+config.set(nil, 'Lua.type.weakUnionCheck', false)
+
config.remove(nil, 'Lua.diagnostics.disable', 'unused-local')
config.remove(nil, 'Lua.diagnostics.disable', 'undefined-global')