From f4520ac1a4ae98223f42813e41d475bf7142e421 Mon Sep 17 00:00:00 2001 From: Rob Wise Date: Wed, 3 May 2017 02:29:28 -0400 Subject: [PATCH] feat(eslint-fallbacks): fallback to user's prettier settings if cannot be inferred from eslint confi Previously, if we couldn't infer a prettier setting from a user's eslint config, we fell back to default prettier settings. Now, we take advantage of a new prettier-eslint feature that let's us provide fallback options to use instead of the prettier defaults. This means that if you don't have a semicolons rule in your eslint config, we will use the semicolon rule from your prettier-atom settings. --- dist/config-schema.json | 2 +- dist/executePrettier.js | 6 ++++-- src/config-schema.json | 2 +- src/executePrettier.js | 4 +++- src/executePrettier.test.js | 20 ++++++++------------ 5 files changed, 17 insertions(+), 17 deletions(-) diff --git a/dist/config-schema.json b/dist/config-schema.json index f09390c1..f68b545b 100644 --- a/dist/config-schema.json +++ b/dist/config-schema.json @@ -8,7 +8,7 @@ }, "useEslint": { "title": "ESlint Integration", - "description": "Use [prettier-eslint](https://github.com/prettier/prettier-eslint). If you enable *Format on Save*, we recommend disabling ESlint's auto-fix to prevent fixing your code twice.

**Note:** This will override any options you choose in the *Prettier Options* section.", + "description": "Use [prettier-eslint](https://github.com/prettier/prettier-eslint) to infer your prettier settings based on your eslint config. If we cannot infer a prettier setting from your eslint config (or if there is no eslint config in the current project), we will fallback to using your settings in the *Prettier Options* section.

**Note:** If you enable *Format on Save*, we recommend disabling ESlint's auto-fix to prevent fixing your code twice.", "type": "boolean", "default": false, "order": 2 diff --git a/dist/executePrettier.js b/dist/executePrettier.js index 71911417..0909233b 100644 --- a/dist/executePrettier.js +++ b/dist/executePrettier.js @@ -33,17 +33,19 @@ var handleError = function handleError(error) { var executePrettier = function executePrettier(editor, text) { try { + var prettierOptions = getPrettierOptions(editor); + if (shouldUseEslint()) { return allowUnsafeNewFunction(function () { return prettierEslint(_extends({}, getPrettierEslintOptions(), { text: text, - filePath: getCurrentFilePath(editor) + filePath: getCurrentFilePath(editor), + fallbackPrettierOptions: prettierOptions })); }); } var prettier = getPrettier(getCurrentFilePath(editor)); - var prettierOptions = getPrettierOptions(editor); return prettier.format(text, prettierOptions); } catch (error) { diff --git a/src/config-schema.json b/src/config-schema.json index f09390c1..f68b545b 100644 --- a/src/config-schema.json +++ b/src/config-schema.json @@ -8,7 +8,7 @@ }, "useEslint": { "title": "ESlint Integration", - "description": "Use [prettier-eslint](https://github.com/prettier/prettier-eslint). If you enable *Format on Save*, we recommend disabling ESlint's auto-fix to prevent fixing your code twice.

**Note:** This will override any options you choose in the *Prettier Options* section.", + "description": "Use [prettier-eslint](https://github.com/prettier/prettier-eslint) to infer your prettier settings based on your eslint config. If we cannot infer a prettier setting from your eslint config (or if there is no eslint config in the current project), we will fallback to using your settings in the *Prettier Options* section.

**Note:** If you enable *Format on Save*, we recommend disabling ESlint's auto-fix to prevent fixing your code twice.", "type": "boolean", "default": false, "order": 2 diff --git a/src/executePrettier.js b/src/executePrettier.js index 98681b48..a559bc37 100644 --- a/src/executePrettier.js +++ b/src/executePrettier.js @@ -29,18 +29,20 @@ const handleError = (error) => { const executePrettier = (editor, text) => { try { + const prettierOptions = getPrettierOptions(editor); + if (shouldUseEslint()) { return allowUnsafeNewFunction(() => prettierEslint({ ...getPrettierEslintOptions(), text, filePath: getCurrentFilePath(editor), + fallbackPrettierOptions: prettierOptions, }), ); } const prettier = getPrettier(getCurrentFilePath(editor)); - const prettierOptions = getPrettierOptions(editor); return prettier.format(text, prettierOptions); } catch (error) { diff --git a/src/executePrettier.test.js b/src/executePrettier.test.js index 7616308e..e38e0494 100644 --- a/src/executePrettier.test.js +++ b/src/executePrettier.test.js @@ -54,28 +54,24 @@ describe('executePrettierOnBufferRange()', () => { test('transforms the given buffer range using prettier-eslint if config enables it', () => { // $FlowFixMe helpers.shouldUseEslint.mockImplementation(() => true); - // $FlowFixMe - helpers.getCurrentFilePath.mockImplementation(() => 'foo.js'); - - executePrettierOnBufferRange(editor, bufferRangeFixture); - - expect(prettierEslint).toHaveBeenCalledWith({ filePath: 'foo.js', text: 'untransformed text' }); - }); - - test('passes prettierLast option to prettier-eslint', () => { // $FlowFixMe helpers.shouldUseEslint.mockImplementation(() => true); // $FlowFixMe - helpers.getCurrentFilePath.mockImplementation(() => 'foo.js'); - // $FlowFixMe helpers.getPrettierEslintOptions.mockImplementation(() => ({ prettierLast: true })); + // $FlowFixMe + helpers.getPrettierOptions.mockImplementation(() => ({ semi: true })); + // $FlowFixMe + helpers.getCurrentFilePath.mockImplementation(() => 'foo.js'); executePrettierOnBufferRange(editor, bufferRangeFixture); expect(prettierEslint).toHaveBeenCalledWith({ - prettierLast: true, filePath: 'foo.js', text: 'untransformed text', + prettierLast: true, + fallbackPrettierOptions: { + semi: true, + }, }); });