Skip to content

Commit a324f57

Browse files
authored
fix: convert type assertions (#1204)
* fix: convert type assertions * fix
1 parent 2c961b0 commit a324f57

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -523,16 +523,30 @@ async function convert_to_ts(js_code: string, indent = '', offset = '') {
523523
while (code.original[start - 1] !== ')') start -= 1;
524524
code.appendLeft(start, `: ${returns}`);
525525
}
526+
} else if (type && ts.isParenthesizedExpression(node)) {
527+
// convert `/* @type {Foo} */ (foo)` to `foo as Foo`
528+
// TODO one day we may need to account for operator precedence
529+
// (i.e. preserve the parens in e.g. `(x as y).z()`)
530+
let start = node.getStart();
531+
while (js_code[start - 1] !== '/') start -= 1;
532+
code.remove(start, node.getStart() + 1);
533+
534+
let end = node.getEnd();
535+
code.overwrite(end - 1, end, ` as ${type}`);
526536
} else {
527-
throw new Error('Unhandled @type JsDoc->TS conversion: ' + js_code);
537+
throw new Error(
538+
'Unhandled @type JsDoc->TS conversion: ' + js_code.slice(node.getStart(), node.getEnd())
539+
);
528540
}
529541

530542
if (!comment) {
531543
// remove the whole thing
532544
let start = jsdoc[0].getStart();
533545
let end = jsdoc[0].getEnd();
534546

535-
while (start > 0 && code.original[start] !== '\n') start -= 1;
547+
while (start > 0 && code.original[start - 1] === '\t') start -= 1;
548+
while (start > 0 && code.original[start - 1] === '\n') start -= 1;
549+
536550
code.overwrite(start, end, '');
537551
}
538552
}
@@ -560,7 +574,7 @@ async function convert_to_ts(js_code: string, indent = '', offset = '') {
560574
while (js_code[i] !== '\n') i += 1;
561575
i += 1;
562576

563-
code.appendLeft(i, import_statements + '\n');
577+
code.appendLeft(i, '\n' + import_statements + '\n');
564578
} else {
565579
code.prependLeft(0, offset + import_statements + '\n');
566580
}

0 commit comments

Comments
 (0)