summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2023-08-14 15:07:49 +0800
committer最萌小汐 <sumneko@hotmail.com>2023-08-14 15:07:49 +0800
commit01a741f8416f074199303235eb533b8f3446e85b (patch)
treef2822f3f76500855879ba2515b097405df5a2327
parent63edb992a363a97dfa3f02a988f5f7771d5b0b03 (diff)
downloadlua-language-server-01a741f8416f074199303235eb533b8f3446e85b.zip
also check `doc.type.table`
-rw-r--r--locale/en-us/script.lua6
-rw-r--r--locale/pt-br/script.lua6
-rw-r--r--locale/zh-cn/script.lua6
-rw-r--r--locale/zh-tw/script.lua6
-rw-r--r--script/core/diagnostics/inject-field.lua18
-rw-r--r--test/diagnostics/inject-field.lua16
6 files changed, 53 insertions, 5 deletions
diff --git a/locale/en-us/script.lua b/locale/en-us/script.lua
index ff89fb75..202d98d7 100644
--- a/locale/en-us/script.lua
+++ b/locale/en-us/script.lua
@@ -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 ...'
diff --git a/locale/pt-br/script.lua b/locale/pt-br/script.lua
index 969ad5cf..05730d56 100644
--- a/locale/pt-br/script.lua
+++ b/locale/pt-br/script.lua
@@ -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 ...'
diff --git a/locale/zh-cn/script.lua b/locale/zh-cn/script.lua
index ef408533..b59411a9 100644
--- a/locale/zh-cn/script.lua
+++ b/locale/zh-cn/script.lua
@@ -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 =
'{} 目前还不支持多工作目录,我可能需要重启才能支持新的工作目录...'
diff --git a/locale/zh-tw/script.lua b/locale/zh-tw/script.lua
index bcd7af44..80c4bec2 100644
--- a/locale/zh-tw/script.lua
+++ b/locale/zh-tw/script.lua
@@ -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 =
'{} 目前還不支援多工作目錄,我可能需要重新啟動才能支援新的工作目錄...'
diff --git a/script/core/diagnostics/inject-field.lua b/script/core/diagnostics/inject-field.lua
index 2e1fc93c..139b8636 100644
--- a/script/core/diagnostics/inject-field.lua
+++ b/script/core/diagnostics/inject-field.lua
@@ -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 {
diff --git a/test/diagnostics/inject-field.lua b/test/diagnostics/inject-field.lua
index 8e2273b5..2c802270 100644
--- a/test/diagnostics/inject-field.lua
+++ b/test/diagnostics/inject-field.lua
@@ -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
+]]