feat: Node.return_types should be a { { string } }
ci/woodpecker/pr/docker-build Pipeline was successful Details
ci/woodpecker/pr/lint Pipeline was successful Details
ci/woodpecker/pr/build-and-run Pipeline failed Details
ci/woodpecker/pr/test Pipeline failed Details

`{ { string } }` means we now have an array of strings lists AKA 2
levels array to define unions of positional types for the return types.

Basically, we can now define a function that returns something like
`A | B, C | D`
This commit is contained in:
Aire-One 2023-12-02 01:16:01 +01:00
parent d29d2eab24
commit 9e5820db7f
5 changed files with 49 additions and 11 deletions

View File

@ -16,6 +16,17 @@ local function render_types(types: { string }, separator: string, with_colon_pre
table.concat(types, separator)) table.concat(types, separator))
end end
local function render_function_return_types(types: { { string } }): string
if not types or #types == 0 then
return ""
end
local generated = ": "
for _, t in ipairs(types) do
generated = generated .. render_types(t, ", ")
end
return generated
end
local function dedent(str: string): string local function dedent(str: string): string
return stringx.dedent(str):sub(1, -2) return stringx.dedent(str):sub(1, -2)
end end
@ -137,7 +148,8 @@ local node_printer <total>: { Node.Token : Node_Printer_Function } = {
"%s: function(%s)%s", "%s: function(%s)%s",
node.name, node.name,
table.concat(args, ", "), table.concat(args, ", "),
render_types(node.return_types, ", ", true)), render_function_return_types(node.return_types)
),
indent_level), indent_level indent_level), indent_level
end, end,
}, },

View File

@ -103,8 +103,24 @@ local function extract_function_parameters(table_html: string, function_name: st
return parameters, parameters_types return parameters, parameters_types
end end
local function extract_function_return_types(ol_html: string): { string } local function extract_function_return_types(ol_html: string): { { string } }
return scraper_utils.scrape(ol_html, "span.types .type", extract_node_text) local raw_types = scraper_utils.scrape(
ol_html,
"span.types .type",
extract_node_text)
local types: { { string } } = {}
for _,raw_t in ipairs(raw_types) do
local ts: { string } = {}
for t in stringx.split(raw_t)
:filter(
function(s: string): boolean
return s ~= "or" and s ~= ","
end):iter() do
table.insert(ts, t)
end
end
return types
end end
local function extract_property_constraints(property_constraint_node: scan.HTMLNode): { string } local function extract_property_constraints(property_constraint_node: scan.HTMLNode): { string }

View File

@ -20,7 +20,7 @@ local record Node
-- for "function" and "metamethod" -- for "function" and "metamethod"
parameters: { Node } parameters: { Node }
return_types: { string } return_types: { { string } }
-- for "module" -- for "module"
module_path: string module_path: string

View File

@ -32,11 +32,17 @@ local function get_all_types_in_node(node: Node): { string }
spread(parameters_types, v.types) spread(parameters_types, v.types)
end end
end end
local return_types = {}
if node.return_types then
for _, rts in ipairs(node.return_types) do
spread(return_types, rts)
end
end
return spread( return spread(
{}, {},
node.types or {}, node.types or {},
node.return_types or {}, return_types,
parameters_types or {}) parameters_types)
end end
local function replace_in_node_type(node: Node, old_type: string, new_type: string) local function replace_in_node_type(node: Node, old_type: string, new_type: string)
@ -59,9 +65,11 @@ local function replace_in_node_type(node: Node, old_type: string, new_type: stri
end end
if node.return_types then if node.return_types then
for i, t in ipairs(node.return_types) do for i, rt in ipairs(node.return_types) do
if t == old_type then for j, t in ipairs(rt) do
node.return_types[i] = new_type if t == old_type then
node.return_types[i][j] = new_type
end
end end
end end
end end

View File

@ -65,8 +65,10 @@ local function check_function_returns(node: Node)
return return
end end
for i, ret in ipairs(node.return_types) do for i, ret_ts in ipairs(node.return_types) do
node.return_types[i] = get_type(ret) for j, t in ipairs(ret_ts) do
node.return_types[i][j] = get_type(t)
end
end end
end end