Better support try/catch comments (#607)

Uses the same technique as for if/then/else. Added a helper function to make it easier.

Fixes #601
This commit is contained in:
Christopher Chedeau authored and James Long committed 2017-02-04 21:25:29 -05:00
1 parent 91087e1e84
commit fb9629ca7f
3 files changed
+106 -6

No files matched your search

+37 -6
View File
@@ -145,7 +145,8 @@ function attach(comments, ast, text) {
// We also need to check if it's the first line of the file.
if (
handleMemberExpressionComment(enclosingNode, followingNode, comment) ||
handleIfStatementComments(enclosingNode, followingNode, comment)
handleIfStatementComments(enclosingNode, followingNode, comment) ||
handleTryStatementComments(enclosingNode, followingNode, comment)
) {
// We're good
} else if (followingNode) {
@@ -286,6 +287,14 @@ function addBlockStatementFirstComment(node, comment) {
}
}
function addBlockOrNotComment(node, comment) {
if (node.type === "BlockStatement") {
addBlockStatementFirstComment(node, comment);
} else {
addLeadingComment(node, comment);
}
}
// There are often comments before the else clause of if statements like
//
// if (1) { ... }
@@ -315,11 +324,33 @@ function handleIfStatementComments(enclosingNode, followingNode, comment) {
}
if (followingNode.type === "IfStatement") {
if (followingNode.consequent.type === "BlockStatement") {
addBlockStatementFirstComment(followingNode.consequent, comment);
} else {
addLeadingComment(followingNode.consequent, comment);
}
addBlockOrNotComment(followingNode.consequent, comment);
return true;
}
return false;
}
// Same as IfStatement but for TryStatement
function handleTryStatementComments(enclosingNode, followingNode, comment) {
if (
!enclosingNode || enclosingNode.type !== "TryStatement" || !followingNode
) {
return false;
}
if (followingNode.type === "BlockStatement") {
addBlockStatementFirstComment(followingNode, comment);
return true;
}
if (followingNode.type === "TryStatement") {
addBlockOrNotComment(followingNode.finalizer, comment);
return true;
}
if (followingNode.type === "CatchClause") {
addBlockOrNotComment(followingNode.body, comment);
return true;
}
@@ -747,3 +747,59 @@ exports[`test jsx.js 2`] = `
</div>;
"
`;
exports[`test try.js 1`] = `
"// comment 1
try {
// comment 2
}
// comment 3
catch(e) {
// comment 4
}
// comment 5
finally // comment 6
{
// comment 7
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// comment 1
try {
// comment 2
} catch (e) {
// comment 3
// comment 4
} finally { // comment 6
// comment 5
// comment 7
}
"
`;
exports[`test try.js 2`] = `
"// comment 1
try {
// comment 2
}
// comment 3
catch(e) {
// comment 4
}
// comment 5
finally // comment 6
{
// comment 7
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// comment 1
try {
// comment 2
} catch (e) {
// comment 3
// comment 4
} finally { // comment 6
// comment 5
// comment 7
}
"
`;
+13
View File
@@ -0,0 +1,13 @@
// comment 1
try {
// comment 2
}
// comment 3
catch(e) {
// comment 4
}
// comment 5
finally // comment 6
{
// comment 7
}