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

Commit

Permalink
feat(options): add editorconfig option to package settings
Browse files Browse the repository at this point in the history
Add editorconfig option which allows user to enable/disable editorconfig integration
  • Loading branch information
RoM4iK committed May 5, 2017
1 parent ee0ed1b commit ad47fae
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 43 deletions.
13 changes: 10 additions & 3 deletions dist/config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 <br />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": {
Expand Down Expand Up @@ -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
}
}
},
Expand Down
5 changes: 4 additions & 1 deletion dist/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
16 changes: 8 additions & 8 deletions src/__snapshots__/options.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
`;
13 changes: 10 additions & 3 deletions src/config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 <br />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": {
Expand Down Expand Up @@ -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
}
}
},
Expand Down
8 changes: 7 additions & 1 deletion src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => ({
Expand Down
77 changes: 55 additions & 22 deletions src/options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
});

Expand Down
10 changes: 5 additions & 5 deletions src/warnAboutLinterEslintFixOnSave.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// @flow
const helpers = require('./options');
const options = require('./options');
const warnAboutLinterEslintFixOnSave = require('./warnAboutLinterEslintFixOnSave');

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', () => {
Expand All @@ -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();

Expand All @@ -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();

Expand Down

0 comments on commit ad47fae

Please sign in to comment.