Repository: prettier/plugin-php
Pull request #194 (2018-03-11 16:53:59)

tests: newlines
> All PHP files MUST use the Unix LF (linefeed) line ending only.

> All PHP files MUST end with a non-blank line, terminated with a single LF.

Just tests

evilebottnawi: Ready for review :+1: 

Patch:
diff --git a/.gitattributes b/.gitattributes
new file mode 100644
index 0000000..3b668f7
--- /dev/null
+++ b/.gitattributes
@@ -0,0 +1,1 @@
+tests/newline/CRLF.php text eol=crlf
diff --git a/src/printer.js b/src/printer.js
index 5f6049e..902d012 100644
--- a/src/printer.js
+++ b/src/printer.js
@@ -1,6 +1,7 @@
 "use strict";
 
 const docBuilders = require("prettier").doc.builders;
+const docUtils = require("prettier").doc.utils;
 const sharedUtil = require("prettier").util;
 
 const util = require("./util");
@@ -14,6 +15,7 @@ const indent = docBuilders.indent;
 const ifBreak = docBuilders.ifBreak;
 const hardline = docBuilders.hardline;
 const softline = docBuilders.softline;
+const willBreak = docUtils.willBreak;
 
 const makeString = sharedUtil.makeString;
 
@@ -281,6 +283,12 @@ function printArgumentsList(path, options, print, argumentsKey = "arguments") {
   if (printed.length === 0) {
     return "()";
   }
+  const breakingIndex = printed.findIndex(willBreak);
+  const breakingInTheMiddle =
+    printed.length > 2 &&
+    breakingIndex !== 0 &&
+    breakingIndex !== printed.length - 1;
+
   const shouldGroupLast = getLast(args).kind === "array";
   const shouldGroupFirst = !shouldGroupLast && args[0].kind === "array";
   const shortForm = concat(["(", join(", ", printed), ")"]);
@@ -302,17 +310,17 @@ function printArgumentsList(path, options, print, argumentsKey = "arguments") {
   const longForm = group(
     concat([
       "(",
-      indent(concat([line, join(concat([",", line]), printed)])),
-      line,
+      indent(concat([softline, join(concat([",", line]), printed)])),
+      softline,
       ")"
-    ]),
-    { shouldBreak: true }
+    ])
   );
