-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathutil.mts
More file actions
28 lines (24 loc) · 801 Bytes
/
util.mts
File metadata and controls
28 lines (24 loc) · 801 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import ts from "typescript";
export function addJSDocToNode<T extends ts.Node>(
node: T,
jsDoc: string | undefined
): T {
if (!jsDoc) {
return node;
}
// replace the first /** with *
// we do this because ts.addSyntheticLeadingComment will add /* to the beginning but we want /**
const removedFirstLine = jsDoc.trim().replace(/^\/\*\*/, "*");
// remove the last */ because ts.addSyntheticLeadingComment will add it
const removedSecondLine = removedFirstLine.replace(/\*\/$/, "");
const split = removedSecondLine.split("\n");
const trimmed = split.map((line) => line.trim());
const joined = trimmed.join("\n");
const nodeWithJSDoc = ts.addSyntheticLeadingComment(
node,
ts.SyntaxKind.MultiLineCommentTrivia,
joined,
true
);
return nodeWithJSDoc;
}