diff --git a/packages/site-kit/src/lib/markdown/renderer.ts b/packages/site-kit/src/lib/markdown/renderer.ts index 23487fb216..74117ca5ef 100644 --- a/packages/site-kit/src/lib/markdown/renderer.ts +++ b/packages/site-kit/src/lib/markdown/renderer.ts @@ -420,13 +420,13 @@ async function convert_to_ts(js_code: string, indent = '', offset = '') { for (const tag of tags) { if (ts.isJSDocTypeTag(tag)) { - type = get_type_info(tag.typeExpression); + type = get_type_info(get_jsdoc_type_expression_text(tag.getText())); } else if (ts.isJSDocParameterTag(tag)) { - params.push(get_type_info(tag.typeExpression!)); + params.push(get_type_info(tag.typeExpression?.getText()!)); } else if (ts.isJSDocReturnTag(tag)) { - returns = get_type_info(tag.typeExpression!); + returns = get_type_info(tag.typeExpression?.getText()!); } else if (ts.isJSDocSatisfiesTag(tag)) { - satisfies = get_type_info(tag.typeExpression!); + satisfies = get_type_info(tag.typeExpression?.getText()!); } else { throw new Error('Unhandled tag'); } @@ -546,10 +546,9 @@ async function convert_to_ts(js_code: string, indent = '', offset = '') { return transformed === js_code ? undefined : transformed; - function get_type_info(expression: ts.JSDocTypeExpression) { - const type = expression - ?.getText()! - .slice(1, -1) // remove surrounding `{` and `}` + function get_type_info(text: string) { + const type = text + .replace(/^\{|\}$/g, '') // remove surrounding `{` and `}` .replace(/ \* ?/gm, '') .replace(/import\('(.+?)'\)\.(\w+)(?:(<.+>))?/gms, (_, source, name, args = '') => { const existing = imports.get(source); @@ -564,6 +563,10 @@ async function convert_to_ts(js_code: string, indent = '', offset = '') { return type; } + + function get_jsdoc_type_expression_text(text: string): string { + return text.replace(/^@type\s*/, '').trim(); + } } function find_nearest_node_modules(file: string): string | null {