| 1 |
import type * as t from ".."; |
| 2 |
|
| 3 |
/** |
| 4 |
* Add comments of certain type to a node. |
| 5 |
*/ |
| 6 |
export default function addComments<T extends t.Node>( |
| 7 |
node: T, |
| 8 |
type: t.CommentTypeShorthand, |
| 9 |
comments: Array<t.Comment>, |
| 10 |
): T { |
| 11 |
if (!comments || !node) return node; |
| 12 |
|
| 13 |
const key = `${type}Comments` as const; |
| 14 |
|
| 15 |
if (node[key]) { |
| 16 |
if (type === "leading") { |
| 17 |
node[key] = comments.concat(node[key]); |
| 18 |
} else { |
| 19 |
node[key].push(...comments); |
| 20 |
} |
| 21 |
} else { |
| 22 |
node[key] = comments; |
| 23 |
} |
| 24 |
|
| 25 |
return node; |
| 26 |
} |