Skip to content

Commit 01a741f

Browse files
committed
also check doc.type.table
1 parent 63edb99 commit 01a741f

File tree

6 files changed

+53
-5
lines changed

6 files changed

+53
-5
lines changed

locale/en-us/script.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,11 @@ DIAG_GLOBAL_ELEMENT =
171171
DIAG_MISSING_FIELDS =
172172
'Missing fields: {}'
173173
DIAG_INJECT_FIELD =
174-
'Fields cannot be injected into the reference of `{class}` for `{field}`. To do so, use `---@class` for `{node}`.'
174+
'Fields cannot be injected into the reference of `{class}` for `{field}`. {fix}'
175+
DIAG_INJECT_FIELD_FIX_CLASS =
176+
'To do so, use `---@class` for `{node}`.'
177+
DIAG_INJECT_FIELD_FIX_TABLE = -- TODO: need translate!
178+
'如要允许注入,请在定义中添加 `{fix}` 。'
175179

176180
MWS_NOT_SUPPORT =
177181
'{} does not support multi workspace for now, I may need to restart to support the new workspace ...'

locale/pt-br/script.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,11 @@ DIAG_GLOBAL_ELEMENT = -- TODO: need translate!
171171
DIAG_MISSING_FIELDS = -- TODO: need translate!
172172
'Missing fields: {}'
173173
DIAG_INJECT_FIELD = -- TODO: need translate!
174-
'Fields cannot be injected into the reference of `{class}` for `{field}`. To do so, use `---@class` for `{node}`.'
174+
'Fields cannot be injected into the reference of `{class}` for `{field}`. {fix}'
175+
DIAG_INJECT_FIELD_FIX_CLASS = -- TODO: need translate!
176+
'To do so, use `---@class` for `{node}`.'
177+
DIAG_INJECT_FIELD_FIX_TABLE = -- TODO: need translate!
178+
'如要允许注入,请在定义中添加 `{fix}` 。'
175179

176180
MWS_NOT_SUPPORT =
177181
'{} não é suportado múltiplos espaços de trabalho por enquanto, posso precisar reiniciar para estabelecer um novo espaço de trabalho ...'

locale/zh-cn/script.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,11 @@ DIAG_GLOBAL_ELEMENT =
171171
DIAG_MISSING_FIELDS =
172172
'缺少字段: {}'
173173
DIAG_INJECT_FIELD =
174-
'不能在 `{class}` 的引用中注入字段 `{field}` 。如要这么做,请对 `{node}` 使用 `---@class` 。'
174+
'不能在 `{class}` 的引用中注入字段 `{field}` 。{fix}'
175+
DIAG_INJECT_FIELD_FIX_CLASS =
176+
'如要允许注入,请对 `{node}` 使用 `{fix}` 。'
177+
DIAG_INJECT_FIELD_FIX_TABLE =
178+
'如要允许注入,请在定义中添加 `{fix}` 。'
175179

176180
MWS_NOT_SUPPORT =
177181
'{} 目前还不支持多工作目录,我可能需要重启才能支持新的工作目录...'

locale/zh-tw/script.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,11 @@ DIAG_GLOBAL_ELEMENT = -- TODO: need translate!
171171
DIAG_MISSING_FIELDS = -- TODO: need translate!
172172
'Missing fields: {}'
173173
DIAG_INJECT_FIELD = -- TODO: need translate!
174-
'Fields cannot be injected into the reference of `{class}` for `{field}`. To do so, use `---@class` for `{node}`.'
174+
'Fields cannot be injected into the reference of `{class}` for `{field}`. {fix}'
175+
DIAG_INJECT_FIELD_FIX_CLASS = -- TODO: need translate!
176+
'To do so, use `---@class` for `{node}`.'
177+
DIAG_INJECT_FIELD_FIX_TABLE = -- TODO: need translate!
178+
'如要允许注入,请在定义中添加 `{fix}` 。'
175179

176180
MWS_NOT_SUPPORT =
177181
'{} 目前還不支援多工作目錄,我可能需要重新啟動才能支援新的工作目錄...'

script/core/diagnostics/inject-field.lua

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,28 @@ return function (uri, callback)
4747
if dnode and vm.getDefinedClass(uri, dnode) then
4848
return
4949
end
50+
if def.type == 'doc.type.field' then
51+
return
52+
end
53+
end
54+
55+
local howToFix = lang.script('DIAG_INJECT_FIELD_FIX_CLASS', {
56+
node = hname(node),
57+
fix = '---@class',
58+
})
59+
for _, ndef in ipairs(vm.getDefs(node)) do
60+
if ndef.type == 'doc.type.table' then
61+
howToFix = lang.script('DIAG_INJECT_FIELD_FIX_TABLE', {
62+
fix = '[any]: any',
63+
})
64+
break
65+
end
5066
end
5167

5268
local message = lang.script('DIAG_INJECT_FIELD', {
5369
class = vm.getInfer(node):view(uri),
5470
field = guide.getKeyName(src),
55-
node = hname(node),
71+
fix = howToFix,
5672
})
5773
if src.type == 'setfield' and src.field then
5874
callback {

test/diagnostics/inject-field.lua

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,19 @@ local m
2323
m.xx = 1 -- OK
2424
m.yy = 1 -- OK
2525
]]
26+
27+
TEST [[
28+
---@type { xx: number }
29+
local m
30+
31+
m.xx = 1 -- OK
32+
m.<!yy!> = 1 -- Warning
33+
]]
34+
35+
TEST [[
36+
---@type { xx: number, [any]: any }
37+
local m
38+
39+
m.xx = 1 -- OK
40+
m.yy = 1 -- OK
41+
]]

0 commit comments

Comments
 (0)