Skip to content

Commit 63edb99

Browse files
committed
new diag: inject-field
#1990
1 parent 525d095 commit 63edb99

File tree

6 files changed

+37
-9
lines changed

6 files changed

+37
-9
lines changed

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## 3.7.0
44
* `NEW` support `---@type` and `--[[@as]]` for return statement
55
* `NEW` commandline parameter `--force-accept-workspace`: allowing the use of the root directory or home directory as the workspace
6+
* `NEW` diagnostic: `inject-field`
67
* `FIX` wrong hover and signature for method with varargs and overloads
78
* `FIX` [#2155]
89
* `FIX` [#2224]

locale/en-us/script.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ DIAG_GLOBAL_ELEMENT =
170170
'Element is global.'
171171
DIAG_MISSING_FIELDS =
172172
'Missing fields: {}'
173+
DIAG_INJECT_FIELD =
174+
'Fields cannot be injected into the reference of `{class}` for `{field}`. To do so, use `---@class` for `{node}`.'
173175

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

locale/pt-br/script.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ DIAG_GLOBAL_ELEMENT = -- TODO: need translate!
170170
'Element is global.'
171171
DIAG_MISSING_FIELDS = -- TODO: need translate!
172172
'Missing fields: {}'
173+
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}`.'
173175

174176
MWS_NOT_SUPPORT =
175177
'{} 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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ DIAG_GLOBAL_ELEMENT =
170170
'全局变量。'
171171
DIAG_MISSING_FIELDS =
172172
'缺少字段: {}'
173+
DIAG_INJECT_FIELD =
174+
'不能在 `{class}` 的引用中注入字段 `{field}` 。如要这么做,请对 `{node}` 使用 `---@class` 。'
173175

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

locale/zh-tw/script.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ DIAG_GLOBAL_ELEMENT = -- TODO: need translate!
170170
'Element is global.'
171171
DIAG_MISSING_FIELDS = -- TODO: need translate!
172172
'Missing fields: {}'
173+
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}`.'
173175

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

script/core/diagnostics/inject-field.lua

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ local vm = require 'vm'
33
local lang = require 'language'
44
local guide = require 'parser.guide'
55
local await = require 'await'
6+
local hname = require 'core.hover.name'
67

78
local skipCheckClass = {
89
['unknown'] = true,
@@ -22,19 +23,37 @@ return function (uri, callback)
2223
await.delay()
2324

2425
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
26+
if not node then
27+
return
28+
end
29+
local ok
30+
for view in vm.getInfer(node):eachView(uri) do
31+
if skipCheckClass[view] then
32+
return
3233
end
33-
if not ok then
34+
ok = true
35+
end
36+
if not ok then
37+
return
38+
end
39+
40+
local class = vm.getDefinedClass(uri, node)
41+
if class then
42+
return
43+
end
44+
45+
for _, def in ipairs(vm.getDefs(src)) do
46+
local dnode = def.node
47+
if dnode and vm.getDefinedClass(uri, dnode) then
3448
return
3549
end
3650
end
37-
local message = lang.script('DIAG_INJECT_FIELD', guide.getKeyName(src))
51+
52+
local message = lang.script('DIAG_INJECT_FIELD', {
53+
class = vm.getInfer(node):view(uri),
54+
field = guide.getKeyName(src),
55+
node = hname(node),
56+
})
3857
if src.type == 'setfield' and src.field then
3958
callback {
4059
start = src.field.start,

0 commit comments

Comments
 (0)