Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions packages/site-kit/src/lib/markdown/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down Expand Up @@ -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);
Expand All @@ -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 {
Expand Down