Repository: jlongster/prettier (js)

Issue:
Inserting space before comma in objects with non-trivial value
It seems when I have an object with a key pointing to a non-trivial value and am using trailing commas, prettier inserts a space before the comma. Since it does not insert a space before commas normally, I assume this is a bug. FWIW, I couldn't reproduce on the live editor linked from this github repo (which appears to be based on the last release), so I'm assuming this is a recent problem introduced in master.

I created a failing spec here: https://github.com/joefiorini/prettier/blob/space-before-trailing-comma/tests/trailing_comma/object.js. I took a stab at making it pass, but don't have time atm to wrap my head around how it all works.

Pull request description:
Fix spurious whitespace
This was introduced by #314 where `line` should have been `softline`. By the way, I was going to propose renaming `line` to `line_or_space` and `softline` to `line_or_nothing` which should make it more explicit what is going on.

Fixes #461

Patch:
diff --git a/src/printer.js b/src/printer.js
index 28990dde8..66d071628 100644
--- a/src/printer.js
+++ b/src/printer.js
@@ -627,7 +627,7 @@ function genericPrintNoParens(path, options, print) {
                   indent(options.tabWidth, concat([softline, printedValue]))
                 ])
               ),
-              line,
+              softline,
               ifBreak(")")
             ])
           );


Test patch:
diff --git a/tests/flow/logical/__snapshots__/jsfmt.spec.js.snap b/tests/flow/logical/__snapshots__/jsfmt.spec.js.snap
index f3cab28a7..2ff1eac92 100644
--- a/tests/flow/logical/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/flow/logical/__snapshots__/jsfmt.spec.js.snap
@@ -901,14 +901,14 @@ function logical12b(y: number): number {
  */
 function logical13(x: number): Array<{ x: string }> {
   return [
-    { x: x && \"bar\"  },
-    { x: true && \"bar\"  },
-    { x: true && false  },
-    { x: false && false  },
-    { x: 1 && \"bar\"  },
-    { x: \"foo\" && \"bar\"  },
-    { x: \"foo\" && \"bar\"  },
-    { x: \"foo\" && \"bar\"  }
+    { x: x && \"bar\" },
+    { x: true && \"bar\" },
+    { x: true && false },
+    { x: false && false },
+    { x: 1 && \"bar\" },
+    { x: \"foo\" && \"bar\" },
+    { x: \"foo\" && \"bar\" },
+    { x: \"foo\" && \"bar\" }
   ];
 }
 
@@ -917,14 +917,14 @@ function logical13(x: number): Array<{ x: string }> {
  */
 function logical14(x: number): Array<{ x: string }> {
   return [
-    { x: x || \"bar\"  },
-    { x: false || \"bar\"  },
-    { x: false || true  },
-    { x: true || false  },
-    { x: 0 || \"bar\"  },
-    { x: \"foo\" || \"bar\"  },
-    { x: \"foo\" || \"bar\"  },
-    { x: \"foo\" || \"bar\"  }
+    { x: x || \"bar\" },
+    { x: false || \"bar\" },
+    { x: false || true },
+    { x: true || false },
+    { x: 0 || \"bar\" },
+    { x: \"foo\" || \"bar\" },
+    { x: \"foo\" || \"bar\" },
+    { x: \"foo\" || \"bar\" }
   ];
 }
 
diff --git a/tests/trailing_comma/__snapshots__/jsfmt.spec.js.snap b/tests/trailing_comma/__snapshots__/jsfmt.spec.js.snap
index d6a325b0a..b03422f52 100644
--- a/tests/trailing_comma/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/trailing_comma/__snapshots__/jsfmt.spec.js.snap
@@ -77,6 +77,11 @@ const aLong = {
   },
   dHasALongName: \'a-long-value-too\'
 };
+
+const aLong = {
+  dHasALongName: \'a-long-value-too\',
+  eHasABooleanExpression: a === a,
+};
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 const a = { b: true, c: { c1: \"hello\" }, d: false };
 
@@ -88,6 +93,11 @@ const aLong = {
   },
   dHasALongName: \"a-long-value-too\"
 };
+
+const aLong = {
+  dHasALongName: \"a-long-value-too\",
+  eHasABooleanExpression: a === a
+};
 "
 `;
 
@@ -108,6 +118,11 @@ const aLong = {
   },
   dHasALongName: \'a-long-value-too\'
 };
+
+const aLong = {
+  dHasALongName: \'a-long-value-too\',
+  eHasABooleanExpression: a === a,
+};
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 const a = { b: true, c: { c1: \"hello\" }, d: false };
 
@@ -119,5 +134,10 @@ const aLong = {
   },
   dHasALongName: \"a-long-value-too\",
 };
+
+const aLong = {
+  dHasALongName: \"a-long-value-too\",
+  eHasABooleanExpression: a === a,
+};
 "
 `;
diff --git a/tests/trailing_comma/object.js b/tests/trailing_comma/object.js
index 9bdc3b1f2..10c074ca9 100644
--- a/tests/trailing_comma/object.js
+++ b/tests/trailing_comma/object.js
@@ -14,3 +14,8 @@ const aLong = {
   },
   dHasALongName: 'a-long-value-too'
 };
+
+const aLong = {
+  dHasALongName: 'a-long-value-too',
+  eHasABooleanExpression: a === a,
+};