Skip to content

Commit 3bdd74a

Browse files
committed
fix #1637
1 parent 2638eeb commit 3bdd74a

File tree

5 files changed

+54
-22
lines changed

5 files changed

+54
-22
lines changed

changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ server will generate `doc.json` and `doc.md` in `LOGPATH`.
4040
* `FIX` [#1599]
4141
* `FIX` [#1606]
4242
* `FIX` [#1608]
43+
* `FIX` [#1637]
4344

4445
[#1177]: https://github.com/sumneko/lua-language-server/issues/1177
4546
[#1458]: https://github.com/sumneko/lua-language-server/issues/1458
@@ -53,6 +54,7 @@ server will generate `doc.json` and `doc.md` in `LOGPATH`.
5354
[#1599]: https://github.com/sumneko/lua-language-server/issues/1599
5455
[#1606]: https://github.com/sumneko/lua-language-server/issues/1606
5556
[#1608]: https://github.com/sumneko/lua-language-server/issues/1608
57+
[#1637]: https://github.com/sumneko/lua-language-server/issues/1637
5658

5759
## 3.5.6
5860
`2022-9-16`

meta/template/io.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ function io.write(...) end
101101
---@class file*
102102
local file = {}
103103

104-
---@alias readmode integer
104+
---@alias readmode integer|string
105105
---#if VERSION >= 5.3 then
106106
---| '"n"' # ---#DESTAIL 'readmode.n'
107107
---| '"a"' # ---#DESTAIL 'readmode.a'

script/progress.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ function m.update()
145145
end
146146

147147
---创建一个进度条
148-
---@param uri uri
148+
---@param uri? uri
149149
---@param title string # 标题
150150
---@param delay number # 至少经过这么久之后才会显示出来
151151
function m.create(uri, title, delay)

script/vm/type.lua

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,27 @@ local function checkValue(parent, child)
8989
return true
9090
end
9191

92+
---@param name string
93+
---@param suri uri
94+
---@return boolean
95+
local function isAlias(name, suri)
96+
local global = vm.getGlobal('type', name)
97+
if not global then
98+
return false
99+
end
100+
for _, set in ipairs(global:getSets(suri)) do
101+
if set.type == 'doc.alias' then
102+
return true
103+
end
104+
end
105+
return false
106+
end
107+
92108
---@param uri uri
93109
---@param child vm.node|string|vm.node.object
94110
---@param parent vm.node|string|vm.node.object
95111
---@param mark? table
96-
---@return boolean
112+
---@return boolean?
97113
function vm.isSubType(uri, child, parent, mark)
98114
mark = mark or {}
99115

@@ -109,31 +125,43 @@ function vm.isSubType(uri, child, parent, mark)
109125
for n in child:eachObject() do
110126
if getNodeName(n) then
111127
hasKnownType = true
112-
if vm.isSubType(uri, n, parent, mark) then
128+
if vm.isSubType(uri, n, parent, mark) == true then
113129
return true
114130
end
115131
end
116132
end
117133
return not hasKnownType
118134
else
119-
local weakNil = config.get(uri, 'Lua.type.weakNilCheck')
135+
local weakNil = config.get(uri, 'Lua.type.weakNilCheck')
120136
for n in child:eachObject() do
121137
local nodeName = getNodeName(n)
122138
if nodeName
123139
and not (nodeName == 'nil' and weakNil)
124-
and not vm.isSubType(uri, n, parent, mark) then
140+
and vm.isSubType(uri, n, parent, mark) == false then
125141
return false
126142
end
127143
end
128144
if not weakNil and child:isOptional() then
129-
if not vm.isSubType(uri, 'nil', parent, mark) then
145+
if vm.isSubType(uri, 'nil', parent, mark) == false then
130146
return false
131147
end
132148
end
133149
return true
134150
end
135151
end
136152

153+
---@cast child vm.node.object
154+
local childName = getNodeName(child)
155+
if childName == 'any'
156+
or childName == 'unknown' then
157+
return true
158+
end
159+
160+
if not childName
161+
or isAlias(childName, uri) then
162+
return nil
163+
end
164+
137165
if type(parent) == 'string' then
138166
local global = vm.getGlobal('type', parent)
139167
if not global then
@@ -143,35 +171,34 @@ function vm.isSubType(uri, child, parent, mark)
143171
elseif parent.type == 'vm.node' then
144172
for n in parent:eachObject() do
145173
if getNodeName(n)
146-
and vm.isSubType(uri, child, n, mark) then
174+
and vm.isSubType(uri, child, n, mark) == true then
147175
return true
148176
end
149177
if n.type == 'doc.generic.name' then
150178
return true
151179
end
152180
end
153181
if parent:isOptional() then
154-
if vm.isSubType(uri, child, 'nil', mark) then
182+
if vm.isSubType(uri, child, 'nil', mark) == true then
155183
return true
156184
end
157185
end
158186
return false
159187
end
160188

161-
---@cast child vm.node.object
162189
---@cast parent vm.node.object
163190

164-
local childName = getNodeName(child)
165191
local parentName = getNodeName(parent)
166-
if childName == 'any'
167-
or parentName == 'any'
168-
or childName == 'unknown'
169-
or parentName == 'unknown'
170-
or not childName
171-
or not parentName then
192+
if parentName == 'any'
193+
or parentName == 'unknown' then
172194
return true
173195
end
174196

197+
if not parentName
198+
or isAlias(parentName, uri) then
199+
return nil
200+
end
201+
175202
if childName == parentName then
176203
if not checkValue(parent, child) then
177204
return false
@@ -223,15 +250,11 @@ function vm.isSubType(uri, child, parent, mark)
223250
for _, ext in ipairs(set.extends) do
224251
if ext.type == 'doc.extends.name'
225252
and (not isBasicType or guide.isBasicType(ext[1]))
226-
and vm.isSubType(uri, ext[1], parent, mark) then
253+
and vm.isSubType(uri, ext[1], parent, mark) == true then
227254
return true
228255
end
229256
end
230257
end
231-
if set.type == 'doc.alias'
232-
or set.type == 'doc.enum' then
233-
return true
234-
end
235258
end
236259
end
237260
mark[childName] = nil

test/diagnostics/type-check.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,13 @@ local function f(x)
834834
end
835835
]]
836836

837+
TEST [[
838+
---@alias test boolean
839+
840+
---@type test
841+
local <!test!> = 4
842+
]]
843+
837844
config.remove(nil, 'Lua.diagnostics.disable', 'unused-local')
838845
config.remove(nil, 'Lua.diagnostics.disable', 'unused-function')
839846
config.remove(nil, 'Lua.diagnostics.disable', 'undefined-global')

0 commit comments

Comments
 (0)