-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
[Fix] export flat configs from plugin root and fix flat config crash #3694
Draft
bradzacher
wants to merge
5
commits into
jsx-eslint:master
Choose a base branch
from
bradzacher:3693-flat-config-crash
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2692c07
[Fix] export flat configs from plugin root and fix flat config crash
bradzacher 60340f8
update docs
mdjermanovic 4be7f0e
[Fix] use same plugin instance in flat configs
mdjermanovic a32c0ad
[Tests] test plugin in flat configs
mdjermanovic a44e025
ignore flat configs in doc generator
mdjermanovic File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,13 @@ | ||
'use strict'; | ||
|
||
const fromEntries = require('object.fromentries'); | ||
const entries = require('object.entries'); | ||
const plugin = require('../index'); | ||
|
||
const allRules = require('../lib/rules'); | ||
|
||
function filterRules(rules, predicate) { | ||
return fromEntries(entries(rules).filter((entry) => predicate(entry[1]))); | ||
} | ||
|
||
/** | ||
* @param {object} rules - rules object mapping rule name to rule module | ||
* @returns {Record<string, 2>} | ||
*/ | ||
function configureAsError(rules) { | ||
return fromEntries(Object.keys(rules).map((key) => [`react/${key}`, 2])); | ||
} | ||
|
||
const activeRules = filterRules(allRules, (rule) => !rule.meta.deprecated); | ||
const activeRulesConfig = configureAsError(activeRules); | ||
|
||
const deprecatedRules = filterRules(allRules, (rule) => rule.meta.deprecated); | ||
const legacyConfig = plugin.configs.all; | ||
|
||
module.exports = { | ||
plugins: { | ||
/** | ||
* @type {{ | ||
* deprecatedRules: Record<string, import('eslint').Rule.RuleModule>, | ||
* rules: Record<string, import('eslint').Rule.RuleModule>, | ||
* }} | ||
*/ | ||
react: { | ||
deprecatedRules, | ||
rules: allRules, | ||
}, | ||
}, | ||
rules: activeRulesConfig, | ||
languageOptions: { | ||
parserOptions: { | ||
ecmaFeatures: { | ||
jsx: true, | ||
}, | ||
}, | ||
}, | ||
plugins: { react: plugin }, | ||
rules: legacyConfig.rules, | ||
languageOptions: { parserOptions: legacyConfig.parserOptions }, | ||
}; | ||
|
||
// this is so the `languageOptions` property won't be warned in the new config system | ||
Object.defineProperty(module.exports, 'languageOptions', { enumerable: false }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,13 @@ | ||
'use strict'; | ||
|
||
const all = require('./all'); | ||
const plugin = require('../index'); | ||
|
||
module.exports = Object.assign({}, all, { | ||
languageOptions: Object.assign({}, all.languageOptions, { | ||
parserOptions: Object.assign({}, all.languageOptions.parserOptions, { | ||
jsxPragma: null, // for @typescript/eslint-parser | ||
}), | ||
}), | ||
rules: { | ||
'react/react-in-jsx-scope': 0, | ||
'react/jsx-uses-react': 0, | ||
}, | ||
}); | ||
const legacyConfig = plugin.configs['jsx-runtime']; | ||
|
||
module.exports = { | ||
plugins: { react: plugin }, | ||
rules: legacyConfig.rules, | ||
languageOptions: { parserOptions: legacyConfig.parserOptions }, | ||
}; | ||
|
||
// this is so the `languageOptions` property won't be warned in the new config system | ||
Object.defineProperty(module.exports, 'languageOptions', { enumerable: false }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,13 @@ | ||
'use strict'; | ||
|
||
const all = require('./all'); | ||
const plugin = require('../index'); | ||
|
||
module.exports = Object.assign({}, all, { | ||
languageOptions: all.languageOptions, | ||
rules: { | ||
'react/display-name': 2, | ||
'react/jsx-key': 2, | ||
'react/jsx-no-comment-textnodes': 2, | ||
'react/jsx-no-duplicate-props': 2, | ||
'react/jsx-no-target-blank': 2, | ||
'react/jsx-no-undef': 2, | ||
'react/jsx-uses-react': 2, | ||
'react/jsx-uses-vars': 2, | ||
'react/no-children-prop': 2, | ||
'react/no-danger-with-children': 2, | ||
'react/no-deprecated': 2, | ||
'react/no-direct-mutation-state': 2, | ||
'react/no-find-dom-node': 2, | ||
'react/no-is-mounted': 2, | ||
'react/no-render-return-value': 2, | ||
'react/no-string-refs': 2, | ||
'react/no-unescaped-entities': 2, | ||
'react/no-unknown-property': 2, | ||
'react/no-unsafe': 0, | ||
'react/prop-types': 2, | ||
'react/react-in-jsx-scope': 2, | ||
'react/require-render-return': 2, | ||
}, | ||
}); | ||
const legacyConfig = plugin.configs.recommended; | ||
|
||
module.exports = { | ||
plugins: { react: plugin }, | ||
rules: legacyConfig.rules, | ||
languageOptions: { parserOptions: legacyConfig.parserOptions }, | ||
}; | ||
|
||
// this is so the `languageOptions` property won't be warned in the new config system | ||
Object.defineProperty(module.exports, 'languageOptions', { enumerable: false }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,109 @@ | ||
'use strict'; | ||
|
||
const configAll = require('./configs/all'); | ||
const configRecommended = require('./configs/recommended'); | ||
const configRuntime = require('./configs/jsx-runtime'); | ||
const fromEntries = require('object.fromentries'); | ||
const entries = require('object.entries'); | ||
|
||
const allRules = require('./lib/rules'); | ||
|
||
function filterRules(rules, predicate) { | ||
return fromEntries(entries(rules).filter((entry) => predicate(entry[1]))); | ||
} | ||
|
||
/** | ||
* @param {object} rules - rules object mapping rule name to rule module | ||
* @returns {Record<string, 2>} | ||
*/ | ||
function configureAsError(rules) { | ||
return fromEntries(Object.keys(rules).map((key) => [`react/${key}`, 2])); | ||
} | ||
|
||
const activeRules = filterRules(allRules, (rule) => !rule.meta.deprecated); | ||
const activeRulesConfig = configureAsError(activeRules); | ||
|
||
const deprecatedRules = filterRules(allRules, (rule) => rule.meta.deprecated); | ||
|
||
// for legacy config system | ||
const plugins = [ | ||
'react', | ||
]; | ||
|
||
module.exports = { | ||
deprecatedRules: configAll.plugins.react.deprecatedRules, | ||
const plugin = { | ||
deprecatedRules, | ||
rules: allRules, | ||
configs: { | ||
recommended: Object.assign({}, configRecommended, { | ||
parserOptions: configRecommended.languageOptions.parserOptions, | ||
recommended: { | ||
plugins, | ||
}), | ||
all: Object.assign({}, configAll, { | ||
parserOptions: configAll.languageOptions.parserOptions, | ||
parserOptions: { | ||
ecmaFeatures: { | ||
jsx: true, | ||
}, | ||
}, | ||
rules: { | ||
'react/display-name': 2, | ||
'react/jsx-key': 2, | ||
'react/jsx-no-comment-textnodes': 2, | ||
'react/jsx-no-duplicate-props': 2, | ||
'react/jsx-no-target-blank': 2, | ||
'react/jsx-no-undef': 2, | ||
'react/jsx-uses-react': 2, | ||
'react/jsx-uses-vars': 2, | ||
'react/no-children-prop': 2, | ||
'react/no-danger-with-children': 2, | ||
'react/no-deprecated': 2, | ||
'react/no-direct-mutation-state': 2, | ||
'react/no-find-dom-node': 2, | ||
'react/no-is-mounted': 2, | ||
'react/no-render-return-value': 2, | ||
'react/no-string-refs': 2, | ||
'react/no-unescaped-entities': 2, | ||
'react/no-unknown-property': 2, | ||
'react/no-unsafe': 0, | ||
'react/prop-types': 2, | ||
'react/react-in-jsx-scope': 2, | ||
'react/require-render-return': 2, | ||
}, | ||
}, | ||
all: { | ||
plugins, | ||
}), | ||
'jsx-runtime': Object.assign({}, configRuntime, { | ||
parserOptions: configRuntime.languageOptions.parserOptions, | ||
parserOptions: { | ||
ecmaFeatures: { | ||
jsx: true, | ||
}, | ||
}, | ||
rules: activeRulesConfig, | ||
}, | ||
'jsx-runtime': { | ||
plugins, | ||
}), | ||
parserOptions: { | ||
ecmaFeatures: { | ||
jsx: true, | ||
}, | ||
jsxPragma: null, // for @typescript/eslint-parser | ||
}, | ||
rules: { | ||
'react/react-in-jsx-scope': 0, | ||
'react/jsx-uses-react': 0, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
plugin.configs['flat/recommended'] = { | ||
plugins: { react: plugin }, | ||
rules: plugin.configs.recommended.rules, | ||
languageOptions: { parserOptions: plugin.configs.recommended.parserOptions }, | ||
}; | ||
|
||
plugin.configs['flat/all'] = { | ||
plugins: { react: plugin }, | ||
rules: plugin.configs.all.rules, | ||
languageOptions: { parserOptions: plugin.configs.all.parserOptions }, | ||
}; | ||
|
||
plugin.configs['flat/jsx-runtime'] = { | ||
plugins: { react: plugin }, | ||
rules: plugin.configs['jsx-runtime'].rules, | ||
languageOptions: { parserOptions: plugin.configs['jsx-runtime'].parserOptions }, | ||
}; | ||
|
||
module.exports = plugin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
tests/fixtures/flat-config/config-all/eslint.config-deep.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
'use strict'; | ||
|
||
const reactAll = require('../../../../configs/all'); | ||
|
||
module.exports = [{ | ||
files: ['**/*.jsx'], | ||
...reactAll, | ||
languageOptions: { | ||
...reactAll.languageOptions | ||
} | ||
}]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
'use strict'; | ||
|
||
const reactPlugin = require('../../../..'); | ||
|
||
module.exports = [{ | ||
files: ['**/*.jsx'], | ||
...reactPlugin.configs['flat/all'] | ||
}]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<div foo="hello"> | ||
test | ||
</div> |
15 changes: 15 additions & 0 deletions
15
tests/fixtures/flat-config/config-jsx-runtime/eslint.config-deep.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
'use strict'; | ||
|
||
const reactRecommended = require('../../../../configs/recommended'); | ||
const reactJSXRuntime = require('../../../../configs/jsx-runtime'); | ||
|
||
module.exports = [ | ||
{ | ||
files: ['**/*.jsx'], | ||
...reactRecommended, | ||
languageOptions: { | ||
...reactRecommended.languageOptions | ||
} | ||
}, | ||
reactJSXRuntime | ||
]; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
this might be more ergonomic if it's a "flat" object with 3 properties, "recommended", "all", "jsx-runtime"?
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.
Unlike eslintrc, there are no restrictions on how flat configs can be exported from plugins. However, the convention ESLint recommends is to still export configs as properties of
configs
(eslint/eslint#18095).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.
true - if we're following that tho we'd avoid "flat" entirely too.
For destructuring, being able to do
{ configs: { flat: { all } } }
seems much more convenient than requiring brackets and quotes, to me. Thoughts?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.
Sounds good to me at the moment.
In some future major version of this plugin, it would be good to switch to:
{ configs: { 'legacy/recommended' /* eslintrc */, 'recommended' /* flat */, ... } }
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.
certainly, if indeed the ecosystem prefers flat config then there will come a time when it doesn't make sense to support anything prior to that.
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.
Here's a commit that updates this: mdjermanovic@bbdaaa7