Repository: prettier/plugin-php
Pull request #276 (2018-03-30 21:29:46)

fix: nested array comments
https://github.com/prettier/plugin-php/issues/273

Also refactoring the getCommentChildNodes to make it more scalable


Patch:
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
new file mode 100644
index 0000000..c0251f3
--- /dev/null
+++ b/CONTRIBUTING.md
@@ -0,0 +1,19 @@
+# Contributing to Prettier for PHP
+
+To get up and running, install the dependencies and run the tests:
+
+```bash
+yarn
+yarn test
+```
+
+Here's what you need to know about the tests:
+
+* The tests use [Jest](https://facebook.github.io/jest/) snapshots.
+* You can make changes and run `jest -u` (or `yarn test -u`) to update the snapshots. Then run `git diff` to take a look at what changed. Always update the snapshots when opening a PR.
+* You can run `AST_COMPARE=1 yarn test` for a more robust test run. That formats each file, re-parses it, and compares the new AST with the original one and makes sure they are semantically equivalent.
+* If you would like to debug prettier locally, you can either debug it in node or the browser. The easiest way to debug it in the browser is to run the interactive `docs` REPL locally. The easiest way to debug it in node, is to create a local test file and run it in an editor like VS Code.
+
+Run `yarn lint --fix` to automatically format files.
+
+If you can, take look at [commands.md](commands.md) and check out [Wadler's paper](http://homepages.inf.ed.ac.uk/wadler/papers/prettier/prettier.pdf) to understand how Prettier works.
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..bcf7454
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,7 @@
+Copyright 2018 Mike Grip, Evilebot Tnawi, Christian Zosel
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/src/index.js b/src/index.js
index d01a085..43a82bb 100644
--- a/src/index.js
+++ b/src/index.js
@@ -49,71 +49,101 @@ const printers = {
     massageAstNode: clean,
     // @TODO: determine if it makes sense to abstract this into a "getChildNodes" util function
     getCommentChildNodes(node) {
-      if (["constant", "property", "classconstant"].includes(node.kind)) {
-        return [node.value];
-      }
-      if (node.kind === "return") {
-        return [node.expr];
-      }
-      if (node.kind === "array") {
-        return node.items;
-      }
-      if (node.kind === "assign") {
-        return [node.left, node.right];
-      }
-      if (node.kind === "if") {
-        return [node.body, node.alternate, node.test];
-      }
-      if (node.kind === "foreach") {
-        return [
-          ...(node.body.children || []),
-          node.key,
-          node.source,
-          node.value
-        ];
-      }
-      if (node.kind === "try") {
-        return [
-          ...((node.always && node.always.children) || []),
-          ...((node.body && node.body.children) || []),
-          ...(node.catches || [])
-        ];
-      }
-      if (node.kind === "call") {
-        return [...(node.what ? [node.what] : []), ...(node.arguments || [])];
-      }
-      if (["offsetlookup", "staticlookup"].includes(node.kind)) {
-        return [
-          ...(node.what ? [node.what] : []),
-          ...(node.offset ? [node.offset] : [])
-        ];
-      }
-      if (node.kind === "catch") {
-        return [
-          ...(node.what || []),
-          ...(node.variable ? [node.variable] : []),
-          ...(node.body.children || [])
-        ];
-      }
-      if (["for", "while", "do"].includes(node.kind)) {
-        return Array.isArray(node.body.children) ? node.body.children : [];
-      }
-      if (node.kind === "class") {
-        return [
-          ...(node.body || []),
-          ...(node.implements || []),
-          ...(node.extends ? [node.extends] : [])
-        ];
-      }
-      if (node.kind === "function" || node.kind === "method") {
-        return [...(node.body.children || []), ...(node.arguments || [])];
-      }
-      if (node.body) {
-        // for some nodes body is array, others its a block node
-        return Array.isArray(node.body) ? node.body : [node.body];
-      }
-      const children = node.children || node.traits || node.arguments;
-      return children ? children : [];
+      const nodeMap = [
+        {
+          kinds: ["constant", "property", "classconstant"],
+          children: { listNodes: [], nodes: ["value"] }
+        },
+        {
+          kinds: ["return"],
+          children: { listNodes: [], nodes: ["expr"] }
+        },
+        {
+          kinds: ["array"],
+          children: { listNodes: ["items"], nodes: [] }
+        },
+        {
+          kinds: ["entry"],
+          children: { listNodes: [], nodes: ["key", "value"] }
+        },
+        {
+          kinds: ["assign"],
+          children: { listNodes: [], nodes: ["left", "right"] }
+        },
+        {
+          kinds: ["if"],
+          children: { listNodes: [], nodes: ["body", "alternate", "test"] }
+        },
+        {
+          kinds: ["block", "program", "namespace", "declare"],
+          children: { listNodes: ["children"], nodes: [] }
+        },
+        {
+          kinds: ["usegroup"],
+          children: { listNodes: ["items"], nodes: [] }
+        },
+        {
+          kinds: ["foreach"],
+          children: {
+            listNodes: ["children"],
+            nodes: ["key", "source", "value"]
+          }
+        },
+        {
+          kinds: ["try"],
+          children: {
+            listNodes: ["catches"],
+            nodes: ["always", "body"]
+          }
+        },
+        {
+          kinds: ["call"],
+          children: { listNodes: ["arguments"], nodes: ["what"] }
+        },
+        {
+          kinds: ["offsetlookup", "staticlookup"],
+          children: { listNodes: [], nodes: ["what", "offset"] }
+        },
+        {
+          kinds: ["catch"],
+          children: {
+            listNodes: ["what"],
+            nodes: ["variable", "body"]
+          }
+        },
+        {
+          kinds: ["for", "while", "do"],
+          children: { listNodes: [], nodes: ["body"] }
+        },
+        {
+          kinds: ["trait", "class"],
+          children: { listNodes: ["implements", "body"], nodes: ["extends"] }
+        },
+        {
+          kinds: ["function", "method", "closure"],
+          children: { listNodes: ["arguments", "uses"], nodes: ["body"] }
+        },
+        {
+          kinds: ["switch", "case"],
+          children: { listNodes: [], nodes: ["test", "body"] }
+        }
+      ];
+      const children = nodeMap.reduce((currentChildren, map) => {
+        if (map.kinds.includes(node.kind)) {
+          return [
+            ...map.children.listNodes.reduce((accumulator, listNode) => {
+              const childValue = node[listNode];
+              return childValue ? [...accumulator, ...childValue] : accumulator;
+            }, []),
+            ...map.children.nodes.reduce((accumulator, childNode) => {
+              const childValue = node[childNode];
+              return childValue ? [...accumulator, childValue] : accumulator;
+            }, [])
+          ];
+        }
+        return currentChildren;
+      }, false);
+      return children !== false ? children : [];
     },
     canAttachComment(node) {
       return (


Test patch:
diff --git a/tests/comments/__snapshots__/jsfmt.spec.js.snap b/tests/comments/__snapshots__/jsfmt.spec.js.snap
index 5357744..fb135a4 100644
--- a/tests/comments/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/comments/__snapshots__/jsfmt.spec.js.snap
@@ -39,7 +39,15 @@ return [
   // foo
   "bar"
 ];
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+$array = [
+    'type'      => [],
+    'config'    => [
+        // comments in nested arrays break things
+        'key' => 1,
+    ],
+    'accepted'  => [],
+];~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 <?php
 $array = [
     /* comment */
@@ -69,13 +77,11 @@ $array = [
     /* comment */
     /* comment */
     /* comment */
-    "foo" => "bar"
-    /* comment */
-    /* comment */, /* comment */
+    "foo" => /* comment */ /* comment */
+    "bar", /* comment */
     /* comment */
-    "bar" => "foo"
-    /* comment */
-    /* comment */ /* comment */
+    "bar" => /* comment */ /* comment */
+    "foo" /* comment */
     /* comment */
     /* comment */
 ];
@@ -106,6 +112,15 @@ return [
     "bar"
 ];
 
+$array = [
+    'type' => [],
+    'config' => [
+        // comments in nested arrays break things
+        'key' => 1
+    ],
+    'accepted' => []
+];
+
 `;
 
 exports[`class.php 1`] = `
diff --git a/tests/comments/array.php b/tests/comments/array.php
index 3d3fe4f..d255bf9 100644
--- a/tests/comments/array.php
+++ b/tests/comments/array.php
@@ -36,3 +36,12 @@ return [
   // foo
   "bar"
 ];
+
+$array = [
+    'type'      => [],
+    'config'    => [
+        // comments in nested arrays break things
+        'key' => 1,
+    ],
+    'accepted'  => [],
+];
\ No newline at end of file