Skip to content

Commit 5d32ad0

Browse files
committed
Consolidate versions of "convertComments"
1 parent 2541fc9 commit 5d32ad0

File tree

3 files changed

+20
-29
lines changed

3 files changed

+20
-29
lines changed

babylon-to-espree/convertComments.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module.exports = function (comments) {
2+
for (var i = 0; i < comments.length; i++) {
3+
var comment = comments[i];
4+
if (comment.type === "CommentBlock") {
5+
comment.type = "Block";
6+
} else if (comment.type === "CommentLine") {
7+
comment.type = "Line";
8+
}
9+
// sometimes comments don't get ranges computed,
10+
// even with options.ranges === true
11+
if (!comment.range) {
12+
comment.range = [comment.start, comment.end];
13+
}
14+
}
15+
};

babylon-to-espree/index.js

+1-15
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,4 @@ exports.attachComments = require("./attachComments");
33
exports.toTokens = require("./toTokens");
44
exports.toAST = require("./toAST");
55

6-
exports.convertComments = function (comments) {
7-
for (var i = 0; i < comments.length; i++) {
8-
var comment = comments[i];
9-
if (comment.type === "CommentBlock") {
10-
comment.type = "Block";
11-
} else if (comment.type === "CommentLine") {
12-
comment.type = "Line";
13-
}
14-
// sometimes comments don't get ranges computed,
15-
// even with options.ranges === true
16-
if (!comment.range) {
17-
comment.range = [comment.start, comment.end];
18-
}
19-
}
20-
};
6+
exports.convertComments = require("./convertComments");

babylon-to-espree/toAST.js

+4-14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
var convertComments = require("./convertComments");
2+
13
module.exports = function (ast, traverse, code) {
24
var state = { source: code };
35
ast.range = [ast.start, ast.end];
@@ -15,18 +17,6 @@ function changeToLiteral(node, state) {
1517
}
1618
}
1719

18-
function changeComments(nodeComments) {
19-
for (var i = 0; i < nodeComments.length; i++) {
20-
var comment = nodeComments[i];
21-
if (comment.type === "CommentLine") {
22-
comment.type = "Line";
23-
} else if (comment.type === "CommentBlock") {
24-
comment.type = "Block";
25-
}
26-
comment.range = [comment.start, comment.end];
27-
}
28-
}
29-
3020
var astTransformVisitor = {
3121
noScope: true,
3222
enter (path) {
@@ -43,11 +33,11 @@ var astTransformVisitor = {
4333
}
4434

4535
if (node.trailingComments) {
46-
changeComments(node.trailingComments);
36+
convertComments(node.trailingComments);
4737
}
4838

4939
if (node.leadingComments) {
50-
changeComments(node.leadingComments);
40+
convertComments(node.leadingComments);
5141
}
5242

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

0 commit comments

Comments
 (0)