Repository: prettier/plugin-php
Pull request #934 (2019-01-29 18:53:42)

refactor: newlines/spaces before and after php tags
Avoid regexp, fix a lot of bugs with `inline` nodes

evilebottnawi: Feel free to feedback

Patch:
diff --git a/src/printer.js b/src/printer.js
index 7056bc1..17754dd 100644
--- a/src/printer.js
+++ b/src/printer.js
@@ -46,8 +46,6 @@ const {
   shouldPrintHardLineAfterStartInControlStructure,
   shouldPrintHardLineBeforeEndInControlStructure,
   getAlignment,
-  getFirstNestedChildNode,
-  getLastNestedChildNode,
   isProgramLikeNode,
   getNodeKindIncludingLogical,
   useSingleQuote,
@@ -1048,49 +1046,50 @@ function printLines(path, options, print, childrenAttribute = "children") {
       );
     }
 
-    const originalText = node.loc.source;
-    const firstNestedChildNode = getFirstNestedChildNode(node);
-    const lastNestedChildNode = getLastNestedChildNode(node);
-    const hasStartTag =
-      firstNestedChildNode && firstNestedChildNode.kind !== "inline";
-    const hasEndTag =
-      lastNestedChildNode &&
-      lastNestedChildNode.kind !== "inline" &&
-      originalText.trim().endsWith("?>");
+    const parts = [];
 
-    let afterOpenTag = " ";
-    let beforeCloseTag = " ";
+    const [firstNode] = node.children;
+    const hasStartTag = !firstNode || firstNode.kind !== "inline";
 
     if (hasStartTag) {
-      const between = originalText.trim().match(/^<\?(php|=)(\s+)?\S/);
+      const between = options.originalText.trim().match(/^<\?(php|=)(\s+)?\S/);
+      const afterOpenTag = concat([
+        between && between[2].includes("\n") && wrappedParts.length > 0
+          ? concat([
+              hardline,
+              between[2].split("\n").length > 2 ? hardline : ""
+            ])
+          : " "
+      ]);
 
-      if (between && between[2]) {
-        afterOpenTag =
-          between[2].includes("\n") && wrappedParts.length > 0
-            ? concat([
-                hardline,
-                between[2].split("\n").length > 2 ? hardline : ""
-              ])
-            : " ";
-      }
+      parts.push(concat(["<?php", afterOpenTag]));
     }
 
+    parts.push(concat(wrappedParts));
+
+    const hasEndTag = options.originalText.trim().endsWith("?>");
+
     if (hasEndTag) {
-      const between = originalText.trim().match(/\S(\s*)?\?>$/);
+      const lastNode = getLast(node.children);
+      const beforeCloseTag = lastNode
+        ? concat([
+            hasNewlineInRange(
+              options.originalText,
+              options.locEnd(lastNode),
+              options.locEnd(node)
+            )
+              ? hardline
+              : " ",
+            isNextLineEmpty(options.originalText, lastNode, options)
+              ? hardline
+              : ""
+          ])
+        : "";
 
-      beforeCloseTag =
-        between && between[1] && between[1].includes("\n")
-          ? hardline
-          : wrappedParts.length > 0
-          ? " "
-          : "";
+      parts.push(lineSuffix(concat([beforeCloseTag, "?>"])));
     }
 
-    return concat([
-      hasStartTag ? concat(["<?php", afterOpenTag]) : "",
-      concat(wrappedParts),
-      hasEndTag ? lineSuffix(concat([beforeCloseTag, "?>"])) : ""
-    ]);
+    return concat(parts);
   }
 
   return concat(wrappedParts);
diff --git a/src/util.js b/src/util.js
index 332fb33..8443518 100644
--- a/src/util.js
+++ b/src/util.js
@@ -539,22 +539,6 @@ function getAlignment(text) {
   return lastLine.length - lastLine.trimLeft().length + 1;
 }
 
