diff --git a/src/awesomewmdtl/printer/teal_type_definition.tl b/src/awesomewmdtl/printer/teal_type_definition.tl index 2635ab8..2563d73 100644 --- a/src/awesomewmdtl/printer/teal_type_definition.tl +++ b/src/awesomewmdtl/printer/teal_type_definition.tl @@ -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 : { 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, }, diff --git a/src/awesomewmdtl/scraper/module_doc.tl b/src/awesomewmdtl/scraper/module_doc.tl index f3c2934..5063d99 100644 --- a/src/awesomewmdtl/scraper/module_doc.tl +++ b/src/awesomewmdtl/scraper/module_doc.tl @@ -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 } diff --git a/src/awesomewmdtl/types/Node.tl b/src/awesomewmdtl/types/Node.tl index 87f5047..f0bba53 100644 --- a/src/awesomewmdtl/types/Node.tl +++ b/src/awesomewmdtl/types/Node.tl @@ -20,7 +20,7 @@ local record Node -- for "function" and "metamethod" parameters: { Node } - return_types: { string } + return_types: { { string } } -- for "module" module_path: string diff --git a/src/awesomewmdtl/visitors/module_dependencies.tl b/src/awesomewmdtl/visitors/module_dependencies.tl index cc1b9f7..2564038 100644 --- a/src/awesomewmdtl/visitors/module_dependencies.tl +++ b/src/awesomewmdtl/visitors/module_dependencies.tl @@ -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 diff --git a/src/awesomewmdtl/visitors/type_mapping.tl b/src/awesomewmdtl/visitors/type_mapping.tl index 0a295c3..bcdc306 100644 --- a/src/awesomewmdtl/visitors/type_mapping.tl +++ b/src/awesomewmdtl/visitors/type_mapping.tl @@ -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