Repository: prettier/prettier-atom
Pull request #358 (2018-01-24 21:52:25)

use File path getter over reading directly from property
In Nuclide, there is a RemoteFile. This object, while similar to Atom's File, doesn't have the exact same signature. However, they both share the same method `getPath`. Using this method in favor of reading the property directly, repairs prettier-atom's ability to 'format on save' when a user is working with remote files.

Fixes [facebook/nuclide#1443](https://github.com/facebook/nuclide/issues/1443) and [prettier/prettier-atom#322](https://github.com/prettier/prettier-atom/issues/322).

___

Due to an issue with the yarn/npm registry at the moment. I am unable to successfully install dependencies. Various babel packages seem to randomly 404. Therefore, I'm unable to run all the required scripts outlined in CONTRIBUTING.md. What would you like me to do in terms of running tests and adding myself to the contributors list?

I'm also unfamiliar with Flow. I attempted to update the `TextEditor` type definition, but was unsure what needed to happen and opted to avoid modifying it as it exists now. Can someone help me?

darahak: Thank you for your contribution!

To complete the PR, you will also need to do this:

- [ ] Amend your commit message and follow this template (for the automatic **changelog** updates): `type(scope): message` (see previous [commits](https://github.com/prettier/prettier-atom/commits/master))
- [ ] Run the `build` script and commit the modifications in `/dist`.
- [ ] Run the `addContributor` script if you want to add yourself to the contributor list and commit the changes.
SavePointSam: I've gone back and updated my commit message, add the type declaration and was able to add my contributing information. However, I'm unable to run a build as I can't download the dependencies still.

```
~/source/prettier-atom issue-322_nuclide-remote*
❯ yarn
yarn install v1.3.2
[1/4] 🔍  Resolving packages...
[2/4] 🚚  Fetching packages...
error An unexpected error occurred: "https://registry.yarnpkg.com/@babel/types/-/types-7.0.0-beta.31.tgz: Request failed \"404 Not Found\"".
info If you think this is a bug, please open a bug report with the information provided in "/Users/savepointsam/source/prettier-atom/yarn-error.log".
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
```

What would you like to do about this?
SavePointSam: Alright, I was finally able to install the dependencies. I'll finish up shortly. Apologies for all the commits.

Patch:
diff --git a/.all-contributorsrc b/.all-contributorsrc
index ecc02da..22b2eac 100644
--- a/.all-contributorsrc
+++ b/.all-contributorsrc
@@ -265,6 +265,16 @@
       "contributions": [
         "code"
       ]
+    },
+    {
+      "login": "SavePointSam",
+      "name": "Sam Horton",
+      "avatar_url": "https://avatars0.githubusercontent.com/u/8203211?v=4",
+      "profile": "https://github.com/SavePointSam",
+      "contributions": [
+        "code",
+        "test"
+      ]
     }
   ]
 }
diff --git a/README.md b/README.md
index 86b93e4..875dd7c 100644
--- a/README.md
+++ b/README.md
@@ -1,13 +1,14 @@
 # Prettier for Atom [![Watch on GitHub][github-watch-badge]][github-watch][![Star on GitHub][github-star-badge]][github-star][![Tweet][twitter-badge]][twitter]
 
 <!-- [![Dependencies][dependencyci-badge]][dependencyci] TODO: Add dependency CI! -->
+
 <!-- [![Code of Conduct][coc-badge]][coc] -->
 
 [![Version][version-badge]][package]
 [![Downloads][downloads-badge]][package]
 [![Build Status][build-badge]][build]
 [![Code Coverage][coverage-badge]][coverage]
