Repository: prettier/plugin-php
Pull request #784 (2018-10-11 17:37:48)

fix: extra newline after php tag with inline nodes
I think we should refactor all around `inline` nodes, i will see what i can do

evilebottnawi: Feel free to feedback

Patch:
diff --git a/src/printer.js b/src/printer.js
index aa6b5ab..fc43561 100644
--- a/src/printer.js
+++ b/src/printer.js
@@ -927,7 +927,8 @@ function printLines(path, options, print, childrenAttribute = "children") {
           : "";
         const shouldBreak = end - start > 1;
         const before = shouldBreak
-          ? isBlockNestedNode && !prevInlineNode
+          ? (isBlockNestedNode && !prevInlineNode) ||
+            (isProgramLikeNode(node) && start === 0)
             ? ""
             : hardline
           : "";


Test patch:
diff --git a/tests/halt/__snapshots__/jsfmt.spec.js.snap b/tests/halt/__snapshots__/jsfmt.spec.js.snap
index 1db8516..6a2d793 100644
--- a/tests/halt/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/halt/__snapshots__/jsfmt.spec.js.snap
@@ -27,7 +27,6 @@ __HALT_COMPILER();
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 <?php
 
-
 $var = 1;
 
 $var = 1;
diff --git a/tests/inline/__snapshots__/jsfmt.spec.js.snap b/tests/inline/__snapshots__/jsfmt.spec.js.snap
index b95a663..6a56b01 100644
--- a/tests/inline/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/inline/__snapshots__/jsfmt.spec.js.snap
@@ -1623,6 +1623,278 @@ func(
 
 `;
 
+exports[`mixed-38.php - php-verify: mixed-38.php 1`] = `
+<?php
+$var = 1;
+$var = 2;
+?>
+<div></div>
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+<?php
+$var = 1;
+$var = 2;
+?>
+<div></div>
+
+`;
+
+exports[`mixed-39.php - php-verify: mixed-39.php 1`] = `
+<?php
+
+$var = 1;
+$var = 2;
+?>
+<div></div>
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+<?php
+
+$var = 1;
+$var = 2;
+?>
+<div></div>
+
+`;
+
+exports[`mixed-40.php - php-verify: mixed-40.php 1`] = `
+<?php
+
+
+$var = 1;
+$var = 2;
+?>
+<div></div>
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+<?php
+
+$var = 1;
+$var = 2;
+?>
+<div></div>
+
+`;
+
+exports[`mixed-41.php - php-verify: mixed-41.php 1`] = `
+<?php
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+<?php
+
+`;
+
+exports[`mixed-42.php - php-verify: mixed-42.php 1`] = `
+<?php
+?>
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+<?php
+?>
+
+`;
+
+exports[`mixed-43.php - php-verify: mixed-43.php 1`] = `
+<?php
+
+?>
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+<?php
+?>
+
+`;
+
+exports[`mixed-44.php - php-verify: mixed-44.php 1`] = `
+<?php
+declare(strict_types=1);
+$var = 1;
+$var = 2;
+?>
+<div></div>
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+<?php
+declare(strict_types=1);
+$var = 1;
+$var = 2;
+?>
+<div></div>
+
+`;
+
+exports[`mixed-45.php - php-verify: mixed-45.php 1`] = `
+<?php
+
+declare(strict_types=1);
+
+$var = 1;
+$var = 2;
+?>
+<div></div>
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+<?php
+
+declare(strict_types=1);
+
+$var = 1;
+$var = 2;
+?>
+<div></div>
+
+`;
+
+exports[`mixed-46.php - php-verify: mixed-46.php 1`] = `
+<?php
+
+
+declare(strict_types=1);
+
+
+$var = 1;
+$var = 2;
+?>
+<div></div>
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+<?php
+
+declare(strict_types=1);
+
+$var = 1;
+$var = 2;
+?>
+<div></div>
+
+`;
+
+exports[`mixed-47.php - php-verify: mixed-47.php 1`] = `
+<?php
+namespace Foo;
+$var = 1;
+$var = 2;
+?>
+<div></div>
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+<?php
+namespace Foo;
+
+$var = 1;
+$var = 2;
+?>
+<div></div>
+
+`;
+
+exports[`mixed-48.php - php-verify: mixed-48.php 1`] = `
+<?php
+
+namespace Foo;
+
+$var = 1;
+$var = 2;
+?>
+<div></div>
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+<?php
+
+namespace Foo;
+
+$var = 1;
+$var = 2;
+?>
+<div></div>
+
+`;
+
+exports[`mixed-49.php - php-verify: mixed-49.php 1`] = `
+<?php
+
+
+namespace Foo;
+
+
+$var = 1;
+$var = 2;
+?>
+<div></div>
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+<?php
+
+namespace Foo;
+
+$var = 1;
+$var = 2;
+?>
+<div></div>
+
+`;
+
+exports[`mixed-50.php - php-verify: mixed-50.php 1`] = `
+<?php
+declare(strict_types=1);
+namespace Vendor\\Package;
+$var = 1;
+$var = 2;
+?>
+<div></div>
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+<?php
+declare(strict_types=1);
+namespace Vendor\\Package;
+
+$var = 1;
+$var = 2;
+?>
+<div></div>
+
+`;
+
+exports[`mixed-51.php - php-verify: mixed-51.php 1`] = `
+<?php
+
+declare(strict_types=1);
+
+namespace Vendor\\Package;
+
+$var = 1;
+$var = 2;
+?>
+<div></div>
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+<?php
+
+declare(strict_types=1);
+
+namespace Vendor\\Package;
+
+$var = 1;
+$var = 2;
+?>
+<div></div>
+
+`;
+
+exports[`mixed-52.php - php-verify: mixed-52.php 1`] = `
+<?php
+
+
+declare(strict_types=1);
+
+
+namespace Vendor\\Package;
+
+
+$var = 1;
+$var = 2;
+?>
+<div></div>
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+<?php
+
+declare(strict_types=1);
+
+namespace Vendor\\Package;
+
+$var = 1;
+$var = 2;
+?>
+<div></div>
+
+`;
+
 exports[`only-html.php - php-verify: only-html.php 1`] = `
 <!DOCTYPE html>
 <html>
diff --git a/tests/inline/mixed-38.php b/tests/inline/mixed-38.php
new file mode 100644
index 0000000..182b367
--- /dev/null
+++ b/tests/inline/mixed-38.php
@@ -0,0 +1,5 @@
+<?php
+$var = 1;
+$var = 2;
+?>
+<div></div>
diff --git a/tests/inline/mixed-39.php b/tests/inline/mixed-39.php
new file mode 100644
index 0000000..84aed94
--- /dev/null
+++ b/tests/inline/mixed-39.php
@@ -0,0 +1,6 @@
+<?php
+
+$var = 1;
+$var = 2;
+?>
+<div></div>
diff --git a/tests/inline/mixed-40.php b/tests/inline/mixed-40.php
new file mode 100644
index 0000000..a5f3b2a
--- /dev/null
+++ b/tests/inline/mixed-40.php
@@ -0,0 +1,7 @@
+<?php
+
+
+$var = 1;
+$var = 2;
+?>
+<div></div>
diff --git a/tests/inline/mixed-41.php b/tests/inline/mixed-41.php
new file mode 100644
index 0000000..b3d9bbc
--- /dev/null
+++ b/tests/inline/mixed-41.php
@@ -0,0 +1,1 @@
+<?php
diff --git a/tests/inline/mixed-42.php b/tests/inline/mixed-42.php
new file mode 100644
index 0000000..acb6c35
--- /dev/null
+++ b/tests/inline/mixed-42.php
@@ -0,0 +1,2 @@
+<?php
+?>
diff --git a/tests/inline/mixed-43.php b/tests/inline/mixed-43.php
new file mode 100644
index 0000000..62a2de0
--- /dev/null
+++ b/tests/inline/mixed-43.php
@@ -0,0 +1,3 @@
+<?php
+
+?>
diff --git a/tests/inline/mixed-44.php b/tests/inline/mixed-44.php
new file mode 100644
index 0000000..13a55c1
--- /dev/null
+++ b/tests/inline/mixed-44.php
@@ -0,0 +1,6 @@
+<?php
+declare(strict_types=1);
+$var = 1;
+$var = 2;
+?>
+<div></div>
diff --git a/tests/inline/mixed-45.php b/tests/inline/mixed-45.php
new file mode 100644
index 0000000..39e7cf4
--- /dev/null
+++ b/tests/inline/mixed-45.php
@@ -0,0 +1,8 @@
+<?php
+
+declare(strict_types=1);
+
+$var = 1;
+$var = 2;
+?>
+<div></div>
diff --git a/tests/inline/mixed-46.php b/tests/inline/mixed-46.php
new file mode 100644
index 0000000..a3c3873
--- /dev/null
+++ b/tests/inline/mixed-46.php
@@ -0,0 +1,10 @@
+<?php
+
+
+declare(strict_types=1);
+
+
+$var = 1;
+$var = 2;
+?>
+<div></div>
diff --git a/tests/inline/mixed-47.php b/tests/inline/mixed-47.php
new file mode 100644
index 0000000..c7e48f6
--- /dev/null
+++ b/tests/inline/mixed-47.php
@@ -0,0 +1,6 @@
+<?php
+namespace Foo;
+$var = 1;
+$var = 2;
+?>
+<div></div>
diff --git a/tests/inline/mixed-48.php b/tests/inline/mixed-48.php
new file mode 100644
index 0000000..5f4834b
--- /dev/null
+++ b/tests/inline/mixed-48.php
@@ -0,0 +1,8 @@
+<?php
+
+namespace Foo;
+
+$var = 1;
+$var = 2;
+?>
+<div></div>
diff --git a/tests/inline/mixed-49.php b/tests/inline/mixed-49.php
new file mode 100644
index 0000000..7fbc1e5
--- /dev/null
+++ b/tests/inline/mixed-49.php
@@ -0,0 +1,10 @@
+<?php
+
+
+namespace Foo;
+
+
+$var = 1;
+$var = 2;
+?>
+<div></div>
diff --git a/tests/inline/mixed-50.php b/tests/inline/mixed-50.php
new file mode 100644
index 0000000..8b27813
--- /dev/null
+++ b/tests/inline/mixed-50.php
@@ -0,0 +1,7 @@
+<?php
+declare(strict_types=1);
+namespace Vendor\Package;
+$var = 1;
+$var = 2;
+?>
+<div></div>
diff --git a/tests/inline/mixed-51.php b/tests/inline/mixed-51.php
new file mode 100644
index 0000000..cd44fbb
--- /dev/null
+++ b/tests/inline/mixed-51.php
@@ -0,0 +1,10 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Vendor\Package;
+
+$var = 1;
+$var = 2;
+?>
+<div></div>
diff --git a/tests/inline/mixed-52.php b/tests/inline/mixed-52.php
new file mode 100644
index 0000000..666e311
--- /dev/null
+++ b/tests/inline/mixed-52.php
@@ -0,0 +1,13 @@
+<?php
+
+
+declare(strict_types=1);
+
+
+namespace Vendor\Package;
+
+
+$var = 1;
+$var = 2;
+?>
+<div></div>