diff --git a/.changeset/kind-apples-itch.md b/.changeset/kind-apples-itch.md new file mode 100644 index 00000000..3b51ed5b --- /dev/null +++ b/.changeset/kind-apples-itch.md @@ -0,0 +1,21 @@ +--- +'eslint-plugin-prettier': minor +--- + +feat: support ESLint flat config system + +`eslint-plugin-prettier` can be used in the flat config format like this with CJS: + +```js +const prettier = require('eslint-plugin-prettier'); + +module.exports = [prettier.configs['recommended-flat']]; +``` + +Or with ESM: + +```js +import prettier from 'eslint-plugin-prettier'; + +export default [prettier.configs['recommended-flat']]; +``` diff --git a/eslint-plugin-prettier.js b/eslint-plugin-prettier.js index b6d0a46e..e68002a0 100644 --- a/eslint-plugin-prettier.js +++ b/eslint-plugin-prettier.js @@ -23,6 +23,7 @@ const { showInvisibles, generateDifferences, } = require('prettier-linter-helpers'); +const { name, version } = require('./package.json'); // ------------------------------------------------------------------------------ // Constants @@ -77,20 +78,14 @@ function reportDifference(context, difference) { // ------------------------------------------------------------------------------ /** - * @type {Plugin} + * Type hint `configs` key as non-nullable to avoid type-checking errors in when + * assigning to `eslintPluginPrettier.configs` below + * + * @type {Plugin & {configs: NonNullable}} */ const eslintPluginPrettier = { - configs: { - recommended: { - extends: ['prettier'], - plugins: ['prettier'], - rules: { - 'prettier/prettier': 'error', - 'arrow-body-style': 'off', - 'prefer-arrow-callback': 'off', - }, - }, - }, + meta: { name, version }, + configs: {}, rules: { prettier: { meta: { @@ -239,4 +234,30 @@ const eslintPluginPrettier = { }, }; +// Assign configs after initial plugin configuration, as flat configs +// require referencing the original plugin that contains the rules. +eslintPluginPrettier.configs = { + recommended: { + extends: 'prettier', + plugins: ['prettier'], + rules: { + 'prettier/prettier': 'error', + 'arrow-body-style': 'off', + 'prefer-arrow-callback': 'off', + }, + }, + ['recommended-flat']: { + // In the flat config, don't extend from the eslint-config-prettier ruleset. + // The consumer should deal with extending from eslint-config-prettier themselves. + plugins: { + prettier: eslintPluginPrettier, + }, + rules: { + 'prettier/prettier': 'error', + 'arrow-body-style': 'off', + 'prefer-arrow-callback': 'off', + }, + }, +}; + module.exports = eslintPluginPrettier;