diff options
m--------- | 3rd/lovr-api | 0 | ||||
-rw-r--r-- | changelog.md | 4 | ||||
-rw-r--r-- | doc/en-us/config.md | 19 | ||||
-rw-r--r-- | doc/pt-br/config.md | 19 | ||||
-rw-r--r-- | doc/zh-cn/config.md | 19 | ||||
-rw-r--r-- | doc/zh-tw/config.md | 19 | ||||
-rw-r--r-- | locale/en-us/setting.lua | 6 | ||||
-rw-r--r-- | locale/pt-br/setting.lua | 6 | ||||
-rw-r--r-- | locale/zh-cn/setting.lua | 6 | ||||
-rw-r--r-- | locale/zh-tw/setting.lua | 6 | ||||
-rw-r--r-- | script/config/template.lua | 1 | ||||
-rw-r--r-- | script/utility.lua | 4 | ||||
-rw-r--r-- | script/vm/type.lua | 12 | ||||
-rw-r--r-- | test/diagnostics/type-check.lua | 22 |
14 files changed, 134 insertions, 9 deletions
diff --git a/3rd/lovr-api b/3rd/lovr-api -Subproject 287070fc7e2e7b75407c4744c173497c89e1e51 +Subproject c148bfadad94144d423c6f9f7ce6033dfdc3779 diff --git a/changelog.md b/changelog.md index d88c78ab..6446f811 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,9 @@ # changelog +## 3.4.1 +* `NEW` settings: + * `type.weakNilCheck` + ## 3.4.0 `2022-6-29` * `NEW` diagnostics: diff --git a/doc/en-us/config.md b/doc/en-us/config.md index 2a76be14..7317271d 100644 --- a/doc/en-us/config.md +++ b/doc/en-us/config.md @@ -1472,6 +1472,25 @@ boolean false ``` +# type.weakNilCheck + +When checking the type of union type, ignore the `nil` in it. + +When this setting is `false`, the `number|nil` type cannot be assigned to the `number` type. It can be with `true`. + + +## type + +```ts +boolean +``` + +## default + +```jsonc +false +``` + # type.weakUnionCheck Once one subtype of a union type meets the condition, the union type also meets the condition. diff --git a/doc/pt-br/config.md b/doc/pt-br/config.md index 55604b55..9cbefc2b 100644 --- a/doc/pt-br/config.md +++ b/doc/pt-br/config.md @@ -1472,6 +1472,25 @@ boolean false ``` +# type.weakNilCheck + +When checking the type of union type, ignore the `nil` in it. + +When this setting is `false`, the `number|nil` type cannot be assigned to the `number` type. It can be with `true`. + + +## type + +```ts +boolean +``` + +## default + +```jsonc +false +``` + # type.weakUnionCheck Once one subtype of a union type meets the condition, the union type also meets the condition. diff --git a/doc/zh-cn/config.md b/doc/zh-cn/config.md index 77f9b74d..cbf12bb1 100644 --- a/doc/zh-cn/config.md +++ b/doc/zh-cn/config.md @@ -1471,6 +1471,25 @@ boolean false ``` +# type.weakNilCheck + +对联合类型进行类型检查时,忽略其中的 `nil`。 + +此设置为 `false` 时,`numer|nil` 类型无法赋给 `number` 类型;为 `true` 是则可以。 + + +## type + +```ts +boolean +``` + +## default + +```jsonc +false +``` + # type.weakUnionCheck 联合类型中只要有一个子类型满足条件,则联合类型也满足条件。 diff --git a/doc/zh-tw/config.md b/doc/zh-tw/config.md index 62a76e57..6d0325b9 100644 --- a/doc/zh-tw/config.md +++ b/doc/zh-tw/config.md @@ -1471,6 +1471,25 @@ boolean false ``` +# type.weakNilCheck + +When checking the type of union type, ignore the `nil` in it. + +When this setting is `false`, the `number|nil` type cannot be assigned to the `number` type. It can be with `true`. + + +## type + +```ts +boolean +``` + +## default + +```jsonc +false +``` + # type.weakUnionCheck 同位類型中只要有一個子類型滿足條件,則同位類型也滿足條件。 diff --git a/locale/en-us/setting.lua b/locale/en-us/setting.lua index b2ba86fd..5ce69de7 100644 --- a/locale/en-us/setting.lua +++ b/locale/en-us/setting.lua @@ -275,6 +275,12 @@ Once one subtype of a union type meets the condition, the union type also meets When this setting is `false`, the `number|boolean` type cannot be assigned to the `number` type. It can be with `true`. ]] +config.type.weakNilCheck = +[[ +When checking the type of union type, ignore the `nil` in it. + +When this setting is `false`, the `number|nil` type cannot be assigned to the `number` type. It can be with `true`. +]] config.diagnostics['unused-local'] = 'Enable unused local variable diagnostics.' config.diagnostics['unused-function'] = diff --git a/locale/pt-br/setting.lua b/locale/pt-br/setting.lua index 3a8049af..e8271ce5 100644 --- a/locale/pt-br/setting.lua +++ b/locale/pt-br/setting.lua @@ -275,6 +275,12 @@ Once one subtype of a union type meets the condition, the union type also meets When this setting is `false`, the `number|boolean` type cannot be assigned to the `number` type. It can be with `true`. ]] +config.type.weakNilCheck = -- TODO: need translate! +[[ +When checking the type of union type, ignore the `nil` in it. + +When this setting is `false`, the `number|nil` type cannot be assigned to the `number` type. It can be with `true`. +]] config.diagnostics['unused-local'] = -- TODO: need translate! '未使用的局部变量' config.diagnostics['unused-function'] = -- TODO: need translate! diff --git a/locale/zh-cn/setting.lua b/locale/zh-cn/setting.lua index 9fcd5e06..54de9293 100644 --- a/locale/zh-cn/setting.lua +++ b/locale/zh-cn/setting.lua @@ -274,6 +274,12 @@ config.type.weakUnionCheck = 此设置为 `false` 时,`number|boolean` 类型无法赋给 `number` 类型;为 `true` 时则可以。 ]] +config.type.weakNilCheck = +[[ +对联合类型进行类型检查时,忽略其中的 `nil`。 + +此设置为 `false` 时,`numer|nil` 类型无法赋给 `number` 类型;为 `true` 是则可以。 +]] config.diagnostics['unused-local'] = '未使用的局部变量' config.diagnostics['unused-function'] = diff --git a/locale/zh-tw/setting.lua b/locale/zh-tw/setting.lua index 3af50c5b..17a3435a 100644 --- a/locale/zh-tw/setting.lua +++ b/locale/zh-tw/setting.lua @@ -274,6 +274,12 @@ config.type.weakUnionCheck = 此設定為 `false` 時,`number|boolean` 類型無法賦給 `number` 類型;為 `true` 時則可以。 ]] +config.type.weakNilCheck = -- TODO: need translate! +[[ +When checking the type of union type, ignore the `nil` in it. + +When this setting is `false`, the `number|nil` type cannot be assigned to the `number` type. It can be with `true`. +]] config.diagnostics['unused-local'] = '未使用的區域變數' config.diagnostics['unused-function'] = 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 diff --git a/test/diagnostics/type-check.lua b/test/diagnostics/type-check.lua index f4765370..32f60f82 100644 --- a/test/diagnostics/type-check.lua +++ b/test/diagnostics/type-check.lua @@ -609,5 +609,27 @@ local mt = G mt._x = nil ]] +config.set(nil, 'Lua.type.weakNilCheck', true) +TEST [[ +---@type number? +local nb + +---@type number +local n + +n = nb +]] + +TEST [[ +---@type number|nil +local nb + +---@type number +local n + +n = nb +]] +config.set(nil, 'Lua.type.weakNilCheck', false) + config.remove(nil, 'Lua.diagnostics.disable', 'unused-local') config.remove(nil, 'Lua.diagnostics.disable', 'undefined-global') |