Repository: prettier/prettier (JavaScript)
Date: 2017-01-28 20:46:00+00:00
Task type: Bug Fix

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




Issue discussion:
cc @rattrayalex, is this something you added for JSX?
Weird. 

I don't think so, but I can try to dig into it later today or tomorrow. 
Actually, the parenthesis were introduced on purpose by @yamafaktory in https://github.com/jlongster/prettier/pull/314

I'll let him and @jlongster comment on whether this is something we want or not. Turning this into a Needs Discussion label instead of bug.
Ohhh, actually I misread the description. There is indeed a bug

At 35, it outputs the closing bracket but not the opening one, which is invalid JavaScript.

```js
const foo = {
  bar: props.bar ? props.bar : noop
  ),
  baz: props.baz
};
```
@vjeux I don't think this is about whether the parens are a good idea;take a look at the example output with width set to 35 😉
Ah great you beat me to it!
The issue is that the `ifBreak("(")` and `ifBreak(")")` are in two different groups that may not break at the same time.

Solution patch:
diff --git a/src/printer.js b/src/printer.js
index 236441092645..6a385cfa459d 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 000000000000..a2cc5d93cc7a
--- /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 000000000000..a49851145c54
--- /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 000000000000..c92e1fa6a663
--- /dev/null
+++ b/tests/object_colon_bug/jsfmt.spec.js
@@ -0,0 +1,1 @@
+run_spec(__dirname, {printWidth: 35});