-function getFirstNestedChildNode(node) {
-  if (node.children && node.children.length > 0) {
-    return getFirstNestedChildNode(node.children[0]);
-  }
-
-  return node;
-}
-
-function getLastNestedChildNode(node) {
-  if (node.children && node.children.length > 0) {
-    return getFirstNestedChildNode(node.children[node.children.length - 1]);
-  }
-
-  return node;
-}
-
 function getNextNode(path, node) {
   const parent = path.getParentNode();
   const children = getNodeListProperty(parent);
@@ -727,8 +711,6 @@ module.exports = {
   shouldPrintHardLineAfterStartInControlStructure,
   shouldPrintHardLineBeforeEndInControlStructure,
   getAlignment,
-  getFirstNestedChildNode,
-  getLastNestedChildNode,
   isProgramLikeNode,
   isReferenceLikeNode,
   getNodeKindIncludingLogical,


Test patch:
diff --git a/tests/comments/__snapshots__/jsfmt.spec.js.snap b/tests/comments/__snapshots__/jsfmt.spec.js.snap
index d2f609d..2468d4a 100644
--- a/tests/comments/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/comments/__snapshots__/jsfmt.spec.js.snap
@@ -2484,6 +2484,7 @@ class Net_Sample
  * c-hanging-comment-ender-p: nil
  * End:
  */
+
 ?>
 
 ================================================================================
@@ -5963,7 +5964,6 @@ printWidth: 80
  * @package WooCommerce/Templates
  * @version 2.0.0
  */
-
 ?>
 
 ================================================================================
diff --git a/tests/inline/__snapshots__/jsfmt.spec.js.snap b/tests/inline/__snapshots__/jsfmt.spec.js.snap
index 346b658..a24d98d 100644
--- a/tests/inline/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/inline/__snapshots__/jsfmt.spec.js.snap
@@ -2024,8 +2024,7 @@ printWidth: 80
 ?>
 
 =====================================output=====================================
-<?php
-?>
+<?php ?>
 
 ================================================================================
 `;
@@ -2041,8 +2040,7 @@ printWidth: 80
 ?>
 
 =====================================output=====================================
-<?php
-?>
+<?php ?>
 
 ================================================================================
 `;
diff --git a/tests/preserve_line/__snapshots__/jsfmt.spec.js.snap b/tests/preserve_line/__snapshots__/jsfmt.spec.js.snap
index 1e79e7c..5466f32 100644
--- a/tests/preserve_line/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/preserve_line/__snapshots__/jsfmt.spec.js.snap
@@ -729,6 +729,336 @@ $a = 1;
 ================================================================================
 `;
 
+exports[`php-tags.php 1`] = `
+====================================options=====================================
+parsers: ["php"]
+printWidth: 80
+                                                                                | printWidth
+=====================================input======================================
+<?php
+$var = 1;
+?>
+
+=====================================output=====================================
+<?php
+$var = 1;
+?>
+
+================================================================================
+`;
+
+exports[`php-tags-2.php 1`] = `
+====================================options=====================================
+parsers: ["php"]
+printWidth: 80
+                                                                                | printWidth
+=====================================input======================================
+<?php
+
+$var = 1;
+?>
+
+=====================================output=====================================
+<?php
+
+$var = 1;
+?>
+
+================================================================================
+`;
+
+exports[`php-tags-3.php 1`] = `
+====================================options=====================================
+parsers: ["php"]
+printWidth: 80
+                                                                                | printWidth
+=====================================input======================================
+<?php
+$var = 1;
+
+?>
+
+=====================================output=====================================
+<?php
+$var = 1;
+
+?>
+
+================================================================================
+`;
+
+exports[`php-tags-4.php 1`] = `
+====================================options=====================================
+parsers: ["php"]
+printWidth: 80
+                                                                                | printWidth
+=====================================input======================================
+<?php
+
+$var = 1;
+
+?>
+
+=====================================output=====================================
+<?php
+
+$var = 1;
+
+?>
+
+================================================================================
+`;
+
+exports[`php-tags-5.php 1`] = `
+====================================options=====================================
+parsers: ["php"]
+printWidth: 80
+                                                                                | printWidth
+=====================================input======================================
+<?php
+
+
+$var = 1;
+
+
+?>
+
+=====================================output=====================================
+<?php
+
+$var = 1;
+
+?>
+
+================================================================================
+`;
+
+exports[`php-tags-6.php 1`] = `
+====================================options=====================================
+parsers: ["php"]
+printWidth: 80
+                                                                                | printWidth
+=====================================input======================================
+<?php $var = 1; ?>
+
+=====================================output=====================================
+<?php $var = 1; ?>
+
+================================================================================
+`;
+
+exports[`php-tags-7.php 1`] = `
+====================================options=====================================
+parsers: ["php"]
+printWidth: 80
+                                                                                | printWidth
+=====================================input======================================
+<?php
+$var = 1;
+
+=====================================output=====================================
+<?php
+$var = 1;
+
+================================================================================
+`;
+
+exports[`php-tags-8.php 1`] = `
+====================================options=====================================
+parsers: ["php"]
+printWidth: 80
+                                                                                | printWidth
+=====================================input======================================
+<?php
+
+$var = 1;
+
+=====================================output=====================================
+<?php
+
+$var = 1;
+
+================================================================================
+`;
+
+exports[`php-tags-9.php 1`] = `
+====================================options=====================================
+parsers: ["php"]
+printWidth: 80
+                                                                                | printWidth
+=====================================input======================================
+<?php
+
+
+$var = 1;
+
+
+=====================================output=====================================
+<?php
+
+$var = 1;
+
+================================================================================
+`;
+
+exports[`php-tags-10.php 1`] = `
+====================================options=====================================
+parsers: ["php"]
+printWidth: 80
+                                                                                | printWidth
+=====================================input======================================
+<?php
+
+=====================================output=====================================
+<?php
+
+================================================================================
+`;
+
+exports[`php-tags-11.php 1`] = `
+====================================options=====================================
+parsers: ["php"]
+printWidth: 80
+                                                                                | printWidth
+=====================================input======================================
+<?php
+$var = 1;
+$var_2 = 2;
+$var_3 = 3;
+?>
+
+=====================================output=====================================
+<?php
+$var = 1;
+$var_2 = 2;
+$var_3 = 3;
+?>
+
+================================================================================
+`;
+
+exports[`php-tags-12.php 1`] = `
+====================================options=====================================
+parsers: ["php"]
+printWidth: 80
+                                                                                | printWidth
+=====================================input======================================
+<?php
+
+$var = 1;
+$var_2 = 2;
+$var_3 = 3;
+
+?>
+
+=====================================output=====================================
+<?php
+
+$var = 1;
+$var_2 = 2;
+$var_3 = 3;
+
+?>
+
+================================================================================
+`;
+
+exports[`php-tags-13.php 1`] = `
+====================================options=====================================
+parsers: ["php"]
+printWidth: 80
+                                                                                | printWidth
+=====================================input======================================
+<?php
+
+
+$var = 1;
+$var_2 = 2;
+$var_3 = 3;
+
+
+?>
+
+=====================================output=====================================
+<?php
+
+$var = 1;
+$var_2 = 2;
+$var_3 = 3;
+
+?>
+
+================================================================================
+`;
+
+exports[`php-tags-14.php 1`] = `
+====================================options=====================================
+parsers: ["php"]
+printWidth: 80
+                                                                                | printWidth
+=====================================input======================================
+<?php
+
+namespace Foo {
+    $var = 1;
+    $var_2 = 2;
+    $var_3 = 3;
+}
+
+
+?>
+
+=====================================output=====================================
+<?php
+
+namespace Foo {
+    $var = 1;
+    $var_2 = 2;
+    $var_3 = 3;
+}
+
+?>
+
+================================================================================
+`;
+
+exports[`php-tags-15.php 1`] = `
+====================================options=====================================
+parsers: ["php"]
+printWidth: 80
+                                                                                | printWidth
+=====================================input======================================
+<?php
+
+
+declare(strict_types=1);
+
+
+namespace Foo {
+    $var = 1;
+    $var_2 = 2;
+    $var_3 = 3;
+}
+
+
+?>
+
+=====================================output=====================================
+<?php
+
+declare(strict_types=1);
+
+namespace Foo {
+    $var = 1;
+    $var_2 = 2;
+    $var_3 = 3;
+}
+
+?>
+
+================================================================================
+`;
+
 exports[`traituse.php 1`] = `
 ====================================options=====================================
 parsers: ["php"]
diff --git a/tests/preserve_line/php-tags-10.php b/tests/preserve_line/php-tags-10.php
new file mode 100644
index 0000000..b3d9bbc
--- /dev/null
+++ b/tests/preserve_line/php-tags-10.php
@@ -0,0 +1,1 @@
+<?php
diff --git a/tests/preserve_line/php-tags-11.php b/tests/preserve_line/php-tags-11.php
new file mode 100644
index 0000000..e1f7f69
--- /dev/null
+++ b/tests/preserve_line/php-tags-11.php
@@ -0,0 +1,5 @@
+<?php
+$var = 1;
+$var_2 = 2;
+$var_3 = 3;
+?>
diff --git a/tests/preserve_line/php-tags-12.php b/tests/preserve_line/php-tags-12.php
new file mode 100644
index 0000000..75c3add
--- /dev/null
+++ b/tests/preserve_line/php-tags-12.php
@@ -0,0 +1,7 @@
+<?php
+
+$var = 1;
+$var_2 = 2;
+$var_3 = 3;
+
+?>
diff --git a/tests/preserve_line/php-tags-13.php b/tests/preserve_line/php-tags-13.php
new file mode 100644
index 0000000..165191b
--- /dev/null
+++ b/tests/preserve_line/php-tags-13.php
@@ -0,0 +1,9 @@
+<?php
+
+
+$var = 1;
+$var_2 = 2;
+$var_3 = 3;
+
+
+?>
diff --git a/tests/preserve_line/php-tags-14.php b/tests/preserve_line/php-tags-14.php
new file mode 100644
index 0000000..bfb6b48
--- /dev/null
+++ b/tests/preserve_line/php-tags-14.php
@@ -0,0 +1,10 @@
+<?php
+
+namespace Foo {
+    $var = 1;
+    $var_2 = 2;
+    $var_3 = 3;
+}
+
+
+?>
diff --git a/tests/preserve_line/php-tags-15.php b/tests/preserve_line/php-tags-15.php
new file mode 100644
index 0000000..6f5d9f1
--- /dev/null
+++ b/tests/preserve_line/php-tags-15.php
@@ -0,0 +1,14 @@
+<?php
+
+
+declare(strict_types=1);
+
+
+namespace Foo {
+    $var = 1;
+    $var_2 = 2;
+    $var_3 = 3;
+}
+
+
+?>
diff --git a/tests/preserve_line/php-tags-2.php b/tests/preserve_line/php-tags-2.php
new file mode 100644
index 0000000..96a8e99
--- /dev/null
+++ b/tests/preserve_line/php-tags-2.php
@@ -0,0 +1,4 @@
+<?php
+
+$var = 1;
+?>
diff --git a/tests/preserve_line/php-tags-3.php b/tests/preserve_line/php-tags-3.php
new file mode 100644
index 0000000..69be47c
--- /dev/null
+++ b/tests/preserve_line/php-tags-3.php
@@ -0,0 +1,4 @@
+<?php
+$var = 1;
+
+?>
diff --git a/tests/preserve_line/php-tags-4.php b/tests/preserve_line/php-tags-4.php
new file mode 100644
index 0000000..d01bcaf
--- /dev/null
+++ b/tests/preserve_line/php-tags-4.php
@@ -0,0 +1,5 @@
+<?php
+
+$var = 1;
+
+?>
diff --git a/tests/preserve_line/php-tags-5.php b/tests/preserve_line/php-tags-5.php
new file mode 100644
index 0000000..bbd0721
--- /dev/null
+++ b/tests/preserve_line/php-tags-5.php
@@ -0,0 +1,7 @@
+<?php
+
+
+$var = 1;
+
+
+?>
diff --git a/tests/preserve_line/php-tags-6.php b/tests/preserve_line/php-tags-6.php
new file mode 100644
index 0000000..8465477
--- /dev/null
+++ b/tests/preserve_line/php-tags-6.php
@@ -0,0 +1,1 @@
+<?php $var = 1; ?>
diff --git a/tests/preserve_line/php-tags-7.php b/tests/preserve_line/php-tags-7.php
new file mode 100644
index 0000000..4016522
--- /dev/null
+++ b/tests/preserve_line/php-tags-7.php
@@ -0,0 +1,2 @@
+<?php
+$var = 1;
diff --git a/tests/preserve_line/php-tags-8.php b/tests/preserve_line/php-tags-8.php
new file mode 100644
index 0000000..8fbaef3
--- /dev/null
+++ b/tests/preserve_line/php-tags-8.php
@@ -0,0 +1,3 @@
+<?php
+
+$var = 1;
diff --git a/tests/preserve_line/php-tags-9.php b/tests/preserve_line/php-tags-9.php
new file mode 100644
index 0000000..ef96e83
--- /dev/null
+++ b/tests/preserve_line/php-tags-9.php
@@ -0,0 +1,5 @@
+<?php
+
+
+$var = 1;
+
diff --git a/tests/preserve_line/php-tags.php b/tests/preserve_line/php-tags.php
new file mode 100644
index 0000000..c1d7ad7
--- /dev/null
+++ b/tests/preserve_line/php-tags.php
@@ -0,0 +1,3 @@
+<?php
+$var = 1;
+?>
diff --git a/tests/program/__snapshots__/jsfmt.spec.js.snap b/tests/program/__snapshots__/jsfmt.spec.js.snap
index 42033b9..bd4f5e8 100644
--- a/tests/program/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/program/__snapshots__/jsfmt.spec.js.snap
@@ -10,8 +10,7 @@ printWidth: 80
 ?>
 
 =====================================output=====================================
-<?php
-?>
+<?php ?>
 
 ================================================================================
 `;
@@ -27,8 +26,7 @@ printWidth: 80
 ?>
 
 =====================================output=====================================
-<?php
-?>
+<?php ?>
 
 ================================================================================
 `;
@@ -45,8 +43,7 @@ printWidth: 80
 ?>
 
 =====================================output=====================================
-<?php
-?>
+<?php ?>
 
 ================================================================================
 `;