| 1 | import { addComment, type Node } from "@babel/types"; |
| 2 | import type { NodePath } from "@babel/traverse"; |
| 3 | |
| 4 | const PURE_ANNOTATION = "#__PURE__"; |
| 5 | |
| 6 | const isPureAnnotated = ({ leadingComments }: Node): boolean => |
| 7 | !!leadingComments && |
| 8 | leadingComments.some(comment => /[@#]__PURE__/.test(comment.value)); |
| 9 | |
| 10 | export default function annotateAsPure(pathOrNode: Node | NodePath): void { |
| 11 | const node = |
| 12 | // @ts-expect-error Node will not have `node` property |
| 13 | (pathOrNode["node"] || pathOrNode) as Node; |
| 14 | if (isPureAnnotated(node)) { |
| 15 | return; |
| 16 | } |
| 17 | addComment(node, "leading", PURE_ANNOTATION); |
| 18 | } |