Repository: prettier/plugin-php
Pull request #642 (2018-09-17 16:58:27)

feat: better print comments in `retif` node

evilebottnawi: Feel free to feedback

Patch:
diff --git a/src/comments.js b/src/comments.js
index 6b66326..6c20500 100644
--- a/src/comments.js
+++ b/src/comments.js
@@ -9,7 +9,7 @@ const {
 } = require("prettier").util;
 const { concat, join, indent, hardline } = require("prettier").doc.builders;
 // TODO: remove after resolve https://github.com/prettier/prettier/pull/5049
-const { hasNewline } = require("./util");
+const { hasNewline, hasNewlineInRange } = require("./util");
 /*
 Comment functions are meant to inspect various edge cases using given comment nodes,
 with information about where those comment nodes exist in the tree (ie enclosingNode,
@@ -50,6 +50,14 @@ function handleOwnLineComment(comment, text, options, ast, isLastComment) {
 function handleEndOfLineComment(comment, text, options, ast, isLastComment) {
   const { precedingNode, enclosingNode, followingNode } = comment;
   return (
+    handleRetifComments(
+      enclosingNode,
+      precedingNode,
+      followingNode,
+      comment,
+      text,
+      options
+    ) ||
     handleClassComments(enclosingNode, followingNode, comment) ||
     handleFunctionParameter(text, enclosingNode, comment, options) ||
     handleFunction(text, enclosingNode, followingNode, comment, options) ||
@@ -76,6 +84,34 @@ function handleRemainingComment(comment, text, options, ast, isLastComment) {
   );
 }
 
+function handleRetifComments(
+  enclosingNode,
+  precedingNode,
+  followingNode,
+  comment,
+  text,
+  options
+) {
+  const isSameLineAsPrecedingNode =
+    precedingNode &&
+    !hasNewlineInRange(
+      text,
+      options.locEnd(precedingNode),
+      options.locStart(comment)
+    );
+
+  if (
+    (!precedingNode || !isSameLineAsPrecedingNode) &&
+    enclosingNode &&
+    enclosingNode.kind === "retif" &&
+    followingNode
+  ) {
+    addLeadingComment(followingNode, comment);
+    return true;
+  }
+  return false;
+}
+
 function handleForComments(
   enclosingNode,
   precedingNode,
diff --git a/src/util.js b/src/util.js
index 305885d..d88cc47 100644
--- a/src/util.js
+++ b/src/util.js
@@ -566,6 +566,16 @@ function hasNewline(text, index, opts) {
   return idx !== idx2;
 }
 
+// TODO: remove after resolve https://github.com/prettier/prettier/pull/5049
+function hasNewlineInRange(text, start, end) {
+  for (let i = start; i < end; ++i) {
+    if (text.charAt(i) === "\n") {
+      return true;
+    }
+  }
+  return false;
+}
+
 module.exports = {
   printNumber,
   getPrecedence,
@@ -593,5 +603,6 @@ module.exports = {
   getLastNestedChildNode,
   isProgramLikeNode,
   getNodeKindIncludingLogical,
-  hasNewline
+  hasNewline,
+  hasNewlineInRange
 };


Test patch:
diff --git a/tests/comments/__snapshots__/jsfmt.spec.js.snap b/tests/comments/__snapshots__/jsfmt.spec.js.snap
index a3b19c5..ed2e703 100644
--- a/tests/comments/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/comments/__snapshots__/jsfmt.spec.js.snap
@@ -1761,6 +1761,70 @@ echo /*Comment*/ /*Comment*/ $a->bar();
 
 `;
 
+exports[`retif.php 1`] = `
+<?php
+
+$inspect = $condition
+    ? // node <= 0.8.x
+    function($v, $colors) {
+        return $util->inspect($v, 0, 0, $colors);
+    }
+    : // node > 0.8.x
+    function($v, $colors) {
+        return $util->inspect($v, [ 'colors' => colors ]);
+    };
+
+$extractTextPluginOptions = $shouldUseRelativeAssetPaths
+    // Making sure that the publicPath goes back to to build folder.
+    ? [ 'publicPath' => (new Foo($cssFilename->split('/')->length))->join('../') ] :
+    [];
+
+$extractTextPluginOptions = $shouldUseRelativeAssetPaths
+    ? // Making sure that the publicPath goes back to to build folder.
+    [ 'publicPath' => (new Foo($cssFilename->split("/")->length))->join("../") ]
+    : [];
+
+$extractTextPluginOptions = $shouldUseRelativeAssetPaths // Making sure that the publicPath goes back to to build folder.
+    ? [ 'publicPath' => (new Foo($cssFilename->split("/")->length))->join("../") ]
+    : [];
+
+$var = $process->env->NODE_ENV === "production"
+    ? call("./configureProdStore") // a
+    : call("./configureDevStore"); // b
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+<?php
+
+$inspect = $condition
+    ? // node <= 0.8.x
+    function ($v, $colors) {
+        return $util->inspect($v, 0, 0, $colors);
+    }
+    : // node > 0.8.x
+    function ($v, $colors) {
+        return $util->inspect($v, ['colors' => colors]);
+    };
+
+$extractTextPluginOptions = $shouldUseRelativeAssetPaths
+    ? // Making sure that the publicPath goes back to to build folder.
+    ['publicPath' => (new Foo($cssFilename->split('/')->length))->join('../')]
+    : [];
+
+$extractTextPluginOptions = $shouldUseRelativeAssetPaths
+    ? // Making sure that the publicPath goes back to to build folder.
+    ['publicPath' => (new Foo($cssFilename->split("/")->length))->join("../")]
+    : [];
+
+$extractTextPluginOptions = $shouldUseRelativeAssetPaths // Making sure that the publicPath goes back to to build folder.
+    ? ['publicPath' => (new Foo($cssFilename->split("/")->length))->join("../")]
+    : [];
+
+$var =
+    $process->env->NODE_ENV === "production"
+        ? call("./configureProdStore") // a
+        : call("./configureDevStore"); // b
+
+`;
+
 exports[`return.php 1`] = `
 <?php
 
diff --git a/tests/comments/retif.php b/tests/comments/retif.php
new file mode 100644
index 0000000..7bb8465
--- /dev/null
+++ b/tests/comments/retif.php
@@ -0,0 +1,29 @@
+<?php
+
+$inspect = $condition
+    ? // node <= 0.8.x
+    function($v, $colors) {
+        return $util->inspect($v, 0, 0, $colors);
+    }
+    : // node > 0.8.x
+    function($v, $colors) {
+        return $util->inspect($v, [ 'colors' => colors ]);
+    };
+
+$extractTextPluginOptions = $shouldUseRelativeAssetPaths
+    // Making sure that the publicPath goes back to to build folder.
+    ? [ 'publicPath' => (new Foo($cssFilename->split('/')->length))->join('../') ] :
+    [];
+
+$extractTextPluginOptions = $shouldUseRelativeAssetPaths
+    ? // Making sure that the publicPath goes back to to build folder.
+    [ 'publicPath' => (new Foo($cssFilename->split("/")->length))->join("../") ]
+    : [];
+
+$extractTextPluginOptions = $shouldUseRelativeAssetPaths // Making sure that the publicPath goes back to to build folder.
+    ? [ 'publicPath' => (new Foo($cssFilename->split("/")->length))->join("../") ]
+    : [];
+
+$var = $process->env->NODE_ENV === "production"
+    ? call("./configureProdStore") // a
+    : call("./configureDevStore"); // b