From fe38dab45ac41ca93f380eedca4bcb732a28aa10 Mon Sep 17 00:00:00 2001 From: Christopher Chedeau Date: Fri, 3 Mar 2017 13:47:13 -0800 Subject: [PATCH] Fix comment for `call( // comment` (#858) This is a pretty exotic way to put a comment and it's been a long time since I have seen prettier output be as broken :) Fixes #857 --- src/comments.js | 11 ++++ .../comments/__snapshots__/jsfmt.spec.js.snap | 50 +++++++++++++++++++ tests/comments/call_comment.js | 9 ++++ 3 files changed, 70 insertions(+) create mode 100644 tests/comments/call_comment.js 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 +);