Skip to content

Commit

Permalink
also check doc.type.table
Browse files Browse the repository at this point in the history
  • Loading branch information
sumneko committed Aug 14, 2023
1 parent 63edb99 commit 01a741f
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 5 deletions.
6 changes: 5 additions & 1 deletion locale/en-us/script.lua
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,11 @@ DIAG_GLOBAL_ELEMENT =
DIAG_MISSING_FIELDS =
'Missing fields: {}'
DIAG_INJECT_FIELD =
'Fields cannot be injected into the reference of `{class}` for `{field}`. To do so, use `---@class` for `{node}`.'
'Fields cannot be injected into the reference of `{class}` for `{field}`. {fix}'
DIAG_INJECT_FIELD_FIX_CLASS =
'To do so, use `---@class` for `{node}`.'
DIAG_INJECT_FIELD_FIX_TABLE = -- TODO: need translate!
'如要允许注入,请在定义中添加 `{fix}` 。'

MWS_NOT_SUPPORT =
'{} does not support multi workspace for now, I may need to restart to support the new workspace ...'
Expand Down
6 changes: 5 additions & 1 deletion locale/pt-br/script.lua
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,11 @@ DIAG_GLOBAL_ELEMENT = -- TODO: need translate!
DIAG_MISSING_FIELDS = -- TODO: need translate!
'Missing fields: {}'
DIAG_INJECT_FIELD = -- TODO: need translate!
'Fields cannot be injected into the reference of `{class}` for `{field}`. To do so, use `---@class` for `{node}`.'
'Fields cannot be injected into the reference of `{class}` for `{field}`. {fix}'
DIAG_INJECT_FIELD_FIX_CLASS = -- TODO: need translate!
'To do so, use `---@class` for `{node}`.'
DIAG_INJECT_FIELD_FIX_TABLE = -- TODO: need translate!
'如要允许注入,请在定义中添加 `{fix}` 。'

MWS_NOT_SUPPORT =
'{} não é suportado múltiplos espaços de trabalho por enquanto, posso precisar reiniciar para estabelecer um novo espaço de trabalho ...'
Expand Down
6 changes: 5 additions & 1 deletion locale/zh-cn/script.lua
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,11 @@ DIAG_GLOBAL_ELEMENT =
DIAG_MISSING_FIELDS =
'缺少字段: {}'
DIAG_INJECT_FIELD =
'不能在 `{class}` 的引用中注入字段 `{field}` 。如要这么做,请对 `{node}` 使用 `---@class` 。'
'不能在 `{class}` 的引用中注入字段 `{field}` 。{fix}'
DIAG_INJECT_FIELD_FIX_CLASS =
'如要允许注入,请对 `{node}` 使用 `{fix}` 。'
DIAG_INJECT_FIELD_FIX_TABLE =
'如要允许注入,请在定义中添加 `{fix}` 。'

MWS_NOT_SUPPORT =
'{} 目前还不支持多工作目录,我可能需要重启才能支持新的工作目录...'
Expand Down
6 changes: 5 additions & 1 deletion locale/zh-tw/script.lua
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,11 @@ DIAG_GLOBAL_ELEMENT = -- TODO: need translate!
DIAG_MISSING_FIELDS = -- TODO: need translate!
'Missing fields: {}'
DIAG_INJECT_FIELD = -- TODO: need translate!
'Fields cannot be injected into the reference of `{class}` for `{field}`. To do so, use `---@class` for `{node}`.'
'Fields cannot be injected into the reference of `{class}` for `{field}`. {fix}'
DIAG_INJECT_FIELD_FIX_CLASS = -- TODO: need translate!
'To do so, use `---@class` for `{node}`.'
DIAG_INJECT_FIELD_FIX_TABLE = -- TODO: need translate!
'如要允许注入,请在定义中添加 `{fix}` 。'

MWS_NOT_SUPPORT =
'{} 目前還不支援多工作目錄,我可能需要重新啟動才能支援新的工作目錄...'
Expand Down
18 changes: 17 additions & 1 deletion script/core/diagnostics/inject-field.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,28 @@ return function (uri, callback)
if dnode and vm.getDefinedClass(uri, dnode) then
return
end
if def.type == 'doc.type.field' then
return
end
end

local howToFix = lang.script('DIAG_INJECT_FIELD_FIX_CLASS', {
node = hname(node),
fix = '---@class',
})
for _, ndef in ipairs(vm.getDefs(node)) do
if ndef.type == 'doc.type.table' then
howToFix = lang.script('DIAG_INJECT_FIELD_FIX_TABLE', {
fix = '[any]: any',
})
break
end
end

local message = lang.script('DIAG_INJECT_FIELD', {
class = vm.getInfer(node):view(uri),
field = guide.getKeyName(src),
node = hname(node),
fix = howToFix,
})
if src.type == 'setfield' and src.field then
callback {
Expand Down
16 changes: 16 additions & 0 deletions test/diagnostics/inject-field.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,19 @@ local m
m.xx = 1 -- OK
m.yy = 1 -- OK
]]

TEST [[
---@type { xx: number }
local m
m.xx = 1 -- OK
m.<!yy!> = 1 -- Warning
]]

TEST [[
---@type { xx: number, [any]: any }
local m
m.xx = 1 -- OK
m.yy = 1 -- OK
]]

0 comments on commit 01a741f

Please sign in to comment.