Skip to content

Commit 6ba0c93

Browse files
authored
Merge pull request #2867 from tomlau10/feat/rawset_with_class
Support using `---@class` on `rawset(_G, ...)` to annotate the created global variable
2 parents 575d607 + 1c3706f commit 6ba0c93

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed

changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
* `NEW` Infer function parameter types when overriding the same-named class function in an instance of that class [#2158](https://github.com/LuaLS/lua-language-server/issues/2158)
77
* `FIX` Eliminate floating point error in test benchmark output
88
* `FIX` Remove luamake install from make scripts
9+
* `FIX` Incorrect `table` type injected to the global variable created by `rawset(_G, ...)` [#2863](https://github.com/LuaLS/lua-language-server/issues/2863)
910
* `NEW` Types with literal fields can be narrowed.
1011
* `NEW` Reference addons installed via the addon manager with `${addons}` [#2866](https://github.com/LuaLS/lua-language-server/pull/2866).
12+
* `NEW` Support using `---@class` on `rawset(_G, ...)` to annotate the created global variable [#2862](https://github.com/LuaLS/lua-language-server/issues/2862)
1113

1214
## 3.10.6
1315
`2024-9-10`

script/parser/luadoc.lua

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1988,7 +1988,9 @@ local function bindDocsBetween(sources, binded, start, finish)
19881988
or src.type == 'setmethod'
19891989
or src.type == 'function'
19901990
or src.type == 'return'
1991-
or src.type == '...' then
1991+
or src.type == '...'
1992+
or src.type == 'call' -- for `rawset`
1993+
then
19921994
if bindDoc(src, binded) then
19931995
ok = true
19941996
end
@@ -2108,12 +2110,23 @@ local bindDocAccept = {
21082110
'setfield' , 'setmethod' , 'setindex' ,
21092111
'tablefield', 'tableindex', 'self' ,
21102112
'function' , 'return' , '...' ,
2113+
'call',
21112114
}
21122115

21132116
local function bindDocs(state)
21142117
local text = state.lua
21152118
local sources = {}
21162119
guide.eachSourceTypes(state.ast, bindDocAccept, function (src)
2120+
-- allow binding docs with rawset(_G, "key", value)
2121+
if src.type == 'call' then
2122+
if src.node.special ~= 'rawset' or not src.args then
2123+
return
2124+
end
2125+
local g, key = src.args[1], src.args[2]
2126+
if not g or not key or g.special ~= '_G' then
2127+
return
2128+
end
2129+
end
21172130
sources[#sources+1] = src
21182131
end)
21192132
table.sort(sources, function (a, b)

script/vm/compiler.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1867,6 +1867,10 @@ local compilerSwitch = util.switch()
18671867
end)
18681868
: case 'call'
18691869
: call(function (source)
1870+
-- ignore rawset
1871+
if source.node.special == 'rawset' then
1872+
return
1873+
end
18701874
local node = getReturn(source.node, 1, source.args)
18711875
if not node then
18721876
return

test/type_inference/common.lua

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,6 +1321,32 @@ TEST 'Test' [[
13211321
_G.<?Test?> = {}
13221322
]]
13231323

1324+
TEST 'integer' [[
1325+
rawset(_G, 'Test', 1)
1326+
print(<?Test?>)
1327+
]]
1328+
1329+
TEST 'string' [[
1330+
rawset(_G, 'Test', '')
1331+
print(<?Test?>)
1332+
]]
1333+
1334+
TEST 'boolean' [[
1335+
rawset(_G, 'Test', true)
1336+
print(<?Test?>)
1337+
]]
1338+
1339+
TEST 'table' [[
1340+
rawset(_G, 'Test', {})
1341+
print(<?Test?>)
1342+
]]
1343+
1344+
TEST 'Test' [[
1345+
---@class Test
1346+
rawset(_G, 'Test', {})
1347+
print(<?Test?>)
1348+
]]
1349+
13241350
TEST 'integer' [[
13251351
local mt = {}
13261352

0 commit comments

Comments
 (0)