|
| 1 | +local vm = require 'vm' |
| 2 | +local files = require 'files' |
| 3 | +local guide = require 'parser.guide' |
| 4 | +local await = require 'await' |
| 5 | + |
| 6 | +---@async |
| 7 | +return function (uri, callback) |
| 8 | + local state = files.getState(uri) |
| 9 | + if not state then |
| 10 | + return |
| 11 | + end |
| 12 | + |
| 13 | + ---@async |
| 14 | + guide.eachSourceType(state.ast, 'table', function (src) |
| 15 | + await.delay() |
| 16 | + |
| 17 | + local defs = vm.getDefs(src) |
| 18 | + local requiresKeys = {} |
| 19 | + for _, def in ipairs(defs) do |
| 20 | + if def.type == 'doc.class' then |
| 21 | + if not def.fields then |
| 22 | + goto continue |
| 23 | + end |
| 24 | + if def.bindSource then |
| 25 | + if guide.isInRange(def.bindSource, src.start) then |
| 26 | + goto continue |
| 27 | + end |
| 28 | + end |
| 29 | + for _, field in ipairs(def.fields) do |
| 30 | + if not field.optional |
| 31 | + and not vm.compileNode(field):isNullable() then |
| 32 | + local key = vm.getKeyName(field) |
| 33 | + if key and not requiresKeys[key] then |
| 34 | + requiresKeys[key] = true |
| 35 | + requiresKeys[#requiresKeys+1] = key |
| 36 | + end |
| 37 | + end |
| 38 | + end |
| 39 | + end |
| 40 | + ::continue:: |
| 41 | + end |
| 42 | + |
| 43 | + if #requiresKeys == 0 then |
| 44 | + return |
| 45 | + end |
| 46 | + |
| 47 | + local myKeys = {} |
| 48 | + for _, field in ipairs(src) do |
| 49 | + local key = vm.getKeyName(field) |
| 50 | + if key then |
| 51 | + myKeys[key] = true |
| 52 | + end |
| 53 | + end |
| 54 | + |
| 55 | + local missedKeys = {} |
| 56 | + for _, key in ipairs(requiresKeys) do |
| 57 | + if not myKeys[key] then |
| 58 | + missedKeys[#missedKeys+1] = ('`%s`'):format(key) |
| 59 | + end |
| 60 | + end |
| 61 | + |
| 62 | + if #missedKeys == 0 then |
| 63 | + return |
| 64 | + end |
| 65 | + |
| 66 | + callback { |
| 67 | + start = src.start, |
| 68 | + finish = src.finish, |
| 69 | + message = string.format('Missing fields: %s', table.concat(missedKeys, ', ')), |
| 70 | + } |
| 71 | + end) |
| 72 | +end |
0 commit comments