Repository: prettier/prettier-atom
Pull request #127 (2017-04-15 11:05:17)

fix(formatOnSave): Don't automatically format if file is saved for the first time
`prettier` runs before the file will be saved (`editor.onWillSave`) which is the desired behavior.

This fixes cases like this:
![prettier](https://cloud.githubusercontent.com/assets/13285808/25063110/91eb7b12-21db-11e7-9f9e-9ea2b0e6e789.gif)




Patch:
diff --git a/.all-contributorsrc b/.all-contributorsrc
index 9a2bb55..de9a501 100644
--- a/.all-contributorsrc
+++ b/.all-contributorsrc
@@ -159,7 +159,8 @@
       "avatar_url": "https://avatars2.githubusercontent.com/u/13285808?v=3",
       "profile": "https://github.com/lgeiger",
       "contributions": [
-        "code"
+        "code",
+        "test"
       ]
     },
     {
diff --git a/README.md b/README.md
index ef18971..d7695e6 100644
--- a/README.md
+++ b/README.md
@@ -56,7 +56,7 @@ Thanks goes to these people ([emoji key][emojis]):
 | [<img src="https://avatars.githubusercontent.com/u/17031?v=3" width="100px;"/><br /><sub>James Long</sub>](http://jlongster.com)<br />💬 [💻](https://github.com/prettier/prettier-atom/commits?author=jlongster) [📖](https://github.com/prettier/prettier-atom/commits?author=jlongster) 🔌 👀 | [<img src="https://avatars.githubusercontent.com/u/6173488?v=3" width="100px;"/><br /><sub>Rob Wise</sub>](https://robwise.github.io)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=robwise) [📖](https://github.com/prettier/prettier-atom/commits?author=robwise) 💬 💡 👀 [⚠️](https://github.com/prettier/prettier-atom/commits?author=robwise) | [<img src="https://avatars.githubusercontent.com/u/1500684?v=3" width="100px;"/><br /><sub>Kent C. Dodds</sub>](https://kentcdodds.com)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=kentcdodds) [📖](https://github.com/prettier/prettier-atom/commits?author=kentcdodds) 🚇 | [<img src="https://avatars.githubusercontent.com/u/1144075?v=3" width="100px;"/><br /><sub>Luca Barone</sub>](https://github.com/cloud-walker)<br /> | [<img src="https://avatars.githubusercontent.com/u/4514159?v=3" width="100px;"/><br /><sub>Arnar Þór Sveinsson</sub>](https://github.com/arnarthor)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=arnarthor) | [<img src="https://avatars.githubusercontent.com/u/131916?v=3" width="100px;"/><br /><sub>Adam Miskiewicz</sub>](http://www.adammiskiewicz.com/)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=skevy) | [<img src="https://avatars.githubusercontent.com/u/2685242?v=3" width="100px;"/><br /><sub>Ori Livni</sub>](http://www.orilivni.com)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=oriSomething) |
 | :---: | :---: | :---: | :---: | :---: | :---: | :---: |
 | [<img src="https://avatars.githubusercontent.com/u/6182852?v=3" width="100px;"/><br /><sub>Leon Chen</sub>](https://transcranial.github.io)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=transcranial) | [<img src="https://avatars.githubusercontent.com/u/197597?v=3" width="100px;"/><br /><sub>Christopher Chedeau</sub>](http://blog.vjeux.com/)<br />💬 [💻](https://github.com/prettier/prettier-atom/commits?author=vjeux) 🔌 | [<img src="https://avatars.githubusercontent.com/u/646693?v=3" width="100px;"/><br /><sub>Christoph Geschwind</sub>](http://christoph-geschwind.de)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=1st8) | [<img src="https://avatars.githubusercontent.com/u/35026?v=3" width="100px;"/><br /><sub>Andrew Hutchings</sub>](https://andrewhutchings.com)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=ahutchings) | [<img src="https://avatars.githubusercontent.com/u/875591?v=3" width="100px;"/><br /><sub>David Schnurr</sub>](http://davidschnurr.com)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=schnerd) | [<img src="https://avatars.githubusercontent.com/u/484801?v=3" width="100px;"/><br /><sub>Ryan Cole</sub>](http://rycole.com/)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=ryancole) | [<img src="https://avatars0.githubusercontent.com/u/11488612?v=3" width="100px;"/><br /><sub>Dara Hak</sub>](https://github.com/darahak)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=darahak) |
-| [<img src="https://avatars3.githubusercontent.com/u/487068?v=3" width="100px;"/><br /><sub>Stephen John Sorensen</sub>](http://www.stephenjohnsorensen.com/)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=spudly) | [<img src="https://avatars2.githubusercontent.com/u/13285808?v=3" width="100px;"/><br /><sub>Lukas Geiger</sub>](https://github.com/lgeiger)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=lgeiger) | [<img src="https://avatars2.githubusercontent.com/u/1517854?v=3" width="100px;"/><br /><sub>Viktor Charypar</sub>](https://github.com/charypar)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=charypar) [⚠️](https://github.com/prettier/prettier-atom/commits?author=charypar) | [<img src="https://avatars0.githubusercontent.com/u/1007436?v=3" width="100px;"/><br /><sub>Mats Högberg</sub>](http://mats.hgbrg.se)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=mhgbrg) |
+| [<img src="https://avatars3.githubusercontent.com/u/487068?v=3" width="100px;"/><br /><sub>Stephen John Sorensen</sub>](http://www.stephenjohnsorensen.com/)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=spudly) | [<img src="https://avatars2.githubusercontent.com/u/13285808?v=3" width="100px;"/><br /><sub>Lukas Geiger</sub>](https://github.com/lgeiger)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=lgeiger) [⚠️](https://github.com/prettier/prettier-atom/commits?author=lgeiger) | [<img src="https://avatars2.githubusercontent.com/u/1517854?v=3" width="100px;"/><br /><sub>Viktor Charypar</sub>](https://github.com/charypar)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=charypar) [⚠️](https://github.com/prettier/prettier-atom/commits?author=charypar) | [<img src="https://avatars0.githubusercontent.com/u/1007436?v=3" width="100px;"/><br /><sub>Mats Högberg</sub>](http://mats.hgbrg.se)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=mhgbrg) |
 <!-- ALL-CONTRIBUTORS-LIST:END -->
 
 This project follows the [all-contributors][all-contributors] specification. Contributions of any kind welcome!
diff --git a/dist/formatOnSave.js b/dist/formatOnSave.js
index 45edd6c..876bdb5 100644
--- a/dist/formatOnSave.js
+++ b/dist/formatOnSave.js
@@ -20,6 +20,8 @@ var formatOnSaveIfAppropriate = function formatOnSaveIfAppropriate(editor) {
 
   var filePath = getCurrentFilePath(editor);
 
+  if (!filePath) return;
+
   if (filePath && isWhitelistProvided() && !isFilePathWhitelisted(filePath)) {
     return;
   }
diff --git a/src/formatOnSave.js b/src/formatOnSave.js
index 75e6a66..a0804db 100644
--- a/src/formatOnSave.js
+++ b/src/formatOnSave.js
@@ -17,6 +17,8 @@ const formatOnSaveIfAppropriate = (editor: TextEditor) => {
 
   const filePath = getCurrentFilePath(editor);
 
+  if (!filePath) return;
+
   if (filePath && isWhitelistProvided() && !isFilePathWhitelisted(filePath)) {
     return;
   }


Test patch:
diff --git a/src/formatOnSave.test.js b/src/formatOnSave.test.js
index f3a23e3..fc507bc 100644
--- a/src/formatOnSave.test.js
+++ b/src/formatOnSave.test.js
@@ -31,7 +31,7 @@ beforeEach(() => {
 });
 
 test('it executes prettier on buffer range', () => {
-  formatOnSave(editor, filePathFixture);
+  formatOnSave(editor);
 
   expect(executePrettierOnBufferRange).toHaveBeenCalledWith(editor, rangeFixture);
 });
@@ -46,7 +46,7 @@ test('it executes prettier on buffer range if file is whitelisted regardless of
   // $FlowFixMe
   helpers.isFilePathWhitelisted.mockImplementation(() => true);
 
-  formatOnSave(editor, filePathFixture);
+  formatOnSave(editor);
 
   expect(executePrettierOnBufferRange).toHaveBeenCalledWith(editor, rangeFixture);
 });
@@ -61,7 +61,7 @@ test('it does not execute prettier if whitelist is provided and file is not whit
   // $FlowFixMe
   helpers.isFilePathWhitelisted.mockImplementation(() => false);
 
-  formatOnSave(editor, filePathFixture);
+  formatOnSave(editor);
 
   expect(executePrettierOnBufferRange).toHaveBeenCalledTimes(0);
 });
@@ -70,7 +70,7 @@ test('it executes prettier on embedded scripts if scope is an embedded scope', (
   // $FlowFixMe
   helpers.isCurrentScopeEmbeddedScope.mockImplementation(() => true);
 
-  formatOnSave(editor, filePathFixture);
+  formatOnSave(editor);
 
   expect(executePrettierOnEmbeddedScripts).toHaveBeenCalledWith(editor);
 });
@@ -79,18 +79,30 @@ test('it does nothing if formatOnSave is not enabled', () => {
   // $FlowFixMe
   helpers.isFormatOnSaveEnabled.mockImplementation(() => false);
 
-  formatOnSave(editor, filePathFixture);
+  formatOnSave(editor);
 
   expect(helpers.isFormatOnSaveEnabled).toHaveBeenCalled();
   expect(executePrettierOnBufferRange).not.toHaveBeenCalled();
   expect(executePrettierOnEmbeddedScripts).not.toHaveBeenCalled();
 });
 
+test('it does nothing if file is saved for the first time', () => {
+  // $FlowFixMe
+  helpers.getCurrentFilePath.mockImplementation(() => undefined);
+
+  formatOnSave(editor);
+
+  expect(helpers.isFormatOnSaveEnabled).toHaveBeenCalled();
+  expect(helpers.getCurrentFilePath).toHaveBeenCalled();
+  expect(executePrettierOnBufferRange).not.toHaveBeenCalled();
+  expect(executePrettierOnEmbeddedScripts).not.toHaveBeenCalled();
+});
+
 test('it does nothing if not a valid scope according to config', () => {
   // $FlowFixMe
   helpers.isInScope.mockImplementation(() => false);
 
-  formatOnSave(editor, filePathFixture);
+  formatOnSave(editor);
 
   expect(helpers.isInScope).toHaveBeenCalled();
   expect(executePrettierOnBufferRange).not.toHaveBeenCalled();
@@ -101,7 +113,7 @@ test('it does nothing if filePath matches an excluded glob', () => {
   // $FlowFixMe
   helpers.isFilePathExcluded.mockImplementation(() => true);
 
-  formatOnSave(editor, filePathFixture);
+  formatOnSave(editor);
 
   expect(helpers.isFilePathExcluded).toHaveBeenCalled();
   expect(executePrettierOnBufferRange).not.toHaveBeenCalled();
@@ -128,7 +140,7 @@ test('does not attempt to check filePath matches .eslintignore if `currentFilePa
   // $FlowFixMe
   helpers.getCurrentFilePath.mockImplementation(() => null);
 
-  formatOnSave(editor, filePathFixture);
+  formatOnSave(editor);
 
   expect(helpers.shouldRespectEslintignore).not.toHaveBeenCalled();
 });