diff --git a/src/comments.js b/src/comments.js index bd5c25d7f..67d40de30 100644 --- a/src/comments.js +++ b/src/comments.js @@ -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; } diff --git a/tests/comments/__snapshots__/jsfmt.spec.js.snap b/tests/comments/__snapshots__/jsfmt.spec.js.snap index 4627eed57..06a16a1fc 100644 --- a/tests/comments/__snapshots__/jsfmt.spec.js.snap +++ b/tests/comments/__snapshots__/jsfmt.spec.js.snap @@ -747,3 +747,59 @@ exports[`test jsx.js 2`] = ` ; " `; + +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 +} +" +`; diff --git a/tests/comments/try.js b/tests/comments/try.js new file mode 100644 index 000000000..1c9205765 --- /dev/null +++ b/tests/comments/try.js @@ -0,0 +1,13 @@ +// comment 1 +try { + // comment 2 +} +// comment 3 +catch(e) { + // comment 4 +} +// comment 5 +finally // comment 6 +{ + // comment 7 +}