Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use new eslint-config-prettier override #422

Merged
merged 1 commit into from
Feb 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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