|
| 1 | +local files = require 'files' |
| 2 | +local guide = require 'parser.guide' |
| 3 | +local vm = require 'vm' |
| 4 | +local lang = require 'language' |
| 5 | + |
| 6 | +---@param uri uri |
| 7 | +---@param func parser.object |
| 8 | +local function hasDocReturn(uri, func) |
| 9 | + if not func.bindDocs then |
| 10 | + return false |
| 11 | + end |
| 12 | + for _, doc in ipairs(func.bindDocs) do |
| 13 | + if doc.type == 'doc.return' then |
| 14 | + -- don't need return with only one `any` |
| 15 | + local lastReturn = doc.returns[#doc.returns] |
| 16 | + if lastReturn.returnIndex ~= 1 |
| 17 | + or vm.getInfer(lastReturn):view(uri) ~= 'any' then |
| 18 | + return true |
| 19 | + end |
| 20 | + end |
| 21 | + end |
| 22 | + return false |
| 23 | +end |
| 24 | + |
| 25 | +---@param block parser.object |
| 26 | +---@return boolean |
| 27 | +local function hasReturn(block) |
| 28 | + if block.hasReturn or block.hasError then |
| 29 | + return true |
| 30 | + end |
| 31 | + if block.type == 'if' then |
| 32 | + local hasElse |
| 33 | + for _, subBlock in ipairs(block) do |
| 34 | + if not hasReturn(subBlock) then |
| 35 | + return false |
| 36 | + end |
| 37 | + if subBlock.type == 'elseblock' then |
| 38 | + hasElse = true |
| 39 | + end |
| 40 | + end |
| 41 | + return hasElse == true |
| 42 | + else |
| 43 | + if block.type == 'while' then |
| 44 | + if vm.testCondition(block.filter) then |
| 45 | + return true |
| 46 | + end |
| 47 | + end |
| 48 | + for _, action in ipairs(block) do |
| 49 | + if guide.isBlockType(action) then |
| 50 | + if hasReturn(action) then |
| 51 | + return true |
| 52 | + end |
| 53 | + end |
| 54 | + end |
| 55 | + end |
| 56 | + return false |
| 57 | +end |
| 58 | + |
| 59 | +return function (uri, callback) |
| 60 | + local state = files.getState(uri) |
| 61 | + if not state then |
| 62 | + return |
| 63 | + end |
| 64 | + |
| 65 | + guide.eachSourceType(state.ast, 'function', function (source) |
| 66 | + -- check declare only |
| 67 | + if #source == 0 then |
| 68 | + return |
| 69 | + end |
| 70 | + if not hasDocReturn(uri, source) then |
| 71 | + return |
| 72 | + end |
| 73 | + if hasReturn(source) then |
| 74 | + return |
| 75 | + end |
| 76 | + local lastAction = source[#source] |
| 77 | + local finish = lastAction.range or lastAction.finish |
| 78 | + callback { |
| 79 | + start = finish, |
| 80 | + finish = finish, |
| 81 | + message = lang.script('DIAG_MISSING_RETURN'), |
| 82 | + } |
| 83 | + end) |
| 84 | +end |
0 commit comments