summaryrefslogtreecommitdiff
path: root/test/diagnostics/type-check.lua
blob: 9d9eb3eccbb95634f03520d21179f8ed4e5907aa (plain)
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
local config = require 'config'

config.get(nil, 'Lua.diagnostics.neededFileStatus')['unused-local'] = 'None'
TEST [[
---@param table     table
---@param metatable table
---@return table
function Setmetatable(table, metatable) end

Setmetatable(<!1!>, {})
]]

TEST [[
---@param table     table
---@param metatable table
---@return table
function Setmetatable(table, metatable) end

Setmetatable(<!'name'!>, {})

]]

TEST [[
---@param table     table
---@param metatable table
---@return table
function Setmetatable(table, metatable) end

---@type table
local name
---@type function
local mt
---err
Setmetatable(name, <!mt!>)
]]

TEST [[
---@param p1 string
---@param p2 number
---@return table
local function func1(p1, p2) end

---@type string
local s
---@type table
local t
---err
func1(s, <!t!>)
]]

TEST [[
---@class bird
---@field wing string

---@class eagle
---@field family bird

---@class chicken
---@field family bird

---@param bd eagle
local function fly(bd) end

---@type chicken
local h
fly(<!h!>)
]]

TEST [[
---@overload fun(x: number, y: number)
---@param x boolean
---@param y boolean
local function f(x, y) end

f(true, true) -- OK
f(0, 0) -- OK

]]

TEST [[
---@class bird
local m = {}
setmetatable(m, {}) -- OK
]]

TEST [[
---@class childString: string
local s
---@param name string
local function f(name) end
f(s)
]]

TEST [[
---@class childString: string

---@type string
local s
---@param name childString
local function f(name) end
f(<!s!>)
]]

TEST [[
---@alias searchmode '"ref"'|'"def"'|'"field"'|'"allref"'|'"alldef"'|'"allfield"'

---@param mode   searchmode
local function searchRefs(mode)end
searchRefs('ref')
]]

TEST [[
---@class markdown
local mt = {}
---@param language string
---@param text string|markdown
function mt:add(language, text)
    if not text then
        return
    end
end
---@type markdown
local desc

desc:add('md', 'hover')
]]

---可选参数和枚举
TEST [[
---@param str string
---@param mode? '"left"'|'"right"'
---@return string
local function trim(str, mode)
    if mode == "left" then
        print(1)
    end
end
trim('str', 'left')
trim('str', nil)
]]

config.get(nil, 'Lua.diagnostics.neededFileStatus')['unused-local'] = 'Any'

---不完整的函数参数定义,会跳过检查
TEST [[
---@param mode string
local function status(source, field, mode)
    print(source, field, mode)
end
status(1, 2, 'name')
]]


TEST [[
---@alias range {start: number, end: number}
---@param uri string
---@param range range
local function location(uri, range)
    print(uri, range)
end
---@type range
local val = {}
location('uri', val)
]]