Skip to content

Commit

Permalink
Consolidate versions of "convertComments"
Browse files Browse the repository at this point in the history
  • Loading branch information
zertosh committed Mar 25, 2017
1 parent 2541fc9 commit 5d32ad0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 29 deletions.
15 changes: 15 additions & 0 deletions babylon-to-espree/convertComments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = function (comments) {
for (var i = 0; i < comments.length; i++) {
var comment = comments[i];
if (comment.type === "CommentBlock") {
comment.type = "Block";
} else if (comment.type === "CommentLine") {
comment.type = "Line";
}
// sometimes comments don't get ranges computed,
// even with options.ranges === true
if (!comment.range) {
comment.range = [comment.start, comment.end];
}
}
};
16 changes: 1 addition & 15 deletions babylon-to-espree/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,4 @@ exports.attachComments = require("./attachComments");
exports.toTokens = require("./toTokens");
exports.toAST = require("./toAST");

exports.convertComments = function (comments) {
for (var i = 0; i < comments.length; i++) {
var comment = comments[i];
if (comment.type === "CommentBlock") {
comment.type = "Block";
} else if (comment.type === "CommentLine") {
comment.type = "Line";
}
// sometimes comments don't get ranges computed,
// even with options.ranges === true
if (!comment.range) {
comment.range = [comment.start, comment.end];
}
}
};
exports.convertComments = require("./convertComments");
18 changes: 4 additions & 14 deletions babylon-to-espree/toAST.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var convertComments = require("./convertComments");

module.exports = function (ast, traverse, code) {
var state = { source: code };
ast.range = [ast.start, ast.end];
Expand All @@ -15,18 +17,6 @@ function changeToLiteral(node, state) {
}
}

function changeComments(nodeComments) {
for (var i = 0; i < nodeComments.length; i++) {
var comment = nodeComments[i];
if (comment.type === "CommentLine") {
comment.type = "Line";
} else if (comment.type === "CommentBlock") {
comment.type = "Block";
}
comment.range = [comment.start, comment.end];
}
}

var astTransformVisitor = {
noScope: true,
enter (path) {
Expand All @@ -43,11 +33,11 @@ var astTransformVisitor = {
}

if (node.trailingComments) {
changeComments(node.trailingComments);
convertComments(node.trailingComments);
}

if (node.leadingComments) {
changeComments(node.leadingComments);
convertComments(node.leadingComments);
}

// make '_paths' non-enumerable (babel-eslint #200)
Expand Down

0 comments on commit 5d32ad0

Please sign in to comment.