Skip to content

Commit

Permalink
fix #1057
Browse files Browse the repository at this point in the history
  • Loading branch information
sumneko committed Apr 16, 2022
1 parent cd6b80d commit fa518d4
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 33 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* `CHG` hover: split `local` into `local` / `parameter` / `upvalue` / `self`.
* `CHG` hover: added parentheses to some words, such as `global` / `field` / `class`.
* `FIX` definition of `table<k, v>`
* `FIX` [#1057](https://github.com/sumneko/lua-language-server/issues/1057)
* `FIX` runtime errors reported by telemetry, see [#1058](https://github.com/sumneko/lua-language-server/issues/1058)

## 3.0.2
Expand Down
8 changes: 4 additions & 4 deletions script/core/completion/completion.lua
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ local function checkModule(state, word, position, results)
goto CONTINUE
end
if targetSource.type == 'getlocal'
and vm.isDeprecated(targetSource.node) then
and vm.getDeprecated(targetSource.node) then
goto CONTINUE
end
results[#results+1] = {
Expand Down Expand Up @@ -495,7 +495,7 @@ local function checkFieldThen(state, name, src, word, startPos, position, parent
kind = kind,
match = name:match '^[^(]+',
insertText = name:match '^[^(]+',
deprecated = vm.isDeprecated(src) or nil,
deprecated = vm.getDeprecated(src) and true or nil,
id = stack(function () ---@async
return {
detail = buildDetail(src),
Expand Down Expand Up @@ -526,7 +526,7 @@ local function checkFieldThen(state, name, src, word, startPos, position, parent
results[#results+1] = {
label = name,
kind = kind,
deprecated = vm.isDeprecated(src) or nil,
deprecated = vm.getDeprecated(src) and true or nil,
textEdit = textEdit,
id = stack(function () ---@async
return {
Expand Down Expand Up @@ -588,7 +588,7 @@ local function checkFieldOfRefs(refs, state, word, startPos, position, parent, o
count = count + 1
goto CONTINUE
end
if vm.isDeprecated(src) then
if vm.getDeprecated(src) then
goto CONTINUE
end
if guide.isSet(src) then
Expand Down
26 changes: 10 additions & 16 deletions script/core/diagnostics/deprecated.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ local guide = require 'parser.guide'
local config = require 'config'
local define = require 'proto.define'
local await = require 'await'
local util = require 'utility'

local types = {'getglobal', 'getfield', 'getindex', 'getmethod'}
---@async
Expand All @@ -16,7 +17,6 @@ return function (uri, callback)

local dglobals = config.get(uri, 'Lua.diagnostics.globals')
local rspecial = config.get(uri, 'Lua.runtime.special')
local cache = {}

guide.eachSourceTypes(ast.ast, types, function (src) ---@async
if src.type == 'getglobal' then
Expand All @@ -34,28 +34,17 @@ return function (uri, callback)

await.delay()

if not vm.isDeprecated(src, true) then
local deprecated = vm.getDeprecated(src, true)
if not deprecated then
return
end

await.delay()

local defs = vm.getDefs(src)
local validVersions
for _, def in ipairs(defs) do
if def.bindDocs then
for _, doc in ipairs(def.bindDocs) do
if doc.type == 'doc.version' then
validVersions = vm.getValidVersions(doc)
break
end
end
end
end

local message = lang.script.DIAG_DEPRECATED
local versions
if validVersions then
if deprecated.type == 'doc.version' then
local validVersions = vm.getValidVersions(deprecated)
versions = {}
for version, valid in pairs(validVersions) do
if valid then
Expand All @@ -71,6 +60,11 @@ return function (uri, callback)
)
end
end
if deprecated.type == 'doc.deprecated' then
if deprecated.comment then
message = ('%s(%s)'):format(message, util.trim(deprecated.comment.text))
end
end

callback {
start = src.start,
Expand Down
6 changes: 3 additions & 3 deletions script/utility.lua
Original file line number Diff line number Diff line change
Expand Up @@ -640,12 +640,12 @@ end
---@return string
function m.trim(str, mode)
if mode == "left" then
return str:gsub('^%s+', '')
return (str:gsub('^%s+', ''))
end
if mode == "right" then
return str:gsub('%s+$', '')
return (str:gsub('%s+$', ''))
end
return str:match '^%s*(%S+)%s*$'
return (str:match '^%s*(.-)%s*$')
end

function m.expandPath(path)
Expand Down
31 changes: 21 additions & 10 deletions script/vm/doc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ function vm.getValidVersions(doc)
return valids
end

local function isDeprecated(value)
---@return parser.object?
local function getDeprecated(value)
if not value.bindDocs then
return false
end
Expand All @@ -94,34 +95,44 @@ local function isDeprecated(value)
end
for _, doc in ipairs(value.bindDocs) do
if doc.type == 'doc.deprecated' then
value._deprecated = true
return true
value._deprecated = doc
return doc
elseif doc.type == 'doc.version' then
local valids = vm.getValidVersions(doc)
if not valids[config.get(guide.getUri(value), 'Lua.runtime.version')] then
value._deprecated = true
return true
value._deprecated = doc
return doc
end
end
end
value._deprecated = false
return false
end

function vm.isDeprecated(value, deep)
---@return parser.object?
function vm.getDeprecated(value, deep)
if deep then
local defs = vm.getDefs(value)
if #defs == 0 then
return false
end
local deprecated = false
for _, def in ipairs(defs) do
if not isDeprecated(def) then
return false
if def.type == 'setglobal'
or def.type == 'setfield'
or def.type == 'setmethod'
or def.type == 'setindex'
or def.type == 'tablefield'
or def.type == 'tableindex' then
deprecated = getDeprecated(def)
if not deprecated then
return false
end
end
end
return true
return deprecated
else
return isDeprecated(value)
return getDeprecated(value)
end
end

Expand Down
8 changes: 8 additions & 0 deletions test/diagnostics/common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1387,3 +1387,11 @@ local value
value = '1'
value = value:gsub()
]]

TEST [[
T = {}
---@deprecated # comment
T.x = 1
print(<!T.x!>)
]]

0 comments on commit fa518d4

Please sign in to comment.