1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
local files = require 'files'
local furi = require 'file-uri'
local vm = require 'vm'
local guide = require 'parser.guide'
local catch = require 'catch'
rawset(_G, 'TEST', true)
local function getSource(uri, pos)
local state = files.getState(uri)
if not state then
return
end
local result
guide.eachSourceContain(state.ast, pos, function (source)
if source.type == 'local'
or source.type == 'getlocal'
or source.type == 'setlocal'
or source.type == 'setglobal'
or source.type == 'getglobal'
or source.type == 'field'
or source.type == 'method'
or source.type == 'function'
or source.type == 'table'
or source.type == 'doc.type.name' then
result = source
end
end)
return result
end
local EXISTS = {}
local function eq(a, b)
if a == EXISTS and b ~= nil then
return true
end
if b == EXISTS and a ~= nil then
return true
end
local tp1, tp2 = type(a), type(b)
if tp1 ~= tp2 then
return false
end
if tp1 == 'table' then
local mark = {}
for k in pairs(a) do
if not eq(a[k], b[k]) then
return false
end
mark[k] = true
end
for k in pairs(b) do
if not mark[k] then
return false
end
end
return true
end
return a == b
end
---@diagnostic disable: await-in-sync
function TEST(expect)
local sourcePos, sourceUri
for _, file in ipairs(expect) do
local script, list = catch(file.content, '?')
local uri = furi.encode(file.path)
files.setText(uri, script)
files.compileState(uri)
if #list['?'] > 0 then
sourceUri = uri
sourcePos = (list['?'][1][1] + list['?'][1][2]) // 2
end
end
local _ <close> = function ()
for _, info in ipairs(expect) do
files.remove(furi.encode(info.path))
end
end
local source = getSource(sourceUri, sourcePos)
assert(source)
local view = vm.getInfer(source):view(sourceUri)
assert(eq(view, expect.infer))
end
TEST {
{
path = 'a.lua',
content = [[
---@class T
local x
---@class V
x.y = 1
]],
},
{
path = 'b.lua',
content = [[
---@type T
local x
if x.y then
print(x.<?y?>)
end
]],
},
infer = 'V',
}
TEST {
{ path = 'a.lua', content = [[
X = 1
X = true
]], },
{ path = 'b.lua', content = [[
print(<?X?>)
]], },
infer = 'integer',
}
TEST {
{ path = 'a.lua', content = [[
---@meta
X = 1
X = true
]], },
{ path = 'b.lua', content = [[
print(<?X?>)
]], },
infer = 'boolean|integer',
}
|