summaryrefslogtreecommitdiff
path: root/script
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2022-06-28 20:06:34 +0800
committer最萌小汐 <sumneko@hotmail.com>2022-06-28 20:06:34 +0800
commit5b950742705515c4bc2502e8a902221f44bf644f (patch)
treef57ba3f1159eecfba3d882703b1b5775e0e72541 /script
parentdeac4582d38908c1281d4d5215b7b69ff8400dca (diff)
downloadlua-language-server-5b950742705515c4bc2502e8a902221f44bf644f.zip
cleanup
Diffstat (limited to 'script')
-rw-r--r--script/await.lua3
-rw-r--r--script/client.lua6
-rw-r--r--script/config/loader.lua6
-rw-r--r--script/core/diagnostics/assign-type-mismatch.lua2
-rw-r--r--script/core/diagnostics/duplicate-set-field.lua8
-rw-r--r--script/core/diagnostics/return-type-mismatch.lua6
-rw-r--r--script/core/diagnostics/unused-function.lua5
-rw-r--r--script/file-uri.lua3
-rw-r--r--script/files.lua6
-rw-r--r--script/fs-utility.lua21
-rw-r--r--script/parser/compile.lua38
-rw-r--r--script/parser/guide.lua51
-rw-r--r--script/parser/luadoc.lua10
-rw-r--r--script/workspace/scope.lua2
-rw-r--r--script/workspace/workspace.lua5
15 files changed, 80 insertions, 92 deletions
diff --git a/script/await.lua b/script/await.lua
index 4fb81cd8..fa2aea13 100644
--- a/script/await.lua
+++ b/script/await.lua
@@ -132,9 +132,6 @@ end
---@param callback function
---@async
function m.wait(callback, ...)
- if not coroutine.isyieldable() then
- return
- end
local co = coroutine.running()
local resumed
callback(function (...)
diff --git a/script/client.lua b/script/client.lua
index d9399ddd..aa24ae24 100644
--- a/script/client.lua
+++ b/script/client.lua
@@ -240,6 +240,9 @@ local function tryModifySpecifiedConfig(uri, finalChanges)
return false
end
local path = workspace.getAbsolutePath(uri, CONFIGPATH)
+ if not path then
+ return false
+ end
util.saveFile(path, json.beautify(scp:get('lastLocalConfig'), { indent = ' ' }))
return true
end
@@ -254,6 +257,9 @@ local function tryModifyRC(uri, finalChanges, create)
return false
end
path = fs.exists(path) and path or workspace.getAbsolutePath(uri, '.luarc.json')
+ if not path then
+ return false
+ end
local buf = util.loadFile(path)
if not buf and not create then
return false
diff --git a/script/config/loader.lua b/script/config/loader.lua
index c95c0a35..bf662ef8 100644
--- a/script/config/loader.lua
+++ b/script/config/loader.lua
@@ -17,7 +17,7 @@ end
---@class config.loader
local m = {}
----@return table
+---@return table?
function m.loadRCConfig(uri, filename)
local scp = scope.getScope(uri)
local path = workspace.getAbsolutePath(uri, filename)
@@ -35,6 +35,7 @@ function m.loadRCConfig(uri, filename)
errorMessage(lang.script('CONFIG_LOAD_ERROR', res))
return scp:get('lastRCConfig')
end
+ ---@cast res table
scp:set('lastRCConfig', res)
return res
end
@@ -62,6 +63,7 @@ function m.loadLocalConfig(uri, filename)
errorMessage(lang.script('CONFIG_LOAD_ERROR', res))
return scp:get('lastLocalConfig')
end
+ ---@cast res table
scp:set('lastLocalConfig', res)
scp:set('lastLocalType', 'json')
return res
@@ -81,7 +83,7 @@ end
---@async
---@param uri? uri
----@return table
+---@return table?
function m.loadClientConfig(uri)
local configs = proto.awaitRequest('workspace/configuration', {
items = {
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
diff --git a/script/file-uri.lua b/script/file-uri.lua
index 877d2a1c..6dbf52a9 100644
--- a/script/file-uri.lua
+++ b/script/file-uri.lua
@@ -67,9 +67,6 @@ end
---@param uri uri
---@return string path
function m.decode(uri)
- if not uri then
- return nil
- end
local scheme, authority, path = uri:match('([^:]*):?/?/?([^/]*)(.*)')
if not scheme then
return ''
diff --git a/script/files.lua b/script/files.lua
index d9148e27..56ccb60b 100644
--- a/script/files.lua
+++ b/script/files.lua
@@ -344,7 +344,7 @@ end
--- 获取文件原始文本
---@param uri uri
----@return string text
+---@return string? text
function m.getOriginText(uri)
local file = m.fileMap[uri]
if not file then
@@ -358,9 +358,7 @@ end
---@return integer[]
function m.getOriginLines(uri)
local file = m.fileMap[uri]
- if not file then
- return nil
- end
+ assert(file, 'file not exists:' .. uri)
return file.originLines
end
diff --git a/script/fs-utility.lua b/script/fs-utility.lua
index db8d177c..3581bd50 100644
--- a/script/fs-utility.lua
+++ b/script/fs-utility.lua
@@ -261,7 +261,7 @@ end
---@param path string|fspath
---@param option table
----@return fspath
+---@return fspath?
local function fsAbsolute(path, option)
if type(path) == 'string' then
local suc, res = pcall(fs.path, path)
@@ -451,6 +451,9 @@ local function fileRemove(path, option)
end
end
+---@param source fspath?
+---@param target fspath?
+---@param option table
local function fileCopy(source, target, option)
if not source or not target then
return
@@ -462,7 +465,7 @@ local function fileCopy(source, target, option)
if isDir2 or fsCreateDirectories(target, option) then
for filePath in fsPairs(source) do
local name = filePath:filename():string()
- fileCopy(filePath, target / name, option)
+ fileCopy(filePath, target / name--[[@as fspath]], option)
end
end
else
@@ -513,7 +516,7 @@ local function fileSync(source, target, option)
if fsCreateDirectories(target) then
for filePath in fsPairs(source) do
local name = filePath:filename():string()
- fileCopy(filePath, target / name, option)
+ fileCopy(filePath, target / name--[[@as fspath]], option)
end
end
end
@@ -595,10 +598,10 @@ end
---@return table
function m.fileCopy(source, target, option)
option = buildOption(option)
- source = fsAbsolute(source, option)
- target = fsAbsolute(target, option)
+ local fsSource = fsAbsolute(source, option)
+ local fsTarget = fsAbsolute(target, option)
- fileCopy(source, target, option)
+ fileCopy(fsSource, fsTarget, option)
return option
end
@@ -609,10 +612,10 @@ end
---@return table
function m.fileSync(source, target, option)
option = buildOption(option)
- source = fsAbsolute(source, option)
- target = fsAbsolute(target, option)
+ local fsSource = fsAbsolute(source, option)
+ local fsTarget = fsAbsolute(target, option)
- fileSync(source, target, option)
+ fileSync(fsSource, fsTarget, option)
return option
end
diff --git a/script/parser/compile.lua b/script/parser/compile.lua
index 8c5979f7..ee35a924 100644
--- a/script/parser/compile.lua
+++ b/script/parser/compile.lua
@@ -266,16 +266,16 @@ local function getPosition(offset, leftOrRight)
end
end
----@return string word
----@return parser.position startPosition
----@return parser.position finishPosition
+---@return string? word
+---@return parser.position? startPosition
+---@return parser.position? finishPosition
local function peekWord()
local word = Tokens[Index + 1]
if not word then
- return nil, nil, nil
+ return nil
end
if not CharMapWord[ssub(word, 1, 1)] then
- return nil, nil, nil
+ return nil
end
local startPos = getPosition(Tokens[Index] , 'left')
local finishPos = getPosition(Tokens[Index] + #word - 1, 'right')
@@ -2590,36 +2590,36 @@ local function skipSeps()
end
end
----@return parser.object first
----@return parser.object second
----@return parser.object[] rest
+---@return parser.object? first
+---@return parser.object? second
+---@return parser.object[]? rest
local function parseSetValues()
skipSpace()
local first = parseExp()
if not first then
- return nil, nil, nil
+ return nil
end
skipSpace()
if Tokens[Index + 1] ~= ',' then
- return first, nil, nil
+ return first
end
Index = Index + 2
skipSeps()
local second = parseExp()
if not second then
missExp()
- return first, nil, nil
+ return first
end
skipSpace()
if Tokens[Index + 1] ~= ',' then
- return first, second, nil
+ return first, second
end
Index = Index + 2
skipSeps()
local third = parseExp()
if not third then
missExp()
- return first, second, nil
+ return first, second
end
local rest = { third }
@@ -2647,18 +2647,18 @@ local function pushActionIntoCurrentChunk(action)
end
end
----@return parser.object second
----@return parser.object[] rest
+---@return parser.object? second
+---@return parser.object[]? rest
local function parseVarTails(parser, isLocal)
if Tokens[Index + 1] ~= ',' then
- return nil, nil
+ return nil
end
Index = Index + 2
skipSpace()
local second = parser(true)
if not second then
missName()
- return nil, nil
+ return nil
end
if isLocal then
createLocal(second, parseLocalAttrs())
@@ -2666,14 +2666,14 @@ local function parseVarTails(parser, isLocal)
end
skipSpace()
if Tokens[Index + 1] ~= ',' then
- return second, nil
+ return second
end
Index = Index + 2
skipSeps()
local third = parser(true)
if not third then
missName()
- return second, nil
+ return second
end
if isLocal then
createLocal(third, parseLocalAttrs())
diff --git a/script/parser/guide.lua b/script/parser/guide.lua
index 8de08ef9..29ca98c1 100644
--- a/script/parser/guide.lua
+++ b/script/parser/guide.lua
@@ -279,7 +279,7 @@ end
--- 寻找父函数
---@param obj parser.object
----@return parser.object
+---@return parser.object?
function m.getParentFunction(obj)
for _ = 1, 10000 do
obj = obj.parent
@@ -296,7 +296,7 @@ end
--- 寻找所在区块
---@param obj parser.object
----@return parser.object
+---@return parser.object?
function m.getBlock(obj)
for _ = 1, 10000 do
if not obj then
@@ -325,7 +325,7 @@ end
--- 寻找所在父区块
---@param obj parser.object
----@return parser.object
+---@return parser.object?
function m.getParentBlock(obj)
for _ = 1, 10000 do
obj = obj.parent
@@ -342,7 +342,7 @@ end
--- 寻找所在可break的父区块
---@param obj parser.object
----@return parser.object
+---@return parser.object?
function m.getBreakBlock(obj)
for _ = 1, 10000 do
obj = obj.parent
@@ -379,7 +379,7 @@ end
--- 寻找所在父类型
---@param obj parser.object
----@return parser.object
+---@return parser.object?
function m.getParentType(obj, want)
for _ = 1, 10000 do
obj = obj.parent
@@ -412,8 +412,7 @@ function m.getRoot(obj)
end
local parent = obj.parent
if not parent then
- log.error('Can not find out root:', obj.type)
- return nil
+ error('Can not find out root:' .. tostring(obj.type))
end
obj = parent
end
@@ -442,30 +441,6 @@ function m.getENV(source, start)
or m.getLocal(source, '@fenv', start)
end
---- 寻找函数的不定参数,返回不定参在第几个参数上,以及该参数对象。
---- 如果函数是主函数,则返回`0, nil`。
----@return table
----@return integer
-function m.getFunctionVarArgs(func)
- if func.type == 'main' then
- return 0, nil
- end
- if func.type ~= 'function' then
- return nil, nil
- end
- local args = func.args
- if not args then
- return nil, nil
- end
- for i = 1, #args do
- local arg = args[i]
- if arg.type == '...' then
- return i, arg
- end
- end
- return nil, nil
-end
-
--- 获取指定区块中可见的局部变量
---@param source parser.object
---@param name string # 变量名
@@ -535,25 +510,25 @@ function m.getVisibleLocals(block, pos)
end
--- 获取指定区块中可见的标签
----@param block table
----@param name string {comment = '标签名'}
+---@param block parser.object
+---@param name string
function m.getLabel(block, name)
- block = m.getBlock(block)
+ local current = m.getBlock(block)
for _ = 1, 10000 do
- if not block then
+ if not current then
return nil
end
- local labels = block.labels
+ local labels = current.labels
if labels then
local label = labels[name]
if label then
return label
end
end
- if block.type == 'function' then
+ if current.type == 'function' then
return nil
end
- block = m.getParentBlock(block)
+ current = m.getParentBlock(current)
end
error('guide.getLocal overstack')
end
diff --git a/script/parser/luadoc.lua b/script/parser/luadoc.lua
index c1a8d19c..af177223 100644
--- a/script/parser/luadoc.lua
+++ b/script/parser/luadoc.lua
@@ -5,7 +5,11 @@ local compile = require 'parser.compile'
local util = require 'utility'
local TokenTypes, TokenStarts, TokenFinishs, TokenContents, TokenMarks
-local Ci, Offset, pushWarning, NextComment, Lines
+---@type integer
+local Ci
+---@type integer
+local Offset
+local pushWarning, NextComment, Lines
local parseType, parseTypeUnit
---@type any
local Parser = re.compile([[
@@ -159,8 +163,8 @@ local function peekToken()
return TokenTypes[Ci+1], TokenContents[Ci+1]
end
----@return string tokenType
----@return string tokenContent
+---@return string? tokenType
+---@return string? tokenContent
local function nextToken()
Ci = Ci + 1
if not TokenTypes[Ci] then
diff --git a/script/workspace/scope.lua b/script/workspace/scope.lua
index 85053612..a81d638c 100644
--- a/script/workspace/scope.lua
+++ b/script/workspace/scope.lua
@@ -85,7 +85,7 @@ function mt:getLinkedUri(uri)
end
---@param uri uri
----@return uri
+---@return uri?
function mt:getRootUri(uri)
if self:isChildUri(uri) then
return self.uri
diff --git a/script/workspace/workspace.lua b/script/workspace/workspace.lua
index d24cbfe9..6425af06 100644
--- a/script/workspace/workspace.lua
+++ b/script/workspace/workspace.lua
@@ -354,9 +354,6 @@ end
---@param path string
---@return string
function m.normalize(path)
- if not path then
- return nil
- end
path = path:gsub('%$%{(.-)%}', function (key)
if key == '3rd' then
return (ROOT / 'meta' / '3rd'):string()
@@ -379,7 +376,7 @@ function m.normalize(path)
return path
end
----@return string
+---@return string?
function m.getAbsolutePath(folderUri, path)
if not path or path == '' then
return nil