Skip to content

Commit aa7e00e

Browse files
domuk-kRich-Harris
andauthored
fix: replace incorrect jsdoc typeExpression in AST traversal (#1018)
* fix: replace incorrect jsdoc typeExpression in AST traversal - the typeExpression of a JSDocTypeTag is truncated due to comment syntax. * snake_case --------- Co-authored-by: Rich Harris <[email protected]>
1 parent 9985efa commit aa7e00e

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

packages/site-kit/src/lib/markdown/renderer.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -420,13 +420,13 @@ async function convert_to_ts(js_code: string, indent = '', offset = '') {
420420

421421
for (const tag of tags) {
422422
if (ts.isJSDocTypeTag(tag)) {
423-
type = get_type_info(tag.typeExpression);
423+
type = get_type_info(get_jsdoc_type_expression_text(tag.getText()));
424424
} else if (ts.isJSDocParameterTag(tag)) {
425-
params.push(get_type_info(tag.typeExpression!));
425+
params.push(get_type_info(tag.typeExpression?.getText()!));
426426
} else if (ts.isJSDocReturnTag(tag)) {
427-
returns = get_type_info(tag.typeExpression!);
427+
returns = get_type_info(tag.typeExpression?.getText()!);
428428
} else if (ts.isJSDocSatisfiesTag(tag)) {
429-
satisfies = get_type_info(tag.typeExpression!);
429+
satisfies = get_type_info(tag.typeExpression?.getText()!);
430430
} else {
431431
throw new Error('Unhandled tag');
432432
}
@@ -546,10 +546,9 @@ async function convert_to_ts(js_code: string, indent = '', offset = '') {
546546

547547
return transformed === js_code ? undefined : transformed;
548548

549-
function get_type_info(expression: ts.JSDocTypeExpression) {
550-
const type = expression
551-
?.getText()!
552-
.slice(1, -1) // remove surrounding `{` and `}`
549+
function get_type_info(text: string) {
550+
const type = text
551+
.replace(/^\{|\}$/g, '') // remove surrounding `{` and `}`
553552
.replace(/ \* ?/gm, '')
554553
.replace(/import\('(.+?)'\)\.(\w+)(?:(<.+>))?/gms, (_, source, name, args = '') => {
555554
const existing = imports.get(source);
@@ -564,6 +563,10 @@ async function convert_to_ts(js_code: string, indent = '', offset = '') {
564563

565564
return type;
566565
}
566+
567+
function get_jsdoc_type_expression_text(text: string): string {
568+
return text.replace(/^@type\s*/, '').trim();
569+
}
567570
}
568571

569572
function find_nearest_node_modules(file: string): string | null {

0 commit comments

Comments
 (0)