Skip to content

Commit

Permalink
Use new eslint-config-prettier override (#422)
Browse files Browse the repository at this point in the history
  • Loading branch information
pvdlg committed Feb 13, 2020
1 parent c2bb1bf commit 9fbdb97
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
20 changes: 13 additions & 7 deletions lib/options-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ const ENGINE_RULES = {
}
};

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

// Keep the same behaviour in mergeWith as deepAssign
const mergeFn = (previousValue, value) => {
if (Array.isArray(previousValue) && Array.isArray(value)) {
Expand Down Expand Up @@ -281,23 +290,20 @@ const buildConfig = options => {

// If the user sets the `prettier` options then add the `prettier` plugin and config
if (options.prettier) {
// Disable formatting rules conflicting with Prettier
config.rules['unicorn/number-literal-case'] = 'off';
// Can be re-enabled when https://github.com/prettier/prettier/issues/4157 is fixed
config.rules['unicorn/no-nested-ternary'] = 'off';
// The prettier plugin uses Prettier to format the code with `--fix`
config.plugins = config.plugins.concat('prettier');
// The prettier config overrides ESLint stylistic rules that are handled by Prettier
config.baseConfig.extends = config.baseConfig.extends.concat('prettier');
config.baseConfig.extends = config.baseConfig.extends.concat('prettier/unicorn');
// The `prettier/prettier` rule reports errors if the code is not formatted in accordance to Prettier
config.rules['prettier/prettier'] = [
'error', mergeWithPrettierConfig(options, prettier.resolveConfig.sync(options.cwd || process.cwd()) || {})
];
// 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 override of ['react', 'flowtype', 'standard']) {
if (options.cwd && resolveFrom.silent(options.cwd, `eslint-plugin-${override}`)) {
config.baseConfig.extends = config.baseConfig.extends.concat(`prettier/${override}`);
for (const [plugin, prettierConfig] of Object.entries(PRETTIER_CONFIG_OVERRIDE)) {
if (options.cwd && resolveFrom.silent(options.cwd, plugin)) {
config.baseConfig.extends = config.baseConfig.extends.concat(prettierConfig);
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion test/options-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ test('buildConfig: prettier: true', t => {
trailingComma: 'none'
}]);
// eslint-prettier-config must always be last
t.deepEqual(config.baseConfig.extends.slice(-1), ['prettier']);
t.deepEqual(config.baseConfig.extends[config.baseConfig.extends.length - 1], 'prettier/unicorn');
t.deepEqual(config.baseConfig.extends[config.baseConfig.extends.length - 2], 'prettier');
// Indent rule is not enabled
t.is(config.rules.indent, undefined);
// Semi rule is not enabled
Expand Down

0 comments on commit 9fbdb97

Please sign in to comment.