-  const formsToConsider = [shortForm, longForm];
-  if (shouldGroupFirst || shouldGroupLast) {
-    formsToConsider.splice(1, 0, mediumForm);
-  }
-  return conditionalGroup(formsToConsider);
+  const formsToConsider = [
+    !breakingInTheMiddle ? shortForm : null,
+    shouldGroupFirst || shouldGroupLast ? mediumForm : null,
+    longForm
+  ];
+  return conditionalGroup(formsToConsider.filter(Boolean));
 }
 
 function wrapPropertyLookup(node, doc) {
@@ -323,6 +331,16 @@ function wrapPropertyLookup(node, doc) {
   return addCurly ? concat(["{", doc, "}"]) : doc;
 }
 
+function isPropertyLookupChain(node) {
+  if (node.kind !== "propertylookup") {
+    return false;
+  }
+  if (node.what.kind === "variable") {
+    return true;
+  }
+  return isPropertyLookupChain(node.what);
+}
+
 const expressionKinds = [
   "array",
   "variable",
@@ -354,13 +372,14 @@ function printExpression(path, options, print) {
   function printLookup(node) {
     switch (node.kind) {
       case "propertylookup": {
+        const canBreak = path.stack.indexOf("left") === -1;
         return group(
           concat([
             path.call(print, "what"),
             "->",
             indent(
               concat([
-                softline,
+                canBreak ? softline : "",
                 wrapPropertyLookup(node, path.call(print, "offset"))
               ])
             )
@@ -922,17 +941,22 @@ function printStatement(path, options, print) {
   }
 
   switch (node.kind) {
-    case "assign":
+    case "assign": {
+      const canBreak =
+        node.right.kind === "bin" ||
+        ["number", "string"].indexOf(node.right.kind) > -1 ||
+        isPropertyLookupChain(node.right);
       return group(
         concat([
           path.call(print, "left"),
           " ",
           node.operator,
-          node.right.kind === "bin"
+          canBreak
             ? indent(concat([line, path.call(print, "right")]))
             : concat([" ", path.call(print, "right")])
         ])
       );
+    }
     case "if": {
       const handleIfAlternate = alternate => {
         if (!alternate) {


Test patch:
diff --git a/tests/call/__snapshots__/jsfmt.spec.js.snap b/tests/call/__snapshots__/jsfmt.spec.js.snap
index 13edfb2..050d7b9 100644
--- a/tests/call/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/call/__snapshots__/jsfmt.spec.js.snap
@@ -95,6 +95,14 @@ $some->other->thing(array(
     11323123,
     1231, 13231233243, 324234234
 ));
+
+$foo->bar(
+    $arg1,
+    function ($arg2) use ($var1) {
+        // body
+    },
+    $arg3
+);
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 <?php
 func();
@@ -194,4 +202,12 @@ $some->other->thing(
     array(11323123, 1231, 13231233243, 324234234)
 );
 
+$foo->bar(
+    $arg1,
+    function ($arg2) use ($var1) {
+        // body
+    },
+    $arg3
+);
+
 `;
diff --git a/tests/call/call.php b/tests/call/call.php
index b8ca852..7e84f8e 100644
--- a/tests/call/call.php
+++ b/tests/call/call.php
@@ -92,3 +92,11 @@ $some->other->thing(array(
     11323123,
     1231, 13231233243, 324234234
 ));
+
+$foo->bar(
+    $arg1,
+    function ($arg2) use ($var1) {
+        // body
+    },
+    $arg3
+);
diff --git a/tests/closure/__snapshots__/jsfmt.spec.js.snap b/tests/closure/__snapshots__/jsfmt.spec.js.snap
index 194deac..45d353b 100644
--- a/tests/closure/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/closure/__snapshots__/jsfmt.spec.js.snap
@@ -108,9 +108,11 @@ $longArgs_shortVars = function (
     // body
 };
 
-$shortArgs_longVars = function (
-    $arg
-) use ($longVar1, $longerVar2, $muchLongerVar3) {
+$shortArgs_longVars = function ($arg) use (
+    $longVar1,
+    $longerVar2,
+    $muchLongerVar3
+) {
     // body
 };
 
diff --git a/tests/newline/CRLF.php b/tests/newline/CRLF.php
new file mode 100644
index 0000000..6065e95
--- /dev/null
+++ b/tests/newline/CRLF.php
@@ -0,0 +1,2 @@
+<?php
+$test = 1;
diff --git a/tests/newline/LF.php b/tests/newline/LF.php
new file mode 100644
index 0000000..6065e95
--- /dev/null
+++ b/tests/newline/LF.php
@@ -0,0 +1,2 @@
+<?php
+$test = 1;
diff --git a/tests/newline/__snapshots__/jsfmt.spec.js.snap b/tests/newline/__snapshots__/jsfmt.spec.js.snap
new file mode 100644
index 0000000..bcc763e
--- /dev/null
+++ b/tests/newline/__snapshots__/jsfmt.spec.js.snap
@@ -0,0 +1,38 @@
+// Jest Snapshot v1, https://goo.gl/fbAQLP
+
+exports[`CRLF.php 1`] = `
+<?php
+$test = 1;
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+<?php
+$test = 1;
+
+`;
+
+exports[`LF.php 1`] = `
+<?php
+$test = 1;
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+<?php
+$test = 1;
+
+`;
+
+exports[`file-terminated-with-multiple-LF.php 1`] = `
+<?php
+$test = 1;
+
+
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+<?php
+$test = 1;
+
+`;
+
+exports[`file-terminated-without-line-ending.php 1`] = `
+<?php
+$test = 1;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+<?php
+$test = 1;
+
+`;
diff --git a/tests/newline/file-terminated-with-multiple-LF.php b/tests/newline/file-terminated-with-multiple-LF.php
new file mode 100644
index 0000000..2ff19cb
--- /dev/null
+++ b/tests/newline/file-terminated-with-multiple-LF.php
@@ -0,0 +1,4 @@
+<?php
+$test = 1;
+
+
diff --git a/tests/newline/file-terminated-without-line-ending.php b/tests/newline/file-terminated-without-line-ending.php
new file mode 100644
index 0000000..a7b7f3a
--- /dev/null
+++ b/tests/newline/file-terminated-without-line-ending.php
@@ -0,0 +1,2 @@
+<?php
+$test = 1;
\ No newline at end of file
diff --git a/tests/newline/jsfmt.spec.js b/tests/newline/jsfmt.spec.js
new file mode 100644
index 0000000..e395434
--- /dev/null
+++ b/tests/newline/jsfmt.spec.js
@@ -0,0 +1,1 @@
+run_spec(__dirname, ["php"]);
diff --git a/tests/propertylookup/__snapshots__/jsfmt.spec.js.snap b/tests/propertylookup/__snapshots__/jsfmt.spec.js.snap
index 32f6843..2b14b03 100644
--- a/tests/propertylookup/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/propertylookup/__snapshots__/jsfmt.spec.js.snap
@@ -12,6 +12,13 @@ $b = $this->{foo['bar']};
 $b = $this->{'foo' . $bar};
 $b = $this->{self::STUFF};
 $object = $this->{'bar' . $foo}->buzz();
+
+$this->loooooooooooongloooooooooooongloooooooooooonglookup = "teeeeeeeeeeeeeeeeeeeeest";
+$this->loooooooooooong->loooooooooooong->loooooooooooong->lookup = "teeeeeeeeeeest";
+$this->loooooooooooong->loooooooooooong->loooooooooooong->lookup = $other->looooooooooong->stuff;
+$this->loooooooooooong->lookup = (int) $this->getRequest()->getParam(
+  'instance-resource-id'
+);
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 <?php
 $a = $obj->value;
@@ -24,4 +31,14 @@ $b = $this->{'foo' . $bar};
 $b = $this->{self::STUFF};
 $object = $this->{'bar' . $foo}->buzz();
 
+$this->loooooooooooongloooooooooooongloooooooooooonglookup =
+    "teeeeeeeeeeeeeeeeeeeeest";
+$this->loooooooooooong->loooooooooooong->loooooooooooong->lookup =
+    "teeeeeeeeeeest";
+$this->loooooooooooong->loooooooooooong->loooooooooooong->lookup =
+    $other->looooooooooong->stuff;
+$this->loooooooooooong->lookup = (int) $this->getRequest()->getParam(
+    'instance-resource-id'
+);
+
 `;
diff --git a/tests/propertylookup/propertylookup.php b/tests/propertylookup/propertylookup.php
index 55d3aae..ec6d08e 100644
--- a/tests/propertylookup/propertylookup.php
+++ b/tests/propertylookup/propertylookup.php
@@ -9,3 +9,10 @@ $b = $this->{foo['bar']};
 $b = $this->{'foo' . $bar};
 $b = $this->{self::STUFF};
 $object = $this->{'bar' . $foo}->buzz();
+
+$this->loooooooooooongloooooooooooongloooooooooooonglookup = "teeeeeeeeeeeeeeeeeeeeest";
+$this->loooooooooooong->loooooooooooong->loooooooooooong->lookup = "teeeeeeeeeeest";
+$this->loooooooooooong->loooooooooooong->loooooooooooong->lookup = $other->looooooooooong->stuff;
+$this->loooooooooooong->lookup = (int) $this->getRequest()->getParam(
+  'instance-resource-id'
+);