Skip to content

Commit

Permalink
Improve Prettier config (#588)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Aug 8, 2021
1 parent ea07509 commit f952701
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 21 deletions.
8 changes: 0 additions & 8 deletions lib/constants.js
Expand Up @@ -106,13 +106,6 @@ const ENGINE_RULES = {
},
};

const PRETTIER_CONFIG_OVERRIDE = {
'eslint-plugin-babel': 'prettier/babel',
'eslint-plugin-flowtype': 'prettier/flowtype',
'eslint-plugin-standard': 'prettier/standard',
'eslint-plugin-vue': 'prettier/vue',
};

const MODULE_NAME = 'xo';

const CONFIG_FILES = [
Expand Down Expand Up @@ -144,7 +137,6 @@ export {
DEFAULT_EXTENSION,
TYPESCRIPT_EXTENSION,
ENGINE_RULES,
PRETTIER_CONFIG_OVERRIDE,
MODULE_NAME,
CONFIG_FILES,
MERGE_OPTIONS_CONCAT,
Expand Down
13 changes: 2 additions & 11 deletions lib/options-manager.js
Expand Up @@ -22,7 +22,6 @@ import {
DEFAULT_EXTENSION,
TYPESCRIPT_EXTENSION,
ENGINE_RULES,
PRETTIER_CONFIG_OVERRIDE,
MODULE_NAME,
CONFIG_FILES,
MERGE_OPTIONS_CONCAT,
Expand Down Expand Up @@ -390,19 +389,11 @@ const buildPrettierConfig = (options, prettierConfig) => config => {
// The prettier plugin uses Prettier to format the code with `--fix`
config.baseConfig.plugins.push('prettier');

// The prettier config overrides ESLint stylistic rules that are handled by Prettier
config.baseConfig.extends.push('prettier');
// The prettier plugin overrides ESLint stylistic rules that are handled by Prettier
config.baseConfig.extends.push('plugin:prettier/recommended');

// The `prettier/prettier` rule reports errors if the code is not formatted in accordance to Prettier
config.baseConfig.rules['prettier/prettier'] = ['error', mergeWithPrettierConfig(options, prettierConfig)];

// If the user has the React, Flowtype, or Standard plugin, add the corresponding Prettier rule overrides
// See https://github.com/prettier/eslint-config-prettier for the list of plugins overrrides
for (const [plugin, prettierConfig] of Object.entries(PRETTIER_CONFIG_OVERRIDE)) {
if (options.cwd && resolveFrom.silent(plugin, options.cwd)) {
config.baseConfig.extends.push(prettierConfig);
}
}
}

return config;
Expand Down
4 changes: 2 additions & 2 deletions test/options-manager.js
Expand Up @@ -101,7 +101,7 @@ test('buildConfig: prettier: true', t => {
trailingComma: 'all',
}]);
// eslint-prettier-config must always be last
t.is(config.baseConfig.extends[config.baseConfig.extends.length - 1], 'prettier');
t.is(config.baseConfig.extends[config.baseConfig.extends.length - 1], 'plugin:prettier/recommended');
// Indent rule is not enabled
t.is(config.baseConfig.rules.indent, undefined);
// Semi rule is not enabled
Expand All @@ -127,7 +127,7 @@ test('buildConfig: prettier: true, typescript file', t => {
}]);

// eslint-prettier-config must always be last
t.is(config.baseConfig.extends[config.baseConfig.extends.length - 1], 'prettier');
t.is(config.baseConfig.extends[config.baseConfig.extends.length - 1], 'plugin:prettier/recommended');
t.is(config.baseConfig.extends[config.baseConfig.extends.length - 2], 'xo-typescript');

// Indent rule is not enabled
Expand Down
24 changes: 24 additions & 0 deletions test/options.js
@@ -0,0 +1,24 @@
import test from 'ava';
import xo from '../index.js';

test('prettier', async t => {
const config = await xo.getConfig({prettier: true, filePath: 'example.js'});

t.is(
config.rules['arrow-body-style'][0],
'off',
'Should extends `eslint-plugin-prettier`, not `eslint-config-prettier`.',
);

t.is(
config.rules['unicorn/empty-brace-spaces'][0],
'off',
'`unicorn/empty-brace-spaces` should be turned off by prettier.',
);

t.is(
config.rules['@typescript-eslint/brace-style'][0],
'off',
'`@typescript-eslint/brace-style` should be turned off even we are not using TypeScript.',
);
});

0 comments on commit f952701

Please sign in to comment.