Repository: prettier/prettier (JavaScript) — Feature

Issue:
Newline after "use strict" on babylon
This may be one of those personal taste issues, but I prefer to leave a newline between the `"use strict"` statement and any imports. It seems to create the same rhythm as leaving newlines between function definitions and import groups (I group by system, vendor, internal).


Discussion:
Note that prettier is actually stripping the newline in this case. Perhaps it could just leave it alone, like it does for other newlines?
Thanks for the report, this is indeed a bug and should be fixed!

Repro case

```js
./bin/prettier.js test.js
/* @flow */
"use strict";
import a from "a";

vjeux();
```

```js
./bin/prettier.js test.js --flow-parser
/* @flow */

"use strict";

import a from "a";

vjeux();
```

https://jlongster.github.io/prettier/#%7B%22content%22%3A%22%2F*%20%40flow%20*%2F%5Cn%5Cn'use%20strict'%3B%5Cn%5Cnimport%20a%20from%20'a'%3B%5Cn%5Cnvjeux()%3B%22%2C%22options%22%3A%7B%22printWidth%22%3A80%2C%22tabWidth%22%3A2%2C%22singleQuote%22%3Afalse%2C%22trailingComma%22%3Afalse%2C%22bracketSpacing%22%3Atrue%7D%7D

It works fine on the flow parser but not on babylon. The reason is that babylon has a separate `directives` field for `'use strict';` instead of being part of the normal ast. We need to apply the newline logic detection for those as well.

Resolving patch:
diff --git a/src/printer.js b/src/printer.js
index 28990dde8b65..57c53040b386 100644
--- a/src/printer.js
+++ b/src/printer.js
@@ -115,6 +115,9 @@ function genericPrintNoParens(path, options, print) {
         path.each(
           function(childPath) {
             parts.push(print(childPath), ";", hardline);
+            if (util.newlineExistsAfter(options.originalText, util.locEnd(childPath.getValue()))) {
+              parts.push(hardline);
+            }
           },
           "directives"
         );


Test patch:
diff --git a/tests/directives/__snapshots__/jsfmt.spec.js.snap b/tests/directives/__snapshots__/jsfmt.spec.js.snap
index f680d05323b2..b63c61eb4450 100644
--- a/tests/directives/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/directives/__snapshots__/jsfmt.spec.js.snap
@@ -1,3 +1,40 @@
+exports[`test newline.js 1`] = `
+"/* @flow */
+
+\"use strict\";
+
+import a from \"a\";
+
+a();
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+/* @flow */
+
+\"use strict\";
+
+import a from \"a\";
+
+a();
+"
+`;
+
+exports[`test newline.js 2`] = `
+"/* @flow */
+
+\"use strict\";
+
+import a from \"a\";
+
+a();
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+/* @flow */
+\"use strict\";
+
+import a from \"a\";
+
+a();
+"
+`;
+
 exports[`test test.js 1`] = `
 "\"use strict\";
 
@@ -12,3 +49,18 @@ function fn() {
 }
 "
 `;
+
+exports[`test test.js 2`] = `
+"\"use strict\";
+
+function fn() {
+  \"use strict\";
+}
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+\"use strict\";
+
+function fn() {
+  \"use strict\";
+}
+"
+`;
diff --git a/tests/directives/jsfmt.spec.js b/tests/directives/jsfmt.spec.js
index 989047bccc52..d5ec770a90fe 100644
--- a/tests/directives/jsfmt.spec.js
+++ b/tests/directives/jsfmt.spec.js
@@ -1,1 +1,2 @@
 run_spec(__dirname);
+run_spec(__dirname, {parser: 'babylon'});
diff --git a/tests/directives/newline.js b/tests/directives/newline.js
new file mode 100644
index 000000000000..8da10321b3e1
--- /dev/null
+++ b/tests/directives/newline.js
@@ -0,0 +1,7 @@
+/* @flow */
+
+"use strict";
+
+import a from "a";
+
+a();