Repository: prettier/plugin-php
Pull request #639 (2018-09-17 12:02:24)

feat: better print variable comments
From here https://github.com/prettier/prettier/blob/master/src/language-js/comments.js#L690

evilebottnawi: Feel free to feedback

Patch:
diff --git a/src/comments.js b/src/comments.js
index ce23bed..a50ad26 100644
--- a/src/comments.js
+++ b/src/comments.js
@@ -55,7 +55,8 @@ function handleEndOfLineComment(comment, text, options, ast, isLastComment) {
     handleFunction(text, enclosingNode, followingNode, comment, options) ||
     handleTryCatch(enclosingNode, comment) ||
     handleOnlyComments(enclosingNode, ast, comment, isLastComment) ||
-    handleHalt(enclosingNode, precedingNode, comment)
+    handleHalt(enclosingNode, precedingNode, comment) ||
+    handleVariableComments(enclosingNode, followingNode, comment)
   );
 }
 
@@ -316,6 +317,21 @@ function handleOnlyComments(enclosingNode, ast, comment, isLastComment) {
   return false;
 }
 
+function handleVariableComments(enclosingNode, followingNode, comment) {
+  if (
+    enclosingNode &&
+    enclosingNode.kind === "assign" &&
+    followingNode &&
+    (followingNode.kind === "array" ||
+      followingNode.kind === "string" ||
+      followingNode.kind === "encapsed")
+  ) {
+    addLeadingComment(followingNode, comment);
+    return true;
+  }
+  return false;
+}
+
 // https://github.com/prettier/prettier/blob/master/src/main/comments.js#L335
 function printComment(commentPath, options) {
   const comment = commentPath.getValue();


Test patch:
diff --git a/tests/comments/__snapshots__/jsfmt.spec.js.snap b/tests/comments/__snapshots__/jsfmt.spec.js.snap
index 8e99009..6ec9c84 100644
--- a/tests/comments/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/comments/__snapshots__/jsfmt.spec.js.snap
@@ -1898,6 +1898,190 @@ use some\\namespaces\\{
 
 `;
 
+exports[`variable.php 1`] = `
+<?php
+
+$obj = // Comment
+[
+'key' => 'val'
+];
+
+$obj // Comment
+= [
+'key' => 'val'
+];
+
+$obj = [ // Comment
+'key' => 'val'
+];
+
+$obj = [
+// Comment
+'key' => 'val'
+];
+
+$obj = // Comment
+[
+'val'
+];
+
+$obj // Comment
+= [
+'val'
+];
+
+$obj = [ // Comment
+'val'
+];
+
+$obj = [
+// Comment
+'val'
+];
+
+$obj = // Comment
+'val';
+
+$obj = // Comment
+'
+val
+val
+';
+
+$obj = // Comment
+        '
+    val
+    val
+    ';
+
+$obj = // Comment
+    "val";
+
+$obj = // Comment
+"
+val
+val
+";
+
+$obj = // Comment
+        "
+    val
+    val
+    ";
+
+$obj = // Comment
+    "val \${$var}";
+
+$obj = // Comment
+"
+val 
+\${$var}
+";
+
+$obj = // Comment
+    "
+    val 
+    \${$var}
+    ";
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+<?php
+
+$obj =
+    // Comment
+    [
+        'key' => 'val'
+    ];
+
+$obj =
+    // Comment
+    [
+        'key' => 'val'
+    ];
+
+$obj = [
+    // Comment
+    'key' => 'val'
+];
+
+$obj = [
+    // Comment
+    'key' => 'val'
+];
+
+$obj =
+    // Comment
+    ['val'];
+
+$obj =
+    // Comment
+    ['val'];
+
+$obj = [
+    // Comment
+    'val'
+];
+
+$obj = [
+    // Comment
+    'val'
+];
+
+$obj =
+    // Comment
+    'val';
+
+$obj =
+    // Comment
+    '
+val
+val
+';
+
+$obj =
+    // Comment
+    '
+    val
+    val
+    ';
+
+$obj =
+    // Comment
+    "val";
+
+$obj =
+    // Comment
+    "
+val
+val
+";
+
+$obj =
+    // Comment
+    "
+    val
+    val
+    ";
+
+$obj =
+    // Comment
+    "val \${$var}";
+
+$obj =
+    // Comment
+    "
+val 
+\${$var}
+";
+
+$obj =
+    // Comment
+    "
+    val 
+    \${$var}
+    ";
+
+`;
+
 exports[`variadic.php 1`] = `
 <?php
 
diff --git a/tests/comments/variable.php b/tests/comments/variable.php
new file mode 100644
index 0000000..2af1ab5
--- /dev/null
+++ b/tests/comments/variable.php
@@ -0,0 +1,84 @@
+<?php
+
+$obj = // Comment
+[
+'key' => 'val'
+];
+
+$obj // Comment
+= [
+'key' => 'val'
+];
+
+$obj = [ // Comment
+'key' => 'val'
+];
+
+$obj = [
+// Comment
+'key' => 'val'
+];
+
+$obj = // Comment
+[
+'val'
+];
+
+$obj // Comment
+= [
+'val'
+];
+
+$obj = [ // Comment
+'val'
+];
+
+$obj = [
+// Comment
+'val'
+];
+
+$obj = // Comment
+'val';
+
+$obj = // Comment
+'
+val
+val
+';
+
+$obj = // Comment
+        '
+    val
+    val
+    ';
+
+$obj = // Comment
+    "val";
+
+$obj = // Comment
+"
+val
+val
+";
+
+$obj = // Comment
+        "
+    val
+    val
+    ";
+
+$obj = // Comment
+    "val ${$var}";
+
+$obj = // Comment
+"
+val 
+${$var}
+";
+
+$obj = // Comment
+    "
+    val 
+    ${$var}
+    ";