-[![MIT License][license-badge]][LICENSE]
+[![MIT License][license-badge]][license]
 [![All Contributors][all-contributors-badge]](#contributors)
 [![PRs Welcome][prs-badge]][prs]
 
@@ -24,7 +25,7 @@ Comes with powerful optional [ESLint](http://eslint.org/) integration.
 apm install prettier-atom
 ```
 
-Or go to *Settings → Install* and search for `prettier-atom`.
+Or go to _Settings → Install_ and search for `prettier-atom`.
 
 Make sure to restart Atom after the package is installed.
 
@@ -32,8 +33,8 @@ Make sure to restart Atom after the package is installed.
 
 There are two modes you can use:
 
-- Invoke manually using the **keyboard shortcut** (if no selection, whole file is formatted): <kbd>CTRL</kbd> + <kbd>ALT</kbd> + <kbd>F</kbd>
-- Automatically **format on save** (requires enabling in *Packages → Prettier → Toggle Format on Save*)
+* Invoke manually using the **keyboard shortcut** (if no selection, whole file is formatted): <kbd>CTRL</kbd> + <kbd>ALT</kbd> + <kbd>F</kbd>
+* Automatically **format on save** (requires enabling in _Packages → Prettier → Toggle Format on Save_)
 
 If you use ESLint, check the "ESLint Integration" checkbox and \*poof\*, everything should work (we use Kent Dodds's [`prettier-eslint`][prettier-eslint] plugin under the hood).
 We will recursively search up the file tree for your `package.json` and ESLint settings, and use them when formatting.
@@ -61,12 +62,14 @@ This repository was created by [James Long][james-long] to go along with his Pre
 Thanks goes to these people ([emoji key][emojis]):
 
 <!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
+
 <!-- prettier-ignore -->
 | [<img src="https://avatars.githubusercontent.com/u/17031?v=3" width="100px;"/><br /><sub><b>James Long</b></sub>](http://jlongster.com)<br />[💬](#question-jlongster "Answering Questions") [💻](https://github.com/prettier/prettier-atom/commits?author=jlongster "Code") [📖](https://github.com/prettier/prettier-atom/commits?author=jlongster "Documentation") [🔌](#plugin-jlongster "Plugin/utility libraries") [👀](#review-jlongster "Reviewed Pull Requests") | [<img src="https://avatars.githubusercontent.com/u/6173488?v=3" width="100px;"/><br /><sub><b>Rob Wise</b></sub>](https://robwise.github.io)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=robwise "Code") [📖](https://github.com/prettier/prettier-atom/commits?author=robwise "Documentation") [💬](#question-robwise "Answering Questions") [💡](#example-robwise "Examples") [👀](#review-robwise "Reviewed Pull Requests") [⚠️](https://github.com/prettier/prettier-atom/commits?author=robwise "Tests") | [<img src="https://avatars.githubusercontent.com/u/1500684?v=3" width="100px;"/><br /><sub><b>Kent C. Dodds</b></sub>](https://kentcdodds.com)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=kentcdodds "Code") [📖](https://github.com/prettier/prettier-atom/commits?author=kentcdodds "Documentation") [🚇](#infra-kentcdodds "Infrastructure (Hosting, Build-Tools, etc)") | [<img src="https://avatars.githubusercontent.com/u/1144075?v=3" width="100px;"/><br /><sub><b>Luca Barone</b></sub>](https://github.com/cloud-walker)<br /> | [<img src="https://avatars.githubusercontent.com/u/4514159?v=3" width="100px;"/><br /><sub><b>Arnar Þór Sveinsson</b></sub>](https://github.com/arnarthor)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=arnarthor "Code") | [<img src="https://avatars.githubusercontent.com/u/131916?v=3" width="100px;"/><br /><sub><b>Adam Miskiewicz</b></sub>](http://www.adammiskiewicz.com/)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=skevy "Code") | [<img src="https://avatars.githubusercontent.com/u/2685242?v=3" width="100px;"/><br /><sub><b>Ori Livni</b></sub>](http://www.orilivni.com)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=oriSomething "Code") |
 | :---: | :---: | :---: | :---: | :---: | :---: | :---: |
 | [<img src="https://avatars.githubusercontent.com/u/6182852?v=3" width="100px;"/><br /><sub><b>Leon Chen</b></sub>](https://transcranial.github.io)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=transcranial "Code") | [<img src="https://avatars.githubusercontent.com/u/197597?v=3" width="100px;"/><br /><sub><b>Christopher Chedeau</b></sub>](http://blog.vjeux.com/)<br />[💬](#question-vjeux "Answering Questions") [💻](https://github.com/prettier/prettier-atom/commits?author=vjeux "Code") [🔌](#plugin-vjeux "Plugin/utility libraries") | [<img src="https://avatars.githubusercontent.com/u/646693?v=3" width="100px;"/><br /><sub><b>Christoph Geschwind</b></sub>](http://christoph-geschwind.de)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=1st8 "Code") | [<img src="https://avatars.githubusercontent.com/u/35026?v=3" width="100px;"/><br /><sub><b>Andrew Hutchings</b></sub>](https://andrewhutchings.com)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=ahutchings "Code") | [<img src="https://avatars.githubusercontent.com/u/875591?v=3" width="100px;"/><br /><sub><b>David Schnurr</b></sub>](http://davidschnurr.com)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=schnerd "Code") | [<img src="https://avatars.githubusercontent.com/u/484801?v=3" width="100px;"/><br /><sub><b>Ryan Cole</b></sub>](http://rycole.com/)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=ryancole "Code") | [<img src="https://avatars0.githubusercontent.com/u/11488612?v=3" width="100px;"/><br /><sub><b>Dara Hak</b></sub>](https://github.com/darahak)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=darahak "Code") [📖](https://github.com/prettier/prettier-atom/commits?author=darahak "Documentation") |
 | [<img src="https://avatars3.githubusercontent.com/u/487068?v=3" width="100px;"/><br /><sub><b>Stephen John Sorensen</b></sub>](http://www.stephenjohnsorensen.com/)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=spudly "Code") | [<img src="https://avatars2.githubusercontent.com/u/13285808?v=3" width="100px;"/><br /><sub><b>Lukas Geiger</b></sub>](https://github.com/lgeiger)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=lgeiger "Code") [⚠️](https://github.com/prettier/prettier-atom/commits?author=lgeiger "Tests") | [<img src="https://avatars2.githubusercontent.com/u/1517854?v=3" width="100px;"/><br /><sub><b>Viktor Charypar</b></sub>](https://github.com/charypar)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=charypar "Code") [⚠️](https://github.com/prettier/prettier-atom/commits?author=charypar "Tests") | [<img src="https://avatars0.githubusercontent.com/u/1007436?v=3" width="100px;"/><br /><sub><b>Mats Högberg</b></sub>](http://mats.hgbrg.se)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=mhgbrg "Code") | [<img src="https://avatars0.githubusercontent.com/u/2602767?v=3" width="100px;"/><br /><sub><b>Roman</b></sub>](https://github.com/RoM4iK)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=RoM4iK "Code") | [<img src="https://avatars2.githubusercontent.com/u/1468518?v=3" width="100px;"/><br /><sub><b>vaibhav</b></sub>](https://vaibhavchatarkar.com)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=da-vaibhav "Code") | [<img src="https://avatars1.githubusercontent.com/u/1935696?v=3" width="100px;"/><br /><sub><b>Karl Horky</b></sub>](https://work.karlhorky.com)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=karlhorky "Code") |
-| [<img src="https://avatars3.githubusercontent.com/u/810438?v=3" width="100px;"/><br /><sub><b>Dan Abramov</b></sub>](http://twitter.com/dan_abramov)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=gaearon "Code") | [<img src="https://avatars3.githubusercontent.com/u/1227109?v=3" width="100px;"/><br /><sub><b>Murphy Randle</b></sub>](https://sploding.rocks)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=splodingsocks "Code") [🐛](https://github.com/prettier/prettier-atom/issues?q=author%3Asplodingsocks "Bug reports") | [<img src="https://avatars3.githubusercontent.com/u/8517072?v=3" width="100px;"/><br /><sub><b>Matthieu Lemoine</b></sub>](https://matthieulemoine.com)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=MatthieuLemoine "Code") | [<img src="https://avatars1.githubusercontent.com/u/37242?v=4" width="100px;"/><br /><sub><b>Ron Green</b></sub>](https://github.com/rgreenjr)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=rgreenjr "Code") | [<img src="https://avatars2.githubusercontent.com/u/1745854?v=4" width="100px;"/><br /><sub><b>Harold Treen</b></sub>](https://haroldtreen.com)<br />[🐛](https://github.com/prettier/prettier-atom/issues?q=author%3Aharoldtreen "Bug reports") [💻](https://github.com/prettier/prettier-atom/commits?author=haroldtreen "Code") | [<img src="https://avatars1.githubusercontent.com/u/3447641?v=4" width="100px;"/><br /><sub><b>Nikita Mashukov</b></sub>](https://github.com/ferdibiflator)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=ferdibiflator "Code") |
+| [<img src="https://avatars3.githubusercontent.com/u/810438?v=3" width="100px;"/><br /><sub><b>Dan Abramov</b></sub>](http://twitter.com/dan_abramov)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=gaearon "Code") | [<img src="https://avatars3.githubusercontent.com/u/1227109?v=3" width="100px;"/><br /><sub><b>Murphy Randle</b></sub>](https://sploding.rocks)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=splodingsocks "Code") [🐛](https://github.com/prettier/prettier-atom/issues?q=author%3Asplodingsocks "Bug reports") | [<img src="https://avatars3.githubusercontent.com/u/8517072?v=3" width="100px;"/><br /><sub><b>Matthieu Lemoine</b></sub>](https://matthieulemoine.com)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=MatthieuLemoine "Code") | [<img src="https://avatars1.githubusercontent.com/u/37242?v=4" width="100px;"/><br /><sub><b>Ron Green</b></sub>](https://github.com/rgreenjr)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=rgreenjr "Code") | [<img src="https://avatars2.githubusercontent.com/u/1745854?v=4" width="100px;"/><br /><sub><b>Harold Treen</b></sub>](https://haroldtreen.com)<br />[🐛](https://github.com/prettier/prettier-atom/issues?q=author%3Aharoldtreen "Bug reports") [💻](https://github.com/prettier/prettier-atom/commits?author=haroldtreen "Code") | [<img src="https://avatars1.githubusercontent.com/u/3447641?v=4" width="100px;"/><br /><sub><b>Nikita Mashukov</b></sub>](https://github.com/ferdibiflator)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=ferdibiflator "Code") | [<img src="https://avatars0.githubusercontent.com/u/8203211?v=4" width="100px;"/><br /><sub><b>Sam Horton</b></sub>](https://github.com/SavePointSam)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=SavePointSam "Code") [⚠️](https://github.com/prettier/prettier-atom/commits?author=SavePointSam "Tests") |
+
 <!-- ALL-CONTRIBUTORS-LIST:END -->
 
 This project follows the [all-contributors][all-contributors] specification. Contributions of any kind are welcome!
diff --git a/decls/index.js b/decls/index.js
index 5eea59b..f564306 100644
--- a/decls/index.js
+++ b/decls/index.js
@@ -32,7 +32,12 @@ declare type TextEditor = {
   getTextInBufferRange: (bufferRange: Range) => string,
   setCursorScreenPosition: (point: Point) => Point,
   setTextInBufferRange: (bufferRange: Range, text: string) => Range,
-  buffer: { file: ?{ path: ?FilePath } },
+  buffer: {
+    file: ?{
+      path: ?FilePath,
+      getPath: () => string,
+    },
+  },
   backwardsScanInBufferRange: (regex: RegExp, Range: Range, iterator: Atom$Iterator) => void,
 };
 declare type Atom$Disposable = any;
diff --git a/dist/editorInterface/index.js b/dist/editorInterface/index.js
index 90ff1de..c155fa5 100644
--- a/dist/editorInterface/index.js
+++ b/dist/editorInterface/index.js
@@ -57,7 +57,7 @@ var isCurrentScopeVueScope = function isCurrentScopeVueScope(editor) {
 };
 
 var getCurrentFilePath = function getCurrentFilePath(editor) {
-  return editor.buffer.file ? editor.buffer.file.path : undefined;
+  return editor.buffer.file ? editor.buffer.file.getPath() : undefined;
 };
 
 var getCurrentDir = function getCurrentDir(editor) {
diff --git a/dist/executePrettier/executePrettierOnBufferRange.js b/dist/executePrettier/executePrettierOnBufferRange.js
index 8111eaa..babaf30 100644
--- a/dist/executePrettier/executePrettierOnBufferRange.js
+++ b/dist/executePrettier/executePrettierOnBufferRange.js
@@ -48,7 +48,7 @@ var executePrettierStylelint = function executePrettierStylelint(editor, text) {
 };
 
 var executePrettierOrIntegration = function () {
-  var _ref = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee(editor, text) {
+  var _ref = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee(editor, text) {
     var formatted;
     return _regenerator2.default.wrap(function _callee$(_context) {
       while (1) {
@@ -99,7 +99,7 @@ var executePrettierOrIntegration = function () {
 }();
 
 var executePrettierOnBufferRange = function () {
-  var _ref2 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee2(editor, bufferRange, options) {
+  var _ref2 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee2(editor, bufferRange, options) {
     var cursorPositionPriorToFormat, textToTransform, transformed, isTextUnchanged;
     return _regenerator2.default.wrap(function _callee2$(_context2) {
       while (1) {
diff --git a/src/editorInterface/index.js b/src/editorInterface/index.js
index 778bab0..f045ddf 100644
--- a/src/editorInterface/index.js
+++ b/src/editorInterface/index.js
@@ -42,7 +42,8 @@ const isCurrentScopeMarkdownScope = (editor: TextEditor) =>
 
 const isCurrentScopeVueScope = (editor: TextEditor) => getVueScopes().includes(getCurrentScope(editor));
 
-const getCurrentFilePath = (editor: TextEditor) => (editor.buffer.file ? editor.buffer.file.path : undefined);
+const getCurrentFilePath = (editor: TextEditor) =>
+  editor.buffer.file ? editor.buffer.file.getPath() : undefined;
 
 const getCurrentDir: (editor: TextEditor) => ?string = editor =>
   lazyFlow()(


Test patch:
diff --git a/src/editorInterface/index.test.js b/src/editorInterface/index.test.js
index f144684..08a4ba0 100644
--- a/src/editorInterface/index.test.js
+++ b/src/editorInterface/index.test.js
@@ -24,8 +24,13 @@ const {
 
 describe('getBufferRange()', () => {
   it('gets the buffer range from the editor', () => {
-    const fakeBufferRange = { start: { row: 0, column: 0 }, end: { row: 0, column: 1 } };
-    const editor = buildMockTextEditor({ getBuffer: () => ({ getRange: () => fakeBufferRange }) });
+    const fakeBufferRange = {
+      start: { row: 0, column: 0 },
+      end: { row: 0, column: 1 },
+    };
+    const editor = buildMockTextEditor({
+      getBuffer: () => ({ getRange: () => fakeBufferRange }),
+    });
 
     const actual = getBufferRange(editor);
 
@@ -197,7 +202,7 @@ describe('isCurrentScopeVueScope()', () => {
 
 describe('getCurrentFilePath()', () => {
   it('returns the current file path if there is one', () => {
-    const file = { path: 'xyz.js' };
+    const file = { path: 'xyz.js', getPath: () => 'xyz.js' };
     const editor = buildMockTextEditor({ buffer: { file } });
 
     const actual = getCurrentFilePath(editor);
@@ -213,4 +218,13 @@ describe('getCurrentFilePath()', () => {
 
     expect(actual).toBeUndefined();
   });
+
+  it('uses the getPath method over reading the path directly from the buffer', () => {
+    const file = { path: 'wrongPath.js', getPath: () => 'rightPath.js' };
+    const editor = buildMockTextEditor({ buffer: { file } });
+
+    const actual = getCurrentFilePath(editor);
+
+    expect(actual).toEqual('rightPath.js');
+  });
 });
diff --git a/src/helpers/getPrettierInstance.test.js b/src/helpers/getPrettierInstance.test.js
index 5ec1fcd..2d64aec 100644
--- a/src/helpers/getPrettierInstance.test.js
+++ b/src/helpers/getPrettierInstance.test.js
@@ -8,7 +8,8 @@ const getPrettierInstance = require('./getPrettierInstance');
 
 test("returns user's project's local prettier instance if it exists", () => {
   const filePath = path.join(__dirname, '..', '..', 'tests', 'fixtures', 'sourceFile.js');
-  const editor = createMockTextEditor({ buffer: { file: { path: filePath } } });
+  const file = { path: filePath, getPath: () => filePath };
+  const editor = createMockTextEditor({ buffer: { file } });
   const prettierLib = path.join(__dirname, '..', '..', 'tests', 'fixtures', 'prettier.js');
   atomLinter.findCached.mockImplementation(() => prettierLib);
 
@@ -24,7 +25,8 @@ test("returns user's project's local prettier instance if it exists", () => {
 test("returns bundled prettier if user's project has no local prettier package", () => {
   atomLinter.findCached.mockImplementation(() => undefined);
   const filePath = path.join(__dirname, 'sourceFile.js');
-  const editor = createMockTextEditor({ buffer: { file: { path: filePath } } });
+  const file = { path: filePath, getPath: () => filePath };
+  const editor = createMockTextEditor({ buffer: { file } });
 
   const actual = getPrettierInstance(editor);
 
@@ -32,7 +34,8 @@ test("returns bundled prettier if user's project has no local prettier package",
 });
 
 test('returns bundled prettier if filePath is null', () => {
-  const editor = createMockTextEditor({ buffer: { file: { path: null } } });
+  const file = { path: null, getPath: () => null };
+  const editor = createMockTextEditor({ buffer: { file } });
 
   const actual = getPrettierInstance(editor);
 
@@ -40,7 +43,8 @@ test('returns bundled prettier if filePath is null', () => {
 });
 
 test('returns bundled prettier if filePath has no parent directory', () => {
-  const editor = createMockTextEditor({ buffer: { file: { path: 'foo.js' } } });
+  const file = { path: 'foo.js', getPath: () => 'foo.js' };
+  const editor = createMockTextEditor({ buffer: { file } });
 
   const actual = getPrettierInstance(editor);
 
diff --git a/tests/mocks/textEditor.js b/tests/mocks/textEditor.js
index 5431813..b768da5 100644
--- a/tests/mocks/textEditor.js
+++ b/tests/mocks/textEditor.js
@@ -7,6 +7,7 @@ const textEditorFactory = (overrides?: {} = {}): TextEditor =>
       buffer: {
         file: {
           path: 'xyz.js',
+          getPath: jest.fn(),
         },
       },
       getBuffer: jest.fn(() => ({