Debian Codegraph @

packages/babel-traverse/src/path/comments.ts

raw symbols SCIP
1 // This file contains methods responsible for dealing with comments.
2 import type * as t from "@babel/types";
3 import type NodePath from "./index";
4 import {
5 addComment as _addComment,
6 addComments as _addComments,
7 } from "@babel/types";
8
9 /**
10 * Share comments amongst siblings.
11 */
12
13 export function shareCommentsWithSiblings(this: NodePath) {
14 // NOTE: this assumes numbered keys
15 if (typeof this.key === "string") return;
16
17 const node = this.node;
18 if (!node) return;
19
20 const trailing = node.trailingComments;
21 const leading = node.leadingComments;
22 if (!trailing && !leading) return;
23
24 const prev = this.getSibling(this.key - 1);
25 const next = this.getSibling(this.key + 1);
26 const hasPrev = Boolean(prev.node);
27 const hasNext = Boolean(next.node);
28 if (hasPrev && !hasNext) {
29 prev.addComments("trailing", trailing);
30 } else if (hasNext && !hasPrev) {
31 next.addComments("leading", leading);
32 }
33 }
34
35 export function addComment(
36 this: NodePath,
37 type: t.CommentTypeShorthand,
38 content: string,
39 line?: boolean,
40 ) {
41 _addComment(this.node, type, content, line);
42 }
43
44 /**
45 * Give node `comments` of the specified `type`.
46 */
47
48 export function addComments(
49 this: NodePath,
50 type: t.CommentTypeShorthand,
51 comments: t.Comment[],
52 ) {
53 _addComments(this.node, type, comments);
54 }