Repository: prettier/prettier (JavaScript)
Date: 2018-01-11 19:50:32+00:00
Task type: Bug Fix

Issue:
Ignore empty scss files instead of writing a newline?
<!-- BUGGY OR UGLY? Please use this template.

Tip! Don't write this stuff manually.

1. Go to https://prettier.io/playground
2. Paste your code and set options
3. Press the "Report issue" button in the lower right

-->

**Prettier 1.9.1**
[Playground link](https://prettier.io/playground/#N4Igxg9gdgLgprEAuEIA0IIAcYEtoDOyoAhgE5kQDuACuQkSiQG4S4Am6IJBMyAZiQA2BOBgBGZEmADWcGAGUs03FADmyGGQCuYkOwhgBw0RlWiyMGlLUBbEsZF6AVgQAeAISmz5CkrbgAGVU4R1NMbRgsSIAmML1lMgtkEAIwAiIMLDJVGAB1DhgAC2QADgAGLMpRPKksFOy4C2ZQjDI4AEdtXHbrEjsHJEEnDFFbXE0dPQJVNSE4AEVtCHhJ3QwYEnEC9mLkGI2pXCFZgGEIW3sUpoBWLm1RABUtxmHRAF93oA)

```sh
# Options (if any):
--single-quote --parser scss --trailing-comma es5
```

**Input:**
```scss
[blank line]
```

**Output:**
```scss
[blank line]
\n
```

**Expected behavior:**
No newline (`\n`)


Issue discussion:
Prettier makes sure that all files it formats ends with a single newline. Why would you want an exception for SCSS files with only a comment in it?
There is no comment in it. That was from the issue template in this repo. There is literally nothing in it. It's not that I want an exception for SCSS. I'm pointing out an SCSS-specific issue.
I can reproduce this:
```
> touch file.css && git add file.css && prettier --write file.css && git diff file.css
```

```diff
diff --git a/file.css b/file.css
index e69de29b..8b137891 100644
--- a/file.css
+++ b/file.css
@@ -0,0 +1 @@
+

It's present in the playground link above as well.
For empty _JS_ files, Prettier does _not_ add a newline. I didn't know it worked that way. Since that is the case for JS, it should work the same for SCSS as well.
Or is JS wrong? Tough question.

Solution patch:
diff --git a/src/language-css/printer-postcss.js b/src/language-css/printer-postcss.js
index 794ae803508c..b2d4afdb4649 100644
--- a/src/language-css/printer-postcss.js
+++ b/src/language-css/printer-postcss.js
@@ -30,7 +30,13 @@ function genericPrint(path, options, print) {
 
   switch (n.type) {
     case "css-root": {
-      return concat([printNodeSequence(path, options, print), hardline]);
+      const nodes = printNodeSequence(path, options, print);
+
+      if (nodes.parts.length) {
+        return concat([nodes, hardline]);
+      }
+
+      return nodes;
     }
     case "css-comment": {
       if (n.raws.content) {


Test patch:
diff --git a/tests/css_empty_file/__snapshots__/jsfmt.spec.js.snap b/tests/css_empty_file/__snapshots__/jsfmt.spec.js.snap
new file mode 100644
index 000000000000..11ebcf01b744
--- /dev/null
+++ b/tests/css_empty_file/__snapshots__/jsfmt.spec.js.snap
@@ -0,0 +1,11 @@
+// Jest Snapshot v1, https://goo.gl/fbAQLP
+
+exports[`empty-file.css 1`] = `
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+`;
+
+exports[`single-space.css 1`] = `
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+`;
diff --git a/tests/css_empty_file/empty-file.css b/tests/css_empty_file/empty-file.css
new file mode 100644
index 000000000000..e69de29bb2d1
diff --git a/tests/css_empty_file/jsfmt.spec.js b/tests/css_empty_file/jsfmt.spec.js
new file mode 100644
index 000000000000..7d3726c81147
--- /dev/null
+++ b/tests/css_empty_file/jsfmt.spec.js
@@ -0,0 +1,1 @@
+run_spec(__dirname, ["css"]);
diff --git a/tests/css_empty_file/single-space.css b/tests/css_empty_file/single-space.css
new file mode 100644
index 000000000000..0519ecba6ea9
--- /dev/null
+++ b/tests/css_empty_file/single-space.css
@@ -0,0 +1,1 @@
+ 
\ No newline at end of file
diff --git a/tests/empty/__snapshots__/jsfmt.spec.js.snap b/tests/empty/__snapshots__/jsfmt.spec.js.snap
index 874fa35ed393..9bfbb43e0918 100644
--- a/tests/empty/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/empty/__snapshots__/jsfmt.spec.js.snap
@@ -6,3 +6,13 @@ exports[`empty.js 1`] = `
 
 
 `;
+
+exports[`empty-file.js 1`] = `
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+`;
+
+exports[`single-space.js 1`] = `
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+`;
diff --git a/tests/empty/empty-file.js b/tests/empty/empty-file.js
new file mode 100644
index 000000000000..e69de29bb2d1
diff --git a/tests/empty/single-space.js b/tests/empty/single-space.js
new file mode 100644
index 000000000000..0519ecba6ea9
--- /dev/null
+++ b/tests/empty/single-space.js
@@ -0,0 +1,1 @@
+ 
\ No newline at end of file