Repository: prettier/prettier (JavaScript)
Date: 2017-02-02 17:13:13+00:00
Task type: Bug Fix

Issue:
Flow comment syntax errors with single arg in arrow functions
Here are examples of Flow syntax errors using comment syntax when annotating an arrow function with a single arg. This is using Prettier v0.15.0. Link: [Playground](https://jlongster.github.io/prettier/#%7B%22content%22%3A%22%2F%2F%20Error%5Cnconst%20beep%20%3D%20(data%2F*%3A%20Object*%2F)%20%3D%3E%20%7B%7D%5Cn%5Cn%2F%2F%20OK%5Cnconst%20beep%20%3D%20(data%2F*%3A%20Object*%2F%2C%20secondData%2F*%3A%20Object*%2F)%20%3D%3E%20%7B%7D%22%2C%22options%22%3A%7B%22printWidth%22%3A80%2C%22tabWidth%22%3A2%2C%22singleQuote%22%3Afalse%2C%22trailingComma%22%3Afalse%2C%22bracketSpacing%22%3Afalse%2C%22doc%22%3Afalse%7D%7D)

Here is the pasted example from the link above for quick viewing:

Before:
``` js
// Error
const beep = (data/*: Object*/) => {}

// OK
const beep = (data/*: Object*/, secondData/*: Object*/) => {}

```

After:
``` js
// Error
const beep = data /*: Object*/ => {};

// OK
const beep = (data /*: Object*/, secondData /*: Object*/) => {};
```

The error from Flow will look something like
```
  1: const beep = data /*: Object*/ => {};
                       ^^^ Unexpected token /*:
```


Issue discussion:
Thanks! This is indeed a bug. We don't check if the argument has comments in order to remove the `()`

Solution patch:
diff --git a/src/printer.js b/src/printer.js
index 538ec61bda55..2d65bcaa0f6c 100644
--- a/src/printer.js
+++ b/src/printer.js
@@ -257,6 +257,8 @@ function genericPrintNoParens(path, options, print) {
           !n.rest &&
           n.params[0].type === "Identifier" &&
           !n.params[0].typeAnnotation &&
+          !n.params[0].leadingComments &&
+          !n.params[0].trailingComments &&
           !n.predicate &&
           !n.returnType
       ) {


Test patch:
diff --git a/tests/flow_comments/__snapshots__/jsfmt.spec.js.snap b/tests/flow_comments/__snapshots__/jsfmt.spec.js.snap
new file mode 100644
index 000000000000..bdc17200d243
--- /dev/null
+++ b/tests/flow_comments/__snapshots__/jsfmt.spec.js.snap
@@ -0,0 +1,29 @@
+exports[`test arrow.js 1`] = `
+"// Error
+const beep = (data/*: Object*/) => {}
+
+// OK
+const beep = (data/*: Object*/, secondData/*: Object*/) => {}
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+// Error
+const beep = (data: Object) => {};
+
+// OK
+const beep = (data: Object, secondData: Object) => {};
+"
+`;
+
+exports[`test arrow.js 2`] = `
+"// Error
+const beep = (data/*: Object*/) => {}
+
+// OK
+const beep = (data/*: Object*/, secondData/*: Object*/) => {}
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+// Error
+const beep = (data /*: Object*/) => {};
+
+// OK
+const beep = (data /*: Object*/, secondData /*: Object*/) => {};
+"
+`;
diff --git a/tests/flow_comments/arrow.js b/tests/flow_comments/arrow.js
new file mode 100644
index 000000000000..7f5d39d75adc
--- /dev/null
+++ b/tests/flow_comments/arrow.js
@@ -0,0 +1,5 @@
+// Error
+const beep = (data/*: Object*/) => {}
+
+// OK
+const beep = (data/*: Object*/, secondData/*: Object*/) => {}
diff --git a/tests/flow_comments/jsfmt.spec.js b/tests/flow_comments/jsfmt.spec.js
new file mode 100644
index 000000000000..d5ec770a90fe
--- /dev/null
+++ b/tests/flow_comments/jsfmt.spec.js
@@ -0,0 +1,2 @@
+run_spec(__dirname);
+run_spec(__dirname, {parser: 'babylon'});