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
|
TEST [[
---@param x number
local function f(x) end
f(<!true!>)
]]
TEST [[
---@class A
---@param n A
local function f(n)
end
---@class B
local a = {}
---@type A?
a.x = XX
f(a.x)
]]
TEST [[
---@alias A string|boolean
---@param x string|boolean
local function f(x) end
---@type A
local x
f(x)
]]
TEST [[
---@alias A string|boolean
---@param x A
local function f(x) end
---@type string|boolean
local x
f(x)
]]
TEST [[
---@param b boolean
local function f(b)
end
---@type boolean
local t
if t then
f(t)
end
]]
TEST [[
---@enum A
local t = {
x = 1,
y = 2,
}
---@param x A
local function f(x)
end
f(<!t!>)
f(t.x)
f(1)
f(<!3!>)
]]
TEST [[
---@enum A
local t = {
x = { h = 1 },
y = { h = 2 },
}
---@param x A
local function f(x)
end
f(t.x)
f(t.y)
f(<!{ h = 1 }!>)
]]
TEST [[
---@enum(key) A
local t = {
x = 1,
['y'] = 2,
}
---@param x A
local function f(x)
end
f('x')
f('y')
f(<!'z'!>)
]]
TEST [[
---@generic T: string | boolean | table
---@param x T
---@return T
local function f(x)
return x
end
f(<!1!>)
]]
TEST [[
---@param opts {a:number, b:number}
local function foo(opts)
end
---@param opts {a:number, b:number}
local function bar(opts)
foo(opts)
end
]]
TEST [[
---@param opts {a:number, b:number}
local function foo(opts)
end
---@param opts {c:number, d:number}
local function bar(opts)
foo(<!opts!>) -- this should raise linting error
end
]]
TEST [[
---@param opts {[number]: boolean}
local function foo(opts)
end
---@param opts {[1]: boolean}
local function bar(opts)
foo(opts)
end
]]
TEST [[
---@generic T
---@param v1 T
---@param v2 T|table
local function func(v1, v2)
end
func('hello', 'world')
]]
TEST [[
---@generic T1, T2, T3, T4, T5
---@param f fun(): T1?, T2?, T3?, T4?, T5?
---@return T1?, T2?, T3?, T4?, T5?
local function foo(f)
return f()
end
local a, b = foo(function()
return 1
end)
]]
TEST [[
---@generic T1, T2, T3, T4, T5
---@param f fun(): T1|nil, T2|nil, T3|nil, T4|nil, T5|nil
---@return T1?, T2?, T3?, T4?, T5?
local function foo(f)
return f()
end
local a, b = foo(function()
return 1
end)
]]
TEST [[
---@param v integer
---@return boolean
local function is_string(v)
return type(v) == 'string'
end
print(is_string(3))
]]
TEST [[
---@param p integer|string
local function get_val(p)
local is_number = type(p) == 'number'
return is_number and p or p
end
get_val('hi')
]]
TEST [[
---@class Class
local Class = {}
---@param source string
function Class.staticCreator(source)
end
Class.staticCreator(<!true!>)
Class<!:!>staticCreator() -- Expecting a waring
]]
TEST [[
---@class A
---@class B : A
---@class C : B
---@class D : B
---@param x A
local function func(x) end
---@type C|D
local var
func(var)
]]
TEST [[
---@class MyClass
---@overload fun(x : string) : MyClass
local MyClass = {}
local w = MyClass(<!1!>)
]]
|