Repository: prettier/plugin-php
Pull request #766 (2018-10-09 16:29:17)

fix: comment between old array syntax and items


Patch:
diff --git a/src/comments.js b/src/comments.js
index 362cd1d..4584187 100644
--- a/src/comments.js
+++ b/src/comments.js
@@ -59,6 +59,14 @@ function handleOwnLineComment(comment, text, options, ast, isLastComment) {
 function handleEndOfLineComment(comment, text, options, ast, isLastComment) {
   const { precedingNode, enclosingNode, followingNode } = comment;
   return (
+    handleArrayComments(
+      text,
+      precedingNode,
+      enclosingNode,
+      followingNode,
+      comment,
+      options
+    ) ||
     handleLastFunctionArgComments(
       text,
       precedingNode,
@@ -134,6 +142,26 @@ function addBlockOrNotComment(node, comment) {
   }
 }
 
+function handleArrayComments(
+  text,
+  precedingNode,
+  enclosingNode,
+  followingNode,
+  comment
+) {
+  if (
+    !precedingNode &&
+    !followingNode &&
+    enclosingNode &&
+    enclosingNode.kind === "array"
+  ) {
+    addTrailingComment(enclosingNode, comment);
+    return true;
+  }
+
+  return false;
+}
+
 function handleLastFunctionArgComments(
   text,
   precedingNode,
diff --git a/src/printer.js b/src/printer.js
index 315a9c9..9b92e5a 100644
--- a/src/printer.js
+++ b/src/printer.js
@@ -2244,7 +2244,14 @@ function printNode(path, options, print) {
     case "post":
       return concat([path.call(print, "what"), node.type + node.type]);
     case "cast":
-      return concat(["(", node.type, ") ", path.call(print, "what")]);
+      return concat([
+        "(",
+        node.type,
+        ") ",
+        node.what.comments
+          ? indent(path.call(print, "what"))
+          : path.call(print, "what")
+      ]);
     case "assign": {
       return printAssignment(
         node.left,


Test patch:
diff --git a/tests/comments/__snapshots__/jsfmt.spec.js.snap b/tests/comments/__snapshots__/jsfmt.spec.js.snap
index a43740a..c6c2b7e 100644
--- a/tests/comments/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/comments/__snapshots__/jsfmt.spec.js.snap
@@ -74,6 +74,25 @@ $array = [
     'key' => // Comment A
         'string'
 ];
+
+array // Comment
+();
+
+$array = array // Comment
+();
+
+$array = array // Comment
+('string', 'other-string');
+
+call(
+    array // Comment
+    ()
+);
+
+call(
+    array // Comment
+    ('string', 'other-string')
+);
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 <?php
 $array = [
@@ -168,6 +187,26 @@ $array = [
     'key' => 'string'
 ];
 
+array(); // Comment
+
+$array = array(); // Comment
+
+$array = array(
+    // Comment
+    'string',
+    'other-string'
+);
+
+call(
+    array() // Comment
+);
+
+call(array(
+    // Comment
+    'string',
+    'other-string'
+));
+
 `;
 
 exports[`assign.php - php-verify: assign.php 1`] = `
@@ -482,11 +521,25 @@ exports[`cast.php - php-verify: cast.php 1`] = `
 <?php
 
 $var = /* Comment */ (int) /* Comment */ '1' /* Comment */;
+
+$var = (int) // Comment
+    $a;
+
+$var = (int) // Comment
+    // Comment
+    $a;
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 <?php
 
 $var = /* Comment */ (int) /* Comment */ '1' /* Comment */;
 
+$var = (int) // Comment
+    $a;
+
+$var = (int) // Comment
+    // Comment
+    $a;
+
 `;
 
 exports[`class.php - php-verify: class.php 1`] = `
diff --git a/tests/comments/array.php b/tests/comments/array.php
index 9963d31..9254c7f 100644
--- a/tests/comments/array.php
+++ b/tests/comments/array.php
@@ -71,3 +71,22 @@ $array = [
     'key' => // Comment A
         'string'
 ];
+
+array // Comment
+();
+
+$array = array // Comment
+();
+
+$array = array // Comment
+('string', 'other-string');
+
+call(
+    array // Comment
+    ()
+);
+
+call(
+    array // Comment
+    ('string', 'other-string')
+);
diff --git a/tests/comments/cast.php b/tests/comments/cast.php
index 24c8f16..76a3f6c 100644
--- a/tests/comments/cast.php
+++ b/tests/comments/cast.php
@@ -1,3 +1,10 @@
 <?php
 
 $var = /* Comment */ (int) /* Comment */ '1' /* Comment */;
+
+$var = (int) // Comment
+    $a;
+
+$var = (int) // Comment
+    // Comment
+    $a;