Skip to content

Commit

Permalink
refactor(buildPrettierOptions.js): Exclusively use local config or pl…
Browse files Browse the repository at this point in the history
…ugin settings

Stop the current behavior of merging a local prettier config with
  those found in the plugin settings.  The reason being a project
  should be formatted according to its own rules, and per Prettier
  convention omitted rules should fallback to prettier defaults.

When a prettier config is found, use its integration with editorconfig
  instead of the plugin's own implementation.

BREAKING CHANGE: The formatting configuration is now built according to different rules

fix prettier#370
  • Loading branch information
Philip Olson committed Feb 8, 2018
1 parent 23dbe72 commit a841cda
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 42 deletions.
9 changes: 9 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,15 @@
"code",
"test"
]
},
{
"login": "olsonpm",
"name": "olsonpm",
"avatar_url": "https://avatars2.githubusercontent.com/u/5957709?v=4",
"profile": "https://github.com/olsonpm",
"contributions": [
"code"
]
}
]
}
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,13 @@ This repository was created by [James Long][james-long] to go along with his Pre
Thanks goes to these people ([emoji key][emojis]):

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->

<!-- prettier-ignore -->
| [<img src="https://avatars.githubusercontent.com/u/17031?v=3" width="100px;"/><br /><sub><b>James Long</b></sub>](http://jlongster.com)<br />[💬](#question-jlongster "Answering Questions") [💻](https://github.com/prettier/prettier-atom/commits?author=jlongster "Code") [📖](https://github.com/prettier/prettier-atom/commits?author=jlongster "Documentation") [🔌](#plugin-jlongster "Plugin/utility libraries") [👀](#review-jlongster "Reviewed Pull Requests") | [<img src="https://avatars.githubusercontent.com/u/6173488?v=3" width="100px;"/><br /><sub><b>Rob Wise</b></sub>](https://robwise.github.io)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=robwise "Code") [📖](https://github.com/prettier/prettier-atom/commits?author=robwise "Documentation") [💬](#question-robwise "Answering Questions") [💡](#example-robwise "Examples") [👀](#review-robwise "Reviewed Pull Requests") [⚠️](https://github.com/prettier/prettier-atom/commits?author=robwise "Tests") | [<img src="https://avatars.githubusercontent.com/u/1500684?v=3" width="100px;"/><br /><sub><b>Kent C. Dodds</b></sub>](https://kentcdodds.com)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=kentcdodds "Code") [📖](https://github.com/prettier/prettier-atom/commits?author=kentcdodds "Documentation") [🚇](#infra-kentcdodds "Infrastructure (Hosting, Build-Tools, etc)") | [<img src="https://avatars.githubusercontent.com/u/1144075?v=3" width="100px;"/><br /><sub><b>Luca Barone</b></sub>](https://github.com/cloud-walker)<br /> | [<img src="https://avatars.githubusercontent.com/u/4514159?v=3" width="100px;"/><br /><sub><b>Arnar Þór Sveinsson</b></sub>](https://github.com/arnarthor)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=arnarthor "Code") | [<img src="https://avatars.githubusercontent.com/u/131916?v=3" width="100px;"/><br /><sub><b>Adam Miskiewicz</b></sub>](http://www.adammiskiewicz.com/)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=skevy "Code") | [<img src="https://avatars.githubusercontent.com/u/2685242?v=3" width="100px;"/><br /><sub><b>Ori Livni</b></sub>](http://www.orilivni.com)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=oriSomething "Code") |
| :---: | :---: | :---: | :---: | :---: | :---: | :---: |
| [<img src="https://avatars.githubusercontent.com/u/6182852?v=3" width="100px;"/><br /><sub><b>Leon Chen</b></sub>](https://transcranial.github.io)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=transcranial "Code") | [<img src="https://avatars.githubusercontent.com/u/197597?v=3" width="100px;"/><br /><sub><b>Christopher Chedeau</b></sub>](http://blog.vjeux.com/)<br />[💬](#question-vjeux "Answering Questions") [💻](https://github.com/prettier/prettier-atom/commits?author=vjeux "Code") [🔌](#plugin-vjeux "Plugin/utility libraries") | [<img src="https://avatars.githubusercontent.com/u/646693?v=3" width="100px;"/><br /><sub><b>Christoph Geschwind</b></sub>](http://christoph-geschwind.de)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=1st8 "Code") | [<img src="https://avatars.githubusercontent.com/u/35026?v=3" width="100px;"/><br /><sub><b>Andrew Hutchings</b></sub>](https://andrewhutchings.com)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=ahutchings "Code") | [<img src="https://avatars.githubusercontent.com/u/875591?v=3" width="100px;"/><br /><sub><b>David Schnurr</b></sub>](http://davidschnurr.com)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=schnerd "Code") | [<img src="https://avatars.githubusercontent.com/u/484801?v=3" width="100px;"/><br /><sub><b>Ryan Cole</b></sub>](http://rycole.com/)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=ryancole "Code") | [<img src="https://avatars0.githubusercontent.com/u/11488612?v=3" width="100px;"/><br /><sub><b>Dara Hak</b></sub>](https://github.com/darahak)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=darahak "Code") [📖](https://github.com/prettier/prettier-atom/commits?author=darahak "Documentation") |
| [<img src="https://avatars3.githubusercontent.com/u/487068?v=3" width="100px;"/><br /><sub><b>Stephen John Sorensen</b></sub>](http://www.stephenjohnsorensen.com/)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=spudly "Code") | [<img src="https://avatars2.githubusercontent.com/u/13285808?v=3" width="100px;"/><br /><sub><b>Lukas Geiger</b></sub>](https://github.com/lgeiger)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=lgeiger "Code") [⚠️](https://github.com/prettier/prettier-atom/commits?author=lgeiger "Tests") | [<img src="https://avatars2.githubusercontent.com/u/1517854?v=3" width="100px;"/><br /><sub><b>Viktor Charypar</b></sub>](https://github.com/charypar)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=charypar "Code") [⚠️](https://github.com/prettier/prettier-atom/commits?author=charypar "Tests") | [<img src="https://avatars0.githubusercontent.com/u/1007436?v=3" width="100px;"/><br /><sub><b>Mats Högberg</b></sub>](http://mats.hgbrg.se)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=mhgbrg "Code") | [<img src="https://avatars0.githubusercontent.com/u/2602767?v=3" width="100px;"/><br /><sub><b>Roman</b></sub>](https://github.com/RoM4iK)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=RoM4iK "Code") | [<img src="https://avatars2.githubusercontent.com/u/1468518?v=3" width="100px;"/><br /><sub><b>vaibhav</b></sub>](https://vaibhavchatarkar.com)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=da-vaibhav "Code") | [<img src="https://avatars1.githubusercontent.com/u/1935696?v=3" width="100px;"/><br /><sub><b>Karl Horky</b></sub>](https://work.karlhorky.com)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=karlhorky "Code") |
| [<img src="https://avatars3.githubusercontent.com/u/810438?v=3" width="100px;"/><br /><sub><b>Dan Abramov</b></sub>](http://twitter.com/dan_abramov)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=gaearon "Code") | [<img src="https://avatars3.githubusercontent.com/u/1227109?v=3" width="100px;"/><br /><sub><b>Murphy Randle</b></sub>](https://sploding.rocks)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=splodingsocks "Code") [🐛](https://github.com/prettier/prettier-atom/issues?q=author%3Asplodingsocks "Bug reports") | [<img src="https://avatars3.githubusercontent.com/u/8517072?v=3" width="100px;"/><br /><sub><b>Matthieu Lemoine</b></sub>](https://matthieulemoine.com)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=MatthieuLemoine "Code") | [<img src="https://avatars1.githubusercontent.com/u/37242?v=4" width="100px;"/><br /><sub><b>Ron Green</b></sub>](https://github.com/rgreenjr)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=rgreenjr "Code") | [<img src="https://avatars2.githubusercontent.com/u/1745854?v=4" width="100px;"/><br /><sub><b>Harold Treen</b></sub>](https://haroldtreen.com)<br />[🐛](https://github.com/prettier/prettier-atom/issues?q=author%3Aharoldtreen "Bug reports") [💻](https://github.com/prettier/prettier-atom/commits?author=haroldtreen "Code") | [<img src="https://avatars1.githubusercontent.com/u/3447641?v=4" width="100px;"/><br /><sub><b>Nikita Mashukov</b></sub>](https://github.com/ferdibiflator)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=ferdibiflator "Code") | [<img src="https://avatars0.githubusercontent.com/u/8203211?v=4" width="100px;"/><br /><sub><b>Sam Horton</b></sub>](https://github.com/SavePointSam)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=SavePointSam "Code") [⚠️](https://github.com/prettier/prettier-atom/commits?author=SavePointSam "Tests") |

| [<img src="https://avatars2.githubusercontent.com/u/5957709?v=4" width="100px;"/><br /><sub><b>olsonpm</b></sub>](https://github.com/olsonpm)<br />[💻](https://github.com/prettier/prettier-atom/commits?author=olsonpm "Code") |
<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the [all-contributors][all-contributors] specification. Contributions of any kind are welcome!
Expand Down
12 changes: 9 additions & 3 deletions dist/executePrettier/buildPrettierOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ var isAppropriateToBuildEditorConfigOptions = _.overEvery([isDefined, shouldUseE
var buildEditorConfigOptionsIfAppropriate = _.flow(getCurrentFilePath, _.cond([[isAppropriateToBuildEditorConfigOptions, buildEditorConfigOptions]]));

var getPrettierConfigOptions = _.cond([[_.flow(getCurrentFilePath, isDefined), function (editor) {
return isDefined(getPrettierInstance(editor).resolveConfig.sync) ? getPrettierInstance(editor).resolveConfig.sync(getCurrentFilePath(editor)) : null;
return isDefined(getPrettierInstance(editor).resolveConfig.sync) ? getPrettierInstance(editor).resolveConfig.sync(getCurrentFilePath(editor), {
editorconfig: shouldUseEditorConfig()
}) : null;
}]]);

var buildPrettierOptions = function buildPrettierOptions(editor) {
var getOptionsFromSettings = function getOptionsFromSettings(editor) {
var optionsFromSettings = getPrettierOptions();

if (optionsFromSettings.tabWidth === 'auto') {
Expand Down Expand Up @@ -69,7 +71,11 @@ var buildPrettierOptions = function buildPrettierOptions(editor) {
optionsFromSettings.parser = 'vue';
}

return (0, _extends3.default)({}, optionsFromSettings, buildEditorConfigOptionsIfAppropriate(editor), getPrettierConfigOptions(editor));
return (0, _extends3.default)({}, optionsFromSettings, buildEditorConfigOptionsIfAppropriate(editor));
};

var buildPrettierOptions = function buildPrettierOptions(editor) {
return getPrettierConfigOptions(editor) || getOptionsFromSettings(editor);
};

module.exports = buildPrettierOptions;
10 changes: 7 additions & 3 deletions src/executePrettier/buildPrettierOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ const getPrettierConfigOptions: (editor: TextEditor) => ?{} = _.cond([
_.flow(getCurrentFilePath, isDefined),
editor =>
isDefined(getPrettierInstance(editor).resolveConfig.sync)
? getPrettierInstance(editor).resolveConfig.sync(getCurrentFilePath(editor))
? getPrettierInstance(editor).resolveConfig.sync(getCurrentFilePath(editor), {
editorconfig: shouldUseEditorConfig(),
})
: null,
],
]);

const buildPrettierOptions = (editor: TextEditor) => {
const getOptionsFromSettings = (editor: TextEditor) => {
const optionsFromSettings = getPrettierOptions();

if (optionsFromSettings.tabWidth === 'auto') {
Expand Down Expand Up @@ -70,8 +72,10 @@ const buildPrettierOptions = (editor: TextEditor) => {
return {
...optionsFromSettings,
...buildEditorConfigOptionsIfAppropriate(editor),
...getPrettierConfigOptions(editor),
};
};

const buildPrettierOptions = (editor: TextEditor) =>
getPrettierConfigOptions(editor) || getOptionsFromSettings(editor);

module.exports = buildPrettierOptions;
88 changes: 54 additions & 34 deletions src/executePrettier/buildPrettierOptions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,40 +109,60 @@ it('uses vue as the parser if current scope is listed as a Vue SFC scope in sett
expect(actual).toEqual({ parser: 'vue' });
});

it('does not use editorconfig options if that setting is not enabled', () => {
const editor = buildMockEditor();
getPrettierOptions.mockImplementation(() => ({}));
shouldUseEditorConfig.mockImplementation(() => false);

buildPrettierOptions(editor);

expect(buildEditorConfigOptions).not.toHaveBeenCalled();
describe('when a prettier config is found', () => {
it('passes the editorconfig option to prettier', () => {
const editorconfig = true;
const editor = buildMockEditor();
const mockPrettierInstance = { resolveConfig: { sync: jest.fn(() => ({})) } };
getPrettierInstance.mockImplementation(() => mockPrettierInstance);
shouldUseEditorConfig.mockImplementation(() => editorconfig);
getCurrentFilePath.mockImplementation(() => 'parent-dir/foo.js');
buildPrettierOptions(editor);
expect(mockPrettierInstance.resolveConfig.sync.mock.calls[0][1]).toEqual({ editorconfig });
});

it('uses settings from the prettier config', () => {
const editor = buildMockEditor();
const fakePrettierConfigOptions = { tabWidth: 4, printWidth: 100, useTabs: true };
const mockPrettierInstance = { resolveConfig: { sync: jest.fn(() => fakePrettierConfigOptions) } };
getPrettierInstance.mockImplementation(() => mockPrettierInstance);
getCurrentFilePath.mockImplementation(() => 'parent-dir/foo.js');

const actual = buildPrettierOptions(editor);

expect(actual).toEqual(fakePrettierConfigOptions);
});
});

it('overrides values with editorconfig values if editor config is enabled', () => {
const editor = buildMockEditor();
const fakePrettierOptions = { tabWidth: 2, printWidth: 80, useTabs: false };
const fakeEditorConfigOptions = { tabWidth: 4, printWidth: 100, useTabs: true };
getPrettierOptions.mockImplementation(() => fakePrettierOptions);
buildEditorConfigOptions.mockImplementation(() => fakeEditorConfigOptions);
shouldUseEditorConfig.mockImplementation(() => true);
getCurrentFilePath.mockImplementation(() => 'parent-dir/foo.js');

const actual = buildPrettierOptions(editor);

expect(actual).toEqual(fakeEditorConfigOptions);
});

it('overrides values with prettier config values if one exists', () => {
const editor = buildMockEditor();
const fakePrettierOptions = { tabWidth: 2, printWidth: 80, useTabs: false };
const fakePrettierConfigOptions = { tabWidth: 4, printWidth: 100, useTabs: true };
const mockPrettierInstance = { resolveConfig: { sync: jest.fn(() => fakePrettierConfigOptions) } };
getPrettierInstance.mockImplementation(() => mockPrettierInstance);
getCurrentFilePath.mockImplementation(() => 'parent-dir/foo.js');
getPrettierOptions.mockImplementation(() => fakePrettierOptions);

const actual = buildPrettierOptions(editor);

expect(actual).toEqual(fakePrettierConfigOptions);
describe('when a prettier config is not found', () => {
it('uses settings from prettier-atom and Editor Config when enabled', () => {
const editor = buildMockEditor();
const fakePrettierOptions = { singleQuote: true, tabWidth: 2 };
const fakeEditorConfigOptions = { tabWidth: 4 };
const mockPrettierInstance = { resolveConfig: { sync: jest.fn(() => null) } };
getPrettierInstance.mockImplementation(() => mockPrettierInstance);
buildEditorConfigOptions.mockImplementation(() => fakeEditorConfigOptions);
shouldUseEditorConfig.mockImplementation(() => true);
getCurrentFilePath.mockImplementation(() => 'parent-dir/foo.js');
getPrettierOptions.mockImplementation(() => fakePrettierOptions);

const actual = buildPrettierOptions(editor);

expect(actual).toEqual({ ...fakePrettierOptions, ...fakeEditorConfigOptions });
});

it('uses settings from prettier-atom and ignores Editor Config when disabled', () => {
const editor = buildMockEditor();
const fakePrettierOptions = { singleQuote: true, tabWidth: 2 };
const mockPrettierInstance = { resolveConfig: { sync: jest.fn(() => null) } };
getPrettierInstance.mockImplementation(() => mockPrettierInstance);
shouldUseEditorConfig.mockImplementation(() => false);
getCurrentFilePath.mockImplementation(() => 'parent-dir/foo.js');
getPrettierOptions.mockImplementation(() => fakePrettierOptions);

const actual = buildPrettierOptions(editor);

expect(buildEditorConfigOptions).not.toHaveBeenCalled();
expect(actual).toEqual(fakePrettierOptions);
});
});

0 comments on commit a841cda

Please sign in to comment.