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

Commit

Permalink
feat(prettier): use Prettier to determine whether a file is formattable
Browse files Browse the repository at this point in the history
Instead of relying on the user to supply prettier-atom with a whole list of Atom scopes for each
Prettier parser type, we instead rely on the new getFileInfo method from Prettier and let Prettier
tell us whether or not a file is parseable. This will make future parser support automatic as well
as allow Prettier plugins to work. Unfortunately, because we rely on this new feature from Prettier,
it will mean that users will be forced to upgrade their local Prettier versions to be compatible.

BREAKING CHANGE: Because prettier-atom now relies on the new `getFileInfo` method recently added to
Prettier, if you are having prettier-atom use a local version of Prettier instead of the version
that is packaged with the plugin, you will need to manually update your project's local Prettier
version.
  • Loading branch information
robwise committed Apr 30, 2018
1 parent 1857831 commit ad88808
Show file tree
Hide file tree
Showing 24 changed files with 153 additions and 242 deletions.
3 changes: 0 additions & 3 deletions dist/atomInterface/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ const getAtomVersion = () => atom.getVersion();

const getPrettierAtomConfig = () => atom.config.get('prettier-atom');

const getScopes = () => getConfigOption('scopes');

const getWhitelistedGlobs = () => getConfigOption('formatOnSaveOptions.whitelistedGlobs');

