Skip to content

Commit d827a71

Browse files
authored
improve jsdoc conversion (#1346)
1 parent 52050c7 commit d827a71

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ async function convert_to_ts(js_code: string, indent = '', offset = '') {
417417
const code = new MagicString(js_code);
418418
const imports = new Map();
419419

420-
async function walk(node: ts.Node) {
420+
async function walk(node: ts.Node, prev: ts.Node | null) {
421421
const jsdoc = get_jsdoc(node);
422422

423423
if (jsdoc) {
@@ -563,19 +563,30 @@ async function convert_to_ts(js_code: string, indent = '', offset = '') {
563563
while (start > 0 && code.original[start - 1] === '\t') start -= 1;
564564
while (start > 0 && code.original[start - 1] === '\n') start -= 1;
565565

566-
const slice = code.original.slice(node.getStart(), node.getEnd());
567-
const is_multiline = slice.includes('\n');
566+
let is_multiline = false;
567+
568+
if (prev) {
569+
is_multiline =
570+
code.original.slice(prev.getStart(), prev.getEnd()).includes('\n') ||
571+
code.original.slice(node.getStart(), node.getEnd()).includes('\n');
572+
}
568573

569574
code.overwrite(start, end, is_multiline ? '\n' : '');
570575
}
571576
}
572577

578+
// the TypeScript API is such a hot mess, AFAICT there is no non-stupid way
579+
// to get the previous sibling within the visitor, so since we need it we
580+
// have to pass it in from the parent visitor
581+
let child_prev: ts.Node | null = null;
582+
573583
for (const child_node of node.getChildren()) {
574-
await walk(child_node);
584+
await walk(child_node, child_prev);
585+
child_prev = child_node;
575586
}
576587
}
577588

578-
await walk(ast);
589+
await walk(ast, null);
579590

580591
if (imports.size) {
581592
const import_statements = Array.from(imports.entries())

0 commit comments

Comments
 (0)