diff options
-rw-r--r-- | changelog.md | 5 | ||||
-rw-r--r-- | script/config/template.lua | 4 | ||||
-rw-r--r-- | script/vm/type.lua | 37 | ||||
-rw-r--r-- | test/diagnostics/type-check.lua | 42 |
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') |