This repository has been archived by the owner on Apr 17, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(format-on-save): add option to not format on save if prettier no…
…t in dependencies We will look for prettier (prettier-eslint/prettier-eslint-cli if you have the eslint integration setting enabled) in the package.json nearest to the file being formatted. The dependency can be in either the "dependencies" or "devDependencies" section. Resolves #43
- Loading branch information
Showing
14 changed files
with
236 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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], | ||
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.