Repository: prettier/prettier-atom
Pull request #187 (2017-06-08 03:47:08)

Only format if prettier is in dependencies


Patch:
diff --git a/dist/atomInterface/index.js b/dist/atomInterface/index.js
index a71dd4e..343ce7d 100644
--- a/dist/atomInterface/index.js
+++ b/dist/atomInterface/index.js
@@ -39,6 +39,10 @@ var isFormatOnSaveEnabled = function isFormatOnSaveEnabled() {
   return getConfigOption('formatOnSaveOptions.enabled');
 };
 
+var isDisabledIfNotInPackageJson = function isDisabledIfNotInPackageJson() {
+  return getConfigOption('formatOnSaveOptions.isDisabledIfNotInPackageJson');
+};
+
 var shouldRespectEslintignore = function shouldRespectEslintignore() {
   return getConfigOption('formatOnSaveOptions.respectEslintignore');
 };
@@ -112,6 +116,7 @@ module.exports = {
   getPrettierOptions: getPrettierOptions,
   getScopes: getScopes,
   getWhitelistedGlobs: getWhitelistedGlobs,
+  isDisabledIfNotInPackageJson: isDisabledIfNotInPackageJson,
   isFormatOnSaveEnabled: isFormatOnSaveEnabled,
   isLinterEslintAutofixEnabled: isLinterEslintAutofixEnabled,
   runLinter: runLinter,
diff --git a/dist/config-schema.json b/dist/config-schema.json
index 01ea8c5..3a71795 100644
--- a/dist/config-schema.json
+++ b/dist/config-schema.json
@@ -77,6 +77,13 @@
         "type": "array",
         "default": [],
         "order": 6
+      },
+      "isDisabledIfNotInPackageJson": {
+        "title": "Only format if prettier is found in your project's dependencies",
+        "description": "Does not format on save if prettier (or prettier-eslint/prettier-eslint-cli if using eslint integration) is in your project's package.json (dependencies or devDependencies)",
+        "type": "boolean",
+        "default": false,
+        "order": 7
       }
     }
   },
diff --git a/dist/displayDebugInfo/index.js b/dist/displayDebugInfo/index.js
index be3299e..8061dc2 100644
--- a/dist/displayDebugInfo/index.js
+++ b/dist/displayDebugInfo/index.js
@@ -1,6 +1,6 @@
 'use strict';
 
-var readPkg = require('read-pkg');
+var readPkgUp = require('read-pkg-up');
 var path = require('path');
 
 var _require = require('../atomInterface'),
