diff --git a/src/comments.js b/src/comments.js index e9c2bc086..2242c113b 100644 --- a/src/comments.js +++ b/src/comments.js @@ -173,6 +173,7 @@ function attach(comments, ast, text, options) { ) || handleImportSpecifierComments(enclosingNode, comment) || handleTemplateLiteralComments(enclosingNode, comment) || + handleCallExpressionComments(precedingNode, enclosingNode, comment) || handleClassComments(enclosingNode, comment) ) { // We're good @@ -501,6 +502,16 @@ function handleImportSpecifierComments(enclosingNode, comment) { return false; } +function handleCallExpressionComments(precedingNode, enclosingNode, comment) { + if (enclosingNode && enclosingNode.type === "CallExpression" && + precedingNode && enclosingNode.callee === precedingNode && + enclosingNode.arguments.length > 0) { + addLeadingComment(enclosingNode.arguments[0], comment); + return true; + } + return false; +} + function printComment(commentPath) { const comment = commentPath.getValue(); comment.printed = true; diff --git a/tests/comments/__snapshots__/jsfmt.spec.js.snap b/tests/comments/__snapshots__/jsfmt.spec.js.snap index 6972cf533..dba4e9cec 100644 --- a/tests/comments/__snapshots__/jsfmt.spec.js.snap +++ b/tests/comments/__snapshots__/jsfmt.spec.js.snap @@ -52,6 +52,56 @@ exports[`blank.js 2`] = ` " `; +exports[`call_comment.js 1`] = ` +"render( // Warm any cache + , + container +); + +React.render( // Warm any cache + , + container +); +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +render( + // Warm any cache + , + container +); + +React.render( + // Warm any cache + , + container +); +" +`; + +exports[`call_comment.js 2`] = ` +"render( // Warm any cache + , + container +); + +React.render( // Warm any cache + , + container +); +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +render( + // Warm any cache + , + container +); + +React.render( + // Warm any cache + , + container +); +" +`; + exports[`dangling.js 1`] = ` "var x = {/* dangling */}; var x = { diff --git a/tests/comments/call_comment.js b/tests/comments/call_comment.js new file mode 100644 index 000000000..ff2ea0eaf --- /dev/null +++ b/tests/comments/call_comment.js @@ -0,0 +1,9 @@ +render( // Warm any cache + , + container +); + +React.render( // Warm any cache + , + container +);