Skip to content

Commit 525d095

Browse files
committed
stash
1 parent 1055a85 commit 525d095

File tree

5 files changed

+81
-8
lines changed

5 files changed

+81
-8
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
local files = require 'files'
2+
local vm = require 'vm'
3+
local lang = require 'language'
4+
local guide = require 'parser.guide'
5+
local await = require 'await'
6+
7+
local skipCheckClass = {
8+
['unknown'] = true,
9+
['any'] = true,
10+
['table'] = true,
11+
}
12+
13+
---@async
14+
return function (uri, callback)
15+
local ast = files.getState(uri)
16+
if not ast then
17+
return
18+
end
19+
20+
---@async
21+
local function checkInjectField(src)
22+
await.delay()
23+
24+
local node = src.node
25+
if node then
26+
local ok
27+
for view in vm.getInfer(node):eachView(uri) do
28+
if skipCheckClass[view] then
29+
return
30+
end
31+
ok = true
32+
end
33+
if not ok then
34+
return
35+
end
36+
end
37+
local message = lang.script('DIAG_INJECT_FIELD', guide.getKeyName(src))
38+
if src.type == 'setfield' and src.field then
39+
callback {
40+
start = src.field.start,
41+
finish = src.field.finish,
42+
message = message,
43+
}
44+
elseif src.type == 'setfield' and src.method then
45+
callback {
46+
start = src.method.start,
47+
finish = src.method.finish,
48+
message = message,
49+
}
50+
end
51+
end
52+
guide.eachSourceType(ast.ast, 'setfield', checkInjectField)
53+
guide.eachSourceType(ast.ast, 'setmethod', checkInjectField)
54+
end

script/core/diagnostics/undefined-field.lua

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,6 @@ local skipCheckClass = {
88
['unknown'] = true,
99
['any'] = true,
1010
['table'] = true,
11-
['nil'] = true,
12-
['number'] = true,
13-
['integer'] = true,
14-
['boolean'] = true,
15-
['function'] = true,
16-
['userdata'] = true,
17-
['lightuserdata'] = true,
1811
}
1912

2013
---@async
@@ -61,5 +54,4 @@ return function (uri, callback)
6154
end
6255
guide.eachSourceType(ast.ast, 'getfield', checkUndefinedField)
6356
guide.eachSourceType(ast.ast, 'getmethod', checkUndefinedField)
64-
guide.eachSourceType(ast.ast, 'getindex', checkUndefinedField)
6557
end

script/proto/diagnostic.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ m.register {
7777
'param-type-mismatch',
7878
'cast-type-mismatch',
7979
'return-type-mismatch',
80+
'inject-field',
8081
} {
8182
group = 'type-check',
8283
severity = 'Warning',

test/diagnostics/init.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ check 'empty-block'
9090
check 'global-element'
9191
check 'global-in-nil-env'
9292
check 'incomplete-signature-doc'
93+
check 'inject-field'
9394
check 'invisible'
9495
check 'lowercase-global'
9596
check 'missing-fields'

test/diagnostics/inject-field.lua

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
TEST [[
2+
---@class Class
3+
local m = {}
4+
5+
m.xx = 1 -- OK
6+
7+
---@type Class
8+
local m
9+
10+
m.xx = 1 -- OK
11+
m.<!yy!> = 1 -- Warning
12+
]]
13+
14+
TEST [[
15+
---@class Class
16+
local m = {}
17+
18+
m.xx = 1 -- OK
19+
20+
---@class Class
21+
local m
22+
23+
m.xx = 1 -- OK
24+
m.yy = 1 -- OK
25+
]]

0 commit comments

Comments
 (0)