From ad47faea64c70547429a452f89c89b38453f6e45 Mon Sep 17 00:00:00 2001 From: Roman Gritsay Date: Fri, 5 May 2017 03:52:46 +0300 Subject: [PATCH] feat(options): add editorconfig option to package settings Add editorconfig option which allows user to enable/disable editorconfig integration --- dist/config-schema.json | 13 +++- dist/options.js | 5 +- src/__snapshots__/options.test.js.snap | 16 ++--- src/config-schema.json | 13 +++- src/options.js | 8 ++- src/options.test.js | 77 +++++++++++++++------- src/warnAboutLinterEslintFixOnSave.test.js | 10 +-- 7 files changed, 99 insertions(+), 43 deletions(-) diff --git a/dist/config-schema.json b/dist/config-schema.json index f68b545b..9ecd29ca 100644 --- a/dist/config-schema.json +++ b/dist/config-schema.json @@ -13,9 +13,16 @@ "default": false, "order": 2 }, + "useEditorConfig": { + "title": "EditorConfig Integration", + "description": "Use [EditorConfig](http://editorconfig.org/) configuration to load project-based settings for prettier formatting
Next options will be overriden: **Use tabs**, **Tab width**, **Print width**", + "type": "boolean", + "default": true, + "order": 3 + }, "formatOnSaveOptions": { "title": "Format on Save", - "order": 3, + "order": 4, "type": "object", "properties": { "enabled": { @@ -55,14 +62,14 @@ "description": "A list of file globs to exclude from formatting on save (takes precedence over scopes). Use commas to seperate each glob.", "type": "array", "default": [], - "order": 4 + "order": 5 }, "whitelistedGlobs": { "title": "Include (list of globs)", "description": "A list of file globs to always format on save (takes precedence over scopes and excluded globs). Use commas to seperate each glob. NOTE: If there are globs in this list, files not matching the globs will not be formatted on save regardless of other options.", "type": "array", "default": [], - "order": 5 + "order": 6 } } }, diff --git a/dist/options.js b/dist/options.js index a7bc1e7d..8376b63b 100644 --- a/dist/options.js +++ b/dist/options.js @@ -58,7 +58,10 @@ var getPrettierOptions = function getPrettierOptions(editor) { useTabs: getPrettierOption('useTabs'), jsxBracketSameLine: getPrettierOption('jsxBracketSameLine') }; - return filePath ? _extends({}, optionsFromSettings, getEditorConfigOptions(filePath)) : optionsFromSettings; + if (!filePath || !getConfigOption('useEditorConfig')) { + return optionsFromSettings; + } + return _extends({}, optionsFromSettings, getEditorConfigOptions(filePath)); }; var getPrettierEslintOptions = function getPrettierEslintOptions() { diff --git a/src/__snapshots__/options.test.js.snap b/src/__snapshots__/options.test.js.snap index b35b5d2a..7cca5fa5 100644 --- a/src/__snapshots__/options.test.js.snap +++ b/src/__snapshots__/options.test.js.snap @@ -6,30 +6,30 @@ Object { } `; -exports[`getPrettierOptions returns all prettier options 1`] = ` +exports[`getPrettierOptions editorconfig integration uses editorconfig options when enabled in config 1`] = ` Object { "bracketSpacing": true, "jsxBracketSameLine": true, "parser": "flow", - "printWidth": 80, + "printWidth": 100, "semi": true, "singleQuote": true, - "tabWidth": 2, + "tabWidth": 4, "trailingComma": true, - "useTabs": true, + "useTabs": false, } `; -exports[`getPrettierOptions uses the editorconfig options if provided 1`] = ` +exports[`getPrettierOptions returns all prettier options 1`] = ` Object { "bracketSpacing": true, "jsxBracketSameLine": true, "parser": "flow", - "printWidth": 100, + "printWidth": 80, "semi": true, "singleQuote": true, - "tabWidth": 4, + "tabWidth": 2, "trailingComma": true, - "useTabs": false, + "useTabs": true, } `; diff --git a/src/config-schema.json b/src/config-schema.json index f68b545b..9ecd29ca 100644 --- a/src/config-schema.json +++ b/src/config-schema.json @@ -13,9 +13,16 @@ "default": false, "order": 2 }, + "useEditorConfig": { + "title": "EditorConfig Integration", + "description": "Use [EditorConfig](http://editorconfig.org/) configuration to load project-based settings for prettier formatting
Next options will be overriden: **Use tabs**, **Tab width**, **Print width**", + "type": "boolean", + "default": true, + "order": 3 + }, "formatOnSaveOptions": { "title": "Format on Save", - "order": 3, + "order": 4, "type": "object", "properties": { "enabled": { @@ -55,14 +62,14 @@ "description": "A list of file globs to exclude from formatting on save (takes precedence over scopes). Use commas to seperate each glob.", "type": "array", "default": [], - "order": 4 + "order": 5 }, "whitelistedGlobs": { "title": "Include (list of globs)", "description": "A list of file globs to always format on save (takes precedence over scopes and excluded globs). Use commas to seperate each glob. NOTE: If there are globs in this list, files not matching the globs will not be formatted on save regardless of other options.", "type": "array", "default": [], - "order": 5 + "order": 6 } } }, diff --git a/src/options.js b/src/options.js index 261ad880..6a99a30c 100644 --- a/src/options.js +++ b/src/options.js @@ -44,7 +44,13 @@ const getPrettierOptions = (editor: TextEditor) => { useTabs: getPrettierOption('useTabs'), jsxBracketSameLine: getPrettierOption('jsxBracketSameLine'), }; - return filePath ? { ...optionsFromSettings, ...getEditorConfigOptions(filePath) } : optionsFromSettings; + if (!filePath || !getConfigOption('useEditorConfig')) { + return optionsFromSettings; + } + return { + ...optionsFromSettings, + ...getEditorConfigOptions(filePath), + }; }; const getPrettierEslintOptions = () => ({ diff --git a/src/options.test.js b/src/options.test.js index 22110f39..c3d59866 100644 --- a/src/options.test.js +++ b/src/options.test.js @@ -124,29 +124,62 @@ describe('getPrettierOptions', () => { expect(actual).toEqual(expected); }); - test('uses the editorconfig options if provided', () => { - const mockGet = option => - ({ - 'prettier-atom.prettierOptions.printWidth': 80, - 'prettier-atom.prettierOptions.tabWidth': 2, - 'prettier-atom.prettierOptions.parser': 'flow', - 'prettier-atom.prettierOptions.singleQuote': true, - 'prettier-atom.prettierOptions.trailingComma': true, - 'prettier-atom.prettierOptions.bracketSpacing': true, - 'prettier-atom.prettierOptions.semi': true, - 'prettier-atom.prettierOptions.useTabs': true, - 'prettier-atom.prettierOptions.jsxBracketSameLine': true, - }[option]); - atom = { config: { get: mockGet } }; - editorconfig.parseSync.mockImplementation(() => ({ - tab_width: 4, - max_line_length: 100, - indent_style: 'space', - })); - const editor = textEditor(); + describe('editorconfig integration', () => { + test('uses editorconfig options when enabled in config', () => { + const mockGet = option => + ({ + 'prettier-atom.useEditorConfig': true, + 'prettier-atom.prettierOptions.printWidth': 80, + 'prettier-atom.prettierOptions.tabWidth': 2, + 'prettier-atom.prettierOptions.parser': 'flow', + 'prettier-atom.prettierOptions.singleQuote': true, + 'prettier-atom.prettierOptions.trailingComma': true, + 'prettier-atom.prettierOptions.bracketSpacing': true, + 'prettier-atom.prettierOptions.semi': true, + 'prettier-atom.prettierOptions.useTabs': true, + 'prettier-atom.prettierOptions.jsxBracketSameLine': true, + }[option]); + atom = { config: { get: mockGet } }; + editorconfig.parseSync.mockImplementation(() => ({ + tab_width: 4, + max_line_length: 100, + indent_style: 'space', + })); + const editor = textEditor(); - const actual = getPrettierOptions(editor); - expect(actual).toMatchSnapshot(); + const actual = getPrettierOptions(editor); + expect(actual).toMatchSnapshot(); + }); + + test('ignores editorconfig when disabled in config', () => { + const mockGet = option => + ({ + 'prettier-atom.useEditorConfig': false, + }[option]); + atom = { config: { get: mockGet } }; + const editor = textEditor(); + + getPrettierOptions(editor); + expect(editorconfig.parseSync).not.toHaveBeenCalled(); + }); + + test('ignores editorconfig for unsaved files', () => { + const mockGet = option => + ({ + 'prettier-atom.useEditorConfig': true, + }[option]); + atom = { config: { get: mockGet } }; + const editor = textEditor({ + buffer: { + file: { + path: '', + }, + }, + }); + + getPrettierOptions(editor); + expect(editorconfig.parseSync).not.toHaveBeenCalled(); + }); }); }); diff --git a/src/warnAboutLinterEslintFixOnSave.test.js b/src/warnAboutLinterEslintFixOnSave.test.js index 2b589744..fc21bab3 100644 --- a/src/warnAboutLinterEslintFixOnSave.test.js +++ b/src/warnAboutLinterEslintFixOnSave.test.js @@ -1,5 +1,5 @@ // @flow -const helpers = require('./options'); +const options = require('./options'); const warnAboutLinterEslintFixOnSave = require('./warnAboutLinterEslintFixOnSave'); jest.mock('./options'); @@ -7,9 +7,9 @@ jest.mock('./options'); beforeEach(() => { atom = { notifications: { addWarning: jest.fn() } }; // $FlowFixMe - helpers.isLinterEslintAutofixEnabled.mockImplementation(() => true); + options.isLinterEslintAutofixEnabled.mockImplementation(() => true); // $FlowFixMe - helpers.shouldUseEslint.mockImplementation(() => true); + options.shouldUseEslint.mockImplementation(() => true); }); test('it warns about not having eslint autofix on', () => { @@ -21,7 +21,7 @@ test('it warns about not having eslint autofix on', () => { test('it does not warn if eslint autofix is disabled', () => { // $FlowFixMe - helpers.isLinterEslintAutofixEnabled.mockImplementation(() => false); + options.isLinterEslintAutofixEnabled.mockImplementation(() => false); warnAboutLinterEslintFixOnSave(); @@ -31,7 +31,7 @@ test('it does not warn if eslint autofix is disabled', () => { test('it does not warn if eslint integration is disabled', () => { // $FlowFixMe - helpers.shouldUseEslint.mockImplementation(() => false); + options.shouldUseEslint.mockImplementation(() => false); warnAboutLinterEslintFixOnSave();