From f0f80fdf1b5c83467687541ffda36cabb8f9bd62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Mon, 23 May 2022 21:24:29 +0800 Subject: fix #1131 --- script/core/diagnostics/missing-parameter.lua | 58 +++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/missing-parameter.lua b/script/core/diagnostics/missing-parameter.lua index 698680ca..f69afa79 100644 --- a/script/core/diagnostics/missing-parameter.lua +++ b/script/core/diagnostics/missing-parameter.lua @@ -3,11 +3,69 @@ local guide = require 'parser.guide' local vm = require 'vm' local lang = require 'language' +---@param source parser.object +---@return integer +local function countReturns(source) + local n = 0 + + local docs = source.bindDocs + if docs then + for _, doc in ipairs(docs) do + if doc.type == 'doc.return' then + for _, rtn in ipairs(doc.returns) do + if rtn.returnIndex and rtn.returnIndex > n then + n = rtn.returnIndex + end + end + end + end + end + + local returns = source.returns + if returns then + for _, rtn in ipairs(returns) do + if #rtn > n then + n = #rtn + end + end + end + + return n +end + +local function countMaxReturns(source) + local hasFounded + local n = 0 + for _, def in ipairs(vm.getDefs(source)) do + if def.type == 'doc.type.function' + or def.type == 'function' then + hasFounded = true + local rets = countReturns(def) + if rets > n then + n = rets + end + end + end + + if hasFounded then + return n + else + return math.huge + end +end + local function countCallArgs(source) local result = 0 if not source.args then return 0 end + local lastArg = source.args[#source.args] + if lastArg.type == 'varargs' then + return math.huge + end + if lastArg.type == 'call' then + result = result + countMaxReturns(lastArg) - 1 + end result = result + #source.args return result end -- cgit v1.2.3 From b51c930f3cca060b10ab724666c9f5265cefd517 Mon Sep 17 00:00:00 2001 From: CppCXY <812125110@qq.com> Date: Tue, 24 May 2022 11:57:39 +0800 Subject: =?UTF-8?q?=E9=87=8D=E7=BD=AE=E4=BB=A3=E7=A0=81=EF=BC=8C=E5=8F=96?= =?UTF-8?q?=E6=B6=88=E5=A4=9A=E4=BD=99=E7=9A=84=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- script/core/diagnostics/spell-check.lua | 34 +++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 script/core/diagnostics/spell-check.lua (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/spell-check.lua b/script/core/diagnostics/spell-check.lua new file mode 100644 index 00000000..ebdb0245 --- /dev/null +++ b/script/core/diagnostics/spell-check.lua @@ -0,0 +1,34 @@ +local files = require 'files' +local converter = require 'proto.converter' +local log = require 'log' +local spell = require 'provider.spell' + + +---@async +return function(uri, callback) + local text = files.getText(uri) + if not text then + return + end + + local status, diagnosticInfos = spell.spellCheck(uri, text) + + if not status then + if diagnosticInfos ~= nil then + log.error(diagnosticInfos) + end + + return + end + + if diagnosticInfos then + for _, diagnosticInfo in ipairs(diagnosticInfos) do + callback { + start = converter.unpackPosition(uri, diagnosticInfo.range.start), + finish = converter.unpackPosition(uri, diagnosticInfo.range["end"]), + message = diagnosticInfo.message, + data = diagnosticInfo.data + } + end + end +end -- cgit v1.2.3 From 680e6c9848a6e5f43ccce11561ec98839822bdea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Tue, 7 Jun 2022 21:27:45 +0800 Subject: cleanup --- script/core/diagnostics/deprecated.lua | 2 +- script/core/diagnostics/init.lua | 4 +++- script/core/diagnostics/lowercase-global.lua | 6 ++---- script/core/diagnostics/undefined-global.lua | 3 ++- 4 files changed, 8 insertions(+), 7 deletions(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/deprecated.lua b/script/core/diagnostics/deprecated.lua index 27920c43..85ae2d19 100644 --- a/script/core/diagnostics/deprecated.lua +++ b/script/core/diagnostics/deprecated.lua @@ -15,7 +15,7 @@ return function (uri, callback) return end - local dglobals = config.get(uri, 'Lua.diagnostics.globals') + local dglobals = util.arrayToHash(config.get(uri, 'Lua.diagnostics.globals')) local rspecial = config.get(uri, 'Lua.runtime.special') guide.eachSourceTypes(ast.ast, types, function (src) ---@async diff --git a/script/core/diagnostics/init.lua b/script/core/diagnostics/init.lua index b4ae3715..a2eb2482 100644 --- a/script/core/diagnostics/init.lua +++ b/script/core/diagnostics/init.lua @@ -3,6 +3,7 @@ local define = require 'proto.define' local config = require 'config' local await = require 'await' local vm = require "vm.vm" +local util = require 'utility' -- 把耗时最长的诊断放到最后面 local diagSort = { @@ -52,7 +53,8 @@ end ---@param isScopeDiag boolean ---@param response async fun(result: any) local function check(uri, name, isScopeDiag, response) - if config.get(uri, 'Lua.diagnostics.disable')[name] then + local disables = config.get(uri, 'Lua.diagnostics.disable') + if util.arrayHas(disables, name) then return end local level = config.get(uri, 'Lua.diagnostics.severity')[name] diff --git a/script/core/diagnostics/lowercase-global.lua b/script/core/diagnostics/lowercase-global.lua index d03e8c70..68bec234 100644 --- a/script/core/diagnostics/lowercase-global.lua +++ b/script/core/diagnostics/lowercase-global.lua @@ -3,6 +3,7 @@ local guide = require 'parser.guide' local lang = require 'language' local config = require 'config' local vm = require 'vm' +local util = require 'utility' local function isDocClass(source) if not source.bindDocs then @@ -23,10 +24,7 @@ return function (uri, callback) return end - local definedGlobal = {} - for name in pairs(config.get(uri, 'Lua.diagnostics.globals')) do - definedGlobal[name] = true - end + local definedGlobal = util.arrayToHash(config.get(uri, 'Lua.diagnostics.globals')) guide.eachSourceType(ast.ast, 'setglobal', function (source) local name = guide.getKeyName(source) diff --git a/script/core/diagnostics/undefined-global.lua b/script/core/diagnostics/undefined-global.lua index bd0aae69..7804aeee 100644 --- a/script/core/diagnostics/undefined-global.lua +++ b/script/core/diagnostics/undefined-global.lua @@ -4,6 +4,7 @@ local lang = require 'language' local config = require 'config' local guide = require 'parser.guide' local await = require 'await' +local util = require 'utility' local requireLike = { ['include'] = true, @@ -19,7 +20,7 @@ return function (uri, callback) return end - local dglobals = config.get(uri, 'Lua.diagnostics.globals') + local dglobals = util.arrayToHash(config.get(uri, 'Lua.diagnostics.globals')) local rspecial = config.get(uri, 'Lua.runtime.special') local cache = {} -- cgit v1.2.3 From 916b8563cc327af32a5c3dccfdb5434711d83377 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Thu, 9 Jun 2022 16:32:05 +0800 Subject: view infer must specify uri --- script/core/diagnostics/close-non-object.lua | 8 ++++---- script/core/diagnostics/no-unknown.lua | 2 +- script/core/diagnostics/not-yieldable.lua | 4 ++-- script/core/diagnostics/undefined-field.lua | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/close-non-object.lua b/script/core/diagnostics/close-non-object.lua index c97014fa..d07aaebe 100644 --- a/script/core/diagnostics/close-non-object.lua +++ b/script/core/diagnostics/close-non-object.lua @@ -25,10 +25,10 @@ return function (uri, callback) return end local infer = vm.getInfer(source.value) - if not infer:hasClass() - and not infer:hasType 'nil' - and not infer:hasType 'table' - and infer:view('any', uri) ~= 'any' then + if not infer:hasClass(uri) + and not infer:hasType(uri, 'nil') + and not infer:hasType(uri, 'table') + and infer:view(uri, 'any') ~= 'any' then callback { start = source.value.start, finish = source.value.finish, diff --git a/script/core/diagnostics/no-unknown.lua b/script/core/diagnostics/no-unknown.lua index 48aab5da..ff9f7a83 100644 --- a/script/core/diagnostics/no-unknown.lua +++ b/script/core/diagnostics/no-unknown.lua @@ -20,7 +20,7 @@ return function (uri, callback) and source.type ~= 'tableindex' then return end - if vm.getInfer(source):view() == 'unknown' then + if vm.getInfer(source):view(uri) == 'unknown' then callback { start = source.start, finish = source.finish, diff --git a/script/core/diagnostics/not-yieldable.lua b/script/core/diagnostics/not-yieldable.lua index a1c84276..055025d4 100644 --- a/script/core/diagnostics/not-yieldable.lua +++ b/script/core/diagnostics/not-yieldable.lua @@ -11,7 +11,7 @@ local function isYieldAble(defs, i) local arg = def.args and def.args[i] if arg then hasFuncDef = true - if vm.getInfer(arg):hasType 'any' + if vm.getInfer(arg):hasType(guide.getUri(def), 'any') or vm.isAsync(arg, true) or arg.type == '...' then return true @@ -22,7 +22,7 @@ local function isYieldAble(defs, i) local arg = def.args and def.args[i] if arg then hasFuncDef = true - if vm.getInfer(arg.extends):hasType 'any' + if vm.getInfer(arg.extends):hasType(guide.getUri(def), 'any') or vm.isAsync(arg.extends, true) then return true end diff --git a/script/core/diagnostics/undefined-field.lua b/script/core/diagnostics/undefined-field.lua index 41fcda48..b03838fd 100644 --- a/script/core/diagnostics/undefined-field.lua +++ b/script/core/diagnostics/undefined-field.lua @@ -34,7 +34,7 @@ return function (uri, callback) local node = src.node if node then local ok - for view in vm.getInfer(node):eachView() do + for view in vm.getInfer(node):eachView(uri) do if not skipCheckClass[view] then ok = true break -- cgit v1.2.3 From 329cd44aedec15f1048742d98baacdd71d434a6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Tue, 14 Jun 2022 21:00:21 +0800 Subject: don't chek nil of any --- script/core/diagnostics/need-check-nil.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/need-check-nil.lua b/script/core/diagnostics/need-check-nil.lua index 98fdfd08..56cb1eae 100644 --- a/script/core/diagnostics/need-check-nil.lua +++ b/script/core/diagnostics/need-check-nil.lua @@ -28,7 +28,7 @@ return function (uri, callback) return end local node = vm.compileNode(src) - if node:hasFalsy() then + if node:hasFalsy() and not vm.getInfer(src):hasType(uri, 'any') then callback { start = src.start, finish = src.finish, -- cgit v1.2.3 From 54a488b7d5899d412ad37083dca1ef3728a7cdba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Wed, 15 Jun 2022 00:05:52 +0800 Subject: fix #1204 `missing-parameter` incorrect should count returns by `call.node` instead of `call` --- script/core/diagnostics/missing-parameter.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/missing-parameter.lua b/script/core/diagnostics/missing-parameter.lua index f69afa79..a475673f 100644 --- a/script/core/diagnostics/missing-parameter.lua +++ b/script/core/diagnostics/missing-parameter.lua @@ -64,7 +64,7 @@ local function countCallArgs(source) return math.huge end if lastArg.type == 'call' then - result = result + countMaxReturns(lastArg) - 1 + result = result + countMaxReturns(lastArg.node) - 1 end result = result + #source.args return result -- cgit v1.2.3 From f40f0a54b1b8ccb97bc30d21d16d4f212b178ada Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Wed, 15 Jun 2022 20:45:49 +0800 Subject: `need-check-nil` check `t[x]` --- script/core/diagnostics/need-check-nil.lua | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/need-check-nil.lua b/script/core/diagnostics/need-check-nil.lua index 56cb1eae..867d0135 100644 --- a/script/core/diagnostics/need-check-nil.lua +++ b/script/core/diagnostics/need-check-nil.lua @@ -24,6 +24,10 @@ return function (uri, callback) if call and call.type == 'call' and call.node == src then checkNil = true end + local setIndex = src.parent + if setIndex and setIndex.type == 'setindex' and setIndex.index == src then + checkNil = true + end if not checkNil then return end -- cgit v1.2.3 From 241c518d2e02f2b65c59460d0bb8d4101d98614a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Wed, 15 Jun 2022 20:47:48 +0800 Subject: check nil --- script/core/diagnostics/duplicate-doc-alias.lua | 3 +++ 1 file changed, 3 insertions(+) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/duplicate-doc-alias.lua b/script/core/diagnostics/duplicate-doc-alias.lua index 3df6f972..821bb1d8 100644 --- a/script/core/diagnostics/duplicate-doc-alias.lua +++ b/script/core/diagnostics/duplicate-doc-alias.lua @@ -17,6 +17,9 @@ return function (uri, callback) for _, doc in ipairs(state.ast.docs) do if doc.type == 'doc.alias' then local name = guide.getKeyName(doc) + if not name then + return + end if not cache[name] then local docs = vm.getDocSets(uri, name) cache[name] = {} -- cgit v1.2.3 From 8a9e8d9cfd12d43496c2bf16f70874af550d4e7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Thu, 16 Jun 2022 20:37:00 +0800 Subject: type-check: `cast-local-type` --- script/core/diagnostics/cast-local-type.lua | 40 +++++++++++++++++++++++++++ script/core/diagnostics/type-check-assign.lua | 20 ++++++++++++++ script/core/diagnostics/type-check.lua | 3 -- 3 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 script/core/diagnostics/cast-local-type.lua create mode 100644 script/core/diagnostics/type-check-assign.lua delete mode 100644 script/core/diagnostics/type-check.lua (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/cast-local-type.lua b/script/core/diagnostics/cast-local-type.lua new file mode 100644 index 00000000..1284e934 --- /dev/null +++ b/script/core/diagnostics/cast-local-type.lua @@ -0,0 +1,40 @@ +local files = require 'files' +local lang = require 'language' +local guide = require 'parser.guide' +local vm = require 'vm' +local await = require 'await' + +---@async +return function (uri, callback) + local state = files.getState(uri) + if not state then + return + end + + ---@async + guide.eachSourceType(state.ast, 'local', function (loc) + if not loc.ref then + return + end + local locNode = vm.compileNode(loc) + if not locNode:getData 'hasDefined' then + return + end + for _, ref in ipairs(loc.ref) do + if ref.type == 'setlocal' then + await.delay() + local refNode = vm.compileNode(ref) + if not vm.isSubType(uri, refNode, locNode) then + callback { + start = ref.start, + finish = ref.finish, + message = lang.script('DIAG_CAST_LOCAL_TYPE', { + loc = vm.getInfer(locNode):view(uri), + ref = vm.getInfer(refNode):view(uri), + }), + } + end + end + end + end) +end diff --git a/script/core/diagnostics/type-check-assign.lua b/script/core/diagnostics/type-check-assign.lua new file mode 100644 index 00000000..6b3c73b3 --- /dev/null +++ b/script/core/diagnostics/type-check-assign.lua @@ -0,0 +1,20 @@ +local files = require 'files' +local lang = require 'language' +local guide = require 'parser.guide' +local vm = require 'vm' + +return function (uri, callback) + local state = files.getState(uri) + if not state then + return + end + + guide.eachSourceType(state.ast, 'setlocal', function (source) + local value = source.value + if not value then + return + end + local locNode = vm.compileNode(source) + local valueNode = vm.compileNode(value) + end) +end diff --git a/script/core/diagnostics/type-check.lua b/script/core/diagnostics/type-check.lua deleted file mode 100644 index cc2b3228..00000000 --- a/script/core/diagnostics/type-check.lua +++ /dev/null @@ -1,3 +0,0 @@ ----@async -return function(uri, callback) -end -- cgit v1.2.3 From b1d96627a1af5e4485720636dfb1d4f767e184d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Thu, 16 Jun 2022 21:13:07 +0800 Subject: don't check `unknown` --- script/core/diagnostics/cast-local-type.lua | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/cast-local-type.lua b/script/core/diagnostics/cast-local-type.lua index 1284e934..f466f923 100644 --- a/script/core/diagnostics/cast-local-type.lua +++ b/script/core/diagnostics/cast-local-type.lua @@ -16,10 +16,14 @@ return function (uri, callback) if not loc.ref then return end + await.delay() local locNode = vm.compileNode(loc) if not locNode:getData 'hasDefined' then return end + if vm.getInfer(loc):hasUnknown(uri) then + return + end for _, ref in ipairs(loc.ref) do if ref.type == 'setlocal' then await.delay() -- cgit v1.2.3 From a1f966af7db434c5dc7b2423ae63f4fe21957b46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Thu, 16 Jun 2022 21:19:32 +0800 Subject: can set `table` or `class` to `nil` --- script/core/diagnostics/cast-local-type.lua | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/cast-local-type.lua b/script/core/diagnostics/cast-local-type.lua index f466f923..4b7f7e84 100644 --- a/script/core/diagnostics/cast-local-type.lua +++ b/script/core/diagnostics/cast-local-type.lua @@ -24,21 +24,28 @@ return function (uri, callback) if vm.getInfer(loc):hasUnknown(uri) then return end + local canSetNil = vm.getInfer(loc):hasClass(uri) + or vm.getInfer(loc):hasType(uri, 'table') for _, ref in ipairs(loc.ref) do if ref.type == 'setlocal' then await.delay() local refNode = vm.compileNode(ref) - if not vm.isSubType(uri, refNode, locNode) then - callback { - start = ref.start, - finish = ref.finish, - message = lang.script('DIAG_CAST_LOCAL_TYPE', { - loc = vm.getInfer(locNode):view(uri), - ref = vm.getInfer(refNode):view(uri), - }), - } + if canSetNil and vm.getInfer(ref):view(uri) == 'nil' then + goto CONTINUE end + if vm.isSubType(uri, refNode, locNode) then + goto CONTINUE + end + callback { + start = ref.start, + finish = ref.finish, + message = lang.script('DIAG_CAST_LOCAL_TYPE', { + loc = vm.getInfer(locNode):view(uri), + ref = vm.getInfer(refNode):view(uri), + }), + } end + ::CONTINUE:: end end) end -- cgit v1.2.3 From 814d7dfccb1ecfbd887af40f4df951fbdecb12ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Thu, 16 Jun 2022 22:37:52 +0800 Subject: small fix --- script/core/diagnostics/init.lua | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/init.lua b/script/core/diagnostics/init.lua index a2eb2482..e9af8ea3 100644 --- a/script/core/diagnostics/init.lua +++ b/script/core/diagnostics/init.lua @@ -7,11 +7,12 @@ local util = require 'utility' -- 把耗时最长的诊断放到最后面 local diagSort = { - ['redundant-value'] = 96, - ['not-yieldable'] = 97, - ['deprecated'] = 98, - ['undefined-field'] = 99, - ['redundant-parameter'] = 100, + ['redundant-value'] = 100, + ['not-yieldable'] = 101, + ['deprecated'] = 102, + ['undefined-field'] = 103, + ['redundant-parameter'] = 104, + ['cast-local-type'] = 105, } local diagList = {} -- cgit v1.2.3 From 05b8cf4e0f92798c08aceb74891de5fb025ae1fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Fri, 17 Jun 2022 15:51:03 +0800 Subject: fix #1223 count returns of `doc.type.function` --- script/core/diagnostics/missing-parameter.lua | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/missing-parameter.lua b/script/core/diagnostics/missing-parameter.lua index a475673f..9844046f 100644 --- a/script/core/diagnostics/missing-parameter.lua +++ b/script/core/diagnostics/missing-parameter.lua @@ -5,7 +5,7 @@ local lang = require 'language' ---@param source parser.object ---@return integer -local function countReturns(source) +local function countReturnsOfFunction(source) local n = 0 local docs = source.bindDocs @@ -33,14 +33,25 @@ local function countReturns(source) return n end +---@param source parser.object +---@return integer +local function countReturnsOfDocFunction(source) + return #source.returns +end + local function countMaxReturns(source) local hasFounded local n = 0 for _, def in ipairs(vm.getDefs(source)) do - if def.type == 'doc.type.function' - or def.type == 'function' then + if def.type == 'function' then + hasFounded = true + local rets = countReturnsOfFunction(def) + if rets > n then + n = rets + end + elseif def.type == 'doc.type.function' then hasFounded = true - local rets = countReturns(def) + local rets = countReturnsOfDocFunction(def) if rets > n then n = rets end -- cgit v1.2.3 From 166504aa87a5b2f7fe6bd41b2c572afccb717534 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Fri, 17 Jun 2022 18:15:13 +0800 Subject: update --- script/core/diagnostics/assign-type-mismatch.lua | 47 ++++++++++++++++++++++++ script/core/diagnostics/init.lua | 13 ++++--- script/core/diagnostics/type-check-assign.lua | 20 ---------- 3 files changed, 54 insertions(+), 26 deletions(-) create mode 100644 script/core/diagnostics/assign-type-mismatch.lua delete mode 100644 script/core/diagnostics/type-check-assign.lua (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/assign-type-mismatch.lua b/script/core/diagnostics/assign-type-mismatch.lua new file mode 100644 index 00000000..38cfc674 --- /dev/null +++ b/script/core/diagnostics/assign-type-mismatch.lua @@ -0,0 +1,47 @@ +local files = require 'files' +local lang = require 'language' +local guide = require 'parser.guide' +local vm = require 'vm' +local await = require 'await' + +local checkTypes = { + 'setlocal', + 'setglobal', + 'setfield', + 'setindex', + 'setmethod', + 'tablefield', + 'tableindex' +} + +---@async +return function (uri, callback) + local state = files.getState(uri) + if not state then + return + end + + ---@async + guide.eachSourceTypes(state.ast, checkTypes, function (source) + local value = source.value + if not value then + return + end + await.delay() + local varNode = vm.compileNode(source) + local valueNode = vm.compileNode(value) + if vm.getInfer(varNode):hasUnknown(uri) then + return + end + if not vm.isSubType(uri, valueNode, varNode) then + callback { + start = source.start, + finish = source.finish, + message = lang.script('DIAG_ASSIGN_TYPE_MISMATCH', { + loc = vm.getInfer(varNode):view(uri), + ref = vm.getInfer(valueNode):view(uri), + }), + } + end + end) +end diff --git a/script/core/diagnostics/init.lua b/script/core/diagnostics/init.lua index e9af8ea3..746052b0 100644 --- a/script/core/diagnostics/init.lua +++ b/script/core/diagnostics/init.lua @@ -7,12 +7,13 @@ local util = require 'utility' -- 把耗时最长的诊断放到最后面 local diagSort = { - ['redundant-value'] = 100, - ['not-yieldable'] = 101, - ['deprecated'] = 102, - ['undefined-field'] = 103, - ['redundant-parameter'] = 104, - ['cast-local-type'] = 105, + ['redundant-value'] = 100, + ['not-yieldable'] = 101, + ['deprecated'] = 102, + ['undefined-field'] = 103, + ['redundant-parameter'] = 104, + ['cast-local-type'] = 105, + ['assign-type-mismatch'] = 106, } local diagList = {} diff --git a/script/core/diagnostics/type-check-assign.lua b/script/core/diagnostics/type-check-assign.lua deleted file mode 100644 index 6b3c73b3..00000000 --- a/script/core/diagnostics/type-check-assign.lua +++ /dev/null @@ -1,20 +0,0 @@ -local files = require 'files' -local lang = require 'language' -local guide = require 'parser.guide' -local vm = require 'vm' - -return function (uri, callback) - local state = files.getState(uri) - if not state then - return - end - - guide.eachSourceType(state.ast, 'setlocal', function (source) - local value = source.value - if not value then - return - end - local locNode = vm.compileNode(source) - local valueNode = vm.compileNode(value) - end) -end -- cgit v1.2.3 From 7b35e39b5eae7c3445cd9ec4d16637185bc26b93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Fri, 17 Jun 2022 23:02:55 +0800 Subject: update --- script/core/diagnostics/assign-type-mismatch.lua | 1 - 1 file changed, 1 deletion(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/assign-type-mismatch.lua b/script/core/diagnostics/assign-type-mismatch.lua index 38cfc674..9a101be1 100644 --- a/script/core/diagnostics/assign-type-mismatch.lua +++ b/script/core/diagnostics/assign-type-mismatch.lua @@ -5,7 +5,6 @@ local vm = require 'vm' local await = require 'await' local checkTypes = { - 'setlocal', 'setglobal', 'setfield', 'setindex', -- cgit v1.2.3 From 6df9d8376bc5339a8c4218cc261dc76d7ac8e7ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Sat, 18 Jun 2022 01:03:25 +0800 Subject: update --- script/core/diagnostics/assign-type-mismatch.lua | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/assign-type-mismatch.lua b/script/core/diagnostics/assign-type-mismatch.lua index 9a101be1..d301faed 100644 --- a/script/core/diagnostics/assign-type-mismatch.lua +++ b/script/core/diagnostics/assign-type-mismatch.lua @@ -5,6 +5,7 @@ local vm = require 'vm' local await = require 'await' local checkTypes = { + 'setlocal', 'setglobal', 'setfield', 'setindex', @@ -27,6 +28,12 @@ return function (uri, callback) return end await.delay() + if source.type == 'setlocal' then + local locNode = vm.compileNode(source.node) + if not locNode:getData 'hasDefined' then + return + end + end local varNode = vm.compileNode(source) local valueNode = vm.compileNode(value) if vm.getInfer(varNode):hasUnknown(uri) then -- cgit v1.2.3 From 5099d8be7125b948691d099b0e0084a72620293b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Sat, 18 Jun 2022 01:20:54 +0800 Subject: update --- script/core/diagnostics/cast-local-type.lua | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/cast-local-type.lua b/script/core/diagnostics/cast-local-type.lua index 4b7f7e84..65cc0368 100644 --- a/script/core/diagnostics/cast-local-type.lua +++ b/script/core/diagnostics/cast-local-type.lua @@ -24,13 +24,25 @@ return function (uri, callback) if vm.getInfer(loc):hasUnknown(uri) then return end - local canSetNil = vm.getInfer(loc):hasClass(uri) - or vm.getInfer(loc):hasType(uri, 'table') + + -- allow `local x = {};x = nil`, + -- but not allow `local x ---@type table;x = nil` + local allowNil = vm.getInfer(loc):hasType(uri, 'table') + and not locNode:hasType 'table' + + -- allow `local x = 0;x = 1.0`, + -- but not allow `local x ---@type integer;x = 1.0` + local allowNumber = vm.getInfer(loc):hasType(uri, 'integer') + and not locNode:hasType 'integer' + for _, ref in ipairs(loc.ref) do if ref.type == 'setlocal' then await.delay() local refNode = vm.compileNode(ref) - if canSetNil and vm.getInfer(ref):view(uri) == 'nil' then + if allowNil and vm.isSubType(uri, refNode, 'nil') then + goto CONTINUE + end + if allowNumber and vm.isSubType(uri, refNode, 'number') then goto CONTINUE end if vm.isSubType(uri, refNode, locNode) then -- cgit v1.2.3 From 53d376ce281906fd856fac42073a072906b2628e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Sat, 18 Jun 2022 01:58:48 +0800 Subject: update --- script/core/diagnostics/assign-type-mismatch.lua | 20 ++++++------ script/core/diagnostics/cast-local-type.lua | 41 ++++++------------------ 2 files changed, 19 insertions(+), 42 deletions(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/assign-type-mismatch.lua b/script/core/diagnostics/assign-type-mismatch.lua index d301faed..168106a6 100644 --- a/script/core/diagnostics/assign-type-mismatch.lua +++ b/script/core/diagnostics/assign-type-mismatch.lua @@ -36,18 +36,16 @@ return function (uri, callback) end local varNode = vm.compileNode(source) local valueNode = vm.compileNode(value) - if vm.getInfer(varNode):hasUnknown(uri) then + if vm.canCastType(uri, varNode, valueNode) then return end - if not vm.isSubType(uri, valueNode, varNode) then - callback { - start = source.start, - finish = source.finish, - message = lang.script('DIAG_ASSIGN_TYPE_MISMATCH', { - loc = vm.getInfer(varNode):view(uri), - ref = vm.getInfer(valueNode):view(uri), - }), - } - end + callback { + start = source.start, + finish = source.finish, + message = lang.script('DIAG_ASSIGN_TYPE_MISMATCH', { + loc = vm.getInfer(varNode):view(uri), + ref = vm.getInfer(valueNode):view(uri), + }), + } end) end diff --git a/script/core/diagnostics/cast-local-type.lua b/script/core/diagnostics/cast-local-type.lua index 65cc0368..ea14ffb6 100644 --- a/script/core/diagnostics/cast-local-type.lua +++ b/script/core/diagnostics/cast-local-type.lua @@ -21,43 +21,22 @@ return function (uri, callback) if not locNode:getData 'hasDefined' then return end - if vm.getInfer(loc):hasUnknown(uri) then - return - end - - -- allow `local x = {};x = nil`, - -- but not allow `local x ---@type table;x = nil` - local allowNil = vm.getInfer(loc):hasType(uri, 'table') - and not locNode:hasType 'table' - - -- allow `local x = 0;x = 1.0`, - -- but not allow `local x ---@type integer;x = 1.0` - local allowNumber = vm.getInfer(loc):hasType(uri, 'integer') - and not locNode:hasType 'integer' - for _, ref in ipairs(loc.ref) do if ref.type == 'setlocal' then await.delay() local refNode = vm.compileNode(ref) - if allowNil and vm.isSubType(uri, refNode, 'nil') then - goto CONTINUE - end - if allowNumber and vm.isSubType(uri, refNode, 'number') then - goto CONTINUE - end - if vm.isSubType(uri, refNode, locNode) then - goto CONTINUE + + if not vm.canCastType(uri, locNode, refNode) then + callback { + start = ref.start, + finish = ref.finish, + message = lang.script('DIAG_CAST_LOCAL_TYPE', { + loc = vm.getInfer(locNode):view(uri), + ref = vm.getInfer(refNode):view(uri), + }), + } end - callback { - start = ref.start, - finish = ref.finish, - message = lang.script('DIAG_CAST_LOCAL_TYPE', { - loc = vm.getInfer(locNode):view(uri), - ref = vm.getInfer(refNode):view(uri), - }), - } end - ::CONTINUE:: end end) end -- cgit v1.2.3 From 7b3bdcfb3f62695bb8ad237046285634a701a697 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Sat, 18 Jun 2022 16:15:26 +0800 Subject: update --- script/core/diagnostics/undefined-field.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/undefined-field.lua b/script/core/diagnostics/undefined-field.lua index b03838fd..a83241f5 100644 --- a/script/core/diagnostics/undefined-field.lua +++ b/script/core/diagnostics/undefined-field.lua @@ -35,10 +35,10 @@ return function (uri, callback) if node then local ok for view in vm.getInfer(node):eachView(uri) do - if not skipCheckClass[view] then - ok = true - break + if skipCheckClass[view] then + return end + ok = true end if not ok then return -- cgit v1.2.3 From ea94ae75080956dd49608d2a870f45f4c6ff82d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Mon, 20 Jun 2022 16:03:09 +0800 Subject: use preview --- script/core/diagnostics/assign-type-mismatch.lua | 3 +++ script/core/diagnostics/cast-local-type.lua | 3 +++ 2 files changed, 6 insertions(+) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/assign-type-mismatch.lua b/script/core/diagnostics/assign-type-mismatch.lua index 168106a6..aae4cccb 100644 --- a/script/core/diagnostics/assign-type-mismatch.lua +++ b/script/core/diagnostics/assign-type-mismatch.lua @@ -16,6 +16,9 @@ local checkTypes = { ---@async return function (uri, callback) + if not PREVIEW then + return + end local state = files.getState(uri) if not state then return diff --git a/script/core/diagnostics/cast-local-type.lua b/script/core/diagnostics/cast-local-type.lua index ea14ffb6..0a236084 100644 --- a/script/core/diagnostics/cast-local-type.lua +++ b/script/core/diagnostics/cast-local-type.lua @@ -6,6 +6,9 @@ local await = require 'await' ---@async return function (uri, callback) + if not PREVIEW then + return + end local state = files.getState(uri) if not state then return -- cgit v1.2.3 From f7399c6206378b76c87a7386707cf8e24d5a98ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Mon, 20 Jun 2022 17:43:58 +0800 Subject: `cast-field-type` --- script/core/diagnostics/assign-type-mismatch.lua | 2 +- script/core/diagnostics/cast-field-type.lua | 70 ++++++++++++++++++++++++ script/core/diagnostics/cast-local-type.lua | 2 +- script/core/diagnostics/init.lua | 13 +++-- 4 files changed, 79 insertions(+), 8 deletions(-) create mode 100644 script/core/diagnostics/cast-field-type.lua (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/assign-type-mismatch.lua b/script/core/diagnostics/assign-type-mismatch.lua index aae4cccb..89ae0a61 100644 --- a/script/core/diagnostics/assign-type-mismatch.lua +++ b/script/core/diagnostics/assign-type-mismatch.lua @@ -16,7 +16,7 @@ local checkTypes = { ---@async return function (uri, callback) - if not PREVIEW then + if not PREVIEW and not TEST then return end local state = files.getState(uri) diff --git a/script/core/diagnostics/cast-field-type.lua b/script/core/diagnostics/cast-field-type.lua new file mode 100644 index 00000000..24299649 --- /dev/null +++ b/script/core/diagnostics/cast-field-type.lua @@ -0,0 +1,70 @@ +local files = require 'files' +local lang = require 'language' +local guide = require 'parser.guide' +local vm = require 'vm' +local await = require 'await' + +---@async +return function (uri, callback) + if not PREVIEW and not TEST then + return + end + local state = files.getState(uri) + if not state then + return + end + + local fieldNodeCache = {} + + local function getParentField(parent, key) + if parent.type == 'getlocal' then + parent = parent.node + end + if not fieldNodeCache[parent] then + fieldNodeCache[parent] = {} + end + if fieldNodeCache[parent][key] then + return fieldNodeCache[parent][key] + end + local fieldNode = vm.createNode() + fieldNodeCache[parent][key] = fieldNode + for _, class in ipairs(vm.getDefs(parent)) do + if class.type == 'doc.class' then + vm.getClassFields(uri, vm.getGlobal('type', class.class[1]), key, false, function (def) + if def.type == 'doc.field' then + fieldNode:merge(vm.compileNode(def)) + end + end) + end + end + return fieldNode + end + + ---@async + guide.eachSourceTypes(state.ast, { 'setfield', 'setindex', 'setmethod', 'tablefield', 'tableindex' }, function (ref) + await.delay() + if not ref.value then + return + end + local key = guide.getKeyName(ref) + if not key then + return nil + end + local parent = ref.node + local fieldNode = getParentField(parent, key) + if not fieldNode or fieldNode:isEmpty() then + return + end + if vm.canCastType(uri, fieldNode, vm.compileNode(ref.value)) then + return + end + callback { + start = ref.start, + finish = ref.finish, + message = lang.script('DIAG_CAST_LOCAL_TYPE', { + def = vm.getInfer(fieldNode):view(uri), + ref = vm.getInfer(ref.value):view(uri), + }), + } + end) +end diff --git a/script/core/diagnostics/cast-local-type.lua b/script/core/diagnostics/cast-local-type.lua index 0a236084..dd3a37e8 100644 --- a/script/core/diagnostics/cast-local-type.lua +++ b/script/core/diagnostics/cast-local-type.lua @@ -6,7 +6,7 @@ local await = require 'await' ---@async return function (uri, callback) - if not PREVIEW then + if not PREVIEW and not TEST then return end local state = files.getState(uri) diff --git a/script/core/diagnostics/init.lua b/script/core/diagnostics/init.lua index 746052b0..efd23bf4 100644 --- a/script/core/diagnostics/init.lua +++ b/script/core/diagnostics/init.lua @@ -8,12 +8,13 @@ local util = require 'utility' -- 把耗时最长的诊断放到最后面 local diagSort = { ['redundant-value'] = 100, - ['not-yieldable'] = 101, - ['deprecated'] = 102, - ['undefined-field'] = 103, - ['redundant-parameter'] = 104, - ['cast-local-type'] = 105, - ['assign-type-mismatch'] = 106, + ['not-yieldable'] = 100, + ['deprecated'] = 100, + ['undefined-field'] = 110, + ['redundant-parameter'] = 110, + ['cast-local-type'] = 120, + ['cast-field-type'] = 120, + ['assign-type-mismatch'] = 120, } local diagList = {} -- cgit v1.2.3 From a04cffb43132645f63e5a319f6ca69e0df87dcdb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Mon, 20 Jun 2022 19:20:53 +0800 Subject: update --- script/core/diagnostics/assign-type-mismatch.lua | 2 +- script/core/diagnostics/cast-field-type.lua | 2 +- script/core/diagnostics/cast-local-type.lua | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/assign-type-mismatch.lua b/script/core/diagnostics/assign-type-mismatch.lua index 89ae0a61..37e18d09 100644 --- a/script/core/diagnostics/assign-type-mismatch.lua +++ b/script/core/diagnostics/assign-type-mismatch.lua @@ -46,7 +46,7 @@ return function (uri, callback) start = source.start, finish = source.finish, message = lang.script('DIAG_ASSIGN_TYPE_MISMATCH', { - loc = vm.getInfer(varNode):view(uri), + def = vm.getInfer(varNode):view(uri), ref = vm.getInfer(valueNode):view(uri), }), } diff --git a/script/core/diagnostics/cast-field-type.lua b/script/core/diagnostics/cast-field-type.lua index 24299649..eaab0bbe 100644 --- a/script/core/diagnostics/cast-field-type.lua +++ b/script/core/diagnostics/cast-field-type.lua @@ -61,7 +61,7 @@ return function (uri, callback) callback { start = ref.start, finish = ref.finish, - message = lang.script('DIAG_CAST_LOCAL_TYPE', { + message = lang.script('DIAG_CAST_FIELD_TYPE', { def = vm.getInfer(fieldNode):view(uri), ref = vm.getInfer(ref.value):view(uri), }), diff --git a/script/core/diagnostics/cast-local-type.lua b/script/core/diagnostics/cast-local-type.lua index dd3a37e8..cc4d4a0b 100644 --- a/script/core/diagnostics/cast-local-type.lua +++ b/script/core/diagnostics/cast-local-type.lua @@ -34,7 +34,7 @@ return function (uri, callback) start = ref.start, finish = ref.finish, message = lang.script('DIAG_CAST_LOCAL_TYPE', { - loc = vm.getInfer(locNode):view(uri), + def = vm.getInfer(locNode):view(uri), ref = vm.getInfer(refNode):view(uri), }), } -- cgit v1.2.3 From c65de666e0a3706b921993648910ef531c47c607 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Mon, 20 Jun 2022 20:58:56 +0800 Subject: update --- script/core/diagnostics/cast-field-type.lua | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/cast-field-type.lua b/script/core/diagnostics/cast-field-type.lua index eaab0bbe..5cf36a5a 100644 --- a/script/core/diagnostics/cast-field-type.lua +++ b/script/core/diagnostics/cast-field-type.lua @@ -6,6 +6,7 @@ local await = require 'await' ---@async return function (uri, callback) + do return end if not PREVIEW and not TEST then return end @@ -31,7 +32,10 @@ return function (uri, callback) for _, class in ipairs(vm.getDefs(parent)) do if class.type == 'doc.class' then vm.getClassFields(uri, vm.getGlobal('type', class.class[1]), key, false, function (def) - if def.type == 'doc.field' then + if def.type == 'doc.field' + or def.type == 'setfield' + or def.type == 'setmethod' + or def.type == 'setindex' then fieldNode:merge(vm.compileNode(def)) end end) @@ -50,6 +54,9 @@ return function (uri, callback) if not key then return nil end + if not vm.canCastType(uri, vm.compileNode(ref), vm.compileNode(ref.value)) then + return + end local parent = ref.node local fieldNode = getParentField(parent, key) if not fieldNode or fieldNode:isEmpty() then -- cgit v1.2.3 From 5f564a8ba866ab83f12da38b2f3c56002ce6bace Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Mon, 20 Jun 2022 21:04:54 +0800 Subject: update --- script/core/diagnostics/cast-field-type.lua | 77 ----------------------------- script/core/diagnostics/init.lua | 1 - 2 files changed, 78 deletions(-) delete mode 100644 script/core/diagnostics/cast-field-type.lua (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/cast-field-type.lua b/script/core/diagnostics/cast-field-type.lua deleted file mode 100644 index 5cf36a5a..00000000 --- a/script/core/diagnostics/cast-field-type.lua +++ /dev/null @@ -1,77 +0,0 @@ -local files = require 'files' -local lang = require 'language' -local guide = require 'parser.guide' -local vm = require 'vm' -local await = require 'await' - ----@async -return function (uri, callback) - do return end - if not PREVIEW and not TEST then - return - end - local state = files.getState(uri) - if not state then - return - end - - local fieldNodeCache = {} - - local function getParentField(parent, key) - if parent.type == 'getlocal' then - parent = parent.node - end - if not fieldNodeCache[parent] then - fieldNodeCache[parent] = {} - end - if fieldNodeCache[parent][key] then - return fieldNodeCache[parent][key] - end - local fieldNode = vm.createNode() - fieldNodeCache[parent][key] = fieldNode - for _, class in ipairs(vm.getDefs(parent)) do - if class.type == 'doc.class' then - vm.getClassFields(uri, vm.getGlobal('type', class.class[1]), key, false, function (def) - if def.type == 'doc.field' - or def.type == 'setfield' - or def.type == 'setmethod' - or def.type == 'setindex' then - fieldNode:merge(vm.compileNode(def)) - end - end) - end - end - return fieldNode - end - - ---@async - guide.eachSourceTypes(state.ast, { 'setfield', 'setindex', 'setmethod', 'tablefield', 'tableindex' }, function (ref) - await.delay() - if not ref.value then - return - end - local key = guide.getKeyName(ref) - if not key then - return nil - end - if not vm.canCastType(uri, vm.compileNode(ref), vm.compileNode(ref.value)) then - return - end - local parent = ref.node - local fieldNode = getParentField(parent, key) - if not fieldNode or fieldNode:isEmpty() then - return - end - if vm.canCastType(uri, fieldNode, vm.compileNode(ref.value)) then - return - end - callback { - start = ref.start, - finish = ref.finish, - message = lang.script('DIAG_CAST_FIELD_TYPE', { - def = vm.getInfer(fieldNode):view(uri), - ref = vm.getInfer(ref.value):view(uri), - }), - } - end) -end diff --git a/script/core/diagnostics/init.lua b/script/core/diagnostics/init.lua index efd23bf4..fe551bc8 100644 --- a/script/core/diagnostics/init.lua +++ b/script/core/diagnostics/init.lua @@ -13,7 +13,6 @@ local diagSort = { ['undefined-field'] = 110, ['redundant-parameter'] = 110, ['cast-local-type'] = 120, - ['cast-field-type'] = 120, ['assign-type-mismatch'] = 120, } -- cgit v1.2.3 From 28d5a33077b28dcbb52974aaf90125cee013342b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Mon, 20 Jun 2022 22:41:29 +0800 Subject: update `array[1] = nil` --- script/core/diagnostics/assign-type-mismatch.lua | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/assign-type-mismatch.lua b/script/core/diagnostics/assign-type-mismatch.lua index 37e18d09..30d0f97c 100644 --- a/script/core/diagnostics/assign-type-mismatch.lua +++ b/script/core/diagnostics/assign-type-mismatch.lua @@ -37,8 +37,18 @@ return function (uri, callback) return end end - local varNode = vm.compileNode(source) local valueNode = vm.compileNode(value) + if source.type == 'setindex' + and vm.isSubType(uri, valueNode, 'nil') then + -- boolean[1] = nil + local tnode = vm.compileNode(source.node) + for n in tnode:eachObject() do + if n.type == 'doc.type.array' then + return + end + end + end + local varNode = vm.compileNode(source) if vm.canCastType(uri, varNode, valueNode) then return end -- cgit v1.2.3 From f629e481de53bbfac24ebae5c952bb80e3300d47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Mon, 20 Jun 2022 22:50:39 +0800 Subject: update --- script/core/diagnostics/assign-type-mismatch.lua | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/assign-type-mismatch.lua b/script/core/diagnostics/assign-type-mismatch.lua index 30d0f97c..c6a4195c 100644 --- a/script/core/diagnostics/assign-type-mismatch.lua +++ b/script/core/diagnostics/assign-type-mismatch.lua @@ -43,7 +43,9 @@ return function (uri, callback) -- boolean[1] = nil local tnode = vm.compileNode(source.node) for n in tnode:eachObject() do - if n.type == 'doc.type.array' then + if n.type == 'doc.type.array' + or n.type == 'doc.type.table' + or n.type == 'table' then return end end -- cgit v1.2.3 From 8af599790936f81cddaecb31d36eede70ad3739c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Tue, 21 Jun 2022 16:40:07 +0800 Subject: fix --- script/core/diagnostics/assign-type-mismatch.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/assign-type-mismatch.lua b/script/core/diagnostics/assign-type-mismatch.lua index c6a4195c..ae4b3512 100644 --- a/script/core/diagnostics/assign-type-mismatch.lua +++ b/script/core/diagnostics/assign-type-mismatch.lua @@ -50,7 +50,7 @@ return function (uri, callback) end end end - local varNode = vm.compileNode(source) + local varNode = vm.compileNode(source) if vm.canCastType(uri, varNode, valueNode) then return end -- cgit v1.2.3 From 7b3e7f4b0a62b7c9c3b895e89b4fe79bb8f350ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Wed, 22 Jun 2022 01:41:16 +0800 Subject: fix --- script/core/diagnostics/missing-parameter.lua | 122 +----------------------- script/core/diagnostics/redundant-parameter.lua | 63 +++--------- 2 files changed, 18 insertions(+), 167 deletions(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/missing-parameter.lua b/script/core/diagnostics/missing-parameter.lua index 9844046f..b6067175 100644 --- a/script/core/diagnostics/missing-parameter.lua +++ b/script/core/diagnostics/missing-parameter.lua @@ -3,116 +3,6 @@ local guide = require 'parser.guide' local vm = require 'vm' local lang = require 'language' ----@param source parser.object ----@return integer -local function countReturnsOfFunction(source) - local n = 0 - - local docs = source.bindDocs - if docs then - for _, doc in ipairs(docs) do - if doc.type == 'doc.return' then - for _, rtn in ipairs(doc.returns) do - if rtn.returnIndex and rtn.returnIndex > n then - n = rtn.returnIndex - end - end - end - end - end - - local returns = source.returns - if returns then - for _, rtn in ipairs(returns) do - if #rtn > n then - n = #rtn - end - end - end - - return n -end - ----@param source parser.object ----@return integer -local function countReturnsOfDocFunction(source) - return #source.returns -end - -local function countMaxReturns(source) - local hasFounded - local n = 0 - for _, def in ipairs(vm.getDefs(source)) do - if def.type == 'function' then - hasFounded = true - local rets = countReturnsOfFunction(def) - if rets > n then - n = rets - end - elseif def.type == 'doc.type.function' then - hasFounded = true - local rets = countReturnsOfDocFunction(def) - if rets > n then - n = rets - end - end - end - - if hasFounded then - return n - else - return math.huge - end -end - -local function countCallArgs(source) - local result = 0 - if not source.args then - return 0 - end - local lastArg = source.args[#source.args] - if lastArg.type == 'varargs' then - return math.huge - end - if lastArg.type == 'call' then - result = result + countMaxReturns(lastArg.node) - 1 - end - result = result + #source.args - return result -end - ----@return integer -local function countFuncArgs(source) - if not source.args or #source.args == 0 then - return 0 - end - local count = 0 - for i = #source.args, 1, -1 do - local arg = source.args[i] - if arg.type ~= '...' - and not (arg.name and arg.name[1] =='...') - and not vm.compileNode(arg):isNullable() then - return i - end - end - return count -end - -local function getFuncArgs(func) - local funcArgs - local defs = vm.getDefs(func) - for _, def in ipairs(defs) do - if def.type == 'function' - or def.type == 'doc.type.function' then - local args = countFuncArgs(def) - if not funcArgs or args < funcArgs then - funcArgs = args - end - end - end - return funcArgs -end - return function (uri, callback) local state = files.getState(uri) if not state then @@ -120,19 +10,15 @@ return function (uri, callback) end guide.eachSourceType(state.ast, 'call', function (source) - local callArgs = countCallArgs(source) + local _, callArgs = vm.countList(source.args) - local func = source.node - local funcArgs = getFuncArgs(func) + local funcNode = vm.compileNode(source.node) + local funcArgs = vm.countParamsOfNode(funcNode) - if not funcArgs then + if callArgs >= funcArgs then return end - local delta = callArgs - funcArgs - if delta >= 0 then - return - end callback { start = source.start, finish = source.finish, diff --git a/script/core/diagnostics/redundant-parameter.lua b/script/core/diagnostics/redundant-parameter.lua index 41781df8..2b7f1230 100644 --- a/script/core/diagnostics/redundant-parameter.lua +++ b/script/core/diagnostics/redundant-parameter.lua @@ -3,43 +3,6 @@ local guide = require 'parser.guide' local vm = require 'vm' local lang = require 'language' -local function countCallArgs(source) - local result = 0 - if not source.args then - return 0 - end - result = result + #source.args - return result -end - -local function countFuncArgs(source) - if not source.args or #source.args == 0 then - return 0 - end - local lastArg = source.args[#source.args] - if lastArg.type == '...' - or (lastArg.name and lastArg.name[1] == '...') then - return math.maxinteger - else - return #source.args - end -end - -local function getFuncArgs(func) - local funcArgs - local defs = vm.getDefs(func) - for _, def in ipairs(defs) do - if def.type == 'function' - or def.type == 'doc.type.function' then - local args = countFuncArgs(def) - if not funcArgs or args > funcArgs then - funcArgs = args - end - end - end - return funcArgs -end - return function (uri, callback) local state = files.getState(uri) if not state then @@ -47,28 +10,30 @@ return function (uri, callback) end guide.eachSourceType(state.ast, 'call', function (source) - local callArgs = countCallArgs(source) + local callArgs = vm.countList(source.args) if callArgs == 0 then return end - local func = source.node - local funcArgs = getFuncArgs(func) - - if not funcArgs then - return - end + local funcNode = vm.compileNode(source.node) + local _, funcArgs = vm.countParamsOfNode(funcNode) - local delta = callArgs - funcArgs - if delta <= 0 then + if callArgs <= funcArgs then return end if callArgs == 1 and source.node.type == 'getmethod' then return end - for i = #source.args - delta + 1, #source.args do - local arg = source.args[i] - if arg then + if funcArgs + 1 > #source.args then + local lastArg = source.args[#source.args] + callback { + start = lastArg.start, + finish = lastArg.finish, + message = lang.script('DIAG_OVER_MAX_ARGS', funcArgs, callArgs) + } + else + for i = funcArgs + 1, #source.args do + local arg = source.args[i] callback { start = arg.start, finish = arg.finish, -- cgit v1.2.3 From 2b499cb652e9ad6b60c85a491aebeb84f2f8941a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Wed, 22 Jun 2022 01:49:34 +0800 Subject: improve one case --- script/core/diagnostics/redundant-parameter.lua | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/redundant-parameter.lua b/script/core/diagnostics/redundant-parameter.lua index 2b7f1230..deda918a 100644 --- a/script/core/diagnostics/redundant-parameter.lua +++ b/script/core/diagnostics/redundant-parameter.lua @@ -26,6 +26,12 @@ return function (uri, callback) end if funcArgs + 1 > #source.args then local lastArg = source.args[#source.args] + if lastArg.type == 'call' and funcArgs > 0 then + -- 如果函数接收至少一个参数,那么调用方最后一个参数是函数调用 + -- 导致的参数数量太多可以忽略。 + -- 如果函数不接收任何参数,那么任何参数都是错误的。 + return + end callback { start = lastArg.start, finish = lastArg.finish, -- cgit v1.2.3 From e5d692cb0845e5de7988904017440bd4db551bb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Wed, 22 Jun 2022 16:13:50 +0800 Subject: `param-type-mismatch` --- script/core/diagnostics/init.lua | 1 + script/core/diagnostics/param-type-mismatch.lua | 61 +++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 script/core/diagnostics/param-type-mismatch.lua (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/init.lua b/script/core/diagnostics/init.lua index fe551bc8..c5cb90ca 100644 --- a/script/core/diagnostics/init.lua +++ b/script/core/diagnostics/init.lua @@ -14,6 +14,7 @@ local diagSort = { ['redundant-parameter'] = 110, ['cast-local-type'] = 120, ['assign-type-mismatch'] = 120, + ['param-type-mismatch'] = 120, } local diagList = {} diff --git a/script/core/diagnostics/param-type-mismatch.lua b/script/core/diagnostics/param-type-mismatch.lua new file mode 100644 index 00000000..a05b03e4 --- /dev/null +++ b/script/core/diagnostics/param-type-mismatch.lua @@ -0,0 +1,61 @@ +local files = require 'files' +local lang = require 'language' +local guide = require 'parser.guide' +local vm = require 'vm' +local await = require 'await' + +---@async +return function (uri, callback) + if not PREVIEW and not TEST then + return + end + local state = files.getState(uri) + if not state then + return + end + + ---@param funcNode vm.node + ---@param i integer + ---@return vm.node? + local function getDefNode(funcNode, i) + local defNode = vm.createNode() + for f in funcNode:eachObject() do + if f.type == 'function' + or f.type == 'doc.type.function' then + local param = f.args and f.args[i] + if param then + defNode:merge(vm.compileNode(param)) + end + end + end + if defNode:isEmpty() then + return nil + end + return defNode + end + + ---@async + guide.eachSourceType(state.ast, 'call', function (source) + if not source.args then + return + end + await.delay() + local funcNode = vm.compileNode(source.node) + for i, arg in ipairs(source.args) do + local refNode = vm.compileNode(arg) + local defNode = getDefNode(funcNode, i) + if defNode then + if not vm.canCastType(uri, defNode, refNode) then + callback { + start = arg.start, + finish = arg.finish, + message = lang.script('DIAG_PARAM_TYPE_MISMATCH', { + def = vm.getInfer(defNode):view(uri), + ref = vm.getInfer(refNode):view(uri), + }) + } + end + end + end + end) +end -- cgit v1.2.3 From 79f76ed6611701ca5994fe110eb09e9309499918 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Wed, 22 Jun 2022 19:00:27 +0800 Subject: update --- script/core/diagnostics/cast-type-mismatch.lua | 45 +++++++++++++++++++++++ script/core/diagnostics/unknown-cast-variable.lua | 32 ++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 script/core/diagnostics/cast-type-mismatch.lua create mode 100644 script/core/diagnostics/unknown-cast-variable.lua (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/cast-type-mismatch.lua b/script/core/diagnostics/cast-type-mismatch.lua new file mode 100644 index 00000000..ecee6a84 --- /dev/null +++ b/script/core/diagnostics/cast-type-mismatch.lua @@ -0,0 +1,45 @@ +local files = require 'files' +local guide = require 'parser.guide' +local lang = require 'language' +local vm = require 'vm' +local await = require 'await' + +---@async +return function (uri, callback) + local state = files.getState(uri) + if not state then + return + end + + if not state.ast.docs then + return + end + + for _, doc in ipairs(state.ast.docs) do + if doc.type == 'doc.cast' then + await.delay() + local defs = vm.getDefs(doc.loc) + local loc = defs[1] + if loc then + local defNode = vm.compileNode(loc) + if defNode:getData 'hasDefined' then + for _, cast in ipairs(doc.casts) do + if not cast.mode and cast.extends then + local refNode = vm.compileNode(cast.extends) + if not vm.canCastType(uri, defNode, refNode) then + callback { + start = cast.extends.start, + finish = cast.extends.finish, + message = lang.script('DIAG_UNKNOWN_CAST_VARIABLE', { + def = vm.getInfer(defNode):view(uri), + ref = vm.getInfer(refNode):view(uri), + }) + } + end + end + end + end + end + end + end +end diff --git a/script/core/diagnostics/unknown-cast-variable.lua b/script/core/diagnostics/unknown-cast-variable.lua new file mode 100644 index 00000000..cfa25ed8 --- /dev/null +++ b/script/core/diagnostics/unknown-cast-variable.lua @@ -0,0 +1,32 @@ +local files = require 'files' +local guide = require 'parser.guide' +local lang = require 'language' +local vm = require 'vm' +local await = require 'await' + +---@async +return function (uri, callback) + local state = files.getState(uri) + if not state then + return + end + + if not state.ast.docs then + return + end + + for _, doc in ipairs(state.ast.docs) do + if doc.type == 'doc.cast' then + await.delay() + local defs = vm.getDefs(doc.loc) + local loc = defs[1] + if not loc then + callback { + start = doc.loc.start, + finish = doc.loc.finish, + message = lang.script('DIAG_UNKNOWN_CAST_VARIABLE', doc[1]) + } + end + end + end +end -- cgit v1.2.3 From ab171866489b903a74ab1da735ff5c35f78e9919 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Thu, 23 Jun 2022 02:06:45 +0800 Subject: check nil --- script/core/diagnostics/cast-type-mismatch.lua | 2 +- script/core/diagnostics/unknown-cast-variable.lua | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/cast-type-mismatch.lua b/script/core/diagnostics/cast-type-mismatch.lua index ecee6a84..0561a8da 100644 --- a/script/core/diagnostics/cast-type-mismatch.lua +++ b/script/core/diagnostics/cast-type-mismatch.lua @@ -16,7 +16,7 @@ return function (uri, callback) end for _, doc in ipairs(state.ast.docs) do - if doc.type == 'doc.cast' then + if doc.type == 'doc.cast' and doc.loc then await.delay() local defs = vm.getDefs(doc.loc) local loc = defs[1] diff --git a/script/core/diagnostics/unknown-cast-variable.lua b/script/core/diagnostics/unknown-cast-variable.lua index cfa25ed8..57377f6e 100644 --- a/script/core/diagnostics/unknown-cast-variable.lua +++ b/script/core/diagnostics/unknown-cast-variable.lua @@ -16,7 +16,7 @@ return function (uri, callback) end for _, doc in ipairs(state.ast.docs) do - if doc.type == 'doc.cast' then + if doc.type == 'doc.cast' and doc.loc then await.delay() local defs = vm.getDefs(doc.loc) local loc = defs[1] -- cgit v1.2.3 From 0ff98557a45c25d7a49520e57f49561a62300f35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Thu, 23 Jun 2022 02:45:02 +0800 Subject: update --- script/core/diagnostics/assign-type-mismatch.lua | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/assign-type-mismatch.lua b/script/core/diagnostics/assign-type-mismatch.lua index ae4b3512..b7d11226 100644 --- a/script/core/diagnostics/assign-type-mismatch.lua +++ b/script/core/diagnostics/assign-type-mismatch.lua @@ -38,14 +38,13 @@ return function (uri, callback) end end local valueNode = vm.compileNode(value) - if source.type == 'setindex' - and vm.isSubType(uri, valueNode, 'nil') then - -- boolean[1] = nil - local tnode = vm.compileNode(source.node) - for n in tnode:eachObject() do - if n.type == 'doc.type.array' - or n.type == 'doc.type.table' - or n.type == 'table' then + if source.type == 'setindex' + or source.type == 'setfield' + or source.type == 'tablefield' + or source.type == 'tableindex' then + if valueNode:hasName 'nil' then + valueNode = valueNode:copy():removeOptional() + if valueNode:isEmpty() then return end end -- cgit v1.2.3 From 838ce36fdd7abdff0b4cab5e67c2500325178805 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Thu, 23 Jun 2022 02:57:13 +0800 Subject: update --- script/core/diagnostics/ambiguity-1.lua | 4 ++-- script/core/diagnostics/newfield-call.lua | 8 ++++---- script/core/diagnostics/newline-call.lua | 2 +- script/core/diagnostics/trailing-space.lua | 4 ++-- 4 files changed, 9 insertions(+), 9 deletions(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/ambiguity-1.lua b/script/core/diagnostics/ambiguity-1.lua index f03f4361..830b2f2f 100644 --- a/script/core/diagnostics/ambiguity-1.lua +++ b/script/core/diagnostics/ambiguity-1.lua @@ -27,10 +27,10 @@ local literalMap = { return function (uri, callback) local state = files.getState(uri) - if not state then + local text = files.getText(uri) + if not state or not text then return end - local text = files.getText(uri) guide.eachSourceType(state.ast, 'binary', function (source) if source.op.type ~= 'or' then return diff --git a/script/core/diagnostics/newfield-call.lua b/script/core/diagnostics/newfield-call.lua index 669ed2bb..b81d8c0a 100644 --- a/script/core/diagnostics/newfield-call.lua +++ b/script/core/diagnostics/newfield-call.lua @@ -3,14 +3,14 @@ local guide = require 'parser.guide' local lang = require 'language' return function (uri, callback) - local ast = files.getState(uri) - if not ast then + local state = files.getState(uri) + local text = files.getText(uri) + if not state or not text then return end - local text = files.getText(uri) - guide.eachSourceType(ast.ast, 'table', function (source) + guide.eachSourceType(state.ast, 'table', function (source) for i = 1, #source do local field = source[i] if field.type ~= 'tableexp' then diff --git a/script/core/diagnostics/newline-call.lua b/script/core/diagnostics/newline-call.lua index 3f2d5ca5..9c526980 100644 --- a/script/core/diagnostics/newline-call.lua +++ b/script/core/diagnostics/newline-call.lua @@ -5,7 +5,7 @@ local lang = require 'language' return function (uri, callback) local state = files.getState(uri) local text = files.getText(uri) - if not state then + if not state or not text then return end diff --git a/script/core/diagnostics/trailing-space.lua b/script/core/diagnostics/trailing-space.lua index cc51cf77..d23b0692 100644 --- a/script/core/diagnostics/trailing-space.lua +++ b/script/core/diagnostics/trailing-space.lua @@ -14,10 +14,10 @@ end return function (uri, callback) local state = files.getState(uri) - if not state then + local text = files.getText(uri) + if not state or not text then return end - local text = files.getText(uri) local lines = state.lines for i = 0, #lines do local startOffset = lines[i] -- cgit v1.2.3 From 420812ce4e77ff4143437b2893dd77fc729c7622 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Thu, 23 Jun 2022 03:32:57 +0800 Subject: update --- script/core/diagnostics/duplicate-doc-field.lua | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/duplicate-doc-field.lua b/script/core/diagnostics/duplicate-doc-field.lua index d4116b9b..78112beb 100644 --- a/script/core/diagnostics/duplicate-doc-field.lua +++ b/script/core/diagnostics/duplicate-doc-field.lua @@ -1,5 +1,6 @@ local files = require 'files' local lang = require 'language' +local vm = require 'vm.vm' local function getFieldEventName(doc) if not doc.extends then @@ -45,7 +46,12 @@ return function (uri, callback) mark = {} elseif doc.type == 'doc.field' then if mark then - local name = ('%q'):format(doc.field[1]) + local name + if doc.field.type == 'doc.type' then + name = ('[%s]'):format(vm.getInfer(doc.field):view(uri)) + else + name = ('%q'):format(doc.field[1]) + end local eventName = getFieldEventName(doc) if eventName then name = name .. '|' .. eventName -- cgit v1.2.3 From 5c0f9c4672b564da6d54ac8649afe199e4d7b1d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Thu, 23 Jun 2022 14:13:05 +0800 Subject: revert `field drop nil` --- script/core/diagnostics/assign-type-mismatch.lua | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/assign-type-mismatch.lua b/script/core/diagnostics/assign-type-mismatch.lua index b7d11226..ae4b3512 100644 --- a/script/core/diagnostics/assign-type-mismatch.lua +++ b/script/core/diagnostics/assign-type-mismatch.lua @@ -38,13 +38,14 @@ return function (uri, callback) end end local valueNode = vm.compileNode(value) - if source.type == 'setindex' - or source.type == 'setfield' - or source.type == 'tablefield' - or source.type == 'tableindex' then - if valueNode:hasName 'nil' then - valueNode = valueNode:copy():removeOptional() - if valueNode:isEmpty() then + if source.type == 'setindex' + and vm.isSubType(uri, valueNode, 'nil') then + -- boolean[1] = nil + local tnode = vm.compileNode(source.node) + for n in tnode:eachObject() do + if n.type == 'doc.type.array' + or n.type == 'doc.type.table' + or n.type == 'table' then return end end -- cgit v1.2.3 From 8c56e6ba9d85f3e01fb3658901386ee89de847ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Thu, 23 Jun 2022 14:39:42 +0800 Subject: update --- script/core/diagnostics/param-type-mismatch.lua | 33 ++++++++++++++++--------- 1 file changed, 22 insertions(+), 11 deletions(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/param-type-mismatch.lua b/script/core/diagnostics/param-type-mismatch.lua index a05b03e4..e4917456 100644 --- a/script/core/diagnostics/param-type-mismatch.lua +++ b/script/core/diagnostics/param-type-mismatch.lua @@ -42,20 +42,31 @@ return function (uri, callback) await.delay() local funcNode = vm.compileNode(source.node) for i, arg in ipairs(source.args) do + if i == 1 and source.node.type == 'getmethod' then + goto CONTINUE + end local refNode = vm.compileNode(arg) local defNode = getDefNode(funcNode, i) - if defNode then - if not vm.canCastType(uri, defNode, refNode) then - callback { - start = arg.start, - finish = arg.finish, - message = lang.script('DIAG_PARAM_TYPE_MISMATCH', { - def = vm.getInfer(defNode):view(uri), - ref = vm.getInfer(refNode):view(uri), - }) - } - end + if not defNode then + goto CONTINUE + end + if arg.type == 'getfield' + or arg.type == 'getindex' then + -- 由于无法对字段进行类型收窄, + -- 因此将假值移除再进行检查 + refNode = refNode:copy():setTruthy() + end + if not vm.canCastType(uri, defNode, refNode) then + callback { + start = arg.start, + finish = arg.finish, + message = lang.script('DIAG_PARAM_TYPE_MISMATCH', { + def = vm.getInfer(defNode):view(uri), + ref = vm.getInfer(refNode):view(uri), + }) + } end + ::CONTINUE:: end end) end -- cgit v1.2.3 From 61d9a99cb32487defbf72486d079e9c7324e4e51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Thu, 23 Jun 2022 17:38:38 +0800 Subject: update --- script/core/diagnostics/assign-type-mismatch.lua | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/assign-type-mismatch.lua b/script/core/diagnostics/assign-type-mismatch.lua index ae4b3512..3953cbc4 100644 --- a/script/core/diagnostics/assign-type-mismatch.lua +++ b/script/core/diagnostics/assign-type-mismatch.lua @@ -38,17 +38,9 @@ return function (uri, callback) end end local valueNode = vm.compileNode(value) - if source.type == 'setindex' - and vm.isSubType(uri, valueNode, 'nil') then + if source.type == 'setindex' then -- boolean[1] = nil - local tnode = vm.compileNode(source.node) - for n in tnode:eachObject() do - if n.type == 'doc.type.array' - or n.type == 'doc.type.table' - or n.type == 'table' then - return - end - end + valueNode = valueNode:copy():removeOptional() end local varNode = vm.compileNode(source) if vm.canCastType(uri, varNode, valueNode) then -- cgit v1.2.3 From 3c08270c7705d6389012eb838e400a5ec623d8ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Thu, 23 Jun 2022 20:09:45 +0800 Subject: build doc --- script/core/diagnostics/assign-type-mismatch.lua | 3 --- script/core/diagnostics/cast-local-type.lua | 3 --- script/core/diagnostics/param-type-mismatch.lua | 3 --- 3 files changed, 9 deletions(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/assign-type-mismatch.lua b/script/core/diagnostics/assign-type-mismatch.lua index 3953cbc4..8080b2ec 100644 --- a/script/core/diagnostics/assign-type-mismatch.lua +++ b/script/core/diagnostics/assign-type-mismatch.lua @@ -16,9 +16,6 @@ local checkTypes = { ---@async return function (uri, callback) - if not PREVIEW and not TEST then - return - end local state = files.getState(uri) if not state then return diff --git a/script/core/diagnostics/cast-local-type.lua b/script/core/diagnostics/cast-local-type.lua index cc4d4a0b..b7abcef6 100644 --- a/script/core/diagnostics/cast-local-type.lua +++ b/script/core/diagnostics/cast-local-type.lua @@ -6,9 +6,6 @@ local await = require 'await' ---@async return function (uri, callback) - if not PREVIEW and not TEST then - return - end local state = files.getState(uri) if not state then return diff --git a/script/core/diagnostics/param-type-mismatch.lua b/script/core/diagnostics/param-type-mismatch.lua index e4917456..a4390b50 100644 --- a/script/core/diagnostics/param-type-mismatch.lua +++ b/script/core/diagnostics/param-type-mismatch.lua @@ -6,9 +6,6 @@ local await = require 'await' ---@async return function (uri, callback) - if not PREVIEW and not TEST then - return - end local state = files.getState(uri) if not state then return -- cgit v1.2.3 From cf4e49ba82484254cfe2a74b12e340575324c80c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Fri, 24 Jun 2022 15:54:38 +0800 Subject: diagnostic group --- script/core/diagnostics/init.lua | 74 +++++++++++++++++++++++++++++++++++----- 1 file changed, 65 insertions(+), 9 deletions(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/init.lua b/script/core/diagnostics/init.lua index c5cb90ca..2030ccea 100644 --- a/script/core/diagnostics/init.lua +++ b/script/core/diagnostics/init.lua @@ -4,6 +4,7 @@ local config = require 'config' local await = require 'await' local vm = require "vm.vm" local util = require 'utility' +local diagd = require 'proto.diagnostic' -- 把耗时最长的诊断放到最后面 local diagSort = { @@ -50,6 +51,64 @@ local function checkSleep(uri, passed) sleepRest = sleepRest - sleeped end +---@param uri uri +---@param name string +---@return string +local function getSeverity(uri, name) + local severity = config.get(uri, 'Lua.diagnostics.severity')[name] + or define.DiagnosticDefaultSeverity[name] + if severity:sub(-1) == '!' then + return severity:sub(1, -2) + end + local groupSeverity = config.get(uri, 'Lua.diagnostics.groupSeverity') + local groups = diagd.getGroups(name) + local groupLevel = 999 + for _, groupName in ipairs(groups) do + local gseverity = groupSeverity[groupName] + if gseverity ~= 'Fallback' then + groupLevel = math.min(groupLevel, define.DiagnosticSeverity[gseverity]) + end + end + if groupLevel == 999 then + return severity + end + for severityName, level in pairs(define.DiagnosticSeverity) do + if level == groupLevel then + return severityName + end + end + return severity +end + +---@param uri uri +---@param name string +---@return string +local function getStatus(uri, name) + local status = config.get(uri, 'Lua.diagnostics.neededFileStatus')[name] + or define.DiagnosticDefaultNeededFileStatus[name] + if status:sub(-1) == '!' then + return status:sub(1, -2) + end + local groupStatus = config.get(uri, 'Lua.diagnostics.groupFileStatus') + local groups = diagd.getGroups(name) + local groupLevel = 0 + for _, groupName in ipairs(groups) do + local gstatus = groupStatus[groupName] + if gstatus ~= 'Fallback' then + groupLevel = math.max(groupLevel, define.DiagnosticFileStatus[gstatus]) + end + end + if groupLevel == 0 then + return status + end + for statusName, level in pairs(define.DiagnosticFileStatus) do + if level == groupLevel then + return statusName + end + end + return status +end + ---@async ---@param uri uri ---@param name string @@ -60,21 +119,18 @@ local function check(uri, name, isScopeDiag, response) if util.arrayHas(disables, name) then return end - local level = config.get(uri, 'Lua.diagnostics.severity')[name] - or define.DiagnosticDefaultSeverity[name] - - local neededFileStatus = config.get(uri, 'Lua.diagnostics.neededFileStatus')[name] - or define.DiagnosticDefaultNeededFileStatus[name] + local severity = getSeverity(uri, name) + local status = getStatus(uri, name) - if neededFileStatus == 'None' then + if status == 'None' then return end - if neededFileStatus == 'Opened' and not files.isOpen(uri) then + if status == 'Opened' and not files.isOpen(uri) then return end - local severity = define.DiagnosticSeverity[level] + local level = define.DiagnosticSeverity[severity] local clock = os.clock() local mark = {} ---@async @@ -90,7 +146,7 @@ local function check(uri, name, isScopeDiag, response) end mark[result.start] = true - result.level = severity or result.level + result.level = level or result.level result.code = name response(result) end, name) -- cgit v1.2.3 From 3534367f05b37b0122441bf3b3ddaaaf707c9514 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Fri, 24 Jun 2022 16:02:34 +0800 Subject: fix --- script/core/diagnostics/init.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/init.lua b/script/core/diagnostics/init.lua index 2030ccea..fd67ac2e 100644 --- a/script/core/diagnostics/init.lua +++ b/script/core/diagnostics/init.lua @@ -65,7 +65,7 @@ local function getSeverity(uri, name) local groupLevel = 999 for _, groupName in ipairs(groups) do local gseverity = groupSeverity[groupName] - if gseverity ~= 'Fallback' then + if gseverity and gseverity ~= 'Fallback' then groupLevel = math.min(groupLevel, define.DiagnosticSeverity[gseverity]) end end @@ -94,7 +94,7 @@ local function getStatus(uri, name) local groupLevel = 0 for _, groupName in ipairs(groups) do local gstatus = groupStatus[groupName] - if gstatus ~= 'Fallback' then + if gstatus and gstatus ~= 'Fallback' then groupLevel = math.max(groupLevel, define.DiagnosticFileStatus[gstatus]) end end -- cgit v1.2.3 From 92b2a185af2d31dd353ef8a7d57469ee9d584cb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Mon, 27 Jun 2022 00:42:31 +0800 Subject: resolve #1236 custom class can extends basic class --- script/core/diagnostics/assign-type-mismatch.lua | 1 + 1 file changed, 1 insertion(+) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/assign-type-mismatch.lua b/script/core/diagnostics/assign-type-mismatch.lua index 8080b2ec..e84e41df 100644 --- a/script/core/diagnostics/assign-type-mismatch.lua +++ b/script/core/diagnostics/assign-type-mismatch.lua @@ -5,6 +5,7 @@ local vm = require 'vm' local await = require 'await' local checkTypes = { + 'local', 'setlocal', 'setglobal', 'setfield', -- cgit v1.2.3 From 24f2bab098c176acdd58a5dc82ec037b2ded8eeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Mon, 27 Jun 2022 01:16:01 +0800 Subject: disable `unused-local` for empty function --- script/core/diagnostics/unused-local.lua | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/unused-local.lua b/script/core/diagnostics/unused-local.lua index d12ceb2b..a637f427 100644 --- a/script/core/diagnostics/unused-local.lua +++ b/script/core/diagnostics/unused-local.lua @@ -63,18 +63,16 @@ local function isDocClass(source) return false end -local function isDocParam(source) - if not source.bindDocs then +---@param source parser.object +local function isDeclareFunctionParam(source) + if source.parent.type ~= 'funcargs' then return false end - for _, doc in ipairs(source.bindDocs) do - if doc.type == 'doc.param' then - if doc.param[1] == source[1] then - return true - end - end + local func = source.parent.parent + if #func > 0 then + return false end - return false + return true end return function (uri, callback) @@ -94,7 +92,7 @@ return function (uri, callback) if isDocClass(source) then return end - if vm.isMetaFile(uri) and isDocParam(source) then + if isDeclareFunctionParam(source) then return end local data = hasGet(source) -- cgit v1.2.3 From 0db0e59ea3649d5be702b977073b5418c1d3b8ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Mon, 27 Jun 2022 14:32:50 +0800 Subject: improve description of `duplicate alias` --- script/core/diagnostics/duplicate-doc-alias.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/duplicate-doc-alias.lua b/script/core/diagnostics/duplicate-doc-alias.lua index 821bb1d8..5fd12847 100644 --- a/script/core/diagnostics/duplicate-doc-alias.lua +++ b/script/core/diagnostics/duplicate-doc-alias.lua @@ -39,7 +39,7 @@ return function (uri, callback) start = doc.alias.start, finish = doc.alias.finish, related = cache, - message = lang.script('DIAG_DUPLICATE_DOC_CLASS', name) + message = lang.script('DIAG_DUPLICATE_DOC_ALIAS', name) } end end -- cgit v1.2.3 From e3fe32a65e7cd0d3a93c42ec24a37ac933881d2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Mon, 27 Jun 2022 16:36:00 +0800 Subject: don't check `trailing-space` for comments --- script/core/diagnostics/trailing-space.lua | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/trailing-space.lua b/script/core/diagnostics/trailing-space.lua index d23b0692..b9e4d32d 100644 --- a/script/core/diagnostics/trailing-space.lua +++ b/script/core/diagnostics/trailing-space.lua @@ -3,13 +3,20 @@ local lang = require 'language' local guide = require 'parser.guide' local function isInString(ast, offset) - local result = false - guide.eachSourceType(ast, 'string', function (source) + return guide.eachSourceType(ast, 'string', function (source) if offset >= source.start and offset <= source.finish then - result = true + return true end end) - return result +end + +local function isInComment(ast, offset) + for _, com in ipairs(ast.state.comms) do + if offset >= com.start and offset <= com.finish then + return true + end + end + return false end return function (uri, callback) @@ -28,7 +35,8 @@ return function (uri, callback) goto NEXT_LINE end local lastPos = guide.offsetToPosition(state, lastOffset) - if isInString(state.ast, lastPos) then + if isInString(state.ast, lastPos) + or isInComment(state.ast, lastPos) then goto NEXT_LINE end local firstOffset = startOffset -- cgit v1.2.3 From edc9797838506728ba65a50b94781ac99e3bdd86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Mon, 27 Jun 2022 19:07:46 +0800 Subject: diagnostic `missing-return-value` --- script/core/diagnostics/missing-return-value.lua | 62 ++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 script/core/diagnostics/missing-return-value.lua (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/missing-return-value.lua b/script/core/diagnostics/missing-return-value.lua new file mode 100644 index 00000000..491475c7 --- /dev/null +++ b/script/core/diagnostics/missing-return-value.lua @@ -0,0 +1,62 @@ +local files = require 'files' +local guide = require 'parser.guide' +local vm = require 'vm' +local lang = require 'language' + +local function hasDocReturn(func) + if not func.bindDocs then + return false + end + for _, doc in ipairs(func.bindDocs) do + if doc.type == 'doc.return' then + return true + end + end + return false +end + +return function (uri, callback) + local state = files.getState(uri) + if not state then + return + end + + guide.eachSourceType(state.ast, 'function', function (source) + if not hasDocReturn(source) then + return + end + local min = vm.countReturnsOfFunction(source) + if min == 0 then + return + end + local returns = source.returns + if not returns then + return + end + for _, ret in ipairs(returns) do + local rmin, rmax = vm.countList(ret) + if rmax < min then + if rmin == rmax then + callback { + start = ret.start, + finish = ret.start + #'return', + message = lang.script('DIAG_MISSING_RETURN_VALUE', { + min = min, + rmax = rmax, + }), + } + else + callback { + start = ret.start, + finish = ret.start + #'return', + message = lang.script('DIAG_MISSING_RETURN_VALUE', { + min = min, + rmin = rmin, + rmax = rmax, + }), + } + end + end + end + end) +end -- cgit v1.2.3 From f9b3b65445e86ae0e9c1026ea1fac44bf5d884da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Mon, 27 Jun 2022 20:46:58 +0800 Subject: #1240 use faster `isInString` --- script/core/diagnostics/trailing-space.lua | 21 ++------------------- 1 file changed, 2 insertions(+), 19 deletions(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/trailing-space.lua b/script/core/diagnostics/trailing-space.lua index b9e4d32d..aad0b53d 100644 --- a/script/core/diagnostics/trailing-space.lua +++ b/script/core/diagnostics/trailing-space.lua @@ -2,23 +2,6 @@ local files = require 'files' local lang = require 'language' local guide = require 'parser.guide' -local function isInString(ast, offset) - return guide.eachSourceType(ast, 'string', function (source) - if offset >= source.start and offset <= source.finish then - return true - end - end) -end - -local function isInComment(ast, offset) - for _, com in ipairs(ast.state.comms) do - if offset >= com.start and offset <= com.finish then - return true - end - end - return false -end - return function (uri, callback) local state = files.getState(uri) local text = files.getText(uri) @@ -35,8 +18,8 @@ return function (uri, callback) goto NEXT_LINE end local lastPos = guide.offsetToPosition(state, lastOffset) - if isInString(state.ast, lastPos) - or isInComment(state.ast, lastPos) then + if guide.isInString(state.ast, lastPos) + or guide.isInComment(state.ast, lastPos) then goto NEXT_LINE end local firstOffset = startOffset -- cgit v1.2.3 From ae024958da9a14ed837495f79972a85458ee3f0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Tue, 28 Jun 2022 03:08:24 +0800 Subject: fix diags --- script/core/diagnostics/missing-return-value.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/missing-return-value.lua b/script/core/diagnostics/missing-return-value.lua index 491475c7..c2e882e3 100644 --- a/script/core/diagnostics/missing-return-value.lua +++ b/script/core/diagnostics/missing-return-value.lua @@ -49,7 +49,7 @@ return function (uri, callback) callback { start = ret.start, finish = ret.start + #'return', - message = lang.script('DIAG_MISSING_RETURN_VALUE', { + message = lang.script('DIAG_MISSING_RETURN_VALUE_RANGE', { min = min, rmin = rmin, rmax = rmax, -- cgit v1.2.3 From abdeade219bde1714fd8f145652d42315e1e6d25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Tue, 28 Jun 2022 03:23:55 +0800 Subject: new diag ``redundant-return-value`` --- script/core/diagnostics/redundant-return-value.lua | 69 ++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 script/core/diagnostics/redundant-return-value.lua (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/redundant-return-value.lua b/script/core/diagnostics/redundant-return-value.lua new file mode 100644 index 00000000..8f8422fe --- /dev/null +++ b/script/core/diagnostics/redundant-return-value.lua @@ -0,0 +1,69 @@ +local files = require 'files' +local guide = require 'parser.guide' +local vm = require 'vm' +local lang = require 'language' + +local function hasDocReturn(func) + if not func.bindDocs then + return false + end + for _, doc in ipairs(func.bindDocs) do + if doc.type == 'doc.return' then + return true + end + end + return false +end + +return function (uri, callback) + local state = files.getState(uri) + if not state then + return + end + + guide.eachSourceType(state.ast, 'function', function (source) + if not hasDocReturn(source) then + return + end + local _, max = vm.countReturnsOfFunction(source) + local returns = source.returns + if not returns then + return + end + for _, ret in ipairs(returns) do + local rmin, rmax = vm.countList(ret) + if rmin > max then + for i = max + 1, #ret - 1 do + callback { + start = ret[i].start, + finish = ret[i].finish, + message = lang.script('DIAG_REDUNDANT_RETURN_VALUE', { + max = max, + rmax = i, + }), + } + end + if #ret == rmax then + callback { + start = ret[#ret].start, + finish = ret[#ret].finish, + message = lang.script('DIAG_REDUNDANT_RETURN_VALUE', { + max = max, + rmax = rmax, + }), + } + else + callback { + start = ret[#ret].start, + finish = ret[#ret].finish, + message = lang.script('DIAG_REDUNDANT_RETURN_VALUE_RANGE', { + max = max, + rmin = #ret, + rmax = rmax, + }), + } + end + end + end + end) +end -- cgit v1.2.3 From df4fb9b5157cbf11f3fc5f6041afef657f0acfba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Tue, 28 Jun 2022 16:28:33 +0800 Subject: diag `missing-return` --- script/core/diagnostics/missing-return.lua | 84 ++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 script/core/diagnostics/missing-return.lua (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/missing-return.lua b/script/core/diagnostics/missing-return.lua new file mode 100644 index 00000000..08932c83 --- /dev/null +++ b/script/core/diagnostics/missing-return.lua @@ -0,0 +1,84 @@ +local files = require 'files' +local guide = require 'parser.guide' +local vm = require 'vm' +local lang = require 'language' + +---@param uri uri +---@param func parser.object +local function hasDocReturn(uri, func) + if not func.bindDocs then + return false + end + for _, doc in ipairs(func.bindDocs) do + if doc.type == 'doc.return' then + -- don't need return with only one `any` + local lastReturn = doc.returns[#doc.returns] + if lastReturn.returnIndex ~= 1 + or vm.getInfer(lastReturn):view(uri) ~= 'any' then + return true + end + end + end + return false +end + +---@param block parser.object +---@return boolean +local function hasReturn(block) + if block.hasReturn or block.hasError then + return true + end + if block.type == 'if' then + local hasElse + for _, subBlock in ipairs(block) do + if not hasReturn(subBlock) then + return false + end + if subBlock.type == 'elseblock' then + hasElse = true + end + end + return hasElse == true + else + if block.type == 'while' then + if vm.testCondition(block.filter) then + return true + end + end + for _, action in ipairs(block) do + if guide.isBlockType(action) then + if hasReturn(action) then + return true + end + end + end + end + return false +end + +return function (uri, callback) + local state = files.getState(uri) + if not state then + return + end + + guide.eachSourceType(state.ast, 'function', function (source) + -- check declare only + if #source == 0 then + return + end + if not hasDocReturn(uri, source) then + return + end + if hasReturn(source) then + return + end + local lastAction = source[#source] + local finish = lastAction.range or lastAction.finish + callback { + start = finish, + finish = finish, + message = lang.script('DIAG_MISSING_RETURN'), + } + end) +end -- cgit v1.2.3 From 7e778b31476831864dff995ae3ae4e5f223c5726 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Tue, 28 Jun 2022 19:09:39 +0800 Subject: new diag `return-type-mismatch` --- script/core/diagnostics/return-type-mismatch.lua | 70 ++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 script/core/diagnostics/return-type-mismatch.lua (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/return-type-mismatch.lua b/script/core/diagnostics/return-type-mismatch.lua new file mode 100644 index 00000000..ba23fa2c --- /dev/null +++ b/script/core/diagnostics/return-type-mismatch.lua @@ -0,0 +1,70 @@ +local files = require 'files' +local lang = require 'language' +local guide = require 'parser.guide' +local vm = require 'vm' +local await = require 'await' + +---@param func parser.object +---@return vm.node[]? +local function getDocReturns(func) + if not func.bindDocs then + return nil + end + local returns = {} + for _, doc in ipairs(func.bindDocs) do + if doc.type == 'doc.return' then + for _, ret in ipairs(doc.returns) do + returns[ret.returnIndex] = vm.compileNode(ret) + end + end + end + if #returns == 0 then + return nil + end + return returns +end +---@async +return function (uri, callback) + local state = files.getState(uri) + if not state then + return + end + + ---@param docReturns vm.node[] + ---@param rets parser.object + local function checkReturn(docReturns, rets) + for i, docRet in ipairs(docReturns) do + local retNode, exp = vm.selectNode(rets, i) + if not exp then + break + end + if not vm.canCastType(uri, docRet, retNode) then + callback { + start = exp.start, + finish = exp.finish, + message = lang.script('DIAG_RETURN_TYPE_MISMATCH', { + def = vm.getInfer(docRet):view(uri), + ref = vm.getInfer(retNode):view(uri), + index = i, + }), + } + end + end + end + + ---@async + guide.eachSourceType(state.ast, 'function', function (source) + if not source.returns then + return + end + local docReturns = getDocReturns(source) + if not docReturns then + return + end + await.delay() + for _, ret in ipairs(source.returns) do + checkReturn(docReturns, ret) + await.delay() + end + end) +end -- cgit v1.2.3 From 5b950742705515c4bc2502e8a902221f44bf644f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Tue, 28 Jun 2022 20:06:34 +0800 Subject: cleanup --- script/core/diagnostics/assign-type-mismatch.lua | 2 +- script/core/diagnostics/duplicate-set-field.lua | 8 +++++--- script/core/diagnostics/return-type-mismatch.lua | 6 ++++++ script/core/diagnostics/unused-function.lua | 5 +++-- 4 files changed, 15 insertions(+), 6 deletions(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/assign-type-mismatch.lua b/script/core/diagnostics/assign-type-mismatch.lua index e84e41df..536cfaff 100644 --- a/script/core/diagnostics/assign-type-mismatch.lua +++ b/script/core/diagnostics/assign-type-mismatch.lua @@ -36,7 +36,7 @@ return function (uri, callback) end end local valueNode = vm.compileNode(value) - if source.type == 'setindex' then + if source.type == 'setindex' then -- boolean[1] = nil valueNode = valueNode:copy():removeOptional() end diff --git a/script/core/diagnostics/duplicate-set-field.lua b/script/core/diagnostics/duplicate-set-field.lua index 8052c420..f5007eb1 100644 --- a/script/core/diagnostics/duplicate-set-field.lua +++ b/script/core/diagnostics/duplicate-set-field.lua @@ -48,10 +48,12 @@ return function (uri, callback) local blocks = {} for _, value in ipairs(values) do local block = guide.getBlock(value) - if not blocks[block] then - blocks[block] = {} + if block then + if not blocks[block] then + blocks[block] = {} + end + blocks[block][#blocks[block]+1] = value end - blocks[block][#blocks[block]+1] = value end for _, defs in pairs(blocks) do if #defs <= 1 then diff --git a/script/core/diagnostics/return-type-mismatch.lua b/script/core/diagnostics/return-type-mismatch.lua index ba23fa2c..c170679f 100644 --- a/script/core/diagnostics/return-type-mismatch.lua +++ b/script/core/diagnostics/return-type-mismatch.lua @@ -38,6 +38,12 @@ return function (uri, callback) if not exp then break end + if retNode:hasName 'nil' then + if exp.type == 'getfield' + or exp.type == 'getindex' then + retNode = retNode:copy():removeOptional() + end + end if not vm.canCastType(uri, docRet, retNode) then callback { start = exp.start, diff --git a/script/core/diagnostics/unused-function.lua b/script/core/diagnostics/unused-function.lua index 813ac804..a873375f 100644 --- a/script/core/diagnostics/unused-function.lua +++ b/script/core/diagnostics/unused-function.lua @@ -18,7 +18,8 @@ local function isToBeClosed(source) return false end ----@param source parser.object +---@param source parser.object? +---@return boolean local function isValidFunction(source) if not source then return false @@ -55,7 +56,7 @@ local function collect(ast, white, roots, links) for _, ref in ipairs(loc.ref or {}) do if ref.type == 'getlocal' then local func = guide.getParentFunction(ref) - if not isValidFunction(func) or roots[func] then + if not func or not isValidFunction(func) or roots[func] then roots[src] = true return end -- cgit v1.2.3 From 08638eedb6cf74bd815e933f4a90e989a1516948 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Tue, 28 Jun 2022 20:30:27 +0800 Subject: some fix --- script/core/diagnostics/param-type-mismatch.lua | 3 +++ script/core/diagnostics/unused-vararg.lua | 3 +++ 2 files changed, 6 insertions(+) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/param-type-mismatch.lua b/script/core/diagnostics/param-type-mismatch.lua index a4390b50..6f34f579 100644 --- a/script/core/diagnostics/param-type-mismatch.lua +++ b/script/core/diagnostics/param-type-mismatch.lua @@ -22,6 +22,9 @@ return function (uri, callback) local param = f.args and f.args[i] if param then defNode:merge(vm.compileNode(param)) + if param[1] == '...' then + defNode:addOptional() + end end end end diff --git a/script/core/diagnostics/unused-vararg.lua b/script/core/diagnostics/unused-vararg.lua index ce033cf3..08f12c4d 100644 --- a/script/core/diagnostics/unused-vararg.lua +++ b/script/core/diagnostics/unused-vararg.lua @@ -15,6 +15,9 @@ return function (uri, callback) end guide.eachSourceType(ast.ast, 'function', function (source) + if #source == 0 then + return + end local args = source.args if not args then return -- cgit v1.2.3 From 08b9dd387a9b912c68749018b7dfe3c1df7094d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Tue, 28 Jun 2022 21:06:53 +0800 Subject: cleanup --- script/core/diagnostics/cast-type-mismatch.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/cast-type-mismatch.lua b/script/core/diagnostics/cast-type-mismatch.lua index 0561a8da..a48e6cca 100644 --- a/script/core/diagnostics/cast-type-mismatch.lua +++ b/script/core/diagnostics/cast-type-mismatch.lua @@ -30,7 +30,7 @@ return function (uri, callback) callback { start = cast.extends.start, finish = cast.extends.finish, - message = lang.script('DIAG_UNKNOWN_CAST_VARIABLE', { + message = lang.script('DIAG_CAST_TYPE_MISMATCH', { def = vm.getInfer(defNode):view(uri), ref = vm.getInfer(refNode):view(uri), }) -- cgit v1.2.3 From c8a7122ca9a1391ac09d05cc98762f1c73eb2712 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Wed, 29 Jun 2022 14:26:14 +0800 Subject: fix #1249 --- script/core/diagnostics/codestyle-check.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/codestyle-check.lua b/script/core/diagnostics/codestyle-check.lua index 34d55ee2..25603b4b 100644 --- a/script/core/diagnostics/codestyle-check.lua +++ b/script/core/diagnostics/codestyle-check.lua @@ -7,7 +7,7 @@ local pformatting = require 'provider.formatting' ---@async return function(uri, callback) - local text = files.getText(uri) + local text = files.getOriginText(uri) if not text then return end -- cgit v1.2.3 From e5f07d0c9830afdfa1ce3b354a123212e5837526 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Wed, 29 Jun 2022 17:43:45 +0800 Subject: don't check `t.field = nil` --- script/core/diagnostics/assign-type-mismatch.lua | 28 ++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/assign-type-mismatch.lua b/script/core/diagnostics/assign-type-mismatch.lua index 536cfaff..a3d63ca8 100644 --- a/script/core/diagnostics/assign-type-mismatch.lua +++ b/script/core/diagnostics/assign-type-mismatch.lua @@ -15,6 +15,21 @@ local checkTypes = { 'tableindex' } +---@param source parser.object +---@return boolean +local function hasMarkType(source) + if not source.bindDocs then + return false + end + for _, doc in ipairs(source.bindDocs) do + if doc.type == 'doc.type' + or doc.type == 'doc.class' then + return true + end + end + return false +end + ---@async return function (uri, callback) local state = files.getState(uri) @@ -35,11 +50,24 @@ return function (uri, callback) return end end + --[[ + ---@class A + local mt + ---@type X + mt._x = nil -- don't warn this + ]] + if value.type == 'nil' then + if hasMarkType(source) then + return + end + end + local valueNode = vm.compileNode(value) if source.type == 'setindex' then -- boolean[1] = nil valueNode = valueNode:copy():removeOptional() end + local varNode = vm.compileNode(source) if vm.canCastType(uri, varNode, valueNode) then return -- cgit v1.2.3 From 457990e081a84b0aec2448ad23a00f5b95f1e457 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Thu, 30 Jun 2022 01:33:39 +0800 Subject: add many delays in diagnostics --- script/core/diagnostics/circle-doc-class.lua | 3 +++ script/core/diagnostics/code-after-break.lua | 4 ++++ script/core/diagnostics/duplicate-doc-alias.lua | 3 +++ script/core/diagnostics/duplicate-doc-field.lua | 3 +++ script/core/diagnostics/duplicate-index.lua | 5 +++- script/core/diagnostics/duplicate-set-field.lua | 4 ++++ script/core/diagnostics/empty-block.lua | 7 +++++- script/core/diagnostics/missing-parameter.lua | 4 ++++ script/core/diagnostics/missing-return-value.lua | 4 ++++ script/core/diagnostics/missing-return.lua | 4 ++++ script/core/diagnostics/need-check-nil.lua | 4 ++++ script/core/diagnostics/newfield-call.lua | 5 +++- script/core/diagnostics/newline-call.lua | 6 +++++ script/core/diagnostics/no-unknown.lua | 27 +++++++++++++--------- script/core/diagnostics/redefined-local.lua | 5 ++++ script/core/diagnostics/redundant-parameter.lua | 4 ++++ script/core/diagnostics/redundant-return-value.lua | 4 ++++ script/core/diagnostics/return-type-mismatch.lua | 2 +- script/core/diagnostics/trailing-space.lua | 3 +++ script/core/diagnostics/unbalanced-assignments.lua | 22 +++++++++++------- script/core/diagnostics/undefined-global.lua | 1 + 21 files changed, 101 insertions(+), 23 deletions(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/circle-doc-class.lua b/script/core/diagnostics/circle-doc-class.lua index 40d4afeb..fcd2021d 100644 --- a/script/core/diagnostics/circle-doc-class.lua +++ b/script/core/diagnostics/circle-doc-class.lua @@ -2,7 +2,9 @@ local files = require 'files' local lang = require 'language' local vm = require 'vm' local guide = require 'parser.guide' +local await = require 'await' +---@async return function (uri, callback) local state = files.getState(uri) if not state then @@ -18,6 +20,7 @@ return function (uri, callback) if not doc.extends then goto CONTINUE end + await.delay() local myName = guide.getKeyName(doc) local list = { doc } local mark = {} diff --git a/script/core/diagnostics/code-after-break.lua b/script/core/diagnostics/code-after-break.lua index 21f7e83a..963fd9ed 100644 --- a/script/core/diagnostics/code-after-break.lua +++ b/script/core/diagnostics/code-after-break.lua @@ -2,7 +2,9 @@ local files = require 'files' local guide = require 'parser.guide' local lang = require 'language' local define = require 'proto.define' +local await = require 'await' +---@async return function (uri, callback) local state = files.getState(uri) if not state then @@ -10,12 +12,14 @@ return function (uri, callback) end local mark = {} + ---@async guide.eachSourceType(state.ast, 'break', function (source) local list = source.parent if mark[list] then return end mark[list] = true + await.delay() for i = #list, 1, -1 do local src = list[i] if src == source then diff --git a/script/core/diagnostics/duplicate-doc-alias.lua b/script/core/diagnostics/duplicate-doc-alias.lua index 5fd12847..2625f88f 100644 --- a/script/core/diagnostics/duplicate-doc-alias.lua +++ b/script/core/diagnostics/duplicate-doc-alias.lua @@ -2,7 +2,9 @@ local files = require 'files' local lang = require 'language' local vm = require 'vm' local guide = require 'parser.guide' +local await = require 'await' +---@async return function (uri, callback) local state = files.getState(uri) if not state then @@ -20,6 +22,7 @@ return function (uri, callback) if not name then return end + await.delay() if not cache[name] then local docs = vm.getDocSets(uri, name) cache[name] = {} diff --git a/script/core/diagnostics/duplicate-doc-field.lua b/script/core/diagnostics/duplicate-doc-field.lua index 78112beb..a30dfa88 100644 --- a/script/core/diagnostics/duplicate-doc-field.lua +++ b/script/core/diagnostics/duplicate-doc-field.lua @@ -1,6 +1,7 @@ local files = require 'files' local lang = require 'language' local vm = require 'vm.vm' +local await = require 'await' local function getFieldEventName(doc) if not doc.extends then @@ -29,6 +30,7 @@ local function getFieldEventName(doc) return nil end +---@async return function (uri, callback) local state = files.getState(uri) if not state then @@ -46,6 +48,7 @@ return function (uri, callback) mark = {} elseif doc.type == 'doc.field' then if mark then + await.delay() local name if doc.field.type == 'doc.type' then name = ('[%s]'):format(vm.getInfer(doc.field):view(uri)) diff --git a/script/core/diagnostics/duplicate-index.lua b/script/core/diagnostics/duplicate-index.lua index 5097ab3a..dfd9bd4b 100644 --- a/script/core/diagnostics/duplicate-index.lua +++ b/script/core/diagnostics/duplicate-index.lua @@ -2,14 +2,17 @@ local files = require 'files' local guide = require 'parser.guide' local lang = require 'language' local define = require 'proto.define' +local await = require 'await' +---@async return function (uri, callback) local ast = files.getState(uri) if not ast then return end - + ---@async guide.eachSourceType(ast.ast, 'table', function (source) + await.delay() local mark = {} for _, obj in ipairs(source) do if obj.type == 'tablefield' diff --git a/script/core/diagnostics/duplicate-set-field.lua b/script/core/diagnostics/duplicate-set-field.lua index f5007eb1..ce67ab46 100644 --- a/script/core/diagnostics/duplicate-set-field.lua +++ b/script/core/diagnostics/duplicate-set-field.lua @@ -3,17 +3,21 @@ local lang = require 'language' local define = require 'proto.define' local guide = require 'parser.guide' local vm = require 'vm' +local await = require 'await' +---@async return function (uri, callback) local ast = files.getState(uri) if not ast then return end + ---@async guide.eachSourceType(ast.ast, 'local', function (source) if not source.ref then return end + await.delay() local sets = {} for _, ref in ipairs(source.ref) do if ref.type ~= 'getlocal' then diff --git a/script/core/diagnostics/empty-block.lua b/script/core/diagnostics/empty-block.lua index fc205d7e..e05b6aef 100644 --- a/script/core/diagnostics/empty-block.lua +++ b/script/core/diagnostics/empty-block.lua @@ -2,15 +2,18 @@ local files = require 'files' local guide = require 'parser.guide' local lang = require 'language' local define = require 'proto.define' +local await = require 'await' --- 检查空代码块 +-- 检查空代码块 -- 但是排除忙等待(repeat/while) +---@async return function (uri, callback) local ast = files.getState(uri) if not ast then return end + await.delay() guide.eachSourceType(ast.ast, 'if', function (source) for _, block in ipairs(source) do if #block > 0 then @@ -24,6 +27,7 @@ return function (uri, callback) message = lang.script.DIAG_EMPTY_BLOCK, } end) + await.delay() guide.eachSourceType(ast.ast, 'loop', function (source) if #source > 0 then return @@ -35,6 +39,7 @@ return function (uri, callback) message = lang.script.DIAG_EMPTY_BLOCK, } end) + await.delay() guide.eachSourceType(ast.ast, 'in', function (source) if #source > 0 then return diff --git a/script/core/diagnostics/missing-parameter.lua b/script/core/diagnostics/missing-parameter.lua index b6067175..78b94a09 100644 --- a/script/core/diagnostics/missing-parameter.lua +++ b/script/core/diagnostics/missing-parameter.lua @@ -2,14 +2,18 @@ local files = require 'files' local guide = require 'parser.guide' local vm = require 'vm' local lang = require 'language' +local await = require 'await' +---@async return function (uri, callback) local state = files.getState(uri) if not state then return end + ---@async guide.eachSourceType(state.ast, 'call', function (source) + await.delay() local _, callArgs = vm.countList(source.args) local funcNode = vm.compileNode(source.node) diff --git a/script/core/diagnostics/missing-return-value.lua b/script/core/diagnostics/missing-return-value.lua index c2e882e3..2156d66c 100644 --- a/script/core/diagnostics/missing-return-value.lua +++ b/script/core/diagnostics/missing-return-value.lua @@ -2,6 +2,7 @@ local files = require 'files' local guide = require 'parser.guide' local vm = require 'vm' local lang = require 'language' +local await = require 'await' local function hasDocReturn(func) if not func.bindDocs then @@ -15,13 +16,16 @@ local function hasDocReturn(func) return false end +---@async return function (uri, callback) local state = files.getState(uri) if not state then return end + ---@async guide.eachSourceType(state.ast, 'function', function (source) + await.delay() if not hasDocReturn(source) then return end diff --git a/script/core/diagnostics/missing-return.lua b/script/core/diagnostics/missing-return.lua index 08932c83..5806f121 100644 --- a/script/core/diagnostics/missing-return.lua +++ b/script/core/diagnostics/missing-return.lua @@ -2,6 +2,7 @@ local files = require 'files' local guide = require 'parser.guide' local vm = require 'vm' local lang = require 'language' +local await = require 'await' ---@param uri uri ---@param func parser.object @@ -56,17 +57,20 @@ local function hasReturn(block) return false end +---@async return function (uri, callback) local state = files.getState(uri) if not state then return end + ---@async guide.eachSourceType(state.ast, 'function', function (source) -- check declare only if #source == 0 then return end + await.delay() if not hasDocReturn(uri, source) then return end diff --git a/script/core/diagnostics/need-check-nil.lua b/script/core/diagnostics/need-check-nil.lua index 867d0135..9c86939a 100644 --- a/script/core/diagnostics/need-check-nil.lua +++ b/script/core/diagnostics/need-check-nil.lua @@ -2,14 +2,18 @@ local files = require 'files' local guide = require 'parser.guide' local vm = require 'vm' local lang = require 'language' +local await = require 'await' +---@async return function (uri, callback) local state = files.getState(uri) if not state then return end + ---@async guide.eachSourceType(state.ast, 'getlocal', function (src) + await.delay() local checkNil local nxt = src.next if nxt then diff --git a/script/core/diagnostics/newfield-call.lua b/script/core/diagnostics/newfield-call.lua index b81d8c0a..e22dcffe 100644 --- a/script/core/diagnostics/newfield-call.lua +++ b/script/core/diagnostics/newfield-call.lua @@ -1,7 +1,9 @@ local files = require 'files' local guide = require 'parser.guide' local lang = require 'language' +local await = require 'await' +---@async return function (uri, callback) local state = files.getState(uri) local text = files.getText(uri) @@ -9,8 +11,9 @@ return function (uri, callback) return end - + ---@async guide.eachSourceType(state.ast, 'table', function (source) + await.delay() for i = 1, #source do local field = source[i] if field.type ~= 'tableexp' then diff --git a/script/core/diagnostics/newline-call.lua b/script/core/diagnostics/newline-call.lua index 9c526980..6460d3a7 100644 --- a/script/core/diagnostics/newline-call.lua +++ b/script/core/diagnostics/newline-call.lua @@ -1,7 +1,9 @@ local files = require 'files' local guide = require 'parser.guide' local lang = require 'language' +local await = require 'await' +---@async return function (uri, callback) local state = files.getState(uri) local text = files.getText(uri) @@ -9,6 +11,7 @@ return function (uri, callback) return end + ---@async guide.eachSourceType(state.ast, 'call', function (source) local node = source.node local args = source.args @@ -20,6 +23,9 @@ return function (uri, callback) if not source.next then return end + + await.delay() + local startOffset = guide.positionToOffset(state, args.start) + 1 local finishOffset = guide.positionToOffset(state, args.finish) if text:sub(startOffset, startOffset) ~= '(' diff --git a/script/core/diagnostics/no-unknown.lua b/script/core/diagnostics/no-unknown.lua index ff9f7a83..e706931a 100644 --- a/script/core/diagnostics/no-unknown.lua +++ b/script/core/diagnostics/no-unknown.lua @@ -2,24 +2,29 @@ local files = require 'files' local guide = require 'parser.guide' local lang = require 'language' local vm = require 'vm' +local await = require 'await' +local types = { + 'local', + 'setlocal', + 'setglobal', + 'getglobal', + 'setfield', + 'setindex', + 'tablefield', + 'tableindex', +} + +---@async return function (uri, callback) local ast = files.getState(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 + ---@async + guide.eachSourceTypes(ast.ast, types, function (source) + await.delay() if vm.getInfer(source):view(uri) == 'unknown' then callback { start = source.start, diff --git a/script/core/diagnostics/redefined-local.lua b/script/core/diagnostics/redefined-local.lua index 2157ae71..1fb3ca6b 100644 --- a/script/core/diagnostics/redefined-local.lua +++ b/script/core/diagnostics/redefined-local.lua @@ -1,18 +1,23 @@ local files = require 'files' local guide = require 'parser.guide' local lang = require 'language' +local await = require 'await' +---@async return function (uri, callback) local ast = files.getState(uri) if not ast then return end + + ---@async guide.eachSourceType(ast.ast, 'local', function (source) local name = source[1] if name == '_' or name == ast.ENVMode then return end + await.delay() local exist = guide.getLocal(source, name, source.start-1) if exist then callback { diff --git a/script/core/diagnostics/redundant-parameter.lua b/script/core/diagnostics/redundant-parameter.lua index deda918a..9898d9bd 100644 --- a/script/core/diagnostics/redundant-parameter.lua +++ b/script/core/diagnostics/redundant-parameter.lua @@ -2,14 +2,18 @@ local files = require 'files' local guide = require 'parser.guide' local vm = require 'vm' local lang = require 'language' +local await = require 'await' +---@async return function (uri, callback) local state = files.getState(uri) if not state then return end + ---@async guide.eachSourceType(state.ast, 'call', function (source) + await.delay() local callArgs = vm.countList(source.args) if callArgs == 0 then return diff --git a/script/core/diagnostics/redundant-return-value.lua b/script/core/diagnostics/redundant-return-value.lua index 8f8422fe..36432f98 100644 --- a/script/core/diagnostics/redundant-return-value.lua +++ b/script/core/diagnostics/redundant-return-value.lua @@ -2,6 +2,7 @@ local files = require 'files' local guide = require 'parser.guide' local vm = require 'vm' local lang = require 'language' +local await = require 'await' local function hasDocReturn(func) if not func.bindDocs then @@ -15,13 +16,16 @@ local function hasDocReturn(func) return false end +---@async return function (uri, callback) local state = files.getState(uri) if not state then return end + ---@async guide.eachSourceType(state.ast, 'function', function (source) + await.delay() if not hasDocReturn(source) then return end diff --git a/script/core/diagnostics/return-type-mismatch.lua b/script/core/diagnostics/return-type-mismatch.lua index c170679f..cce4aad8 100644 --- a/script/core/diagnostics/return-type-mismatch.lua +++ b/script/core/diagnostics/return-type-mismatch.lua @@ -63,11 +63,11 @@ return function (uri, callback) if not source.returns then return end + await.delay() local docReturns = getDocReturns(source) if not docReturns then return end - await.delay() for _, ret in ipairs(source.returns) do checkReturn(docReturns, ret) await.delay() diff --git a/script/core/diagnostics/trailing-space.lua b/script/core/diagnostics/trailing-space.lua index aad0b53d..2e0398b2 100644 --- a/script/core/diagnostics/trailing-space.lua +++ b/script/core/diagnostics/trailing-space.lua @@ -1,7 +1,9 @@ local files = require 'files' local lang = require 'language' local guide = require 'parser.guide' +local await = require 'await' +---@async return function (uri, callback) local state = files.getState(uri) local text = files.getText(uri) @@ -10,6 +12,7 @@ return function (uri, callback) end local lines = state.lines for i = 0, #lines do + await.delay() local startOffset = lines[i] local finishOffset = text:find('[\r\n]', startOffset) or (#text + 1) local lastOffset = finishOffset - 1 diff --git a/script/core/diagnostics/unbalanced-assignments.lua b/script/core/diagnostics/unbalanced-assignments.lua index df71f0c9..c21ca993 100644 --- a/script/core/diagnostics/unbalanced-assignments.lua +++ b/script/core/diagnostics/unbalanced-assignments.lua @@ -2,7 +2,17 @@ local files = require 'files' local define = require 'proto.define' local lang = require 'language' local guide = require 'parser.guide' +local await = require 'await' +local types = { + 'local', + 'setlocal', + 'setglobal', + 'setfield', + 'setindex' , +} + +---@async return function (uri, callback, code) local ast = files.getState(uri) if not ast then @@ -31,13 +41,9 @@ return function (uri, callback, code) end end - guide.eachSource(ast.ast, function (source) - if source.type == 'local' - or source.type == 'setlocal' - or source.type == 'setglobal' - or source.type == 'setfield' - or source.type == 'setindex' then - checkSet(source) - end + ---@async + guide.eachSourceTypes(ast.ast, types, function (source) + await.delay() + checkSet(source) end) end diff --git a/script/core/diagnostics/undefined-global.lua b/script/core/diagnostics/undefined-global.lua index 7804aeee..bf161c73 100644 --- a/script/core/diagnostics/undefined-global.lua +++ b/script/core/diagnostics/undefined-global.lua @@ -41,6 +41,7 @@ return function (uri, callback) return end if cache[key] == nil then + await.delay() cache[key] = vm.hasGlobalSets(uri, 'variable', key) end if cache[key] then -- cgit v1.2.3 From fd5aaac3348907ac2618f84e013ab0902cfc20dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Fri, 1 Jul 2022 16:28:00 +0800 Subject: cleanup --- script/core/diagnostics/spell-check.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/spell-check.lua b/script/core/diagnostics/spell-check.lua index ebdb0245..7369a235 100644 --- a/script/core/diagnostics/spell-check.lua +++ b/script/core/diagnostics/spell-check.lua @@ -6,7 +6,7 @@ local spell = require 'provider.spell' ---@async return function(uri, callback) - local text = files.getText(uri) + local text = files.getOriginText(uri) if not text then return end -- cgit v1.2.3 From 342ff9af837cdbe0369e717585fcd36638d60002 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Fri, 1 Jul 2022 16:59:35 +0800 Subject: fix #1257 don't need to return if returns nil --- script/core/diagnostics/missing-return.lua | 46 ++++++++++++++---------------- script/core/diagnostics/unused-local.lua | 16 ++++++++--- 2 files changed, 34 insertions(+), 28 deletions(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/missing-return.lua b/script/core/diagnostics/missing-return.lua index 5806f121..435e8a96 100644 --- a/script/core/diagnostics/missing-return.lua +++ b/script/core/diagnostics/missing-return.lua @@ -4,25 +4,6 @@ local vm = require 'vm' local lang = require 'language' local await = require 'await' ----@param uri uri ----@param func parser.object -local function hasDocReturn(uri, func) - if not func.bindDocs then - return false - end - for _, doc in ipairs(func.bindDocs) do - if doc.type == 'doc.return' then - -- don't need return with only one `any` - local lastReturn = doc.returns[#doc.returns] - if lastReturn.returnIndex ~= 1 - or vm.getInfer(lastReturn):view(uri) ~= 'any' then - return true - end - end - end - return false -end - ---@param block parser.object ---@return boolean local function hasReturn(block) @@ -57,6 +38,17 @@ local function hasReturn(block) return false end +---@param func parser.object +---@return boolean +local function isEmptyFunction(func) + if #func > 0 then + return false + end + local startRow = guide.rowColOf(func.start) + local finishRow = guide.rowColOf(func.finish) + return finishRow - startRow <= 1 +end + ---@async return function (uri, callback) local state = files.getState(uri) @@ -67,21 +59,27 @@ return function (uri, callback) ---@async guide.eachSourceType(state.ast, 'function', function (source) -- check declare only - if #source == 0 then + if isEmptyFunction(source) then return end await.delay() - if not hasDocReturn(uri, source) then + if vm.countReturnsOfFunction(source) == 0 then return end if hasReturn(source) then return end local lastAction = source[#source] - local finish = lastAction.range or lastAction.finish + local pos + if lastAction then + pos = lastAction.range or lastAction.finish + else + local row = guide.rowColOf(source.finish) + pos = guide.positionOf(row - 1, 0) + end callback { - start = finish, - finish = finish, + start = pos, + finish = pos, message = lang.script('DIAG_MISSING_RETURN'), } end) diff --git a/script/core/diagnostics/unused-local.lua b/script/core/diagnostics/unused-local.lua index a637f427..8bff7dcb 100644 --- a/script/core/diagnostics/unused-local.lua +++ b/script/core/diagnostics/unused-local.lua @@ -63,16 +63,24 @@ local function isDocClass(source) return false end +---@param func parser.object +---@return boolean +local function isEmptyFunction(func) + if #func > 0 then + return false + end + local startRow = guide.rowColOf(func.start) + local finishRow = guide.rowColOf(func.finish) + return finishRow - startRow <= 1 +end + ---@param source parser.object local function isDeclareFunctionParam(source) if source.parent.type ~= 'funcargs' then return false end local func = source.parent.parent - if #func > 0 then - return false - end - return true + return isEmptyFunction(func) end return function (uri, callback) -- cgit v1.2.3 From 2dfed8a4aabd66c98777d37245ef5482b5ffdd48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Sun, 3 Jul 2022 15:34:36 +0800 Subject: fix --- script/core/diagnostics/missing-return.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/missing-return.lua b/script/core/diagnostics/missing-return.lua index 435e8a96..e3539ac0 100644 --- a/script/core/diagnostics/missing-return.lua +++ b/script/core/diagnostics/missing-return.lua @@ -63,7 +63,7 @@ return function (uri, callback) return end await.delay() - if vm.countReturnsOfFunction(source) == 0 then + if vm.countReturnsOfFunction(source, true) == 0 then return end if hasReturn(source) then -- cgit v1.2.3 From e711f30dbcf840e8adec49a78d734d1a0306934b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Tue, 5 Jul 2022 16:04:12 +0800 Subject: resolve #1280 --- script/core/diagnostics/assign-type-mismatch.lua | 26 ++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/assign-type-mismatch.lua b/script/core/diagnostics/assign-type-mismatch.lua index a3d63ca8..d98aa021 100644 --- a/script/core/diagnostics/assign-type-mismatch.lua +++ b/script/core/diagnostics/assign-type-mismatch.lua @@ -30,6 +30,20 @@ local function hasMarkType(source) return false end +---@param source parser.object +---@return boolean +local function hasMarkClass(source) + if not source.bindDocs then + return false + end + for _, doc in ipairs(source.bindDocs) do + if doc.type == 'doc.class' then + return true + end + end + return false +end + ---@async return function (uri, callback) local state = files.getState(uri) @@ -72,6 +86,18 @@ return function (uri, callback) if vm.canCastType(uri, varNode, valueNode) then return end + + if value.type == 'select' + and value.sindex == 1 + and value.vararg + and value.vararg.type == 'call' + and value.vararg.node.special == 'setmetatable' + and hasMarkClass(source) then + if vm.canCastType(uri, valueNode:copy():remove 'table', varNode) then + return + end + end + callback { start = source.start, finish = source.finish, -- cgit v1.2.3 From 2ab618716a33378449aa4938864db632a141719e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Tue, 5 Jul 2022 19:47:15 +0800 Subject: resolve #1254 can suppress syntax errors --- script/core/diagnostics/unknown-diag-code.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/unknown-diag-code.lua b/script/core/diagnostics/unknown-diag-code.lua index 9e492a29..07128a27 100644 --- a/script/core/diagnostics/unknown-diag-code.lua +++ b/script/core/diagnostics/unknown-diag-code.lua @@ -1,6 +1,6 @@ local files = require 'files' local lang = require 'language' -local define = require 'proto.define' +local diag = require 'proto.diagnostic' return function (uri, callback) local state = files.getState(uri) @@ -17,7 +17,7 @@ return function (uri, callback) if doc.names then for _, nameUnit in ipairs(doc.names) do local code = nameUnit[1] - if not define.DiagnosticDefaultSeverity[code] then + if not diag.getDiagAndErrNameMap()[code] then callback { start = nameUnit.start, finish = nameUnit.finish, -- cgit v1.2.3 From 997109ebfd4e9ec2bd2b22cc8c35fd1e7cdf0b91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Wed, 6 Jul 2022 15:17:23 +0800 Subject: improve diag --- script/core/diagnostics/init.lua | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/init.lua b/script/core/diagnostics/init.lua index fd67ac2e..c33de6ce 100644 --- a/script/core/diagnostics/init.lua +++ b/script/core/diagnostics/init.lua @@ -8,14 +8,17 @@ local diagd = require 'proto.diagnostic' -- 把耗时最长的诊断放到最后面 local diagSort = { - ['redundant-value'] = 100, - ['not-yieldable'] = 100, - ['deprecated'] = 100, - ['undefined-field'] = 110, - ['redundant-parameter'] = 110, - ['cast-local-type'] = 120, - ['assign-type-mismatch'] = 120, - ['param-type-mismatch'] = 120, + ['redundant-value'] = 100, + ['not-yieldable'] = 100, + ['deprecated'] = 100, + ['undefined-field'] = 110, + ['redundant-parameter'] = 110, + ['cast-local-type'] = 120, + ['assign-type-mismatch'] = 120, + ['param-type-mismatch'] = 120, + ['missing-return'] = 120, + ['missing-return-value'] = 120, + ['redundant-return-value'] = 120, } local diagList = {} -- cgit v1.2.3 From eca994d10e4f069a4cc9348d27a4f3723a19f551 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Wed, 6 Jul 2022 15:35:16 +0800 Subject: resolve #1285 ignore `nil` in `getfield` --- script/core/diagnostics/assign-type-mismatch.lua | 8 ++++++++ script/core/diagnostics/cast-local-type.lua | 10 +++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/assign-type-mismatch.lua b/script/core/diagnostics/assign-type-mismatch.lua index d98aa021..6809760c 100644 --- a/script/core/diagnostics/assign-type-mismatch.lua +++ b/script/core/diagnostics/assign-type-mismatch.lua @@ -82,11 +82,19 @@ return function (uri, callback) valueNode = valueNode:copy():removeOptional() end + if value.type == 'getfield' + or value.type == 'getindex' then + -- 由于无法对字段进行类型收窄, + -- 因此将假值移除再进行检查 + valueNode = valueNode:copy():setTruthy() + end + local varNode = vm.compileNode(source) if vm.canCastType(uri, varNode, valueNode) then return end + -- local Cat = setmetatable({}, {__index = Animal}) 允许逆变 if value.type == 'select' and value.sindex == 1 and value.vararg diff --git a/script/core/diagnostics/cast-local-type.lua b/script/core/diagnostics/cast-local-type.lua index b7abcef6..c3d6e1bb 100644 --- a/script/core/diagnostics/cast-local-type.lua +++ b/script/core/diagnostics/cast-local-type.lua @@ -22,9 +22,17 @@ return function (uri, callback) return end for _, ref in ipairs(loc.ref) do - if ref.type == 'setlocal' then + if ref.type == 'setlocal' and ref.value then await.delay() local refNode = vm.compileNode(ref) + local value = ref.value + + if value.type == 'getfield' + or value.type == 'getindex' then + -- 由于无法对字段进行类型收窄, + -- 因此将假值移除再进行检查 + refNode = refNode:copy():setTruthy() + end if not vm.canCastType(uri, locNode, refNode) then callback { -- cgit v1.2.3 From 25cae7df6bff76e6a44adf85dfef06742ebf7193 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Wed, 6 Jul 2022 16:01:05 +0800 Subject: dose not infer as `nil` by `t.field = nil` --- script/core/diagnostics/assign-type-mismatch.lua | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/assign-type-mismatch.lua b/script/core/diagnostics/assign-type-mismatch.lua index 6809760c..0cccc7ee 100644 --- a/script/core/diagnostics/assign-type-mismatch.lua +++ b/script/core/diagnostics/assign-type-mismatch.lua @@ -64,16 +64,20 @@ return function (uri, callback) return end end - --[[ - ---@class A - local mt - ---@type X - mt._x = nil -- don't warn this - ]] if value.type == 'nil' then + --[[ + ---@class A + local mt + ---@type X + mt._x = nil -- don't warn this + ]] if hasMarkType(source) then return end + if source.type == 'setfield' + or source.type == 'setindex' then + return + end end local valueNode = vm.compileNode(value) -- cgit v1.2.3 From 27dcfa8d1463936d0b2514cd82eebe1910ab3bb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Wed, 6 Jul 2022 23:21:28 +0800 Subject: completion and diags for operator --- script/core/diagnostics/unknown-operator.lua | 34 ++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 script/core/diagnostics/unknown-operator.lua (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/unknown-operator.lua b/script/core/diagnostics/unknown-operator.lua new file mode 100644 index 00000000..29a86eaa --- /dev/null +++ b/script/core/diagnostics/unknown-operator.lua @@ -0,0 +1,34 @@ +local files = require 'files' +local guide = require 'parser.guide' +local lang = require 'language' +local vm = require 'vm' +local await = require 'await' +local util = require 'utility' + +---@async +return function (uri, callback) + local state = files.getState(uri) + if not state then + return + end + + if not state.ast.docs then + return + end + + for _, doc in ipairs(state.ast.docs) do + if doc.type == 'doc.operator' then + local op = doc.op + if op then + if not util.arrayHas(vm.UNARY_OP, op[1]) + and not util.arrayHas(vm.BINARY_OP, op[1]) then + callback { + start = doc.op.start, + finish = doc.op.finish, + message = lang.script('DIAG_UNKNOWN_OPERATOR', op[1]) + } + end + end + end + end +end -- cgit v1.2.3 From 79cc877095745be377b9dd189ee5e38cf35a3c5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Wed, 6 Jul 2022 23:34:54 +0800 Subject: semantic and completion description --- script/core/diagnostics/unknown-operator.lua | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/unknown-operator.lua b/script/core/diagnostics/unknown-operator.lua index 29a86eaa..5e8177c3 100644 --- a/script/core/diagnostics/unknown-operator.lua +++ b/script/core/diagnostics/unknown-operator.lua @@ -20,12 +20,13 @@ return function (uri, callback) if doc.type == 'doc.operator' then local op = doc.op if op then - if not util.arrayHas(vm.UNARY_OP, op[1]) - and not util.arrayHas(vm.BINARY_OP, op[1]) then + local opName = op[1] + if not vm.BINARY_MAP[opName] + and not vm.UNARY_MAP[opName] then callback { start = doc.op.start, finish = doc.op.finish, - message = lang.script('DIAG_UNKNOWN_OPERATOR', op[1]) + message = lang.script('DIAG_UNKNOWN_OPERATOR', opName) } end end -- cgit v1.2.3 From a19a1f4f077830014dd2d2c378988e7770be2be1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Wed, 6 Jul 2022 23:49:28 +0800 Subject: support `call` --- script/core/diagnostics/unknown-operator.lua | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/unknown-operator.lua b/script/core/diagnostics/unknown-operator.lua index 5e8177c3..7404b5ef 100644 --- a/script/core/diagnostics/unknown-operator.lua +++ b/script/core/diagnostics/unknown-operator.lua @@ -21,8 +21,9 @@ return function (uri, callback) local op = doc.op if op then local opName = op[1] - if not vm.BINARY_MAP[opName] - and not vm.UNARY_MAP[opName] then + if not vm.OP_BINARY_MAP[opName] + and not vm.OP_UNARY_MAP[opName] + and not vm.OP_OTHER_MAP[opName] then callback { start = doc.op.start, finish = doc.op.finish, -- cgit v1.2.3 From dd127fd3161c49a1604e5911c3d2e8dd5d2ac67c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Thu, 7 Jul 2022 17:54:27 +0800 Subject: resolve #619 `diagnostics.unusedLocalExclude` --- script/core/diagnostics/unused-local.lua | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/unused-local.lua b/script/core/diagnostics/unused-local.lua index 8bff7dcb..8f2ee217 100644 --- a/script/core/diagnostics/unused-local.lua +++ b/script/core/diagnostics/unused-local.lua @@ -3,6 +3,8 @@ local guide = require 'parser.guide' local define = require 'proto.define' local lang = require 'language' local vm = require 'vm.vm' +local config = require 'config.config' +local glob = require 'glob' local function hasGet(loc) if not loc.ref then @@ -88,12 +90,17 @@ return function (uri, callback) if not ast then return end + local ignorePatterns = config.get(uri, 'Lua.diagnostics.unusedLocalExclude') + local ignore = glob.glob(ignorePatterns) guide.eachSourceType(ast.ast, 'local', function (source) local name = source[1] if name == '_' or name == ast.ENVMode then return end + if ignore(name) then + return + end if isToBeClosed(source) then return end -- cgit v1.2.3 From 7c29a6012bf5fe2cc1ff79598f8be6f80528e767 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Fri, 8 Jul 2022 17:26:18 +0800 Subject: cleanup --- script/core/diagnostics/undefined-doc-param.lua | 42 +++++-------------------- 1 file changed, 7 insertions(+), 35 deletions(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/undefined-doc-param.lua b/script/core/diagnostics/undefined-doc-param.lua index 98919284..7a60a74f 100644 --- a/script/core/diagnostics/undefined-doc-param.lua +++ b/script/core/diagnostics/undefined-doc-param.lua @@ -1,21 +1,6 @@ local files = require 'files' local lang = require 'language' -local function hasParamName(func, name) - if not func.args then - return false - end - for _, arg in ipairs(func.args) do - if arg[1] == name then - return true - end - if arg.type == '...' and name == '...' then - return true - end - end - return false -end - return function (uri, callback) local state = files.getState(uri) if not state then @@ -27,26 +12,13 @@ return function (uri, callback) end for _, doc in ipairs(state.ast.docs) do - if doc.type ~= 'doc.param' then - goto CONTINUE - end - local binds = doc.bindSources - if not binds then - goto CONTINUE - end - local param = doc.param - local name = param[1] - for _, source in ipairs(binds) do - if source.type == 'function' then - if not hasParamName(source, name) then - callback { - start = param.start, - finish = param.finish, - message = lang.script('DIAG_UNDEFINED_DOC_PARAM', name) - } - end - end + if doc.type == 'doc.param' + and not doc.bindSource then + callback { + start = doc.param.start, + finish = doc.param.finish, + message = lang.script('DIAG_UNDEFINED_DOC_PARAM', doc.param[1]) + } end - ::CONTINUE:: end end -- cgit v1.2.3 From db4ae9c48b7d2f97823469e8a32aff170ecba12f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Fri, 8 Jul 2022 17:38:44 +0800 Subject: resolve #993 supports multi-types --- script/core/diagnostics/undefined-doc-name.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/undefined-doc-name.lua b/script/core/diagnostics/undefined-doc-name.lua index 69edb380..bacd4288 100644 --- a/script/core/diagnostics/undefined-doc-name.lua +++ b/script/core/diagnostics/undefined-doc-name.lua @@ -32,7 +32,7 @@ return function (uri, callback) return end local name = source[1] - if name == '...' then + if name == '...' or name == '_' then return end if #vm.getDocSets(uri, name) > 0 -- cgit v1.2.3 From ecd09fc50a5199bc2f4888b5c11aec754101da5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Mon, 11 Jul 2022 15:21:56 +0800 Subject: small fix --- script/core/diagnostics/unknown-cast-variable.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/unknown-cast-variable.lua b/script/core/diagnostics/unknown-cast-variable.lua index 57377f6e..3f082a50 100644 --- a/script/core/diagnostics/unknown-cast-variable.lua +++ b/script/core/diagnostics/unknown-cast-variable.lua @@ -24,7 +24,7 @@ return function (uri, callback) callback { start = doc.loc.start, finish = doc.loc.finish, - message = lang.script('DIAG_UNKNOWN_CAST_VARIABLE', doc[1]) + message = lang.script('DIAG_UNKNOWN_CAST_VARIABLE', doc.loc[1]) } end end -- cgit v1.2.3 From 3820962ca2d23d525bc8417c8088e7eeb667cba1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Mon, 11 Jul 2022 20:32:03 +0800 Subject: resolve #1209 treat `_ENV = {}` as `local _ENV = {}` `local _ENV = nil` will disable all globals `local _ENV = {}` will enable all globals `local _ENV = {} ---@type mathlib` will open globals in mathlib --- script/core/diagnostics/global-in-nil-env.lua | 68 +++++++------------------ script/core/diagnostics/undefined-env-child.lua | 32 +++++++++--- script/core/diagnostics/undefined-global.lua | 6 +-- 3 files changed, 48 insertions(+), 58 deletions(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/global-in-nil-env.lua b/script/core/diagnostics/global-in-nil-env.lua index 334fd81a..e154080c 100644 --- a/script/core/diagnostics/global-in-nil-env.lua +++ b/script/core/diagnostics/global-in-nil-env.lua @@ -2,65 +2,35 @@ local files = require 'files' local guide = require 'parser.guide' local lang = require 'language' --- TODO: 检查路径是否可达 -local function mayRun(path) - return true -end - return function (uri, callback) - local ast = files.getState(uri) - if not ast then - return - end - local root = guide.getRoot(ast.ast) - local env = guide.getENV(root) - - local nilDefs = {} - if not env or not env.ref then - return - end - for _, ref in ipairs(env.ref) do - if ref.type == 'setlocal' then - if ref.value and ref.value.type == 'nil' then - nilDefs[#nilDefs+1] = ref - end - end - end - - if #nilDefs == 0 then + local state = files.getState(uri) + if not state then return end local function check(source) local node = source.node if node.tag == '_ENV' then - local ok - for _, nilDef in ipairs(nilDefs) do - local mode, pathA = guide.getPath(nilDef, source) - if mode == 'before' - and mayRun(pathA) then - ok = nilDef - break - end - end - if ok then - callback { - start = source.start, - finish = source.finish, - uri = uri, - message = lang.script.DIAG_GLOBAL_IN_NIL_ENV, - related = { - { - start = ok.start, - finish = ok.finish, - uri = uri, - } + return + end + + if not node.value or node.value.type == 'nil' then + callback { + start = source.start, + finish = source.finish, + uri = uri, + message = lang.script.DIAG_GLOBAL_IN_NIL_ENV, + related = { + { + start = node.start, + finish = node.finish, + uri = uri, } } - end + } end end - guide.eachSourceType(ast.ast, 'getglobal', check) - guide.eachSourceType(ast.ast, 'setglobal', check) + guide.eachSourceType(state.ast, 'getglobal', check) + guide.eachSourceType(state.ast, 'setglobal', check) end diff --git a/script/core/diagnostics/undefined-env-child.lua b/script/core/diagnostics/undefined-env-child.lua index 2f559697..1dff575b 100644 --- a/script/core/diagnostics/undefined-env-child.lua +++ b/script/core/diagnostics/undefined-env-child.lua @@ -3,20 +3,40 @@ local guide = require 'parser.guide' local lang = require 'language' local vm = require "vm.vm" +---@param source parser.object +---@return boolean +local function isBindDoc(source) + if not source.bindDocs then + return false + end + for _, doc in ipairs(source.bindDocs) do + if doc.type == 'doc.type' + or doc.type == 'doc.class' then + return true + end + end + return false +end + return function (uri, callback) - local ast = files.getState(uri) - if not ast then + local state = files.getState(uri) + if not state then return end - guide.eachSourceType(ast.ast, 'getglobal', function (source) - -- 单独验证自己是否在重载过的 _ENV 中有定义 + + guide.eachSourceType(state.ast, 'getglobal', function (source) if source.node.tag == '_ENV' then return end - local defs = vm.getDefs(source) - if #defs > 0 then + + if not isBindDoc(source.node) then return end + + if #vm.getDefs(source) > 0 then + return + end + local key = source[1] callback { start = source.start, diff --git a/script/core/diagnostics/undefined-global.lua b/script/core/diagnostics/undefined-global.lua index bf161c73..179c9204 100644 --- a/script/core/diagnostics/undefined-global.lua +++ b/script/core/diagnostics/undefined-global.lua @@ -15,8 +15,8 @@ local requireLike = { ---@async return function (uri, callback) - local ast = files.getState(uri) - if not ast then + local state = files.getState(uri) + if not state then return end @@ -25,7 +25,7 @@ return function (uri, callback) local cache = {} -- 遍历全局变量,检查所有没有 set 模式的全局变量 - guide.eachSourceType(ast.ast, 'getglobal', function (src) ---@async + guide.eachSourceType(state.ast, 'getglobal', function (src) ---@async local key = src[1] if not key then return -- cgit v1.2.3 From 8f056d36ec61dd91cfab282780dc1786df646fcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Mon, 11 Jul 2022 20:34:02 +0800 Subject: Revert "resolve #1209" This reverts commit 3820962ca2d23d525bc8417c8088e7eeb667cba1. --- script/core/diagnostics/global-in-nil-env.lua | 68 ++++++++++++++++++------- script/core/diagnostics/undefined-env-child.lua | 32 +++--------- script/core/diagnostics/undefined-global.lua | 6 +-- 3 files changed, 58 insertions(+), 48 deletions(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/global-in-nil-env.lua b/script/core/diagnostics/global-in-nil-env.lua index e154080c..334fd81a 100644 --- a/script/core/diagnostics/global-in-nil-env.lua +++ b/script/core/diagnostics/global-in-nil-env.lua @@ -2,35 +2,65 @@ local files = require 'files' local guide = require 'parser.guide' local lang = require 'language' +-- TODO: 检查路径是否可达 +local function mayRun(path) + return true +end + return function (uri, callback) - local state = files.getState(uri) - if not state then + local ast = files.getState(uri) + if not ast then + return + end + local root = guide.getRoot(ast.ast) + local env = guide.getENV(root) + + local nilDefs = {} + if not env or not env.ref then + return + end + for _, ref in ipairs(env.ref) do + if ref.type == 'setlocal' then + if ref.value and ref.value.type == 'nil' then + nilDefs[#nilDefs+1] = ref + end + end + end + + if #nilDefs == 0 then return end local function check(source) local node = source.node if node.tag == '_ENV' then - return - end - - if not node.value or node.value.type == 'nil' then - callback { - start = source.start, - finish = source.finish, - uri = uri, - message = lang.script.DIAG_GLOBAL_IN_NIL_ENV, - related = { - { - start = node.start, - finish = node.finish, - uri = uri, + local ok + for _, nilDef in ipairs(nilDefs) do + local mode, pathA = guide.getPath(nilDef, source) + if mode == 'before' + and mayRun(pathA) then + ok = nilDef + break + end + end + if ok then + callback { + start = source.start, + finish = source.finish, + uri = uri, + message = lang.script.DIAG_GLOBAL_IN_NIL_ENV, + related = { + { + start = ok.start, + finish = ok.finish, + uri = uri, + } } } - } + end end end - guide.eachSourceType(state.ast, 'getglobal', check) - guide.eachSourceType(state.ast, 'setglobal', check) + guide.eachSourceType(ast.ast, 'getglobal', check) + guide.eachSourceType(ast.ast, 'setglobal', check) end diff --git a/script/core/diagnostics/undefined-env-child.lua b/script/core/diagnostics/undefined-env-child.lua index 1dff575b..2f559697 100644 --- a/script/core/diagnostics/undefined-env-child.lua +++ b/script/core/diagnostics/undefined-env-child.lua @@ -3,40 +3,20 @@ local guide = require 'parser.guide' local lang = require 'language' local vm = require "vm.vm" ----@param source parser.object ----@return boolean -local function isBindDoc(source) - if not source.bindDocs then - return false - end - for _, doc in ipairs(source.bindDocs) do - if doc.type == 'doc.type' - or doc.type == 'doc.class' then - return true - end - end - return false -end - return function (uri, callback) - local state = files.getState(uri) - if not state then + local ast = files.getState(uri) + if not ast then return end - - guide.eachSourceType(state.ast, 'getglobal', function (source) + guide.eachSourceType(ast.ast, 'getglobal', function (source) + -- 单独验证自己是否在重载过的 _ENV 中有定义 if source.node.tag == '_ENV' then return end - - if not isBindDoc(source.node) then + local defs = vm.getDefs(source) + if #defs > 0 then return end - - if #vm.getDefs(source) > 0 then - return - end - local key = source[1] callback { start = source.start, diff --git a/script/core/diagnostics/undefined-global.lua b/script/core/diagnostics/undefined-global.lua index 179c9204..bf161c73 100644 --- a/script/core/diagnostics/undefined-global.lua +++ b/script/core/diagnostics/undefined-global.lua @@ -15,8 +15,8 @@ local requireLike = { ---@async return function (uri, callback) - local state = files.getState(uri) - if not state then + local ast = files.getState(uri) + if not ast then return end @@ -25,7 +25,7 @@ return function (uri, callback) local cache = {} -- 遍历全局变量,检查所有没有 set 模式的全局变量 - guide.eachSourceType(state.ast, 'getglobal', function (src) ---@async + guide.eachSourceType(ast.ast, 'getglobal', function (src) ---@async local key = src[1] if not key then return -- cgit v1.2.3 From 2469dbae153066fad83edcced82f17aee77d0a9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Mon, 11 Jul 2022 20:34:44 +0800 Subject: resolve #1029 treat _ENV = {} as local _ENV = {} local _ENV = nil will disable all globals local _ENV = {} will enable all globals local _ENV = {} ---@type mathlib will open globals in mathlib --- script/core/diagnostics/global-in-nil-env.lua | 68 +++++++------------------ script/core/diagnostics/undefined-env-child.lua | 32 +++++++++--- script/core/diagnostics/undefined-global.lua | 6 +-- 3 files changed, 48 insertions(+), 58 deletions(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/global-in-nil-env.lua b/script/core/diagnostics/global-in-nil-env.lua index 334fd81a..e154080c 100644 --- a/script/core/diagnostics/global-in-nil-env.lua +++ b/script/core/diagnostics/global-in-nil-env.lua @@ -2,65 +2,35 @@ local files = require 'files' local guide = require 'parser.guide' local lang = require 'language' --- TODO: 检查路径是否可达 -local function mayRun(path) - return true -end - return function (uri, callback) - local ast = files.getState(uri) - if not ast then - return - end - local root = guide.getRoot(ast.ast) - local env = guide.getENV(root) - - local nilDefs = {} - if not env or not env.ref then - return - end - for _, ref in ipairs(env.ref) do - if ref.type == 'setlocal' then - if ref.value and ref.value.type == 'nil' then - nilDefs[#nilDefs+1] = ref - end - end - end - - if #nilDefs == 0 then + local state = files.getState(uri) + if not state then return end local function check(source) local node = source.node if node.tag == '_ENV' then - local ok - for _, nilDef in ipairs(nilDefs) do - local mode, pathA = guide.getPath(nilDef, source) - if mode == 'before' - and mayRun(pathA) then - ok = nilDef - break - end - end - if ok then - callback { - start = source.start, - finish = source.finish, - uri = uri, - message = lang.script.DIAG_GLOBAL_IN_NIL_ENV, - related = { - { - start = ok.start, - finish = ok.finish, - uri = uri, - } + return + end + + if not node.value or node.value.type == 'nil' then + callback { + start = source.start, + finish = source.finish, + uri = uri, + message = lang.script.DIAG_GLOBAL_IN_NIL_ENV, + related = { + { + start = node.start, + finish = node.finish, + uri = uri, } } - end + } end end - guide.eachSourceType(ast.ast, 'getglobal', check) - guide.eachSourceType(ast.ast, 'setglobal', check) + guide.eachSourceType(state.ast, 'getglobal', check) + guide.eachSourceType(state.ast, 'setglobal', check) end diff --git a/script/core/diagnostics/undefined-env-child.lua b/script/core/diagnostics/undefined-env-child.lua index 2f559697..1dff575b 100644 --- a/script/core/diagnostics/undefined-env-child.lua +++ b/script/core/diagnostics/undefined-env-child.lua @@ -3,20 +3,40 @@ local guide = require 'parser.guide' local lang = require 'language' local vm = require "vm.vm" +---@param source parser.object +---@return boolean +local function isBindDoc(source) + if not source.bindDocs then + return false + end + for _, doc in ipairs(source.bindDocs) do + if doc.type == 'doc.type' + or doc.type == 'doc.class' then + return true + end + end + return false +end + return function (uri, callback) - local ast = files.getState(uri) - if not ast then + local state = files.getState(uri) + if not state then return end - guide.eachSourceType(ast.ast, 'getglobal', function (source) - -- 单独验证自己是否在重载过的 _ENV 中有定义 + + guide.eachSourceType(state.ast, 'getglobal', function (source) if source.node.tag == '_ENV' then return end - local defs = vm.getDefs(source) - if #defs > 0 then + + if not isBindDoc(source.node) then return end + + if #vm.getDefs(source) > 0 then + return + end + local key = source[1] callback { start = source.start, diff --git a/script/core/diagnostics/undefined-global.lua b/script/core/diagnostics/undefined-global.lua index bf161c73..179c9204 100644 --- a/script/core/diagnostics/undefined-global.lua +++ b/script/core/diagnostics/undefined-global.lua @@ -15,8 +15,8 @@ local requireLike = { ---@async return function (uri, callback) - local ast = files.getState(uri) - if not ast then + local state = files.getState(uri) + if not state then return end @@ -25,7 +25,7 @@ return function (uri, callback) local cache = {} -- 遍历全局变量,检查所有没有 set 模式的全局变量 - guide.eachSourceType(ast.ast, 'getglobal', function (src) ---@async + guide.eachSourceType(state.ast, 'getglobal', function (src) ---@async local key = src[1] if not key then return -- cgit v1.2.3 From 7340483dc3071c33d5d2a726032a61632c28509a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Tue, 12 Jul 2022 16:16:58 +0800 Subject: unreachable-code --- script/core/diagnostics/unreachable-code.lua | 68 ++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 script/core/diagnostics/unreachable-code.lua (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/unreachable-code.lua b/script/core/diagnostics/unreachable-code.lua new file mode 100644 index 00000000..28aee6ea --- /dev/null +++ b/script/core/diagnostics/unreachable-code.lua @@ -0,0 +1,68 @@ +local files = require 'files' +local guide = require 'parser.guide' +local vm = require 'vm' +local lang = require 'language' +local await = require 'await' + +---@param block parser.object +---@return boolean +local function hasReturn(block) + if block.hasReturn or block.hasError then + return true + end + if block.type == 'if' then + local hasElse + for _, subBlock in ipairs(block) do + if not hasReturn(subBlock) then + return false + end + if subBlock.type == 'elseblock' then + hasElse = true + end + end + return hasElse == true + else + if block.type == 'while' then + if vm.testCondition(block.filter) then + return true + end + end + for _, action in ipairs(block) do + if guide.isBlockType(action) then + if hasReturn(action) then + return true + end + end + end + end + return false +end + +---@async +return function (uri, callback) + local state = files.getState(uri) + if not state then + return + end + + ---@async + guide.eachSourceTypes(state.ast, {'main', 'function'}, function (source) + await.delay() + if not source.returns then + return + end + for i, action in ipairs(source) do + if guide.isBlockType(action) + and hasReturn(action) then + if i < #source then + callback { + start = source[i+1].start, + finish = source[#source].finish, + message = lang.script('DIAG_UNREACHABLE_CODE'), + } + end + return + end + end + end) +end -- cgit v1.2.3 From f0d3ef728726217a791764bdfc2d2375e1217afb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Tue, 12 Jul 2022 16:32:28 +0800 Subject: unnecessary tag --- script/core/diagnostics/unreachable-code.lua | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/unreachable-code.lua b/script/core/diagnostics/unreachable-code.lua index 28aee6ea..a386198c 100644 --- a/script/core/diagnostics/unreachable-code.lua +++ b/script/core/diagnostics/unreachable-code.lua @@ -3,6 +3,7 @@ local guide = require 'parser.guide' local vm = require 'vm' local lang = require 'language' local await = require 'await' +local define = require 'proto.define' ---@param block parser.object ---@return boolean @@ -56,8 +57,9 @@ return function (uri, callback) and hasReturn(action) then if i < #source then callback { - start = source[i+1].start, - finish = source[#source].finish, + start = source[i+1].start, + finish = source[#source].finish, + tags = { define.DiagnosticTag.Unnecessary }, message = lang.script('DIAG_UNREACHABLE_CODE'), } end -- cgit v1.2.3 From 7f35d4640b4a7338fdd34fb1c3509c3c671fa0ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Tue, 12 Jul 2022 16:40:49 +0800 Subject: contravariance is allowed at the class declaration --- script/core/diagnostics/assign-type-mismatch.lua | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/assign-type-mismatch.lua b/script/core/diagnostics/assign-type-mismatch.lua index 0cccc7ee..2d5c3f98 100644 --- a/script/core/diagnostics/assign-type-mismatch.lua +++ b/script/core/diagnostics/assign-type-mismatch.lua @@ -99,12 +99,7 @@ return function (uri, callback) end -- local Cat = setmetatable({}, {__index = Animal}) 允许逆变 - if value.type == 'select' - and value.sindex == 1 - and value.vararg - and value.vararg.type == 'call' - and value.vararg.node.special == 'setmetatable' - and hasMarkClass(source) then + if hasMarkClass(source) then if vm.canCastType(uri, valueNode:copy():remove 'table', varNode) then return end -- cgit v1.2.3 From 0d79784ccf47a03f9f35e1afbd579ecbab0acd5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Thu, 14 Jul 2022 16:29:15 +0800 Subject: fix --- script/core/diagnostics/unreachable-code.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/unreachable-code.lua b/script/core/diagnostics/unreachable-code.lua index a386198c..84b25b45 100644 --- a/script/core/diagnostics/unreachable-code.lua +++ b/script/core/diagnostics/unreachable-code.lua @@ -24,7 +24,8 @@ local function hasReturn(block) return hasElse == true else if block.type == 'while' then - if vm.testCondition(block.filter) then + if vm.testCondition(block.filter) + and not block.breaks then return true end end -- cgit v1.2.3 From 04c671114d1d4c4bcfbad74c8341b24844552865 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Sat, 16 Jul 2022 23:47:40 +0800 Subject: doc.enum --- script/core/diagnostics/duplicate-doc-alias.lua | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/duplicate-doc-alias.lua b/script/core/diagnostics/duplicate-doc-alias.lua index 2625f88f..e9ee5aab 100644 --- a/script/core/diagnostics/duplicate-doc-alias.lua +++ b/script/core/diagnostics/duplicate-doc-alias.lua @@ -17,7 +17,8 @@ return function (uri, callback) local cache = {} for _, doc in ipairs(state.ast.docs) do - if doc.type == 'doc.alias' then + if doc.type == 'doc.alias' + or doc.type == 'doc.enum' then local name = guide.getKeyName(doc) if not name then return @@ -28,7 +29,8 @@ return function (uri, callback) cache[name] = {} for _, otherDoc in ipairs(docs) do if otherDoc.type == 'doc.alias' - or otherDoc.type == 'doc.class' then + or otherDoc.type == 'doc.class' + or otherDoc.type == 'doc.enum' then cache[name][#cache[name]+1] = { start = otherDoc.start, finish = otherDoc.finish, -- cgit v1.2.3 From 028a53341827dff350fe3a626404c607f4f67c8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Tue, 19 Jul 2022 21:51:25 +0800 Subject: `close-non-object` don't check `unknown` --- script/core/diagnostics/close-non-object.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/close-non-object.lua b/script/core/diagnostics/close-non-object.lua index d07aaebe..1a42b800 100644 --- a/script/core/diagnostics/close-non-object.lua +++ b/script/core/diagnostics/close-non-object.lua @@ -28,7 +28,8 @@ return function (uri, callback) if not infer:hasClass(uri) and not infer:hasType(uri, 'nil') and not infer:hasType(uri, 'table') - and infer:view(uri, 'any') ~= 'any' then + and not infer:hasUnknown(uri) + and not infer:hasAny(uri) then callback { start = source.value.start, finish = source.value.finish, -- cgit v1.2.3 From d09c74c69306fa5b3deb0db68552c9223b6d76df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Fri, 22 Jul 2022 15:24:40 +0800 Subject: refactor `require-path` --- script/core/diagnostics/different-requires.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/different-requires.lua b/script/core/diagnostics/different-requires.lua index de063c9f..22e3e681 100644 --- a/script/core/diagnostics/different-requires.lua +++ b/script/core/diagnostics/different-requires.lua @@ -21,7 +21,7 @@ return function (uri, callback) return end local literal = arg1[1] - local results = rpath.findUrisByRequirePath(uri, literal) + local results = rpath.findUrisByRequireName(uri, literal) if not results or #results ~= 1 then return end -- cgit v1.2.3 From 05a098f7ed731b2a3a6d068d5e9d7dfeacc67be0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Fri, 22 Jul 2022 17:51:15 +0800 Subject: fix #1363 don't lookup globals in blocks --- script/core/diagnostics/unreachable-code.lua | 3 --- 1 file changed, 3 deletions(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/unreachable-code.lua b/script/core/diagnostics/unreachable-code.lua index 84b25b45..772a1764 100644 --- a/script/core/diagnostics/unreachable-code.lua +++ b/script/core/diagnostics/unreachable-code.lua @@ -50,9 +50,6 @@ return function (uri, callback) ---@async guide.eachSourceTypes(state.ast, {'main', 'function'}, function (source) await.delay() - if not source.returns then - return - end for i, action in ipairs(source) do if guide.isBlockType(action) and hasReturn(action) then -- cgit v1.2.3 From 26732327848c0eaa8dd76abbd127d4a7a5a12121 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Mon, 25 Jul 2022 23:14:35 +0800 Subject: fix #1365 --- script/core/diagnostics/count-down-loop.lua | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/count-down-loop.lua b/script/core/diagnostics/count-down-loop.lua index 9bc4b273..88cb06ab 100644 --- a/script/core/diagnostics/count-down-loop.lua +++ b/script/core/diagnostics/count-down-loop.lua @@ -10,12 +10,15 @@ return function (uri, callback) end guide.eachSourceType(state.ast, 'loop', function (source) - local maxNumer = source.max and tonumber(source.max[1]) - if maxNumer ~= 1 then + local maxNumber = source.max and tonumber(source.max[1]) + if not maxNumber then return end local minNumber = source.init and tonumber(source.init[1]) - if minNumber and minNumber <= 1 then + if minNumber and maxNumber and minNumber <= maxNumber then + return + end + if not minNumber and maxNumber > 1 then return end if not source.step then @@ -24,7 +27,7 @@ return function (uri, callback) finish = source.max.finish, message = lang.script('DIAG_COUNT_DOWN_LOOP' , ('%s, %s'):format(text:sub( - guide.positionToOffset(state, source.init.start), + guide.positionToOffset(state, source.init.start + 1), guide.positionToOffset(state, source.max.finish) ), '-1') ) @@ -37,7 +40,7 @@ return function (uri, callback) finish = source.step.finish, message = lang.script('DIAG_COUNT_DOWN_LOOP' , ('%s, -%s'):format(text:sub( - guide.positionToOffset(state, source.init.start), + guide.positionToOffset(state, source.init.start + 1), guide.positionToOffset(state, source.max.finish) ), source.step[1]) ) -- cgit v1.2.3 From d74aba7d2372d3f151bb53b27adccd01509d88f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Mon, 25 Jul 2022 23:27:54 +0800 Subject: fix message of `newline-call` and `newfield-call` --- script/core/diagnostics/newfield-call.lua | 5 +++-- script/core/diagnostics/newline-call.lua | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/newfield-call.lua b/script/core/diagnostics/newfield-call.lua index e22dcffe..bd114959 100644 --- a/script/core/diagnostics/newfield-call.lua +++ b/script/core/diagnostics/newfield-call.lua @@ -2,6 +2,7 @@ local files = require 'files' local guide = require 'parser.guide' local lang = require 'language' local await = require 'await' +local sub = require 'core.substring' ---@async return function (uri, callback) @@ -36,8 +37,8 @@ return function (uri, callback) start = call.start, finish = call.finish, message = lang.script('DIAG_PREFIELD_CALL' - , text:sub(func.start, func.finish) - , text:sub(args.start, args.finish) + , sub(state)(func.start + 1, func.finish) + , sub(state)(args.start + 1, args.finish) ) } end diff --git a/script/core/diagnostics/newline-call.lua b/script/core/diagnostics/newline-call.lua index 6460d3a7..2ba2ce03 100644 --- a/script/core/diagnostics/newline-call.lua +++ b/script/core/diagnostics/newline-call.lua @@ -2,6 +2,7 @@ local files = require 'files' local guide = require 'parser.guide' local lang = require 'language' local await = require 'await' +local sub = require 'core.substring' ---@async return function (uri, callback) @@ -44,8 +45,8 @@ return function (uri, callback) start = node.start, finish = args.finish, message = lang.script('DIAG_PREVIOUS_CALL' - , text:sub(node.start, node.finish) - , text:sub(args.start, args.finish) + , sub(state)(node.start + 1, node.finish) + , sub(state)(args.start + 1, args.finish) ), } end -- cgit v1.2.3 From 3d21c63ec3e330dc717008b37f8fb1fa38d41862 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Tue, 26 Jul 2022 15:26:50 +0800 Subject: only checks `1` --- script/core/diagnostics/count-down-loop.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/count-down-loop.lua b/script/core/diagnostics/count-down-loop.lua index 88cb06ab..bd6e5ee3 100644 --- a/script/core/diagnostics/count-down-loop.lua +++ b/script/core/diagnostics/count-down-loop.lua @@ -18,7 +18,7 @@ return function (uri, callback) if minNumber and maxNumber and minNumber <= maxNumber then return end - if not minNumber and maxNumber > 1 then + if not minNumber and maxNumber ~= 1 then return end if not source.step then -- cgit v1.2.3 From 8944c513f5d0d511f0bb4151bf3838b65b49be22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Thu, 28 Jul 2022 19:34:56 +0800 Subject: fix runtime error --- script/core/diagnostics/duplicate-doc-alias.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/duplicate-doc-alias.lua b/script/core/diagnostics/duplicate-doc-alias.lua index e9ee5aab..360358e4 100644 --- a/script/core/diagnostics/duplicate-doc-alias.lua +++ b/script/core/diagnostics/duplicate-doc-alias.lua @@ -41,8 +41,8 @@ return function (uri, callback) end if #cache[name] > 1 then callback { - start = doc.alias.start, - finish = doc.alias.finish, + start = (doc.alias or doc.enum).start, + finish = (doc.alias or doc.enum).finish, related = cache, message = lang.script('DIAG_DUPLICATE_DOC_ALIAS', name) } -- cgit v1.2.3 From f78d4944b4d811956b6b1493b8a022f633babfdd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Fri, 29 Jul 2022 16:22:02 +0800 Subject: fix #1406 --- script/core/diagnostics/unreachable-code.lua | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'script/core/diagnostics') diff --git a/script/core/diagnostics/unreachable-code.lua b/script/core/diagnostics/unreachable-code.lua index 772a1764..4f0a38b7 100644 --- a/script/core/diagnostics/unreachable-code.lua +++ b/script/core/diagnostics/unreachable-code.lua @@ -5,6 +5,21 @@ local lang = require 'language' local await = require 'await' local define = require 'proto.define' +---@param source parser.object +---@return boolean +local function allLiteral(source) + local result = true + guide.eachSource(source, function (src) + if src.type ~= 'unary' + and src.type ~= 'binary' + and not guide.isLiteral(src) then + result = false + return false + end + end) + return result +end + ---@param block parser.object ---@return boolean local function hasReturn(block) @@ -25,7 +40,8 @@ local function hasReturn(block) else if block.type == 'while' then if vm.testCondition(block.filter) - and not block.breaks then + and not block.breaks + and allLiteral(block.filter) then return true end end -- cgit v1.2.3