feat: manage Object Properties constraints enum
ci/woodpecker/pr/lint Pipeline was successful Details
ci/woodpecker/pr/build Pipeline was successful Details

This commit is contained in:
Aire-One 2022-10-31 15:30:40 +01:00
parent 9d9f263c69
commit 35c4567baf
4 changed files with 36 additions and 22 deletions

View File

@ -7,6 +7,8 @@ local record Variable_Info
name: string
types: List<string>
constraints: List<string>
end
local __Variable_Info: metatable<Variable_Info> = {

View File

@ -45,12 +45,6 @@ function snippets.render_record_functions(items: List<Function_Info.Function_Inf
end):concat("\n")
end
function snippets.render_record_properties(items: List<Variable_Info.Variable_Info>): string
return items:map(function(item: Variable_Info.Variable_Info): string
return snippets.render_typed_variable(item.name, item.types)
end):concat("\n")
end
function snippets.render_enum(name: string, values: List<string>): string
local tmpl = [[
enum $(name)
@ -69,4 +63,18 @@ end
return utils.do_or_fail(template.substitute, tmpl, tmpl_args)
end
function snippets.render_record_properties(items: List<Variable_Info.Variable_Info>): string
return items:map(function(item: Variable_Info.Variable_Info): string
if not item.constraints or #item.constraints == 0 then
return snippets.render_typed_variable(item.name, item.types)
end
local enum_type = utils.capitalize(item.name)
return string.format(
"%s%s",
snippets.render_enum(enum_type, item.constraints),
snippets.render_typed_variable(item.name, List({ enum_type })))
end):concat("\n")
end
return snippets

View File

@ -55,18 +55,12 @@ local function extract_function_return_types(function_return_types_node: scan.HT
return scraper_utils.scrape(html, selector, extract_node_text)
end
local function extract_property_types(property_summary_type: scan.HTMLNode, property_constraint_node: scan.HTMLNode): List<string>
-- local constraint_types = scraper_utils.scrape(
-- property_constraint_node:outer_html(),
-- "tr.see_also_sublist",
-- extract_node_text
-- )
-- if #constraint_types == 0 then
return parse_parameter_types(extract_node_text(property_summary_type))
-- end
-- return constraint_types
local function extract_property_constraints(property_constraint_node: scan.HTMLNode): { string }
return scraper_utils.scrape(
property_constraint_node:outer_html(),
"tr.see_also_sublist",
extract_node_text
)
end
local function extract_section_functions(dl: string): { Function_Info.Function_Info }
@ -111,9 +105,15 @@ local function extract_section_variables(dl: string): { Variable_Info.Variable_I
local variable_info = Variable_Info()
variable_info.name = extract_item_name(nodes[query_selectors.variable_name])
variable_info.types = extract_property_types(
nodes[query_selectors.variable_summary_type],
nodes[query_selectors.variable_property_constraint])
variable_info.types = parse_parameter_types(extract_node_text(nodes[query_selectors.variable_summary_type]))
if variable_info.types:contains("string") then
variable_info.constraints = List(extract_property_constraints(nodes[query_selectors.variable_property_constraint])):map(
function(constraint: string): string
return (constraint:gsub("&quot;", ""))
end
)
end
return variable_info
end
@ -139,7 +139,7 @@ function module.get_doc_from_page(html: string, module_name: string): Module_Doc
end
local module_doc = Module_Doc()
module_doc.record_name = module_name:gsub(".*%.", ""):gsub("^%l", string.upper)
module_doc.record_name = utils.capitalize((module_name:gsub(".*%.", "")))
for i = 1, #nodes:get("h2.section-header") do
local h2 = nodes:get("h2.section-header")[i]

View File

@ -39,6 +39,10 @@ function utils.sanitize_string(s: string): string
return (stringx.strip(web_sanitize.extract_text(s)))
end
function utils.capitalize(s: string): string
return (s:gsub("^%l", string.upper))
end
-- At some point, we should probably write a wrapper to make penlight's function work with pcalls.
function utils.do_or_fail<T>(func: function<T>(...: any): (T | nil, string), ...: any): T
local logger = require "logger"