Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
feat(settings): add ability not to format on save if a prettier confi…
Browse files Browse the repository at this point in the history
…g is not present
  • Loading branch information
robwise committed Sep 15, 2017
1 parent 881bd09 commit 97bdf8e
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 4 deletions.
5 changes: 5 additions & 0 deletions dist/atomInterface/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ var isDisabledIfNotInPackageJson = function isDisabledIfNotInPackageJson() {
return getConfigOption('formatOnSaveOptions.isDisabledIfNotInPackageJson');
};

var isDisabledIfNoConfigFile = function isDisabledIfNoConfigFile() {
return getConfigOption('formatOnSaveOptions.isDisabledIfNoConfigFile');
};

var shouldRespectEslintignore = function shouldRespectEslintignore() {
return getConfigOption('formatOnSaveOptions.respectEslintignore');
};
Expand Down Expand Up @@ -152,6 +156,7 @@ module.exports = {
getAllScopes: getAllScopes,
getWhitelistedGlobs: getWhitelistedGlobs,
isDisabledIfNotInPackageJson: isDisabledIfNotInPackageJson,
isDisabledIfNoConfigFile: isDisabledIfNoConfigFile,
isFormatOnSaveEnabled: isFormatOnSaveEnabled,
isLinterEslintAutofixEnabled: isLinterEslintAutofixEnabled,
runLinter: runLinter,
Expand Down
8 changes: 8 additions & 0 deletions dist/config-schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions dist/formatOnSave/shouldFormatOnSave.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
var _ = require('lodash/fp');

var _require = require('../helpers'),
someGlobsMatchFilePath = _require.someGlobsMatchFilePath;
someGlobsMatchFilePath = _require.someGlobsMatchFilePath,
getPrettierInstance = _require.getPrettierInstance;

var _require2 = require('../editorInterface'),
getCurrentFilePath = _require2.getCurrentFilePath,
Expand All @@ -14,7 +15,8 @@ var _require3 = require('../atomInterface'),
getAllScopes = _require3.getAllScopes,
getExcludedGlobs = _require3.getExcludedGlobs,
getWhitelistedGlobs = _require3.getWhitelistedGlobs,
isDisabledIfNotInPackageJson = _require3.isDisabledIfNotInPackageJson;
isDisabledIfNotInPackageJson = _require3.isDisabledIfNotInPackageJson,
isDisabledIfNoConfigFile = _require3.isDisabledIfNoConfigFile;

var isFilePathEslintignored = require('./isFilePathEslintIgnored');
var isFilePathPrettierIgnored = require('./isFilePathPrettierIgnored');
Expand Down Expand Up @@ -42,6 +44,10 @@ var isFilePathNotEslintignored = _.flow(getCurrentFilePath, _.negate(isFilePathE

var isFilePathNotPrettierIgnored = _.flow(getCurrentFilePath, _.negate(isFilePathPrettierIgnored));

var shouldFormatOnSave = _.overEvery([isFormatOnSaveEnabled, hasFilePath, isInScope, _.overSome([isFilePathWhitelisted, _.overEvery([noWhitelistGlobsPresent, filePathDoesNotMatchBlacklistGlobs])]), isFilePathNotEslintignored, isFilePathNotPrettierIgnored, _.overSome([_.negate(isDisabledIfNotInPackageJson), isPrettierInPackageJson])]);
var isPrettierConfigPresent = function isPrettierConfigPresent(editor) {
return !!getPrettierInstance(editor).resolveConfig.sync && _.flow(getCurrentFilePath, getPrettierInstance(editor).resolveConfig.sync, _.negate(_.isNil))(editor);
};

var shouldFormatOnSave = _.overEvery([isFormatOnSaveEnabled, hasFilePath, isInScope, _.overSome([isFilePathWhitelisted, _.overEvery([noWhitelistGlobsPresent, filePathDoesNotMatchBlacklistGlobs])]), isFilePathNotEslintignored, isFilePathNotPrettierIgnored, _.overSome([_.negate(isDisabledIfNotInPackageJson), isPrettierInPackageJson]), _.overSome([_.negate(isDisabledIfNoConfigFile), isPrettierConfigPresent])]);

module.exports = shouldFormatOnSave;
3 changes: 3 additions & 0 deletions src/atomInterface/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ const isFormatOnSaveEnabled = () => getConfigOption('formatOnSaveOptions.enabled
const isDisabledIfNotInPackageJson = () =>
getConfigOption('formatOnSaveOptions.isDisabledIfNotInPackageJson');

const isDisabledIfNoConfigFile = () => getConfigOption('formatOnSaveOptions.isDisabledIfNoConfigFile');

const shouldRespectEslintignore = () => getConfigOption('formatOnSaveOptions.respectEslintignore');

const getJavascriptScopes = () => getConfigOption('formatOnSaveOptions.javascriptScopes');
Expand Down Expand Up @@ -106,6 +108,7 @@ module.exports = {
getAllScopes,
getWhitelistedGlobs,
isDisabledIfNotInPackageJson,
isDisabledIfNoConfigFile,
isFormatOnSaveEnabled,
isLinterEslintAutofixEnabled,
runLinter,
Expand Down
8 changes: 8 additions & 0 deletions src/config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@
"type": "boolean",
"default": false,
"order": 11
},
"isDisabledIfNoConfigFile": {
"title": "Only format if a Prettier config is found",
"description":
"Only format on save if we find a `.prettierrc` file (written in YAML or JSON), a `prettier.config.js` file that exports an object, or a 'prettier' key in your `package.json` file.",
"type": "boolean",
"default": false,
"order": 12
}
}
},
Expand Down
8 changes: 7 additions & 1 deletion src/formatOnSave/shouldFormatOnSave.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// @flow
const _ = require('lodash/fp');
const { someGlobsMatchFilePath } = require('../helpers');
const { someGlobsMatchFilePath, getPrettierInstance } = require('../helpers');
const { getCurrentFilePath, getCurrentScope } = require('../editorInterface');
const {
isFormatOnSaveEnabled,
getAllScopes,
getExcludedGlobs,
getWhitelistedGlobs,
isDisabledIfNotInPackageJson,
isDisabledIfNoConfigFile,
} = require('../atomInterface');
const isFilePathEslintignored = require('./isFilePathEslintIgnored');
const isFilePathPrettierIgnored = require('./isFilePathPrettierIgnored');
Expand Down Expand Up @@ -41,6 +42,10 @@ const isFilePathNotPrettierIgnored: (editor: TextEditor) => boolean = _.flow(
_.negate(isFilePathPrettierIgnored),
);

const isPrettierConfigPresent = (editor: TextEditor): boolean =>
!!getPrettierInstance(editor).resolveConfig.sync &&
_.flow(getCurrentFilePath, getPrettierInstance(editor).resolveConfig.sync, _.negate(_.isNil))(editor);

const shouldFormatOnSave: (editor: TextEditor) => boolean = _.overEvery([
isFormatOnSaveEnabled,
hasFilePath,
Expand All @@ -52,6 +57,7 @@ const shouldFormatOnSave: (editor: TextEditor) => boolean = _.overEvery([
isFilePathNotEslintignored,
isFilePathNotPrettierIgnored,
_.overSome([_.negate(isDisabledIfNotInPackageJson), isPrettierInPackageJson]),
_.overSome([_.negate(isDisabledIfNoConfigFile), isPrettierConfigPresent]),
]);

module.exports = shouldFormatOnSave;
26 changes: 26 additions & 0 deletions src/formatOnSave/shouldFormatOnSave.test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
jest.mock('../editorInterface');
jest.mock('../atomInterface');
jest.mock('../helpers/getPrettierInstance');
jest.mock('./isFilePathEslintIgnored');
jest.mock('./isFilePathPrettierIgnored');
jest.mock('./isPrettierInPackageJson');

const createMockTextEditor = require('../../tests/mocks/textEditor');
const {
isDisabledIfNotInPackageJson,
isDisabledIfNoConfigFile,
isFormatOnSaveEnabled,
getExcludedGlobs,
getWhitelistedGlobs,
getAllScopes,
} = require('../atomInterface');
const getPrettierInstance = require('../helpers/getPrettierInstance');
const { getCurrentScope, getCurrentFilePath } = require('../editorInterface');
const isFilePathEslintIgnored = require('./isFilePathEslintIgnored');
const isFilePathPrettierIgnored = require('./isFilePathPrettierIgnored');
Expand All @@ -28,6 +31,7 @@ beforeEach(() => {
getCurrentFilePath.mockImplementation(() => fakeCurrentFilePath);
isFilePathEslintIgnored.mockImplementation(() => false);
isDisabledIfNotInPackageJson.mockImplementation(() => false);
isDisabledIfNoConfigFile.mockImplementation(() => false);
});

it('returns true if should format on save', () => {
Expand Down Expand Up @@ -118,3 +122,25 @@ it("returns false if prettier needs to be in package json and it isn't", () => {

expect(actual).toBe(false);
});

it("returns true if prettier config file needs to be present and it's found", () => {
isDisabledIfNoConfigFile.mockImplementation(() => true);
const fakeSync = jest.fn(() => ({ tabWidth: 100 }));
getPrettierInstance.mockImplementation(() => ({ resolveConfig: { sync: fakeSync } }));

const actual = callShouldFormatOnSave();

expect(actual).toBe(true);
expect(fakeSync).toHaveBeenCalledWith(fakeCurrentFilePath);
});

it("returns false if prettier config file needs to be present and it isn't", () => {
isDisabledIfNoConfigFile.mockImplementation(() => true);
const fakeSync = jest.fn(() => null);
getPrettierInstance.mockImplementation(() => ({ resolveConfig: { sync: fakeSync } }));

const actual = callShouldFormatOnSave();

expect(actual).toBe(false);
expect(fakeSync).toHaveBeenCalledWith(fakeCurrentFilePath);
});

0 comments on commit 97bdf8e

Please sign in to comment.