Repository: prettier/plugin-php
Pull request #792 (2018-10-12 15:21:27)

fix: newline after namespace
In some cases we lose newline after namespace, let's fix it.

evilebottnawi: Feel free to feedback

Patch:
diff --git a/src/printer.js b/src/printer.js
index dc3afee..9a35442 100644
--- a/src/printer.js
+++ b/src/printer.js
@@ -19,7 +19,6 @@ const {
 } = require("prettier").doc.builders;
 const { willBreak, removeLines } = require("prettier").doc.utils;
 const {
-  isPreviousLineEmpty,
   isNextLineEmpty,
   isNextLineEmptyAfterIndex,
   getNextNonSpaceNonCommentCharacterIndex
@@ -49,7 +48,8 @@ const {
   isProgramLikeNode,
   getNodeKindIncludingLogical,
   hasEmptyBody,
-  hasNewline
+  hasNewline,
+  isNextLineEmptyAfterNamespace
 } = require("./util");
 
 function shouldPrintComma(options) {
@@ -1489,10 +1489,10 @@ function printNode(path, options, print) {
             ? indent(concat([hardline, printLines(path, options, print)]))
             : concat([
                 hardline,
-                isPreviousLineEmpty(
+                isNextLineEmptyAfterNamespace(
                   options.originalText,
-                  node.children[0],
-                  options
+                  node,
+                  options.locStart
                 )
                   ? hardline
                   : "",
diff --git a/src/util.js b/src/util.js
index 9c51c0e..59ce8d1 100644
--- a/src/util.js
+++ b/src/util.js
@@ -522,6 +522,7 @@ function skip(chars) {
 }
 
 const skipSpaces = skip(" \t");
+const skipEverythingButNewLine = skip(/[^\r\n]/);
 
 // This one doesn't use the above helper function because it wants to
 // test \r\n in order and `skip` doesn't support ordering and we only
@@ -591,6 +592,13 @@ function hasEmptyBody(path, name = "body") {
   );
 }
 
+function isNextLineEmptyAfterNamespace(text, node, locStart) {
+  let idx = locStart(node);
+  idx = skipEverythingButNewLine(text, idx);
+  idx = skipNewline(text, idx);
+  return hasNewline(text, idx);
+}
+
 module.exports = {
   printNumber,
   getPrecedence,
@@ -620,5 +628,6 @@ module.exports = {
   getNodeKindIncludingLogical,
   hasNewline,
   hasNewlineInRange,
-  hasEmptyBody
+  hasEmptyBody,
+  isNextLineEmptyAfterNamespace
 };


Test patch:
diff --git a/tests/comments/__snapshots__/jsfmt.spec.js.snap b/tests/comments/__snapshots__/jsfmt.spec.js.snap
index 0a6a259..6b55051 100644
--- a/tests/comments/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/comments/__snapshots__/jsfmt.spec.js.snap
@@ -3473,6 +3473,293 @@ namespace Monolog\\Handler;
 
 `;
 
+exports[`namespace-10.php - php-verify: namespace-10.php 1`] = `
+<?php
+
+namespace Monolog\\Formatter;
+
+/**
+ * formats the record to be used in the FlowdockHandler
+ *
+ * @author Dominik Liebler <liebler.dominik@gmail.com>
+ */
+class FlowdockFormatter
+{
+}
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+<?php
+
+namespace Monolog\\Formatter;
+
+/**
+ * formats the record to be used in the FlowdockHandler
+ *
+ * @author Dominik Liebler <liebler.dominik@gmail.com>
+ */
+class FlowdockFormatter
+{
+}
+
+`;
+
+exports[`namespace-11.php - php-verify: namespace-11.php 1`] = `
+<?php
+
+namespace Monolog\\Formatter;
+/**
+ * formats the record to be used in the FlowdockHandler
+ *
+ * @author Dominik Liebler <liebler.dominik@gmail.com>
+ */
+class FlowdockFormatter
+{
+}
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+<?php
+
+namespace Monolog\\Formatter;
+/**
+ * formats the record to be used in the FlowdockHandler
+ *
+ * @author Dominik Liebler <liebler.dominik@gmail.com>
+ */
+class FlowdockFormatter
+{
+}
+
+`;
+
+exports[`namespace-12.php - php-verify: namespace-12.php 1`] = `
+<?php
+
+// Comment
+namespace Monolog\\Formatter;
+// Comment
+
+/**
+ * formats the record to be used in the FlowdockHandler
+ *
+ * @author Dominik Liebler <liebler.dominik@gmail.com>
+ */
+class FlowdockFormatter
+{
+}
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+<?php
+
+// Comment
+namespace Monolog\\Formatter;
+// Comment
+
+/**
+ * formats the record to be used in the FlowdockHandler
+ *
+ * @author Dominik Liebler <liebler.dominik@gmail.com>
+ */
+class FlowdockFormatter
+{
+}
+
+`;
+
+exports[`namespace-13.php - php-verify: namespace-13.php 1`] = `
+<?php
+// Comment
+namespace Monolog\\Formatter;
+// Comment
+/**
+ * formats the record to be used in the FlowdockHandler
+ *
+ * @author Dominik Liebler <liebler.dominik@gmail.com>
+ */
+class FlowdockFormatter
+{
+}
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+<?php
+// Comment
+namespace Monolog\\Formatter;
+// Comment
+/**
+ * formats the record to be used in the FlowdockHandler
+ *
+ * @author Dominik Liebler <liebler.dominik@gmail.com>
+ */
+class FlowdockFormatter
+{
+}
+
+`;
+
+exports[`namespace-14.php - php-verify: namespace-14.php 1`] = `
+<?php
+
+namespace Monolog\\Formatter; // Comment
+
+/**
+ * formats the record to be used in the FlowdockHandler
+ *
+ * @author Dominik Liebler <liebler.dominik@gmail.com>
+ */
+class FlowdockFormatter
+{
+}
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+<?php
+
+namespace Monolog\\Formatter;
+
+// Comment
+
+/**
+ * formats the record to be used in the FlowdockHandler
+ *
+ * @author Dominik Liebler <liebler.dominik@gmail.com>
+ */
+class FlowdockFormatter
+{
+}
+
+`;
+
+exports[`namespace-15.php - php-verify: namespace-15.php 1`] = `
+<?php
+namespace Monolog\\Formatter; // Comment
+/**
+ * formats the record to be used in the FlowdockHandler
+ *
+ * @author Dominik Liebler <liebler.dominik@gmail.com>
+ */
+class FlowdockFormatter
+{
+}
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+<?php
+namespace Monolog\\Formatter;
+// Comment
+/**
+ * formats the record to be used in the FlowdockHandler
+ *
+ * @author Dominik Liebler <liebler.dominik@gmail.com>
+ */
+class FlowdockFormatter
+{
+}
+
+`;
+
+exports[`namespace-16.php - php-verify: namespace-16.php 1`] = `
+<?php
+namespace /* Comment */ Monolog\\Formatter /* Comment */; /* Comment */
+/**
+ * formats the record to be used in the FlowdockHandler
+ *
+ * @author Dominik Liebler <liebler.dominik@gmail.com>
+ */
+class FlowdockFormatter
+{
+}
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+<?php
+namespace Monolog\\Formatter;
+/* Comment */ /* Comment */ /* Comment */
+/**
+ * formats the record to be used in the FlowdockHandler
+ *
+ * @author Dominik Liebler <liebler.dominik@gmail.com>
+ */
+class FlowdockFormatter
+{
+}
+
+`;
+
+exports[`namespace-17.php - php-verify: namespace-17.php 1`] = `
+<?php
+
+namespace /* Comment */ Monolog\\Formatter /* Comment */; /* Comment */
+
+/**
+ * formats the record to be used in the FlowdockHandler
+ *
+ * @author Dominik Liebler <liebler.dominik@gmail.com>
+ */
+class FlowdockFormatter
+{
+}
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+<?php
+
+namespace Monolog\\Formatter;
+
+/* Comment */ /* Comment */ /* Comment */
+
+/**
+ * formats the record to be used in the FlowdockHandler
+ *
+ * @author Dominik Liebler <liebler.dominik@gmail.com>
+ */
+class FlowdockFormatter
+{
+}
+
+`;
+
+exports[`namespace-18.php - php-verify: namespace-18.php 1`] = `
+<?php
+
+namespace /* Comment */ Monolog\\Formatter /* Comment */; /* Comment */ // Comment
+
+/**
+ * formats the record to be used in the FlowdockHandler
+ *
+ * @author Dominik Liebler <liebler.dominik@gmail.com>
+ */
+class FlowdockFormatter
+{
+}
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+<?php
+
+namespace Monolog\\Formatter;
+
+/* Comment */ /* Comment */ /* Comment */ // Comment
+
+/**
+ * formats the record to be used in the FlowdockHandler
+ *
+ * @author Dominik Liebler <liebler.dominik@gmail.com>
+ */
+class FlowdockFormatter
+{
+}
+
+`;
+
+exports[`namespace-19.php - php-verify: namespace-19.php 1`] = `
+<?php
+namespace Foo; // Comment
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+<?php
+namespace Foo; // Comment
+
+`;
+
+exports[`namespace-20.php - php-verify: namespace-20.php 1`] = `
+<?php
+namespace Foo; // Comment
+
+use Foo\\Bar;
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+<?php
+namespace Foo;
+
+// Comment
+
+use Foo\\Bar;
+
+`;
+
 exports[`new.php - php-verify: new.php 1`] = `
 <?php
 $a = new Foo(/*Comment*/);
diff --git a/tests/comments/namespace-10.php b/tests/comments/namespace-10.php
new file mode 100644
index 0000000..73edd3b
--- /dev/null
+++ b/tests/comments/namespace-10.php
@@ -0,0 +1,12 @@
+<?php
+
+namespace Monolog\Formatter;
+
+/**
+ * formats the record to be used in the FlowdockHandler
+ *
+ * @author Dominik Liebler <liebler.dominik@gmail.com>
+ */
+class FlowdockFormatter
+{
+}
diff --git a/tests/comments/namespace-11.php b/tests/comments/namespace-11.php
new file mode 100644
index 0000000..bb392fd
--- /dev/null
+++ b/tests/comments/namespace-11.php
@@ -0,0 +1,11 @@
+<?php
+
+namespace Monolog\Formatter;
+/**
+ * formats the record to be used in the FlowdockHandler
+ *
+ * @author Dominik Liebler <liebler.dominik@gmail.com>
+ */
+class FlowdockFormatter
+{
+}
diff --git a/tests/comments/namespace-12.php b/tests/comments/namespace-12.php
new file mode 100644
index 0000000..352ef65
--- /dev/null
+++ b/tests/comments/namespace-12.php
@@ -0,0 +1,14 @@
+<?php
+
+// Comment
+namespace Monolog\Formatter;
+// Comment
+
+/**
+ * formats the record to be used in the FlowdockHandler
+ *
+ * @author Dominik Liebler <liebler.dominik@gmail.com>
+ */
+class FlowdockFormatter
+{
+}
diff --git a/tests/comments/namespace-13.php b/tests/comments/namespace-13.php
new file mode 100644
index 0000000..02f447f
--- /dev/null
+++ b/tests/comments/namespace-13.php
@@ -0,0 +1,12 @@
+<?php
+// Comment
+namespace Monolog\Formatter;
+// Comment
+/**
+ * formats the record to be used in the FlowdockHandler
+ *
+ * @author Dominik Liebler <liebler.dominik@gmail.com>
+ */
+class FlowdockFormatter
+{
+}
diff --git a/tests/comments/namespace-14.php b/tests/comments/namespace-14.php
new file mode 100644
index 0000000..d5be8cd
--- /dev/null
+++ b/tests/comments/namespace-14.php
@@ -0,0 +1,12 @@
+<?php
+
+namespace Monolog\Formatter; // Comment
+
+/**
+ * formats the record to be used in the FlowdockHandler
+ *
+ * @author Dominik Liebler <liebler.dominik@gmail.com>
+ */
+class FlowdockFormatter
+{
+}
diff --git a/tests/comments/namespace-15.php b/tests/comments/namespace-15.php
new file mode 100644
index 0000000..4d06c59
--- /dev/null
+++ b/tests/comments/namespace-15.php
@@ -0,0 +1,10 @@
+<?php
+namespace Monolog\Formatter; // Comment
+/**
+ * formats the record to be used in the FlowdockHandler
+ *
+ * @author Dominik Liebler <liebler.dominik@gmail.com>
+ */
+class FlowdockFormatter
+{
+}
diff --git a/tests/comments/namespace-16.php b/tests/comments/namespace-16.php
new file mode 100644
index 0000000..e1ef43b
--- /dev/null
+++ b/tests/comments/namespace-16.php
@@ -0,0 +1,10 @@
+<?php
+namespace /* Comment */ Monolog\Formatter /* Comment */; /* Comment */
+/**
+ * formats the record to be used in the FlowdockHandler
+ *
+ * @author Dominik Liebler <liebler.dominik@gmail.com>
+ */
+class FlowdockFormatter
+{
+}
diff --git a/tests/comments/namespace-17.php b/tests/comments/namespace-17.php
new file mode 100644
index 0000000..94a2667
--- /dev/null
+++ b/tests/comments/namespace-17.php
@@ -0,0 +1,12 @@
+<?php
+
+namespace /* Comment */ Monolog\Formatter /* Comment */; /* Comment */
+
+/**
+ * formats the record to be used in the FlowdockHandler
+ *
+ * @author Dominik Liebler <liebler.dominik@gmail.com>
+ */
+class FlowdockFormatter
+{
+}
diff --git a/tests/comments/namespace-18.php b/tests/comments/namespace-18.php
new file mode 100644
index 0000000..beb7364
--- /dev/null
+++ b/tests/comments/namespace-18.php
@@ -0,0 +1,12 @@
+<?php
+
+namespace /* Comment */ Monolog\Formatter /* Comment */; /* Comment */ // Comment
+
+/**
+ * formats the record to be used in the FlowdockHandler
+ *
+ * @author Dominik Liebler <liebler.dominik@gmail.com>
+ */
+class FlowdockFormatter
+{
+}
diff --git a/tests/comments/namespace-19.php b/tests/comments/namespace-19.php
new file mode 100644
index 0000000..19c4827
--- /dev/null
+++ b/tests/comments/namespace-19.php
@@ -0,0 +1,2 @@
+<?php
+namespace Foo; // Comment
diff --git a/tests/comments/namespace-20.php b/tests/comments/namespace-20.php
new file mode 100644
index 0000000..badbaf8
--- /dev/null
+++ b/tests/comments/namespace-20.php
@@ -0,0 +1,4 @@
+<?php
+namespace Foo; // Comment
+
+use Foo\Bar;
diff --git a/tests/namespace/__snapshots__/jsfmt.spec.js.snap b/tests/namespace/__snapshots__/jsfmt.spec.js.snap
index e7127ba..8f1de3c 100644
--- a/tests/namespace/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/namespace/__snapshots__/jsfmt.spec.js.snap
@@ -51,6 +51,7 @@ call();
 <?php
 
 namespace Foo;
+
 call();
 
 `;
diff --git a/tests/parens/__snapshots__/jsfmt.spec.js.snap b/tests/parens/__snapshots__/jsfmt.spec.js.snap
index 864a708..f17e8db 100644
--- a/tests/parens/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/parens/__snapshots__/jsfmt.spec.js.snap
@@ -2082,6 +2082,7 @@ namespace Foo;
 <?php
 
 namespace Foo;
+
 $a->c();
 
 `;