From 87e0a180f97cd6bc31909b60228f1148c0289529 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Fri, 1 Jul 2022 16:10:26 +0800 Subject: #1260 `type.weakNilCheck` --- script/config/template.lua | 1 + script/utility.lua | 4 ++-- script/vm/type.lua | 12 +++++------- 3 files changed, 8 insertions(+), 9 deletions(-) (limited to 'script') diff --git a/script/config/template.lua b/script/config/template.lua index c7162012..3d5bad3d 100644 --- a/script/config/template.lua +++ b/script/config/template.lua @@ -366,6 +366,7 @@ local template = { ['Lua.misc.parameters'] = Type.Array(Type.String), ['Lua.type.castNumberToInteger'] = Type.Boolean >> false, ['Lua.type.weakUnionCheck'] = Type.Boolean >> false, + ['Lua.type.weakNilCheck'] = Type.Boolean >> false, -- VSCode ['files.associations'] = Type.Hash(Type.String, Type.String), diff --git a/script/utility.lua b/script/utility.lua index 034f19d5..e7aabaca 100644 --- a/script/utility.lua +++ b/script/utility.lua @@ -275,7 +275,7 @@ local function sortTable(tbl) end --- 创建一个有序表 ----@param tbl table {optional = 'self'} +---@param tbl? table ---@return table function m.container(tbl) return sortTable(tbl) @@ -570,7 +570,7 @@ end ---遍历文本的每一行 ---@param text string ---@param keepNL? boolean # 保留换行符 ----@return fun(text:string):string, integer +---@return fun():string, integer function m.eachLine(text, keepNL) local offset = 1 local lineCount = 0 diff --git a/script/vm/type.lua b/script/vm/type.lua index 399cbffc..ac3b8986 100644 --- a/script/vm/type.lua +++ b/script/vm/type.lua @@ -62,20 +62,18 @@ function vm.isSubType(uri, child, parent, mark) return true end end - if child:isOptional() then - if vm.isSubType(uri, 'nil', parent, mark) then - return true - end - end return false else + local weakNil = config.get(uri, 'Lua.type.weakNilCheck') for n in child:eachObject() do - if getNodeName(n) + local nodeName = getNodeName(n) + if nodeName + and not (nodeName == 'nil' and weakNil) and not vm.isSubType(uri, n, parent, mark) then return false end end - if child:isOptional() then + if not weakNil and child:isOptional() then if not vm.isSubType(uri, 'nil', parent, mark) then return false end -- cgit v1.2.3