-
-
Notifications
You must be signed in to change notification settings - Fork 190
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
feat: support flat config system #592
Changes from all commits
9bec86b
ee4be92
b65e1c5
713bca0
6904c98
c9810e8
a58e92c
313aa73
4f3d200
1ac30c5
923e913
1d5bdd8
419e32b
95388da
90d3a0b
9c45c5d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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']]; | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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<Plugin['configs']>}} | ||
*/ | ||
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']: { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we use camelCase here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. looks like config names should be as rules with dashes also eslint have config naming convention for eslint plugins with dashes too There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The previous rules are for legacy configs, for flat configs, we're using js object reference instead, so camelCase is preferred IMO. And I think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
https://eslint.org/docs/latest/extend/plugin-migration-flat-config#backwards-compatibility They're recommending appending There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That strategy is an unnecessary breaking change IMO, and it can be made in next major version. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, that's why the suggestion is to add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My suggestion is to add |
||
// 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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would it be a circular ref? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, you're actually right! I'll make a comment about this below. |
||
}, | ||
rules: { | ||
'prettier/prettier': 'error', | ||
'arrow-body-style': 'off', | ||
'prefer-arrow-callback': 'off', | ||
}, | ||
}, | ||
}; | ||
|
||
module.exports = eslintPluginPrettier; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still recommend this config name.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But even better with a separate
flat.js
file, right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have such preference, a clear document is enough IMO.