From 2af9a1aabb6133add5897f822628ccc336d826c7 Mon Sep 17 00:00:00 2001 From: Ben Scott Date: Tue, 19 Dec 2023 10:58:13 -0800 Subject: [PATCH] feat: extend eslint-config-prettier --- .changeset/old-doors-kiss.md | 4 +--- README.md | 8 +++----- eslint.config.js | 2 -- recommended.js | 8 +++++--- 4 files changed, 9 insertions(+), 13 deletions(-) diff --git a/.changeset/old-doors-kiss.md b/.changeset/old-doors-kiss.md index 5108e868..a4bb5d0e 100644 --- a/.changeset/old-doors-kiss.md +++ b/.changeset/old-doors-kiss.md @@ -4,16 +4,14 @@ Add recommended config for the flat config format. -If you are using flat config, import the recommended config from `eslint-plugin-prettier/recommended`. Note that in contrast to the legacy recommended config, the flat recommended config does not add `eslint-config-prettier` automatically, consumers will need to explictly add that themselves. +If you are using flat config, import the recommended config from `eslint-plugin-prettier/recommended`. Like the legacy format recommended config, this automatically includes the contents of `eslint-config-prettier`. ```js // eslint.config.js const eslintPluginPrettierRecommended = require('eslint-plugin-prettier/recommended'); -const eslintConfigPrettier = require('eslint-config-prettier'); module.exports = [ // Any other config imports go at the top eslintPluginPrettierRecommended, - eslintConfigPrettier, ]; ``` diff --git a/README.md b/README.md index 5c3eb8fa..0ec5c03a 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ npm install --save-dev --save-exact prettier **_`eslint-plugin-prettier` does not install Prettier or ESLint for you._** _You must install these yourself._ -This plugin works best if you disable all other ESLint rules relating to code formatting, and only enable rules that detect potential bugs. If another active ESLint rule disagrees with `prettier` about how code should be formatted, it will be impossible to avoid lint errors. You should use [`eslint-config-prettier``](https://github.com/prettier/eslint-config-prettier) to disable all formatting-related ESLint rules. +This plugin works best if you disable all other ESLint rules relating to code formatting, and only enable rules that detect potential bugs. If another active ESLint rule disagrees with `prettier` about how code should be formatted, it will be impossible to avoid lint errors. Our recommended configuration automatically enables [`eslint-config-prettier`](https://github.com/prettier/eslint-config-prettier) to disable all formatting-related ESLint rules. ## Configuration (legacy: `.eslintrc*`) @@ -65,18 +65,16 @@ This will: ## Configuration (new: `eslint.config.js`) -For [flat configuration](https://eslint.org/docs/latest/use/configure/configuration-files-new), this plugin ships with an `eslint-plugin-prettier/recommended` config that sets up `eslint-plugin-prettier`. Note that unlike the legacy recommended configuration, the flat config does not configure [eslint-config-prettier](https://github.com/prettier/eslint-config-prettier) for you, and you should import and configure that within your own config. +For [flat configuration](https://eslint.org/docs/latest/use/configure/configuration-files-new), this plugin ships with an `eslint-plugin-prettier/recommended` config that sets up both `eslint-plugin-prettier` and [`eslint-config-prettier`](https://github.com/prettier/eslint-config-prettier) in one go. -Import `eslint-plugin-prettier/recommended` and `eslint-config-prettier` and add them as the _last_ items in the configuration array in your `eslint.config.js` file so that `eslint-config-prettier` has the opportunity to override other configs: +Import `eslint-plugin-prettier/recommended` and add it as the _last_ item in the configuration array in your `eslint.config.js` file so that `eslint-config-prettier` has the opportunity to override other configs: ```js const eslintPluginPrettierRecommended = require('eslint-plugin-prettier/recommended'); -const eslintConfigPrettier = require('eslint-config-prettier'); module.exports = [ // Any other config imports go at the top eslintPluginPrettierRecommended, - eslintConfigPrettier, ]; ``` diff --git a/eslint.config.js b/eslint.config.js index 6d03e950..ad706b4e 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -3,7 +3,6 @@ const eslintPluginN = require('eslint-plugin-n'); const eslintPluginEslintComments = require('@eslint-community/eslint-plugin-eslint-comments'); const eslintPluginEslintPluginRecommended = require('eslint-plugin-eslint-plugin/configs/recommended'); const eslintPluginMdx = require('eslint-plugin-mdx'); -const eslintConfigPrettier = require('eslint-config-prettier'); const eslintPluginPrettierRecommended = require('./recommended'); module.exports = [ @@ -21,7 +20,6 @@ module.exports = [ eslintPluginMdx.flat, eslintPluginMdx.flatCodeBlocks, eslintPluginPrettierRecommended, - eslintConfigPrettier, { rules: { 'eslint-plugin/report-message-format': ['error', '^[^a-z].*\\.$'], diff --git a/recommended.js b/recommended.js index 9d5d418e..5a03e5c2 100644 --- a/recommended.js +++ b/recommended.js @@ -1,13 +1,15 @@ +const eslintConfigPrettier = require('eslint-config-prettier'); const eslintPluginPrettier = require('./eslint-plugin-prettier'); -// This diverges from the legacy format recommended config, as it does not -// extend from the `eslint-config-prettier` ruleset. When using the flat config -// the consumer should add eslint-config-prettier to their own root config. +// Merge the contents of eslint-config-prettier into every module.exports = { + ...eslintConfigPrettier, plugins: { + ...eslintConfigPrettier?.plugins, prettier: eslintPluginPrettier, }, rules: { + ...eslintConfigPrettier?.rules, 'prettier/prettier': 'error', 'arrow-body-style': 'off', 'prefer-arrow-callback': 'off',