summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2021-02-23 19:31:48 +0800
committer最萌小汐 <sumneko@hotmail.com>2021-02-23 19:31:48 +0800
commit63396ada5c7d2037ec19ed45e99e1a56f23c590b (patch)
tree9a1beb761861b8628cd86715aee4a5040f6f3b3b
parent54876cb601466c409eb0be1af489c7ceca7f22cf (diff)
downloadlua-language-server-63396ada5c7d2037ec19ed45e99e1a56f23c590b.zip
close #328 new diagnostic: `no-implicit-any`
-rw-r--r--changelog.md1
-rw-r--r--locale/en-us/script.lua1
-rw-r--r--locale/zh-cn/script.lua1
-rw-r--r--script/core/diagnostics/init.lua10
-rw-r--r--script/core/diagnostics/no-implicit-any.lua32
-rw-r--r--script/proto/define.lua3
6 files changed, 46 insertions, 2 deletions
diff --git a/changelog.md b/changelog.md
index 3419e364..46916979 100644
--- a/changelog.md
+++ b/changelog.md
@@ -2,6 +2,7 @@
## 1.16.2
* `NEW` diagnostic: `duplicate-set-field`
+* `NEW` diagnostic: `no-implicit-any`, disabled by default
* `FIX` [#406](https://github.com/sumneko/lua-language-server/issues/406)
## 1.16.1
diff --git a/locale/en-us/script.lua b/locale/en-us/script.lua
index a11772cb..3d04b55c 100644
--- a/locale/en-us/script.lua
+++ b/locale/en-us/script.lua
@@ -39,6 +39,7 @@ DIAG_UNBALANCED_ASSIGNMENTS = 'The value is assigned as `nil` because the number
DIAG_REQUIRE_LIKE = 'You can treat `{}` as `require` by setting.'
DIAG_COSE_NON_OBJECT = 'Cannot close a value of this type. (Unless set `__close` meta method)'
DIAG_COUNT_DOWN_LOOP = 'Do you mean `{}` ?'
+DIAG_IMPLICIT_ANY = 'Can not infer type.'
DIAG_CIRCLE_DOC_CLASS = 'Circularly inherited classes.'
DIAG_DOC_FIELD_NO_CLASS = 'The field must be defined after the class.'
diff --git a/locale/zh-cn/script.lua b/locale/zh-cn/script.lua
index ef975c0c..a1bc4f5c 100644
--- a/locale/zh-cn/script.lua
+++ b/locale/zh-cn/script.lua
@@ -39,6 +39,7 @@ DIAG_UNBALANCED_ASSIGNMENTS = '由于值的数量不够而被赋值为了 `nil`
DIAG_REQUIRE_LIKE = '你可以在设置中将 `{}` 视为 `require`。'
DIAG_COSE_NON_OBJECT = '无法 close 此类型的值。(除非给此类型设置 `__close` 元方法)'
DIAG_COUNT_DOWN_LOOP = '你的意思是 `{}` 吗?'
+DIAG_IMPLICIT_ANY = '无法推测出类型。'
DIAG_CIRCLE_DOC_CLASS = '循环继承的类。'
DIAG_DOC_FIELD_NO_CLASS = '字段必须定义在类之后。'
diff --git a/script/core/diagnostics/init.lua b/script/core/diagnostics/init.lua
index bc3f3d8c..87afd4af 100644
--- a/script/core/diagnostics/init.lua
+++ b/script/core/diagnostics/init.lua
@@ -24,11 +24,17 @@ local function check(uri, name, results)
local level = config.config.diagnostics.severity[name]
or define.DiagnosticDefaultSeverity[name]
- local neededFileStatus = config.config.diagnostics.neededFileStatus[name]
- or define.DiagnosticDefaultNeededFileStatus[name]
+ local neededFileStatus = config.config.diagnostics.neededFileStatus[name]
+ or define.DiagnosticDefaultNeededFileStatus[name]
+
+ if neededFileStatus == 'None' then
+ return
+ end
+
if neededFileStatus == 'Opened' and not files.isOpen(uri) then
return
end
+
local severity = define.DiagnosticSeverity[level]
local clock = os.clock()
require('core.diagnostics.' .. name)(uri, function (result)
diff --git a/script/core/diagnostics/no-implicit-any.lua b/script/core/diagnostics/no-implicit-any.lua
new file mode 100644
index 00000000..a2062b37
--- /dev/null
+++ b/script/core/diagnostics/no-implicit-any.lua
@@ -0,0 +1,32 @@
+local files = require 'files'
+local guide = require 'parser.guide'
+local lang = require 'language'
+local define = require 'proto.define'
+local vm = require 'vm'
+
+return function (uri, callback)
+ local ast = files.getAst(uri)
+ if not ast then
+ return
+ end
+
+ guide.eachSource(ast.ast, function (source)
+ if source.type ~= 'local'
+ and source.type ~= 'setlocal'
+ and source.type ~= 'setglobal'
+ and source.type ~= 'getglobal'
+ and source.type ~= 'setfield'
+ and source.type ~= 'setindex'
+ and source.type ~= 'tablefield'
+ and source.type ~= 'tableindex' then
+ return
+ end
+ if vm.getInferType(source, 0) == 'any' then
+ callback {
+ start = source.start,
+ finish = source.finish,
+ message = lang.script('DIAG_IMPLICIT_ANY'),
+ }
+ end
+ end)
+end
diff --git a/script/proto/define.lua b/script/proto/define.lua
index 7c8dea34..3df97520 100644
--- a/script/proto/define.lua
+++ b/script/proto/define.lua
@@ -75,6 +75,7 @@ m.DiagnosticDefaultSeverity = {
['unbalanced-assignments'] = 'Warning',
['close-non-object'] = 'Warning',
['count-down-loop'] = 'Warning',
+ ['no-implicit-any'] = 'Information',
['duplicate-doc-class'] = 'Warning',
['undefined-doc-class'] = 'Warning',
@@ -89,6 +90,7 @@ m.DiagnosticDefaultSeverity = {
---@alias DiagnosticDefaultNeededFileStatus
---| '"Any"'
---| '"Opened"'
+---| '"None"'
-- 文件状态
m.FileStatus = {
@@ -122,6 +124,7 @@ m.DiagnosticDefaultNeededFileStatus = {
['unbalanced-assignments'] = 'Any',
['close-non-object'] = 'Any',
['count-down-loop'] = 'Any',
+ ['no-implicit-any'] = 'None',
['duplicate-doc-class'] = 'Any',
['undefined-doc-class'] = 'Any',