split out path comment methods into a separate file
This commit is contained in:
parent
fce977f1d7
commit
6c268cdf21
@ -8,3 +8,4 @@
|
||||
- `removal` - Methods responsible for removing a node.
|
||||
- `family` - Methods responsible for dealing with/retrieving children or siblings.
|
||||
- `introspection` - Methods responsible for introspecting the current path for certain values.
|
||||
- `comments` - Methods responsible for dealing with comments.
|
||||
|
||||
51
src/babel/traversal/path/comments.js
Normal file
51
src/babel/traversal/path/comments.js
Normal file
@ -0,0 +1,51 @@
|
||||
/**
|
||||
* Share comments amongst siblings.
|
||||
*/
|
||||
|
||||
export function shareCommentsWithSiblings() {
|
||||
var node = this.node;
|
||||
if (!node) return;
|
||||
|
||||
var trailing = node.trailingComments;
|
||||
var leading = node.leadingComments;
|
||||
if (!trailing && !leading) return;
|
||||
|
||||
var prev = this.getSibling(this.key - 1);
|
||||
var next = this.getSibling(this.key + 1);
|
||||
|
||||
if (!prev.node) prev = next;
|
||||
if (!next.node) next = prev;
|
||||
|
||||
prev.addComments("trailing", leading);
|
||||
next.addComments("leading", trailing);
|
||||
}
|
||||
|
||||
/**
|
||||
* Description
|
||||
*/
|
||||
|
||||
export function addComment(type, content, line?) {
|
||||
this.addComments(type, [{
|
||||
type: line ? "CommentLine" : "CommentBlock",
|
||||
value: content
|
||||
}]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Give node `comments` of the specified `type`.
|
||||
*/
|
||||
|
||||
export function addComments(type: string, comments: Array) {
|
||||
if (!comments) return;
|
||||
|
||||
var node = this.node;
|
||||
if (!node) return;
|
||||
|
||||
var key = `${type}Comments`;
|
||||
|
||||
if (node[key]) {
|
||||
node[key] = node[key].concat(comments);
|
||||
} else {
|
||||
node[key] = comments;
|
||||
}
|
||||
}
|
||||
@ -106,6 +106,7 @@ assign(NodePath.prototype, require("./context"));
|
||||
assign(NodePath.prototype, require("./removal"));
|
||||
assign(NodePath.prototype, require("./modification"));
|
||||
assign(NodePath.prototype, require("./family"));
|
||||
assign(NodePath.prototype, require("./comments"));
|
||||
|
||||
for (let type in virtualTypes) {
|
||||
if (type[0] === "_") continue;
|
||||
|
||||
@ -57,58 +57,3 @@ export function _assertUnremoved() {
|
||||
throw this.errorWithNode("NodePath has been removed so is read-only.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Description
|
||||
*/
|
||||
|
||||
export function addComment(type, content) {
|
||||
var node = this.node;
|
||||
var key = `${type}Comments`;
|
||||
var comments = node[key] = node[key] || [];
|
||||
comments.push({
|
||||
type: "CommentBlock",
|
||||
value: content
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Share comments amongst siblings.
|
||||
*/
|
||||
|
||||
export function shareCommentsWithSiblings() {
|
||||
var node = this.node;
|
||||
if (!node) return;
|
||||
|
||||
var trailing = node.trailingComments;
|
||||
var leading = node.leadingComments;
|
||||
if (!trailing && !leading) return;
|
||||
|
||||
var prev = this.getSibling(this.key - 1);
|
||||
var next = this.getSibling(this.key + 1);
|
||||
|
||||
if (!prev.node) prev = next;
|
||||
if (!next.node) next = prev;
|
||||
|
||||
prev.giveComments("trailing", leading);
|
||||
next.giveComments("leading", trailing);
|
||||
}
|
||||
|
||||
/**
|
||||
* Give node `comments` of the specified `type`.
|
||||
*/
|
||||
|
||||
export function giveComments(type: string, comments: Array) {
|
||||
if (!comments) return;
|
||||
|
||||
var node = this.node;
|
||||
if (!node) return;
|
||||
|
||||
var key = `${type}Comments`;
|
||||
|
||||
if (node[key]) {
|
||||
node[key] = node[key].concat(comments);
|
||||
} else {
|
||||
node[key] = comments;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user