diff --git a/cli/codegen.ts b/cli/codegen.ts index 396e09e..e31189c 100644 --- a/cli/codegen.ts +++ b/cli/codegen.ts @@ -37,9 +37,12 @@ export const voidElements = [ "source", "track", "wbr", -]; +] as const; -const voidElementSet = new Set(voidElements); +/** + * voidElementSet is a set of all void elements in HTML. + */ +const voidElementSet = new Set(voidElements); /** * isVoid returns true if the given tag is a void element. @@ -230,7 +233,12 @@ if (import.meta.main) { } } -interface Descriptor { +/** + * Descriptor represents the data for an HTML element. + * + * From this data, we can generate a TypeScript file for each element. + */ +export interface Descriptor { tag: string; functionName: string; propsInterfaceName: string; @@ -266,16 +274,25 @@ export function getDescriptors(): Descriptor[] { return descriptors; } -function getAttrs(tag: string): string[] { +/** + * getAttrs returns the attributes for the given HTML element tag. + */ +export function getAttrs(tag: string): string[] { return Object.keys(bcd.html.elements[tag]) .filter((attr) => !attr.includes("_")); } -function capitalize(s: string): string { +/** + * capitalize capitalizes the first letter of the given string. + */ +export function capitalize(s: string): string { return s.charAt(0).toUpperCase() + s.slice(1); } -function toFunctionName(s: string): string { +/** + * toFunctionName converts the given string to a valid function name. + */ +export function toFunctionName(s: string): string { // Handle special cases to avoid conflicts. if (s === "var") { return "var_"; @@ -284,12 +301,15 @@ function toFunctionName(s: string): string { return s; } -function toDocs(data: { +/** + * toDocs is a helper function that converts the given data to a JSDoc comment. + */ +export function toDocs(data: { description?: string; see?: string; isDeprecated?: boolean; isExperimental?: boolean; -}) { +}): Record[] | undefined { const doc: Record = {}; if (data.description) { doc.description = data.description;