From 6c268cdf2100470eac36beb1fb9f0e945ab05fcc Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sun, 7 Jun 2015 23:49:29 +0100 Subject: [PATCH] split out path comment methods into a separate file --- src/babel/traversal/path/README.md | 1 + src/babel/traversal/path/comments.js | 51 ++++++++++++++++++++++++++ src/babel/traversal/path/index.js | 1 + src/babel/traversal/path/removal.js | 55 ---------------------------- 4 files changed, 53 insertions(+), 55 deletions(-) create mode 100644 src/babel/traversal/path/comments.js diff --git a/src/babel/traversal/path/README.md b/src/babel/traversal/path/README.md index 46b9037959..5cd632ba30 100644 --- a/src/babel/traversal/path/README.md +++ b/src/babel/traversal/path/README.md @@ -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. diff --git a/src/babel/traversal/path/comments.js b/src/babel/traversal/path/comments.js new file mode 100644 index 0000000000..007040d5e1 --- /dev/null +++ b/src/babel/traversal/path/comments.js @@ -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; + } +} diff --git a/src/babel/traversal/path/index.js b/src/babel/traversal/path/index.js index beb87d931c..bdbbaf89de 100644 --- a/src/babel/traversal/path/index.js +++ b/src/babel/traversal/path/index.js @@ -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; diff --git a/src/babel/traversal/path/removal.js b/src/babel/traversal/path/removal.js index 8521ca5246..fc6175ef2a 100644 --- a/src/babel/traversal/path/removal.js +++ b/src/babel/traversal/path/removal.js @@ -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; - } -}