Repository: prettier/prettier-atom
Pull request #463 (2018-11-19 19:44:27)

fix(ignores): ensure we always pass a relative string to the ignores package

codecov-io: # [Codecov](https://codecov.io/gh/prettier/prettier-atom/pull/463?src=pr&el=h1) Report
> Merging [#463](https://codecov.io/gh/prettier/prettier-atom/pull/463?src=pr&el=desc) into [master](https://codecov.io/gh/prettier/prettier-atom/commit/1507075c3277bc9dd55cc58462c84b645aa8b316?src=pr&el=desc) will **decrease** coverage by `0.06%`.
> The diff coverage is `66.66%`.

[![Impacted file tree graph](https://codecov.io/gh/prettier/prettier-atom/pull/463/graphs/tree.svg?width=650&token=Pdibkvk5Zo&height=150&src=pr)](https://codecov.io/gh/prettier/prettier-atom/pull/463?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #463      +/-   ##
==========================================
- Coverage    76.4%   76.33%   -0.07%     
==========================================
  Files          28       28              
  Lines         445      448       +3     
  Branches       44       45       +1     
==========================================
+ Hits          340      342       +2     
  Misses         89       89              
- Partials       16       17       +1
```


| [Impacted Files](https://codecov.io/gh/prettier/prettier-atom/pull/463?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/formatOnSave/shouldFormatOnSave.js](https://codecov.io/gh/prettier/prettier-atom/pull/463/diff?src=pr&el=tree#diff-c3JjL2Zvcm1hdE9uU2F2ZS9zaG91bGRGb3JtYXRPblNhdmUuanM=) | `100% <ø> (ø)` | :arrow_up: |
| [src/atomInterface/index.js](https://codecov.io/gh/prettier/prettier-atom/pull/463/diff?src=pr&el=tree#diff-c3JjL2F0b21JbnRlcmZhY2UvaW5kZXguanM=) | `90% <66.66%> (-1.9%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/prettier/prettier-atom/pull/463?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/prettier/prettier-atom/pull/463?src=pr&el=footer). Last update [1507075...ab97db7](https://codecov.io/gh/prettier/prettier-atom/pull/463?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/decls/index.js b/decls/index.js
index 1b76928..3324ed0 100644
--- a/decls/index.js
+++ b/decls/index.js
@@ -84,6 +84,9 @@ declare var atom: {
   packages: {
     isPackageActive: (name: string) => boolean,
   },
+  project: {
+    relativizePath: (path: string) => [string | null, string],
+  },
   tooltips: {
     add: (target: HTMLElement, options?: Atom$Tooltips$Options) => Atom$Disposable,
   },
diff --git a/dist/atomInterface/index.js b/dist/atomInterface/index.js
index 76f49c6..e1d62ff 100644
--- a/dist/atomInterface/index.js
+++ b/dist/atomInterface/index.js
@@ -6,6 +6,8 @@ var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);
 
 function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
 
+const _ = require('lodash/fp');
+
 // constants
 const LINTER_LINT_COMMAND = 'linter:lint';
 
@@ -68,6 +70,8 @@ const attemptWithErrorNotification = (() => {
 
 const runLinter = editor => isLinterLintCommandDefined(editor) && atom.commands.dispatch(atom.views.getView(editor), LINTER_LINT_COMMAND);
 
+const relativizePathFromAtomProject = path => path ? _.get('[1]', atom.project.relativizePath(path)) : null;
+
 module.exports = {
   addErrorNotification,
   addInfoNotification,
@@ -82,6 +86,7 @@ module.exports = {
   isDisabledIfNoConfigFile,
   isFormatOnSaveEnabled,
   isLinterEslintAutofixEnabled,
+  relativizePathFromAtomProject,
   runLinter,
   shouldRespectEslintignore,
   shouldUseEslint,
diff --git a/dist/formatOnSave/isFilePathEslintIgnored.js b/dist/formatOnSave/isFilePathEslintIgnored.js
index cf6fede..aedcd79 100644
--- a/dist/formatOnSave/isFilePathEslintIgnored.js
+++ b/dist/formatOnSave/isFilePathEslintIgnored.js
@@ -11,7 +11,7 @@ const getNearestEslintignorePath = filePath => findCachedFromFilePath(filePath,
 
 const safeRelativePath = _.curry(
 // $FlowFixMe
-(from, to) => !!from && !!to ? path.relative(from, to) : undefined);
+(from, to) => !!from && !!to ? path.join(path.relative(from, to)) : undefined);
 
 const getFilePathRelativeToEslintignore = (filePath
 // $FlowIssue: lodashfp placeholders not supported yet
diff --git a/dist/formatOnSave/shouldFormatOnSave.js b/dist/formatOnSave/shouldFormatOnSave.js
index b3752f9..24ae4d1 100644
--- a/dist/formatOnSave/shouldFormatOnSave.js
+++ b/dist/formatOnSave/shouldFormatOnSave.js
@@ -14,6 +14,7 @@ const {
   isFormatOnSaveEnabled,
   isDisabledIfNotInPackageJson,
   isDisabledIfNoConfigFile,
+  relativizePathFromAtomProject,
   shouldRespectEslintignore
 } = require('../atomInterface');
 const isFilePathEslintIgnored = require('./isFilePathEslintIgnored');
@@ -21,12 +22,12 @@ const isPrettierInPackageJson = require('./isPrettierInPackageJson');
 
 const hasFilePath = editor => !!getCurrentFilePath(editor);
 
-const filePathDoesNotMatchBlacklistGlobs = _.flow(getCurrentFilePath, filePath => _.negate(someGlobsMatchFilePath)(getExcludedGlobs(), filePath));
+const filePathDoesNotMatchBlacklistGlobs = _.flow(getCurrentFilePath, relativizePathFromAtomProject, filePath => _.negate(someGlobsMatchFilePath)(getExcludedGlobs(), filePath));
 
 // $FlowFixMe
 const noWhitelistGlobsPresent = _.flow(getWhitelistedGlobs, _.isEmpty);
 
-const isFilePathWhitelisted = _.flow(getCurrentFilePath, filePath => someGlobsMatchFilePath(getWhitelistedGlobs(), filePath));
+const isFilePathWhitelisted = _.flow(getCurrentFilePath, relativizePathFromAtomProject, filePath => someGlobsMatchFilePath(getWhitelistedGlobs(), filePath));
 
 const isEslintIgnored = _.flow(getCurrentFilePath, isFilePathEslintIgnored);
 
diff --git a/src/atomInterface/index.js b/src/atomInterface/index.js
index 91463af..e150893 100644
--- a/src/atomInterface/index.js
+++ b/src/atomInterface/index.js
@@ -1,4 +1,5 @@
 // @flow
+const _ = require('lodash/fp');
 
 // constants
 const LINTER_LINT_COMMAND = 'linter:lint';
@@ -67,6 +68,9 @@ const runLinter = (editor: TextEditor) =>
   isLinterLintCommandDefined(editor) &&
   atom.commands.dispatch(atom.views.getView(editor), LINTER_LINT_COMMAND);
 
+const relativizePathFromAtomProject = (path: ?string) =>
+  path ? _.get('[1]', atom.project.relativizePath(path)) : null;
+
 module.exports = {
   addErrorNotification,
   addInfoNotification,
@@ -81,6 +85,7 @@ module.exports = {
   isDisabledIfNoConfigFile,
   isFormatOnSaveEnabled,
   isLinterEslintAutofixEnabled,
+  relativizePathFromAtomProject,
   runLinter,
   shouldRespectEslintignore,
   shouldUseEslint,
diff --git a/src/formatOnSave/isFilePathEslintIgnored.js b/src/formatOnSave/isFilePathEslintIgnored.js
index 6996cab..bb99523 100644
--- a/src/formatOnSave/isFilePathEslintIgnored.js
+++ b/src/formatOnSave/isFilePathEslintIgnored.js
@@ -11,7 +11,8 @@ const getNearestEslintignorePath = (filePath: FilePath): ?FilePath =>
 
 const safeRelativePath = _.curry(
   // $FlowFixMe
-  (from: ?FilePath, to: ?FilePath): ?FilePath => (!!from && !!to ? path.relative(from, to) : undefined),
+  (from: ?FilePath, to: ?FilePath): ?FilePath =>
+    !!from && !!to ? path.join(path.relative(from, to)) : undefined,
 );
 
 const getFilePathRelativeToEslintignore = (filePath: FilePath): ?FilePath =>
diff --git a/src/formatOnSave/shouldFormatOnSave.js b/src/formatOnSave/shouldFormatOnSave.js
index d622fd9..4c72cb9 100644
--- a/src/formatOnSave/shouldFormatOnSave.js
+++ b/src/formatOnSave/shouldFormatOnSave.js
@@ -13,6 +13,7 @@ const {
   isFormatOnSaveEnabled,
   isDisabledIfNotInPackageJson,
   isDisabledIfNoConfigFile,
+  relativizePathFromAtomProject,
   shouldRespectEslintignore,
 } = require('../atomInterface');
 const isFilePathEslintIgnored = require('./isFilePathEslintIgnored');
@@ -22,6 +23,7 @@ const hasFilePath = (editor: TextEditor) => !!getCurrentFilePath(editor);
 
 const filePathDoesNotMatchBlacklistGlobs: (editor: TextEditor) => boolean = _.flow(
   getCurrentFilePath,
+  relativizePathFromAtomProject,
   (filePath: ?FilePath) => _.negate(someGlobsMatchFilePath)(getExcludedGlobs(), filePath),
 );
 
@@ -33,6 +35,7 @@ const noWhitelistGlobsPresent: () => boolean = _.flow(
 
 const isFilePathWhitelisted: (editor: TextEditor) => boolean = _.flow(
   getCurrentFilePath,
+  relativizePathFromAtomProject,
   (filePath: ?FilePath) => someGlobsMatchFilePath(getWhitelistedGlobs(), filePath),
 );
 


Test patch:
diff --git a/src/formatOnSave/shouldFormatOnSave.test.js b/src/formatOnSave/shouldFormatOnSave.test.js
index e12e9b5..3912b78 100644
--- a/src/formatOnSave/shouldFormatOnSave.test.js
+++ b/src/formatOnSave/shouldFormatOnSave.test.js
@@ -13,6 +13,7 @@ const {
   isFormatOnSaveEnabled,
   getExcludedGlobs,
   getWhitelistedGlobs,
+  relativizePathFromAtomProject,
   shouldRespectEslintignore,
 } = require('../atomInterface');
 const getPrettierInstance = require('../helpers/getPrettierInstance');
@@ -31,6 +32,7 @@ beforeEach(() => {
   isFormatOnSaveEnabled.mockImplementation(() => true);
   isFileFormattable.mockImplementation(() => true);
   getCurrentFilePath.mockImplementation(() => fakeCurrentFilePath);
+  relativizePathFromAtomProject.mockImplementation(() => fakeCurrentFilePath);
   isFilePathEslintIgnored.mockImplementation(() => false);
   isPrettierProperVersion.mockImplementation(() => true);
   isDisabledIfNotInPackageJson.mockImplementation(() => false);