Skip to content

Commit 297ac3a

Browse files
committed
new diagnostic: missing-fields
1 parent e7bc094 commit 297ac3a

File tree

5 files changed

+223
-2
lines changed

5 files changed

+223
-2
lines changed

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# changelog
22

33
## 3.6.24
4+
* `NEW` diagnostic: `missing-fields`
45
* `FIX` shake of `codeLens`
56
* `FIX` [#2145]
67

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
local vm = require 'vm'
2+
local files = require 'files'
3+
local guide = require 'parser.guide'
4+
local await = require 'await'
5+
6+
---@async
7+
return function (uri, callback)
8+
local state = files.getState(uri)
9+
if not state then
10+
return
11+
end
12+
13+
---@async
14+
guide.eachSourceType(state.ast, 'table', function (src)
15+
await.delay()
16+
17+
local defs = vm.getDefs(src)
18+
local requiresKeys = {}
19+
for _, def in ipairs(defs) do
20+
if def.type == 'doc.class' then
21+
if not def.fields then
22+
goto continue
23+
end
24+
if def.bindSource then
25+
if guide.isInRange(def.bindSource, src.start) then
26+
goto continue
27+
end
28+
end
29+
for _, field in ipairs(def.fields) do
30+
if not field.optional
31+
and not vm.compileNode(field):isNullable() then
32+
local key = vm.getKeyName(field)
33+
if key and not requiresKeys[key] then
34+
requiresKeys[key] = true
35+
requiresKeys[#requiresKeys+1] = key
36+
end
37+
end
38+
end
39+
end
40+
::continue::
41+
end
42+
43+
if #requiresKeys == 0 then
44+
return
45+
end
46+
47+
local myKeys = {}
48+
for _, field in ipairs(src) do
49+
local key = vm.getKeyName(field)
50+
if key then
51+
myKeys[key] = true
52+
end
53+
end
54+
55+
local missedKeys = {}
56+
for _, key in ipairs(requiresKeys) do
57+
if not myKeys[key] then
58+
missedKeys[#missedKeys+1] = ('`%s`'):format(key)
59+
end
60+
end
61+
62+
if #missedKeys == 0 then
63+
return
64+
end
65+
66+
callback {
67+
start = src.start,
68+
finish = src.finish,
69+
message = string.format('Missing fields: %s', table.concat(missedKeys, ', ')),
70+
}
71+
end)
72+
end

script/proto/diagnostic.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ m.register {
6262
'missing-return-value',
6363
'redundant-return-value',
6464
'missing-return',
65+
'missing-fields',
6566
} {
6667
group = 'unbalanced',
6768
severity = 'Warning',

test/diagnostics/common.lua

Lines changed: 148 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,13 @@ print(A) -- no warning
156156

157157
TEST [[
158158
---@type iolib
159-
_ENV = {}
159+
_ENV = io
160160
<!print!>(stderr) -- `print` is warning but `stderr` is not
161161
]]
162162

163163
TEST [[
164164
---@type iolib
165-
local _ENV = {}
165+
local _ENV = io
166166
<!print!>(stderr) -- `print` is warning but `stderr` is not
167167
]]
168168

@@ -2206,6 +2206,7 @@ end
22062206

22072207
TEST [[
22082208
---@diagnostic disable: unused-local
2209+
---@diagnostic disable: missing-fields
22092210
---@class A
22102211
---@field private x number
22112212
local mt = {}
@@ -2220,6 +2221,7 @@ end
22202221

22212222
TEST [[
22222223
---@diagnostic disable: unused-local
2224+
---@diagnostic disable: missing-fields
22232225
---@class A
22242226
---@field private x number
22252227
local mt = {}
@@ -2268,3 +2270,147 @@ local function foo(_ENV)
22682270
Joe = "human"
22692271
end
22702272
]]
2273+
2274+
TEST [[
2275+
---@diagnostic disable: unused-local
2276+
---@class A
2277+
---@field x number
2278+
---@field y? number
2279+
---@field z number
2280+
2281+
---@type A
2282+
local t = <!{}!>
2283+
]]
2284+
2285+
TEST [[
2286+
---@diagnostic disable: unused-local
2287+
---@class A
2288+
---@field x number
2289+
---@field y? number
2290+
---@field z number
2291+
2292+
---@type A
2293+
local t = <!{
2294+
x = 1,
2295+
}!>
2296+
]]
2297+
2298+
TEST [[
2299+
---@diagnostic disable: unused-local
2300+
---@class A
2301+
---@field x number
2302+
---@field y? number
2303+
---@field z number
2304+
2305+
---@type A
2306+
local t = <!{
2307+
x = 1,
2308+
y = 2,
2309+
}!>
2310+
]]
2311+
2312+
TEST [[
2313+
---@diagnostic disable: unused-local
2314+
---@class A
2315+
---@field x number
2316+
---@field y? number
2317+
---@field z number
2318+
2319+
---@type A
2320+
local t = {
2321+
x = 1,
2322+
y = 2,
2323+
z = 3,
2324+
}
2325+
]]
2326+
2327+
TEST [[
2328+
---@diagnostic disable: unused-local
2329+
---@class A
2330+
---@field x number
2331+
---@field y? number
2332+
---@field z number
2333+
2334+
---@type A
2335+
local t = {
2336+
x = 1,
2337+
z = 3,
2338+
}
2339+
]]
2340+
2341+
TEST [[
2342+
---@diagnostic disable: unused-local
2343+
---@class A
2344+
---@field x number
2345+
---@field y? number
2346+
---@field z number
2347+
2348+
---@param a A
2349+
local function f(a) end
2350+
2351+
f <!{}!>
2352+
]]
2353+
2354+
TEST [[
2355+
---@diagnostic disable: unused-local
2356+
---@class A
2357+
---@field x number
2358+
---@field y? number
2359+
---@field z number
2360+
2361+
---@param a A
2362+
local function f(a) end
2363+
2364+
f <!{
2365+
x = 1,
2366+
}!>
2367+
]]
2368+
2369+
TEST [[
2370+
---@diagnostic disable: unused-local
2371+
---@class A
2372+
---@field x number
2373+
---@field y? number
2374+
---@field z number
2375+
2376+
---@param a A
2377+
local function f(a) end
2378+
2379+
f <!{
2380+
x = 1,
2381+
y = 2,
2382+
}!>
2383+
]]
2384+
2385+
TEST [[
2386+
---@diagnostic disable: unused-local
2387+
---@class A
2388+
---@field x number
2389+
---@field y? number
2390+
---@field z number
2391+
2392+
---@param a A
2393+
local function f(a) end
2394+
2395+
f {
2396+
x = 1,
2397+
y = 2,
2398+
z = 3,
2399+
}
2400+
]]
2401+
2402+
TEST [[
2403+
---@diagnostic disable: unused-local
2404+
---@class A
2405+
---@field x number
2406+
---@field y? number
2407+
---@field z number
2408+
2409+
---@param a A
2410+
local function f(a) end
2411+
2412+
f {
2413+
x = 1,
2414+
z = 3,
2415+
}
2416+
]]

test/diagnostics/type-check.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ m.ints = {}
174174
]]
175175

176176
TEST [[
177+
---@diagnostic disable: missing-fields
177178
---@class A
178179
---@field x A
179180

0 commit comments

Comments
 (0)