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))
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
return stringx.dedent(str):sub(1, -2)
end
@ -137,7 +148,8 @@ local node_printer <total>: { Node.Token : Node_Printer_Function } = {
"%s: function(%s)%s",
node.name,
table.concat(args, ", "),
render_types(node.return_types, ", ", true)),
render_function_return_types(node.return_types)
),
indent_level), indent_level
end,
},

View File

@ -103,8 +103,24 @@ local function extract_function_parameters(table_html: string, function_name: st
return parameters, parameters_types
end
local function extract_function_return_types(ol_html: string): { string }
return scraper_utils.scrape(ol_html, "span.types .type", extract_node_text)
local function extract_function_return_types(ol_html: string): { { string } }
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
local function extract_property_constraints(property_constraint_node: scan.HTMLNode): { string }

View File

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

View File

@ -32,11 +32,17 @@ local function get_all_types_in_node(node: Node): { string }
spread(parameters_types, v.types)
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(
{},
node.types or {},
node.return_types or {},
parameters_types or {})
return_types,
parameters_types)
end
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
if node.return_types then
for i, t in ipairs(node.return_types) do
if t == old_type then
node.return_types[i] = new_type
for i, rt in ipairs(node.return_types) do
for j, t in ipairs(rt) do
if t == old_type then
node.return_types[i][j] = new_type
end
end
end
end

View File

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