Repository: jlongster/prettier (js)

Issue:
Invalid parens around ternary expressions
Try this code with width 34, 35, 36. At 35 prettier will add one extra bracket.

Input:

```js
const foo = {
  bar: props.bar ? props.bar : noop,
  baz: props.baz
}
```

Output:

```js
// At 34: good
const foo = {
  bar: (
    props.bar ? props.bar : noop
  ),
  baz: props.baz
};
```

```js
// At 35: bad
const foo = {
  bar: props.bar ? props.bar : noop
  ),
  baz: props.baz
};
```

```js
// At 36: good
const foo = {
  bar: props.bar ? props.bar : noop,
  baz: props.baz
};
```

https://jlongster.github.io/prettier/#%7B%22content%22%3A%22const%20foo%20%3D%20%7B%5Cn%20%20bar%3A%20props.bar%20%3F%20props.bar%20%3A%20noop%2C%5Cn%20%20baz%3A%20props.baz%5Cn%7D%22%2C%22options%22%3A%7B%22printWidth%22%3A35%2C%22tabWidth%22%3A2%2C%22singleQuote%22%3Afalse%2C%22trailingComma%22%3Afalse%2C%22bracketSpacing%22%3Atrue%2C%22doc%22%3Afalse%7D%7D



Pull request description:
Remove extra group when printing object values
It turns out that in an unlikely turn of event, the inner group can be inline and not print the opening paren but the outer group breaks and outputs the closing paren which generates invalid JavaScript.

I tried removing the group altogether and no tests failed, so I'm assuming the group wasn't needed in the first place. If it was, we should add tests to cover this.

Fixes #501

Patch:
diff --git a/bin/prettier.js b/bin/prettier.js
index 16ea3381f..2de67aae9 100755
--- a/bin/prettier.js
+++ b/bin/prettier.js
@@ -191,13 +191,17 @@ if (stdin) {
       }
 
       if (write) {
-        fs.writeFile(filename, output, "utf8", err => {
-          if (err) {
-            console.error("Unable to write file: " + filename + "\n" + err);
-            // Don't exit the process if one file failed
-            process.exitCode = 2;
-          }
-        });
+        // Don't write the file if it won't change in order not to invalidate
+        // mtime based caches.
+        if (output !== input) {
+          fs.writeFile(filename, output, "utf8", err => {
+            if (err) {
+              console.error("Unable to write file: " + filename + "\n" + err);
+              // Don't exit the process if one file failed
+              process.exitCode = 2;
+            }
+          });
+        }
       } else {
         // Don't use `console.log` here since it adds an extra newline at the end.
         process.stdout.write(output);
diff --git a/src/printer.js b/src/printer.js
index 236441092..6a385cfa4 100644
--- a/src/printer.js
+++ b/src/printer.js
@@ -635,13 +635,9 @@ function genericPrintNoParens(path, options, print) {
         } else {
           parts.push(
             concat([
-              group(
-                concat([
-                  ":",
-                  ifBreak(" (", " "),
-                  indent(options.tabWidth, concat([softline, printedValue]))
-                ])
-              ),
+              ":",
+              ifBreak(" (", " "),
+              indent(options.tabWidth, concat([softline, printedValue])),
               softline,
               ifBreak(")")
             ])


Test patch:
diff --git a/tests/object_colon_bug/__snapshots__/jsfmt.spec.js.snap b/tests/object_colon_bug/__snapshots__/jsfmt.spec.js.snap
new file mode 100644
index 000000000..a2cc5d93c
--- /dev/null
+++ b/tests/object_colon_bug/__snapshots__/jsfmt.spec.js.snap
@@ -0,0 +1,14 @@
+exports[`test bug.js 1`] = `
+"const foo = {
+  bar: props.bar ? props.bar : noop,
+  baz: props.baz
+}
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+const foo = {
+  bar: (
+    props.bar ? props.bar : noop
+  ),
+  baz: props.baz
+};
+"
+`;
diff --git a/tests/object_colon_bug/bug.js b/tests/object_colon_bug/bug.js
new file mode 100644
index 000000000..a49851145
--- /dev/null
+++ b/tests/object_colon_bug/bug.js
@@ -0,0 +1,4 @@
+const foo = {
+  bar: props.bar ? props.bar : noop,
+  baz: props.baz
+}
diff --git a/tests/object_colon_bug/jsfmt.spec.js b/tests/object_colon_bug/jsfmt.spec.js
new file mode 100644
index 000000000..c92e1fa6a
--- /dev/null
+++ b/tests/object_colon_bug/jsfmt.spec.js
@@ -0,0 +1,1 @@
+run_spec(__dirname, {printWidth: 35});