Skip to content

Commit

Permalink
add comment tests back, fixup changes to comment attachment from babel
Browse files Browse the repository at this point in the history
- same as babel/babel-eslint changes (9d8e305)
  • Loading branch information
hzoo committed Jul 22, 2015
1 parent d95fd8e commit 2d21116
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 29 deletions.
4 changes: 3 additions & 1 deletion acorn-to-esprima.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ exports.toTokens = function (tokens, code) {
convertTemplateType(tokens);
source = code;

return tokens.map(exports.toToken);
return tokens.filter(function (token) {
return token.type !== "CommentLine" && token.type !== "CommentBlock";
}).map(exports.toToken);
};

function convertTemplateType(tokens) {
Expand Down
52 changes: 52 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,49 @@
var acornToEsprima = require("./acorn-to-esprima");
var parse = require("babel-core").parse;

exports.attachComments = function (ast, comments, tokens) {
if (comments.length) {
var firstComment = comments[0];
var lastComment = comments[comments.length - 1];
// fixup program start
if (!tokens.length) {
// if no tokens, the program starts at the end of the last comment
ast.range[0] = lastComment.range[1];
ast.loc.start.line = lastComment.loc.end.line;
ast.loc.start.column = lastComment.loc.end.column;
} else if (firstComment.start < tokens[0].range[0]) {
// if there are comments before the first token, the program starts at the first token
var token = tokens[0];
ast.range[0] = token.range[0];
ast.loc.start.line = token.loc.start.line;
ast.loc.start.column = token.loc.start.column;

// estraverse do not put leading comments on first node when the comment
// appear before the first token
if (ast.body.length) {
var node = ast.body[0];
node.leadingComments = [];
var firstTokenStart = token.range[0];
var len = comments.length;
for (var i = 0; i < len && comments[i].start < firstTokenStart; i++) {
node.leadingComments.push(comments[i]);
}
}
}
// fixup program end
if (tokens.length) {
var lastToken = tokens[tokens.length - 1];
if (lastComment.end > lastToken.range[1]) {
// If there is a comment after the last token, the program ends at the
// last token and not the comment
ast.range[1] = lastToken.range[1];
ast.loc.end.line = lastToken.loc.end.line;
ast.loc.end.column = lastToken.loc.end.column;
}
}
}
};

exports.parse = function (code, mode) {
var opts = {
locations: true,
Expand Down Expand Up @@ -35,7 +78,16 @@ exports.parse = function (code, mode) {
ast.tokens = acornToEsprima.toTokens(tokens, code);

// add 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";
}
}
ast.comments = comments;
exports.attachComments(ast, comments, ast.tokens);

// transform esprima and acorn divergent nodes
acornToEsprima.toAST(ast);
Expand Down
55 changes: 27 additions & 28 deletions test/babel-jscs.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function parseAndAssertSame(code, parser, parserName) {
loc: true,
range: true,
comment: true,
attachComment: true,
attachComments: true,
sourceType: "module"
});
var acornAST = babelJSCS.parse(code);
Expand Down Expand Up @@ -249,33 +249,32 @@ describe("acorn-to-esprima", function () {
parseAndAssertSame("export { foo as bar };");
});

// comment out tests since had to remove attachComment method of estraverse - #10
// it("empty program with line comment", function () {
// parseAndAssertSame("// single comment");
// });

// it("empty program with block comment", function () {
// parseAndAssertSame(" /* multiline\n * comment\n*/");
// });

// it("line comments", function () {
// parseAndAssertSame([
// " // single comment",
// "var foo = 15; // comment next to statement",
// "// second comment after statement"
// ].join("\n"));
// });

// it("block comments", function () {
// parseAndAssertSame([
// " /* single comment */ ",
// "var foo = 15; /* comment next to statement */",
// "/*",
// " * multiline",
// " * comment",
// " */"
// ].join("\n"));
// });
it("empty program with line comment", function () {
parseAndAssertSame("// single comment");
});

it("empty program with block comment", function () {
parseAndAssertSame(" /* multiline\n * comment\n*/");
});

it("line comments", function () {
parseAndAssertSame([
" // single comment",
"var foo = 15; // comment next to statement",
"// second comment after statement"
].join("\n"));
});

it("block comments", function () {
parseAndAssertSame([
" /* single comment */ ",
"var foo = 15; /* comment next to statement */",
"/*",
" * multiline",
" * comment",
" */"
].join("\n"));
});

it("null", function () {
parseAndAssertSame("null");
Expand Down

0 comments on commit 2d21116

Please sign in to comment.