Skip to content

Commit b1bba09

Browse files
committed
Refine boolean-context diagnostics
1 parent 655a6ed commit b1bba09

3 files changed

Lines changed: 32 additions & 8 deletions

File tree

script/core/diagnostics/boolean-context.lua

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,32 @@ local function isBooleanOnly(infer, uri)
2323
if infer:hasAny(uri) or infer:hasUnknown(uri) then
2424
return nil
2525
end
26-
local hasBoolean = infer:hasType(uri, 'boolean')
27-
if not hasBoolean then
28-
return false
29-
end
3026
for view in infer:eachView(uri) do
31-
if view ~= 'boolean' then
27+
if view ~= 'boolean'
28+
and view ~= 'true'
29+
and view ~= 'false' then
3230
return false
3331
end
3432
end
3533
return true
3634
end
3735

36+
---@param source parser.object?
37+
---@return boolean
38+
local function isConditionFilter(source)
39+
if not source or not source.parent then
40+
return false
41+
end
42+
local parent = source.parent
43+
if parent.type ~= 'ifblock'
44+
and parent.type ~= 'elseifblock'
45+
and parent.type ~= 'while'
46+
and parent.type ~= 'repeat' then
47+
return false
48+
end
49+
return parent.filter == source
50+
end
51+
3852
---@param source parser.object?
3953
---@param uri uri
4054
---@param callback fun(result: diag.result)
@@ -90,6 +104,11 @@ return function (uri, callback)
90104
if op ~= 'and' and op ~= 'or' then
91105
return
92106
end
107+
if isConditionFilter(source) then
108+
checkExpression(source[1], uri, callback)
109+
checkExpression(source[2], uri, callback)
110+
return
111+
end
93112
checkExpression(source[1], uri, callback)
94113
end)
95114
end

script/vm/node.lua

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,6 @@ end
148148
---Almost an inverse of alwaysTruthy, but strict about "any" and "unknown" types.
149149
---@return boolean
150150
function mt:alwaysFalsy()
151-
if self.optional then
152-
return false
153-
end
154151
if #self == 0 then
155152
return false
156153
end

test/diagnostics/boolean-context.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ local y = <!d!> and "4"
1919
local e
2020
if e then end
2121
22+
---@type true
23+
local t
24+
if t then end
25+
2226
---@type boolean|nil
2327
local g
2428
if <!g!> then end
@@ -27,6 +31,10 @@ if <!g!> then end
2731
local h
2832
if <!h!> and true then end
2933
34+
---@type number
35+
local i
36+
if true and <!i!> then end
37+
3038
---@type any
3139
local f
3240
local z = f or "3"

0 commit comments

Comments
 (0)