diff options
-rw-r--r-- | script/client.lua | 2 | ||||
-rw-r--r-- | script/core/completion/completion.lua | 2 | ||||
-rw-r--r-- | script/core/highlight.lua | 2 | ||||
-rw-r--r-- | script/filewatch.lua | 2 | ||||
-rw-r--r-- | script/fs-utility.lua | 15 | ||||
-rw-r--r-- | script/parser/guide.lua | 4 | ||||
-rw-r--r-- | script/utility.lua | 4 | ||||
-rw-r--r-- | script/vm/function.lua | 32 | ||||
-rw-r--r-- | script/vm/sign.lua | 2 | ||||
-rw-r--r-- | script/workspace/workspace.lua | 2 | ||||
-rw-r--r-- | test.lua | 4 | ||||
-rw-r--r-- | test/diagnostics/type-check.lua | 12 |
12 files changed, 57 insertions, 26 deletions
diff --git a/script/client.lua b/script/client.lua index 1e4689a8..8ab3ac0e 100644 --- a/script/client.lua +++ b/script/client.lua @@ -400,7 +400,7 @@ function m.editText(uri, edits) }) end ----@param callback async fun() +---@param callback async fun(ev: string) function m.event(callback) m._eventList[#m._eventList+1] = callback end diff --git a/script/core/completion/completion.lua b/script/core/completion/completion.lua index d75cbea0..39b54209 100644 --- a/script/core/completion/completion.lua +++ b/script/core/completion/completion.lua @@ -33,7 +33,7 @@ local diagnosticModes = { local stackID = 0 local stacks = {} ----@param callback async fun(newSource: parser.object) +---@param callback async fun(newSource: parser.object): table local function stack(oldSource, callback) stackID = stackID + 1 local uri = guide.getUri(oldSource) diff --git a/script/core/highlight.lua b/script/core/highlight.lua index edd8c95d..80088680 100644 --- a/script/core/highlight.lua +++ b/script/core/highlight.lua @@ -7,7 +7,7 @@ local guide = require 'parser.guide' ---@async local function eachRef(source, callback) - local refs = vm.getRefs(source, function () + local refs = vm.getRefs(source, function (_) return false end) for _, ref in ipairs(refs) do diff --git a/script/filewatch.lua b/script/filewatch.lua index 2b687dd2..85a50046 100644 --- a/script/filewatch.lua +++ b/script/filewatch.lua @@ -60,7 +60,7 @@ function m.watch(path) end end ----@param callback async fun() +---@param callback async fun(ev: string, path: string) function m.event(callback) m._eventList[#m._eventList+1] = callback end diff --git a/script/fs-utility.lua b/script/fs-utility.lua index 5ac4139a..792df1a8 100644 --- a/script/fs-utility.lua +++ b/script/fs-utility.lua @@ -294,6 +294,9 @@ local function fsIsDirectory(path, option) return status == 'directory' end +---@param path fs.path|dummyfs|nil +---@param option table +---@return fun(): fs.path|dummyfs|nil local function fsPairs(path, option) if not path then return function () end @@ -421,6 +424,8 @@ local function fsCopy(source, target, option) return true end +---@param path dummyfs|fs.path +---@param option table local function fsCreateDirectories(path, option) if not path then return @@ -444,7 +449,7 @@ local function fileRemove(path, option) return end if fsIsDirectory(path, option) then - for child in fsPairs(path) do + for child in fsPairs(path, option) do fileRemove(child, option) end end @@ -465,7 +470,7 @@ local function fileCopy(source, target, option) local isExists = fsExists(target, option) if isDir1 then if isDir2 or fsCreateDirectories(target, option) then - for filePath in fsPairs(source) do + for filePath in fsPairs(source, option) do local name = filePath:filename():string() fileCopy(filePath, target / name, option) end @@ -505,7 +510,7 @@ local function fileSync(source, target, option) for filePath in fs.pairs(target) do fileList[filePath] = true end - for filePath in fsPairs(source) do + for filePath in fsPairs(source, option) do local name = filePath:filename():string() local targetPath = target / name fileSync(filePath, targetPath, option) @@ -518,8 +523,8 @@ local function fileSync(source, target, option) if isExists then fileRemove(target, option) end - if fsCreateDirectories(target) then - for filePath in fsPairs(source) do + if fsCreateDirectories(target, option) then + for filePath in fsPairs(source, option) do local name = filePath:filename():string() fileCopy(filePath, target / name, option) end diff --git a/script/parser/guide.lua b/script/parser/guide.lua index b6dbd65c..4a398fcb 100644 --- a/script/parser/guide.lua +++ b/script/parser/guide.lua @@ -635,7 +635,7 @@ end --- 遍历所有包含position的source ---@param ast parser.object ---@param position integer ----@param callback fun(src: parser.object) +---@param callback fun(src: parser.object): any function m.eachSourceContain(ast, position, callback) local list = { ast } local mark = {} @@ -744,7 +744,7 @@ end --- 遍历所有的source ---@param ast parser.object ----@param callback fun(src: parser.object) +---@param callback fun(src: parser.object): boolean? function m.eachSource(ast, callback) local cache = ast._eachCache if not cache then diff --git a/script/utility.lua b/script/utility.lua index 2ed44ba9..fbb5094f 100644 --- a/script/utility.lua +++ b/script/utility.lua @@ -581,7 +581,7 @@ end ---遍历文本的每一行 ---@param text string ---@param keepNL? boolean # 保留换行符 ----@return fun():string, integer +---@return fun():string?, integer? function m.eachLine(text, keepNL) local offset = 1 local lineCount = 0 @@ -730,7 +730,7 @@ function switchMT:has(name) end ---@param name string ----@return any ... +---@return ... function switchMT:__call(name, ...) local callback = self.map[name] or self._default if not callback then diff --git a/script/vm/function.lua b/script/vm/function.lua index b4466668..bdd7e229 100644 --- a/script/vm/function.lua +++ b/script/vm/function.lua @@ -297,22 +297,36 @@ function vm.countList(list, mark) if not lastArg then return 0, 0, 0 end + ---@type integer, number, integer + local min, max, def = #list, #list, #list if lastArg.type == '...' - or lastArg.type == 'varargs' then - return #list - 1, math.huge, #list - end - if lastArg.type == 'call' then + or lastArg.type == 'varargs' + or (lastArg.type == 'doc.type' and lastArg.name and lastArg.name[1] == '...') then + max = math.huge + elseif lastArg.type == 'call' then if not mark then mark = {} end if mark[lastArg] then - return #list - 1, math.huge, #list + min = min - 1 + max = math.huge + else + mark[lastArg] = true + local rmin, rmax, rdef = vm.countReturnsOfCall(lastArg.node, lastArg.args, mark) + return min - 1 + rmin, max - 1 + rmax, def - 1 + rdef + end + end + for i = min, 1, -1 do + local arg = list[i] + if arg.type == 'doc.type' + and ((arg.name and arg.name[1] == '...') + or vm.compileNode(arg):isNullable()) then + min = i - 1 + else + break end - mark[lastArg] = true - local rmin, rmax, rdef = vm.countReturnsOfCall(lastArg.node, lastArg.args, mark) - return #list - 1 + rmin, #list - 1 + rmax, #list - 1 + rdef end - return #list, #list, #list + return min, max, def end ---@param func parser.object diff --git a/script/vm/sign.lua b/script/vm/sign.lua index 3fc9f981..b70aaec5 100644 --- a/script/vm/sign.lua +++ b/script/vm/sign.lua @@ -312,7 +312,7 @@ function vm.getSign(source) or source.type == 'doc.type.table' or source.type == 'doc.type.array' then local hasGeneric - guide.eachSourceType(source, 'doc.generic.name', function () + guide.eachSourceType(source, 'doc.generic.name', function (_) hasGeneric = true end) if not hasGeneric then diff --git a/script/workspace/workspace.lua b/script/workspace/workspace.lua index 2e6e8652..b6be6fc5 100644 --- a/script/workspace/workspace.lua +++ b/script/workspace/workspace.lua @@ -315,7 +315,7 @@ function m.awaitPreload(scp) local uri = files.getRealUri(furi.encode(path)) scp:get('cachedUris')[uri] = true ld:loadFile(uri) - end, function () ---@async + end, function (_) ---@async count = count + 1 if count == 100000 then client.showMessage('Warning', lang.script('WORKSPACE_SCAN_TOO_MUCH', count, furi.decode(scp.uri))) @@ -37,8 +37,8 @@ local function test(name) local clock = os.clock() print(('测试[%s]...'):format(name)) local originRequire = require - require = function (n, ...) - local v, p = originRequire(n, ...) + require = function (n) + local v, p = originRequire(n) if p and p:find 'test/' then package.loaded[n] = nil end diff --git a/test/diagnostics/type-check.lua b/test/diagnostics/type-check.lua index a8525ab0..267c3a27 100644 --- a/test/diagnostics/type-check.lua +++ b/test/diagnostics/type-check.lua @@ -991,6 +991,18 @@ end ]] TEST [[ +---@type fun():number? +local function f() +end +]] + +TEST [[ +---@type fun():... +local function f() +end +]] + +TEST [[ ---@type fun():number local function f() return 1, <!true!> |