const getExcludedGlobs = () => getConfigOption('formatOnSaveOptions.excludedGlobs');
Expand Down Expand Up @@ -78,7 +76,6 @@ module.exports = {
getAtomVersion,
getPrettierAtomConfig,
getPrettierEslintOptions,
getScopes,
getWhitelistedGlobs,
getExcludedGlobs,
isDisabledIfNotInPackageJson,
Expand Down
30 changes: 0 additions & 30 deletions dist/config-schema.json

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

5 changes: 0 additions & 5 deletions dist/editorInterface/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

const path = require('path');

const { getScopes } = require('../atomInterface');

let flow;
const lazyFlow = () => {
if (!flow) {
Expand All @@ -23,8 +21,6 @@ const isCurrentScopeEmbeddedScope = editor => EMBEDDED_SCOPES.includes(getCurren

const isCurrentScopeStyleLintScope = editor => STYLELINT_SCOPES.includes(getCurrentScope(editor));

const isInScope = editor => getScopes().includes(getCurrentScope(editor));

const getCurrentFilePath = editor => editor.buffer.file ? editor.buffer.file.getPath() : undefined;

const getCurrentDir = editor => lazyFlow()(getCurrentFilePath, maybeFilePath => typeof maybeFilePath === 'string' ? path.dirname(maybeFilePath) : undefined)(editor);
Expand All @@ -33,7 +29,6 @@ module.exports = {
getBufferRange,
isCurrentScopeEmbeddedScope,
isCurrentScopeStyleLintScope,
isInScope,
getCurrentScope,
getCurrentFilePath,
getCurrentDir
Expand Down
26 changes: 0 additions & 26 deletions dist/formatOnSave/isFilePathPrettierIgnored.js

This file was deleted.

9 changes: 3 additions & 6 deletions dist/formatOnSave/shouldFormatOnSave.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

const _ = require('lodash/fp');
const { getPrettierInstance, someGlobsMatchFilePath } = require('../helpers');
const { getCurrentFilePath, isInScope } = require('../editorInterface');
const { getPrettierInstance, someGlobsMatchFilePath, isFileFormattable } = require('../helpers');
const { getCurrentFilePath } = require('../editorInterface');
const {
getExcludedGlobs,
getWhitelistedGlobs,
Expand All @@ -12,7 +12,6 @@ const {
shouldRespectEslintignore
} = require('../atomInterface');
const isFilePathEslintIgnored = require('./isFilePathEslintIgnored');
const isFilePathPrettierIgnored = require('./isFilePathPrettierIgnored');
const isPrettierInPackageJson = require('./isPrettierInPackageJson');

const hasFilePath = editor => !!getCurrentFilePath(editor);
Expand All @@ -26,8 +25,6 @@ const isFilePathWhitelisted = _.flow(getCurrentFilePath, filePath => someGlobsMa

const isEslintIgnored = _.flow(getCurrentFilePath, isFilePathEslintIgnored);

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

const isResolveConfigDefined = (editor
// $FlowFixMe
) => !!getPrettierInstance(editor).resolveConfig.sync;
Expand All @@ -38,6 +35,6 @@ const isResolveConfigSuccessful = (editor

const isPrettierConfigPresent = _.overEvery([isResolveConfigDefined, isResolveConfigSuccessful]);

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

module.exports = shouldFormatOnSave;
4 changes: 3 additions & 1 deletion dist/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
const getPrettierInstance = require('./getPrettierInstance');
const general = require('./general');
const atomRelated = require('./atomRelated');
const isFileFormattable = require('./isFileFormattable');

module.exports = (0, _extends3.default)({}, general, atomRelated, {
getPrettierInstance
getPrettierInstance,
isFileFormattable
});
11 changes: 11 additions & 0 deletions dist/helpers/isFileFormattable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

const _ = require('lodash/fp');
const getPrettierInstance = require('./getPrettierInstance');
const { getCurrentFilePath } = require('../editorInterface');

const isFileFormattable = editor => !!editor && !!getCurrentFilePath(editor) && _.flow(getCurrentFilePath,
// $FlowFixMe: getFileInfo is not yet added to flow typed
filePath => getPrettierInstance().getFileInfo(filePath, {}, '.prettierignore'), fileInfo => fileInfo.exists && !fileInfo.ignored && !!fileInfo.inferredParser)(editor);

module.exports = isFileFormattable;
5 changes: 2 additions & 3 deletions dist/statusTile/updateStatusTileScope.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
'use strict';

const { getCurrentScope } = require('../editorInterface');
const { getScopes } = require('../atomInterface');
const { isFileFormattable } = require('../helpers');

const updateStatusTileScope = (element, editor) => {
// The editor can be undefined if there is no active editor (e.g. closed all tabs).
// eslint-disable-next-line no-param-reassign
element.dataset.prettierMatchScope = editor && getScopes().includes(getCurrentScope(editor)) ? 'true' : 'false';
element.dataset.prettierCanFormatFile = editor && isFileFormattable(editor) ? 'true' : 'false';
};

module.exports = updateStatusTileScope;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"ignore": "^3.3.7",
"lodash": "^4.17.5",
"loophole": "^1.1.0",
"prettier": "1.12.1",
"prettier": "github:kachkaev/prettier#file-info-dist",
"prettier-eslint": "^8.8.1",
"prettier-stylelint": "^0.4.2",
"read-pkg-up": "^3.0.0",
Expand Down
3 changes: 0 additions & 3 deletions src/atomInterface/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ const getAtomVersion = () => atom.getVersion();

const getPrettierAtomConfig = () => atom.config.get('prettier-atom');

const getScopes = () => getConfigOption('scopes');

const getWhitelistedGlobs = () => getConfigOption('formatOnSaveOptions.whitelistedGlobs');

const getExcludedGlobs = () => getConfigOption('formatOnSaveOptions.excludedGlobs');
Expand Down Expand Up @@ -77,7 +75,6 @@ module.exports = {
getAtomVersion,
getPrettierAtomConfig,
getPrettierEslintOptions,
getScopes,
getWhitelistedGlobs,
getExcludedGlobs,
isDisabledIfNotInPackageJson,
Expand Down
30 changes: 0 additions & 30 deletions src/config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,6 @@
"default": false,
"order": 2
},
"scopes": {
"title": "Only format if a Prettier config is found",
"description":
"Select which files should be formatted.<br>Use the command `Editor: Log Cursor Scope` to determine the scopes for a file.",
"type": "array",
"default": [
"source.js",
"source.jsx",
"source.js.jsx",
"source.babel",
"source.js-semantic",
"text.html.basic",
"text.html.vue",
"source.css",
"source.less",
"source.css.less",
"source.scss",
"source.css.scss",
"source.css.postcss",
"source.json",
"source.graphql",
"source.md",
"source.gfm",
"text.md"
],
"items": {
"type": "string"
},
"order": 3
},
"formatOnSaveOptions": {
"title": "Format on Save",
"order": 4,
Expand Down
6 changes: 0 additions & 6 deletions src/editorInterface/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// @flow

const path = require('path');

const { getScopes } = require('../atomInterface');

let flow;
const lazyFlow = () => {
if (!flow) {
Expand Down Expand Up @@ -31,8 +28,6 @@ const isCurrentScopeEmbeddedScope = (editor: TextEditor) => EMBEDDED_SCOPES.incl
const isCurrentScopeStyleLintScope = (editor: TextEditor) =>
STYLELINT_SCOPES.includes(getCurrentScope(editor));

const isInScope = (editor: TextEditor) => getScopes().includes(getCurrentScope(editor));

const getCurrentFilePath: (editor: TextEditor) => ?FilePath = editor =>
editor.buffer.file ? editor.buffer.file.getPath() : undefined;

Expand All @@ -46,7 +41,6 @@ module.exports = {
getBufferRange,
isCurrentScopeEmbeddedScope,
isCurrentScopeStyleLintScope,
isInScope,
getCurrentScope,
getCurrentFilePath,
getCurrentDir,
Expand Down
34 changes: 0 additions & 34 deletions src/formatOnSave/isFilePathPrettierIgnored.js

This file was deleted.

26 changes: 0 additions & 26 deletions src/formatOnSave/isFilePathPrettierIgnored.test.js

This file was deleted.

13 changes: 3 additions & 10 deletions src/formatOnSave/shouldFormatOnSave.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @flow
const _ = require('lodash/fp');
const { getPrettierInstance, someGlobsMatchFilePath } = require('../helpers');
const { getCurrentFilePath, isInScope } = require('../editorInterface');
const { getPrettierInstance, someGlobsMatchFilePath, isFileFormattable } = require('../helpers');
const { getCurrentFilePath } = require('../editorInterface');
const {
getExcludedGlobs,
getWhitelistedGlobs,
Expand All @@ -11,7 +11,6 @@ const {
shouldRespectEslintignore,
} = require('../atomInterface');
const isFilePathEslintIgnored = require('./isFilePathEslintIgnored');
const isFilePathPrettierIgnored = require('./isFilePathPrettierIgnored');
const isPrettierInPackageJson = require('./isPrettierInPackageJson');

const hasFilePath = (editor: TextEditor) => !!getCurrentFilePath(editor);
Expand All @@ -31,11 +30,6 @@ const isFilePathWhitelisted: (editor: TextEditor) => boolean = _.flow(

const isEslintIgnored: (editor: TextEditor) => boolean = _.flow(getCurrentFilePath, isFilePathEslintIgnored);

const isFilePathNotPrettierIgnored: (editor: TextEditor) => boolean = _.flow(
getCurrentFilePath,
_.negate(isFilePathPrettierIgnored),
);

const isResolveConfigDefined = (editor: TextEditor): boolean =>
// $FlowFixMe
!!getPrettierInstance(editor).resolveConfig.sync;
Expand All @@ -52,15 +46,14 @@ const isPrettierConfigPresent: TextEditor => boolean = _.overEvery([
const shouldFormatOnSave: (editor: TextEditor) => boolean = _.overEvery([
isFormatOnSaveEnabled,
hasFilePath,
isInScope,
_.overSome([
isFilePathWhitelisted,
_.overEvery([noWhitelistGlobsPresent, filePathDoesNotMatchBlacklistGlobs]),
]),
_.overSome([_.negate(shouldRespectEslintignore), _.negate(isEslintIgnored)]),
isFilePathNotPrettierIgnored,
_.overSome([_.negate(isDisabledIfNotInPackageJson), isPrettierInPackageJson]),
_.overSome([_.negate(isDisabledIfNoConfigFile), isPrettierConfigPresent]),
isFileFormattable,
]);

module.exports = shouldFormatOnSave;
Loading

0 comments on commit ad88808

Please sign in to comment.