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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#[cfg(test)]
mod tests {
use crate::handlers::test_lib::{ProviderVirtualWorkspace, VirtualLocation, check};
use glua_code_analysis::Emmyrc;
use googletest::prelude::*;
#[gtest]
fn test_1() -> Result<()> {
let mut ws = ProviderVirtualWorkspace::new();
ws.def_file(
"2.lua",
r#"
delete = require("virtual_0").delete
delete()
"#,
);
ws.def_file(
"3.lua",
r#"
delete = require("virtual_0").delete
delete()
"#,
);
check!(ws.check_implementation(
r#"
local M = {}
function M.de<??>lete(a)
end
return M
"#,
vec![VirtualLocation {
file: "".to_string(),
line: 2,
}],
));
Ok(())
}
#[gtest]
fn test_2() -> Result<()> {
let mut ws = ProviderVirtualWorkspace::new();
ws.def_file(
"1.lua",
r#"
---@class (partial) Test
test = {}
test.a = 1
"#,
);
ws.def_file(
"2.lua",
r#"
---@class (partial) Test
test = {}
test.a = 1
"#,
);
ws.def_file(
"3.lua",
r#"
local a = test.a
"#,
);
check!(ws.check_implementation(
r#"
t<??>est
"#,
vec![
VirtualLocation {
file: "".to_string(),
line: 1,
},
VirtualLocation {
file: "1.lua".to_string(),
line: 2,
},
VirtualLocation {
file: "2.lua".to_string(),
line: 2,
}
],
));
Ok(())
}
#[gtest]
fn test_3() -> Result<()> {
let mut ws = ProviderVirtualWorkspace::new();
ws.def_file(
"1.lua",
r#"
---@class YYY
---@field a number
yyy = {}
if false then
yyy.a = 1
if yyy.a then
end
end
"#,
);
check!(ws.check_implementation(
r#"
yyy.<??>a = 2
"#,
vec![
VirtualLocation {
file: "".to_string(),
line: 1,
},
VirtualLocation {
file: "1.lua".to_string(),
line: 2,
},
VirtualLocation {
file: "1.lua".to_string(),
line: 6,
},
],
));
Ok(())
}
#[gtest]
fn test_table_field_definition_1() -> Result<()> {
let mut ws = ProviderVirtualWorkspace::new();
check!(ws.check_implementation(
r#"
---@class T
---@field func fun(self: T) 注释注释
---@type T
local t = {
func = function(self)
end,
}
t:fun<??>c()
"#,
vec![
VirtualLocation {
file: "".to_string(),
line: 2,
},
VirtualLocation {
file: "".to_string(),
line: 6,
},
],
));
Ok(())
}
#[gtest]
fn test_table_field_definition_2() -> Result<()> {
let mut ws = ProviderVirtualWorkspace::new();
check!(ws.check_implementation(
r#"
---@class T
---@field func fun(self: T) 注释注释
---@type T
local t = {
f<??>unc = function(self)
end,
}
"#,
vec![
VirtualLocation {
file: "".to_string(),
line: 2,
},
VirtualLocation {
file: "".to_string(),
line: 6,
},
],
));
Ok(())
}
#[gtest]
fn test_dynamic_key_table_field_implementation() -> Result<()> {
let mut ws = ProviderVirtualWorkspace::new();
let mut emmyrc = Emmyrc::default();
emmyrc.gmod.enabled = true;
emmyrc.gmod.infer_dynamic_fields = true;
ws.analysis.update_config(emmyrc.into());
check!(ws.check_implementation(
r#"
local rec = { data = {} }
local data = rec.data
for key, value in pairs({ forwardSlip = 1, sideSlip = 2 }) do
data[key] = value
end
local d = rec.data
math.abs(d.forw<??>ardSlip or 0)
"#,
vec![VirtualLocation {
file: "".to_string(),
line: 4,
}],
));
Ok(())
}
#[gtest]
fn test_separation_of_define_and_impl() -> Result<()> {
let mut ws = ProviderVirtualWorkspace::new();
check!(ws.check_implementation(
r#"
local a<??>bc
abc = function()
end
local _a = abc
local _b = abc()
abc = function()
end
"#,
vec![
VirtualLocation {
file: "".to_string(),
line: 1,
},
VirtualLocation {
file: "".to_string(),
line: 3,
},
VirtualLocation {
file: "".to_string(),
line: 9,
},
],
));
Ok(())
}
#[gtest]
fn test_member_variable_implementation_with_ref_prefix() -> Result<()> {
let mut ws = ProviderVirtualWorkspace::new();
check!(ws.check_implementation(
r#"
---@class MyClass
---@field myField number
---@type MyClass
local MyClass = {}
MyClass.myField = 1
---@type MyClass
local obj
obj.my<??>Field = 2
"#,
vec![
VirtualLocation {
file: "".to_string(),
line: 2,
},
VirtualLocation {
file: "".to_string(),
line: 6,
},
VirtualLocation {
file: "".to_string(),
line: 10,
},
],
));
Ok(())
}
/// Test C: the prefix-name fallback must resolve the prefix semantically,
/// not just by name text. An unrelated local named the same as the class
/// but typed as a different class must NOT match via the prefix fallback.
#[gtest]
fn test_implementation_prefix_fallback_requires_semantic_resolution() -> Result<()> {
let mut ws = ProviderVirtualWorkspace::new();
// `MyClass` is a class with field `myField`. A separate file has a
// local also named `MyClass` but annotated as `@type OtherClass`.
// The prefix fallback should NOT match because the local's type
// doesn't correspond to MyClass.
ws.def_file(
"other.lua",
r#"
---@class OtherClass
---@field myField string
---@type OtherClass
local MyClass = {}
MyClass.myField = "hello"
"#,
);
check!(ws.check_implementation(
r#"
---@class MyClass
---@field my<??>Field number
"#,
vec![
// Only the @field declaration. Neither the untyped local
// in the main file nor the OtherClass-typed local in
// other.lua should appear.
VirtualLocation {
file: "".to_string(),
line: 2,
},
],
));
Ok(())
}
/// Regression: an untyped `local MyClass = {}; MyClass.myField = 999`
/// must NOT be included in implementations for a @class field of the
/// same name, because the table has no semantic tie to the class
/// (no @type annotation). The TableConst name-text fallback is removed.
#[gtest]
fn test_implementation_unrelated_untyped_table_const_excluded() -> Result<()> {
let mut ws = ProviderVirtualWorkspace::new();
// Separate file with an untyped local that shares the class name.
ws.def_file(
"unrelated.lua",
r#"
local MyClass = {}
MyClass.myField = 999
"#,
);
check!(ws.check_implementation(
r#"
---@class MyClass
---@field my<??>Field number
"#,
vec![
// Only the @field declaration. The untyped table in
// unrelated.lua must NOT appear.
VirtualLocation {
file: "".to_string(),
line: 2,
},
],
));
Ok(())
}
}