@@ -13,7 +13,7 @@ var getDepPath = function getDepPath(dep) {
 };
 
 var getDebugInfo = function getDebugInfo() {
-  return ('\nAtom version: ' + getAtomVersion() + '\nprettier-atom version: ' + readPkg.sync(path.join(__dirname, '..', '..')).version + '\nprettier version: ' + readPkg.sync(getDepPath('prettier')).version + '\nprettier-eslint version: ' + readPkg.sync(getDepPath('prettier-eslint')).version + '\nprettier-atom configuration: ' + JSON.stringify(getPrettierAtomConfig(), null, 2) + '\n').trim();
+  return ('\nAtom version: ' + getAtomVersion() + '\nprettier-atom version: ' + readPkgUp.sync(__dirname).version + '\nprettier version: ' + readPkgUp.sync(getDepPath('prettier')).version + '\nprettier-eslint version: ' + readPkgUp.sync(getDepPath('prettier-eslint')).version + '\nprettier-atom configuration: ' + JSON.stringify(getPrettierAtomConfig(), null, 2) + '\n').trim();
 };
 
 var displayDebugInfo = function displayDebugInfo() {
diff --git a/dist/editorInterface/index.js b/dist/editorInterface/index.js
index 6ac1d80..4988637 100644
--- a/dist/editorInterface/index.js
+++ b/dist/editorInterface/index.js
@@ -1,5 +1,8 @@
 'use strict';
 
+var _ = require('lodash/fp');
+var path = require('path');
+
 var EMBEDDED_SCOPES = ['text.html.vue', 'text.html.basic'];
 
 var getBufferRange = function getBufferRange(editor) {
@@ -18,9 +21,14 @@ var getCurrentFilePath = function getCurrentFilePath(editor) {
   return editor.buffer.file ? editor.buffer.file.path : undefined;
 };
 
+var getCurrentDir = _.flow(getCurrentFilePath, function (maybeFilePath) {
+  return typeof maybeFilePath === 'string' ? path.dirname(maybeFilePath) : undefined;
+});
+
 module.exports = {
   getBufferRange: getBufferRange,
   isCurrentScopeEmbeddedScope: isCurrentScopeEmbeddedScope,
   getCurrentScope: getCurrentScope,
-  getCurrentFilePath: getCurrentFilePath
+  getCurrentFilePath: getCurrentFilePath,
+  getCurrentDir: getCurrentDir
 };
\ No newline at end of file
diff --git a/dist/executePrettier/getPrettierInstance.js b/dist/executePrettier/getPrettierInstance.js
index f2a16f3..d4e2282 100644
--- a/dist/executePrettier/getPrettierInstance.js
+++ b/dist/executePrettier/getPrettierInstance.js
@@ -16,14 +16,6 @@ var getLocalPrettierPath = function getLocalPrettierPath(filePath) {
   return findCachedFromFilePath(filePath, PRETTIER_INDEX_PATH);
 };
 
-// const getLocalPrettierPath = (filePath: ?FilePath): ?FilePath => {
-//   if (!filePath) return null;
-//
-//   const dirPath = getDirFromFilePath(filePath);
-//
-//   return dirPath ? findCached(dirPath, PRETTIER_INDEX_PATH) : null;
-// };
-
 // charypar: This is currently the best way to use local prettier instance.
 // Using the CLI introduces a noticeable delay and there is currently no
 // way to use prettier as a long-running process for formatting files as needed
diff --git a/dist/formatOnSave/isPrettierInPackageJson.js b/dist/formatOnSave/isPrettierInPackageJson.js
new file mode 100644
index 0000000..90d9dce
--- /dev/null
+++ b/dist/formatOnSave/isPrettierInPackageJson.js
@@ -0,0 +1,30 @@
+'use strict';
+
+var _ = require('lodash/fp');
+var readPgkUp = require('read-pkg-up');
+
+var _require = require('../editorInterface'),
+    getCurrentDir = _require.getCurrentDir;
+
+var _require2 = require('../atomInterface'),
+    shouldUseEslint = _require2.shouldUseEslint;
+
+var hasPackageDependency = function hasPackageDependency(packageName) {
+  return _.flow(_.get('pkg.dependencies'), _.has(packageName));
+};
+
+var hasPackageDevDependency = function hasPackageDevDependency(packageName) {
+  return _.flow(_.get('pkg.devDependencies'), _.has(packageName));
+};
+
+var hasPackage = function hasPackage(packageName) {
+  return _.overSome([hasPackageDependency(packageName), hasPackageDevDependency(packageName)]);
+};
+
+var readContentsOfNearestPackageJson = _.flow(getCurrentDir, _.set('cwd', _, {}), readPgkUp.sync);
+
+var isPrettierInPackageJson = _.flow(readContentsOfNearestPackageJson, hasPackage('prettier'));
+
+var isPrettierEslintInPackageJson = _.flow(readContentsOfNearestPackageJson, _.overSome([hasPackage('prettier-eslint'), hasPackage('prettier-eslint-cli')]));
+
+module.exports = _.cond([[shouldUseEslint, isPrettierEslintInPackageJson], [_.stubTrue, isPrettierInPackageJson]]);
\ No newline at end of file
diff --git a/dist/formatOnSave/shouldFormatOnSave.js b/dist/formatOnSave/shouldFormatOnSave.js
index a1b138a..2b2c16f 100644
--- a/dist/formatOnSave/shouldFormatOnSave.js
+++ b/dist/formatOnSave/shouldFormatOnSave.js
@@ -13,9 +13,11 @@ var _require3 = require('../atomInterface'),
     isFormatOnSaveEnabled = _require3.isFormatOnSaveEnabled,
     getScopes = _require3.getScopes,
     getExcludedGlobs = _require3.getExcludedGlobs,
-    getWhitelistedGlobs = _require3.getWhitelistedGlobs;
+    getWhitelistedGlobs = _require3.getWhitelistedGlobs,
+    isDisabledIfNotInPackageJson = _require3.isDisabledIfNotInPackageJson;
 
 var isFilePathEslintignored = require('./isFilePathEslintIgnored');
+var isPrettierInPackageJson = require('./isPrettierInPackageJson');
 
 var hasFilePath = function hasFilePath(editor) {
   return !!getCurrentFilePath(editor);
@@ -37,6 +39,6 @@ var isFilePathWhitelisted = _.flow(getCurrentFilePath, function (filePath) {
 
 var isFilePathNotEslintignored = _.flow(getCurrentFilePath, _.negate(isFilePathEslintignored));
 
-var shouldFormatOnSave = _.overEvery([isFormatOnSaveEnabled, hasFilePath, isInScope, _.overSome([isFilePathWhitelisted, _.overEvery([noWhitelistGlobsPresent, filePathDoesNotMatchBlacklistGlobs])]), isFilePathNotEslintignored]);
+var shouldFormatOnSave = _.overEvery([isFormatOnSaveEnabled, hasFilePath, isInScope, _.overSome([isFilePathWhitelisted, _.overEvery([noWhitelistGlobsPresent, filePathDoesNotMatchBlacklistGlobs])]), isFilePathNotEslintignored, _.overSome([_.negate(isDisabledIfNotInPackageJson), isPrettierInPackageJson])]);
 
 module.exports = shouldFormatOnSave;
\ No newline at end of file
diff --git a/flow-typed/npm/read-pkg-up_vx.x.x.js b/flow-typed/npm/read-pkg-up_vx.x.x.js
new file mode 100644
index 0000000..c8c530d
--- /dev/null
+++ b/flow-typed/npm/read-pkg-up_vx.x.x.js
@@ -0,0 +1,33 @@
+// flow-typed signature: 641430480f0e2b7d2e3570b558e8c659
+// flow-typed version: <<STUB>>/read-pkg-up_v^2.0.0/flow_v0.46.0
+
+/**
+ * This is an autogenerated libdef stub for:
+ *
+ *   'read-pkg-up'
+ *
+ * Fill this stub out by replacing all the `any` types.
+ *
+ * Once filled out, we encourage you to share your work with the
+ * community by sending a pull request to:
+ * https://github.com/flowtype/flow-typed
+ */
+
+declare module 'read-pkg-up' {
+  declare module.exports: any;
+}
+
+/**
+ * We include stubs for each file inside this npm package in case you need to
+ * require those files directly. Feel free to delete any files that aren't
+ * needed.
+ */
+
+
+// Filename aliases
+declare module 'read-pkg-up/index' {
+  declare module.exports: $Exports<'read-pkg-up'>;
+}
+declare module 'read-pkg-up/index.js' {
+  declare module.exports: $Exports<'read-pkg-up'>;
+}
diff --git a/package.json b/package.json
index bea0506..4c4e0e5 100644
--- a/package.json
+++ b/package.json
@@ -28,7 +28,7 @@
     "loophole": "^1.1.0",
     "prettier": "^1.4.1",
     "prettier-eslint": "^6.2.3",
-    "read-pkg": "^2.0.0"
+    "read-pkg-up": "^2.0.0"
   },
   "devDependencies": {
     "all-contributors-cli": "^4.3.0",
diff --git a/src/atomInterface/index.js b/src/atomInterface/index.js
index d62bb45..e2ea723 100644
--- a/src/atomInterface/index.js
+++ b/src/atomInterface/index.js
@@ -25,6 +25,9 @@ const shouldDisplayErrors = () => !getConfigOption('silenceErrors');
 
 const isFormatOnSaveEnabled = () => getConfigOption('formatOnSaveOptions.enabled');
 
+const isDisabledIfNotInPackageJson = () =>
+  getConfigOption('formatOnSaveOptions.isDisabledIfNotInPackageJson');
+
 const shouldRespectEslintignore = () => getConfigOption('formatOnSaveOptions.respectEslintignore');
 
 const getScopes = () => getConfigOption('formatOnSaveOptions.scopes');
@@ -75,6 +78,7 @@ module.exports = {
   getPrettierOptions,
   getScopes,
   getWhitelistedGlobs,
+  isDisabledIfNotInPackageJson,
   isFormatOnSaveEnabled,
   isLinterEslintAutofixEnabled,
   runLinter,
diff --git a/src/config-schema.json b/src/config-schema.json
index 01ea8c5..3a71795 100644
--- a/src/config-schema.json
+++ b/src/config-schema.json
@@ -77,6 +77,13 @@
         "type": "array",
         "default": [],
         "order": 6
+      },
+      "isDisabledIfNotInPackageJson": {
+        "title": "Only format if prettier is found in your project's dependencies",
+        "description": "Does not format on save if prettier (or prettier-eslint/prettier-eslint-cli if using eslint integration) is in your project's package.json (dependencies or devDependencies)",
+        "type": "boolean",
+        "default": false,
+        "order": 7
       }
     }
   },
diff --git a/src/displayDebugInfo/index.js b/src/displayDebugInfo/index.js
index e83f0e2..47a9360 100644
--- a/src/displayDebugInfo/index.js
+++ b/src/displayDebugInfo/index.js
@@ -1,5 +1,5 @@
 // @flow
-const readPkg = require('read-pkg');
+const readPkgUp = require('read-pkg-up');
 const path = require('path');
 const { getAtomVersion, getPrettierAtomConfig, addInfoNotification } = require('../atomInterface');
 
@@ -8,9 +8,9 @@ const getDepPath = (dep: string) => path.join(__dirname, '..', '..', 'node_modul
 const getDebugInfo = () =>
   `
 Atom version: ${getAtomVersion()}
-prettier-atom version: ${readPkg.sync(path.join(__dirname, '..', '..')).version}
-prettier version: ${readPkg.sync(getDepPath('prettier')).version}
-prettier-eslint version: ${readPkg.sync(getDepPath('prettier-eslint')).version}
+prettier-atom version: ${readPkgUp.sync(__dirname).version}
+prettier version: ${readPkgUp.sync(getDepPath('prettier')).version}
+prettier-eslint version: ${readPkgUp.sync(getDepPath('prettier-eslint')).version}
 prettier-atom configuration: ${JSON.stringify(getPrettierAtomConfig(), null, 2)}
 `.trim();
 
diff --git a/src/editorInterface/index.js b/src/editorInterface/index.js
index 74b6c0f..9d4e191 100644
--- a/src/editorInterface/index.js
+++ b/src/editorInterface/index.js
@@ -1,4 +1,7 @@
 // @flow
+const _ = require('lodash/fp');
+const path = require('path');
+
 const EMBEDDED_SCOPES = ['text.html.vue', 'text.html.basic'];
 
 const getBufferRange = (editor: TextEditor) => editor.getBuffer().getRange();
@@ -9,9 +12,15 @@ const isCurrentScopeEmbeddedScope = (editor: TextEditor) => EMBEDDED_SCOPES.incl
 
 const getCurrentFilePath = (editor: TextEditor) => (editor.buffer.file ? editor.buffer.file.path : undefined);
 
+const getCurrentDir: (editor: TextEditor) => ?string = _.flow(
+  getCurrentFilePath,
+  (maybeFilePath: ?string) => (typeof maybeFilePath === 'string' ? path.dirname(maybeFilePath) : undefined),
+);
+
 module.exports = {
   getBufferRange,
   isCurrentScopeEmbeddedScope,
   getCurrentScope,
   getCurrentFilePath,
+  getCurrentDir,
 };
diff --git a/src/executePrettier/getPrettierInstance.js b/src/executePrettier/getPrettierInstance.js
index 81de153..b1980b3 100644
--- a/src/executePrettier/getPrettierInstance.js
+++ b/src/executePrettier/getPrettierInstance.js
@@ -10,14 +10,6 @@ const PRETTIER_INDEX_PATH = path.join('node_modules', 'prettier', 'index.js');
 const getLocalPrettierPath = (filePath: ?FilePath): ?FilePath =>
   findCachedFromFilePath(filePath, PRETTIER_INDEX_PATH);
 
-// const getLocalPrettierPath = (filePath: ?FilePath): ?FilePath => {
-//   if (!filePath) return null;
-//
-//   const dirPath = getDirFromFilePath(filePath);
-//
-//   return dirPath ? findCached(dirPath, PRETTIER_INDEX_PATH) : null;
-// };
-
 // charypar: This is currently the best way to use local prettier instance.
 // Using the CLI introduces a noticeable delay and there is currently no
 // way to use prettier as a long-running process for formatting files as needed
diff --git a/src/formatOnSave/isPrettierInPackageJson.js b/src/formatOnSave/isPrettierInPackageJson.js
new file mode 100644
index 0000000..e26c998
--- /dev/null
+++ b/src/formatOnSave/isPrettierInPackageJson.js
@@ -0,0 +1,36 @@
+// @flow
+const _ = require('lodash/fp');
+const readPgkUp = require('read-pkg-up');
+
+const { getCurrentDir } = require('../editorInterface');
+const { shouldUseEslint } = require('../atomInterface');
+
+const hasPackageDependency = (packageName: string): ((packageJson: {}) => boolean) =>
+  _.flow(_.get('pkg.dependencies'), _.has(packageName));
+
+const hasPackageDevDependency = (packageName: string) =>
+  _.flow(_.get('pkg.devDependencies'), _.has(packageName));
+
+const hasPackage = (packageName: string): ((packageJson: {}) => boolean) =>
+  _.overSome([hasPackageDependency(packageName), hasPackageDevDependency(packageName)]);
+
+const readContentsOfNearestPackageJson: TextEditor => {} = _.flow(
+  getCurrentDir,
+  _.set('cwd', _, {}),
+  readPgkUp.sync,
+);
+
+const isPrettierInPackageJson: (editor: TextEditor) => boolean = _.flow(
+  readContentsOfNearestPackageJson,
+  hasPackage('prettier'),
+);
+
+const isPrettierEslintInPackageJson: (editor: TextEditor) => boolean = _.flow(
+  readContentsOfNearestPackageJson,
+  _.overSome([hasPackage('prettier-eslint'), hasPackage('prettier-eslint-cli')]),
+);
+
+module.exports = _.cond([
+  [shouldUseEslint, isPrettierEslintInPackageJson],
+  [_.stubTrue, isPrettierInPackageJson],
+]);
diff --git a/src/formatOnSave/shouldFormatOnSave.js b/src/formatOnSave/shouldFormatOnSave.js
index f424358..98cd806 100644
--- a/src/formatOnSave/shouldFormatOnSave.js
+++ b/src/formatOnSave/shouldFormatOnSave.js
@@ -7,8 +7,10 @@ const {
   getScopes,
   getExcludedGlobs,
   getWhitelistedGlobs,
+  isDisabledIfNotInPackageJson,
 } = require('../atomInterface');
 const isFilePathEslintignored = require('./isFilePathEslintIgnored');
+const isPrettierInPackageJson = require('./isPrettierInPackageJson');
 
 const hasFilePath = (editor: TextEditor) => !!getCurrentFilePath(editor);
 
@@ -42,6 +44,7 @@ const shouldFormatOnSave: (editor: TextEditor) => boolean = _.overEvery([
     _.overEvery([noWhitelistGlobsPresent, filePathDoesNotMatchBlacklistGlobs]),
   ]),
   isFilePathNotEslintignored,
+  _.overSome([_.negate(isDisabledIfNotInPackageJson), isPrettierInPackageJson]),
 ]);
 
 module.exports = shouldFormatOnSave;


Test patch:
diff --git a/src/displayDebugInfo/index.test.js b/src/displayDebugInfo/index.test.js
index 6054b41..b750018 100644
--- a/src/displayDebugInfo/index.test.js
+++ b/src/displayDebugInfo/index.test.js
@@ -1,7 +1,7 @@
-jest.mock('read-pkg');
+jest.mock('read-pkg-up');
 jest.mock('../atomInterface');
 
-const readPkg = require('read-pkg');
+const readPkgUp = require('read-pkg-up');
 const displayDebugInfo = require('./index');
 const { getAtomVersion, getPrettierAtomConfig, addInfoNotification } = require('../atomInterface');
 
@@ -12,7 +12,7 @@ test('it displays a notification on Atom with package information', () => {
     title = _title;
     options = _options;
   });
-  readPkg.sync.mockImplementation(() => ({ version: 'FAKE_PACKAGE_VERSION' }));
+  readPkgUp.sync.mockImplementation(() => ({ version: 'FAKE_PACKAGE_VERSION' }));
   getAtomVersion.mockImplementation(() => 'FAKE_ATOM_VERSION');
   getPrettierAtomConfig.mockImplementation(() => 'FAKE_PRETTIER_ATOM_CONFIG');
 
diff --git a/src/formatOnSave/isPrettierInPackageJson.test.js b/src/formatOnSave/isPrettierInPackageJson.test.js
new file mode 100644
index 0000000..fcf8a4e
--- /dev/null
+++ b/src/formatOnSave/isPrettierInPackageJson.test.js
@@ -0,0 +1,98 @@
+jest.mock('read-pkg-up');
+jest.mock('../editorInterface');
+jest.mock('../atomInterface');
+
+const readPkgUp = require('read-pkg-up');
+const { getCurrentDir } = require('../editorInterface');
+const { shouldUseEslint } = require('../atomInterface');
+const isPrettierInPackageJson = require('./isPrettierInPackageJson');
+
+describe('when shouldUseEslint is false', () => {
+  beforeEach(() => {
+    shouldUseEslint.mockImplementation(() => false);
+  });
+
+  it('calls read-pkg-up with the current filepath', () => {
+    getCurrentDir.mockImplementation(() => '/parent-dir-of-file');
+
+    isPrettierInPackageJson();
+
+    expect(readPkgUp.sync).toHaveBeenCalledWith({ cwd: '/parent-dir-of-file' });
+  });
+
+  it('is true if prettier is a dependency', () => {
+    readPkgUp.sync.mockImplementation(() => ({ pkg: { dependencies: { prettier: '^0.0.1' } } }));
+
+    const actual = isPrettierInPackageJson();
+
+    expect(actual).toBe(true);
+  });
+
+  it('is true if prettier is a dev dependency', () => {
+    readPkgUp.sync.mockImplementation(() => ({ pkg: { devDependencies: { prettier: '^0.0.1' } } }));
+
+    const actual = isPrettierInPackageJson();
+
+    expect(actual).toBe(true);
+  });
+
+  it('is false if prettier is not a dependency', () => {
+    const actual = isPrettierInPackageJson();
+
+    expect(actual).toBe(false);
+  });
+});
+
+describe('when shouldUseEslint is true', () => {
+  beforeEach(() => {
+    shouldUseEslint.mockImplementation(() => true);
+  });
+
+  it('calls read-pkg-up with the current filepath', () => {
+    getCurrentDir.mockImplementation(() => '/parent-dir-of-file');
+
+    isPrettierInPackageJson();
+
+    expect(readPkgUp.sync).toHaveBeenCalledWith({ cwd: '/parent-dir-of-file' });
+  });
+
+  it('is true if prettier-eslint is a dependency', () => {
+    readPkgUp.sync.mockImplementation(() => ({ pkg: { dependencies: { 'prettier-eslint': '^0.0.1' } } }));
+
+    const actual = isPrettierInPackageJson();
+
+    expect(actual).toBe(true);
+  });
+
+  it('is true if prettier-eslint is a dev dependency', () => {
+    readPkgUp.sync.mockImplementation(() => ({ pkg: { devDependencies: { 'prettier-eslint': '^0.0.1' } } }));
+
+    const actual = isPrettierInPackageJson();
+
+    expect(actual).toBe(true);
+  });
+
+  it('is true if prettier-eslint-cli is a dependency', () => {
+    readPkgUp.sync.mockImplementation(() => ({ pkg: { dependencies: { 'prettier-eslint-cli': '^0.0.1' } } }));
+
+    const actual = isPrettierInPackageJson();
+
+    expect(actual).toBe(true);
+  });
+
+  it('is true if prettier-eslint-cli is a dev dependency', () => {
+    readPkgUp.sync.mockImplementation(() => ({
+      pkg: { devDependencies: { 'prettier-eslint-cli': '^0.0.1' } },
+    }));
+
+    const actual = isPrettierInPackageJson();
+
+    expect(actual).toBe(true);
+  });
+
+  it('is false if neither prettier-eslint nor prettier-eslint-cli are dependencies', () => {
+    const actual = isPrettierInPackageJson();
+
+    expect(actual).toBe(false);
+  });
+});
diff --git a/src/formatOnSave/shouldFormatOnSave.test.js b/src/formatOnSave/shouldFormatOnSave.test.js
index a06a52e..c983af5 100644
--- a/src/formatOnSave/shouldFormatOnSave.test.js
+++ b/src/formatOnSave/shouldFormatOnSave.test.js
@@ -1,9 +1,11 @@
 jest.mock('../editorInterface');
 jest.mock('../atomInterface');
 jest.mock('./isFilePathEslintIgnored');
+jest.mock('./isPrettierInPackageJson');
 
 const createMockTextEditor = require('../../tests/mocks/textEditor');
 const {
+  isDisabledIfNotInPackageJson,
   isFormatOnSaveEnabled,
   getExcludedGlobs,
   getWhitelistedGlobs,
@@ -12,6 +14,7 @@ const {
 const { getCurrentScope, getCurrentFilePath } = require('../editorInterface');
 const isFilePathEslintIgnored = require('./isFilePathEslintIgnored');
 const shouldFormatOnSave = require('./shouldFormatOnSave');
+const isPrettierInPackageJson = require('./isPrettierInPackageJson');
 
 const fakeCurrentFilePath = 'foo.js';
 const callShouldFormatOnSave = () => shouldFormatOnSave(createMockTextEditor());
@@ -22,6 +25,7 @@ beforeEach(() => {
   getCurrentScope.mockImplementation(() => 'js');
   getCurrentFilePath.mockImplementation(() => fakeCurrentFilePath);
   isFilePathEslintIgnored.mockImplementation(() => false);
+  isDisabledIfNotInPackageJson.mockImplementation(() => false);
 });
 
 it('returns true if should format on save', () => {
@@ -86,3 +90,21 @@ it('returns false if the filepath is eslintignored', () => {
 
   expect(actual).toBe(false);
 });
+
+it('returns true if prettier needs to be in package json and is found in package json', () => {
+  isDisabledIfNotInPackageJson.mockImplementation(() => true);
+  isPrettierInPackageJson.mockImplementation(() => true);
+
+  const actual = callShouldFormatOnSave();
+
+  expect(actual).toBe(true);
+});
+
+it("returns false if prettier needs to be in package json and it isn't", () => {
+  isDisabledIfNotInPackageJson.mockImplementation(() => true);
+  isPrettierInPackageJson.mockImplementation(() => false);
+
+  const actual = callShouldFormatOnSave();
+
+  expect(actual).toBe(false);
+});