aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/gen_annotations.lua
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/gen_annotations.lua')
-rw-r--r--scripts/gen_annotations.lua30
1 files changed, 19 insertions, 11 deletions
diff --git a/scripts/gen_annotations.lua b/scripts/gen_annotations.lua
index 1612d82b..1ca3d100 100644
--- a/scripts/gen_annotations.lua
+++ b/scripts/gen_annotations.lua
@@ -255,25 +255,33 @@ local function lua_type_for(prop)
' | '
)
end
+ if prop.const ~= nil then
+ return vim.inspect(prop.const)
+ end
local types = type(prop.type) == 'table' and prop.type or { prop.type }
- if vim.tbl_isempty(types) and type(prop.anyOf) == 'table' then
- return table.concat(
- vim.tbl_map(function(p)
- return lua_type_for(p)
- end, prop.anyOf),
- '|'
- )
+ -- Convert `anyOf`/`oneOf` to a union, excluding null:
+ -- `{ anyOf = { { type = 'string' }, { type = 'number' }, { type = 'null' } } }` => `string|number`.
+ local alternatives = prop.anyOf or prop.oneOf
+ if vim.tbl_isempty(types) and type(alternatives) == 'table' then
+ local alternative_types = {}
+ for _, alternative in ipairs(alternatives) do
+ if alternative.type ~= 'null' then
+ table.insert(alternative_types, lua_type_for(alternative))
+ end
+ end
+ return #alternative_types > 0 and table.concat(alternative_types, '|') or 'any'
end
types = vim.tbl_map(function(t)
if t == 'null' then
return
end
if t == 'array' then
- if prop.items and prop.items.type then
- if type(prop.items.type) == 'table' then
- return 'any[]'
+ if type(prop.items) == 'table' then
+ local item_type = lua_type_for(prop.items)
+ if item_type:find('|', 1, true) then
+ item_type = '(' .. item_type .. ')'
end
- return (prop.items.type == 'object' and 'table' or prop.items.type) .. '[]'
+ return item_type .. '[]'
end
return 'any[]'
end