-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
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
Bug: context.parserOptions
becomes empty with flat config and custom parser
#16559
Comments
Hi @ryym, thanks for the issue! This is intentional. Some rules will need to be changed to be compatible with flat config. In particular:
I believe this will be included in #13481 Phase 3: Compatibility testing, so I'm not sure if we should keep this open as a separate issue. @nzakas what do you think? |
You’re correct, this is intentional. When using flat config, all of the options are enclosed in languageOptions, so you need to use context.languageOptions to access them. This will also work with eslintrc configs because that mapping is easy. It’s not as easy to go in the opposite direction, so changing to always use context.languageOptions is the preferred choice. We can keep this open for phase 3, as I’m sure this question will come up in the future again, and maybe we will figure out a better way to map these two approaches. |
Thank you for your responses! As an example, if you run ESLint with the following rule, // rule js
module.exports = {
create(context) {
console.log(context.parserOptions);
return {};
},
}; // eslint.config.js
const tsParser = require("@typescript-eslint/parser");
module.exports = [
{
// console output: { sourceType: 'module' }
languageOptions: { sourceType: 'module' },
// console output: {}
languageOptions: { sourceType: 'module', parser: tsParser },
},
]; |
Because |
Given this file: top-level-await.js import { glob } from 'glob'
await glob('*.js', { ignore: 'node_modules/**' }) With this config: export default [
// This very well could be an extended plugin config, such as one from eslint-plugin-n
{
languageOptions: {
parserOptions: {
ecmaVersion: 2021,
sourceType: 'module'
}
}
},
// Say this is my own custom config
{
languageOptions: {
ecmaVersion: 2023,
sourceType: 'module'
},
files: ['top-level-await.js'],
rules: {
'no-console': 'error'
}
}
] Running
I would have though that this statement
means I do not need to include an additional export default [
{
languageOptions: {
parserOptions: {
ecmaVersion: 2021,
sourceType: 'module'
}
}
},
{
languageOptions: {
ecmaVersion: 2023,
sourceType: 'module',
parserOptions: {
ecmaVersion: 2023
}
},
files: ['top-level-await.js'],
rules: {
'no-console': 'error'
}
}
] So, is this expected behavior? Can |
If In your first example, you'll end up with a config that looks like this: {
languageOptions: {
ecmaVersion: 2023,
sourceType: 'module',
parserOptions: {
ecmaVersion: 2021,
sourceType: 'module'
}
},
files: ['top-level-await.js'],
rules: {
'no-console': 'error'
}
} So even though you have In your second example, the nested Note: If you have further questions about this, please do so in a discussion; this is not related to this issue. |
Ah, there are Discussions 👌 , noted. Thanks for your prompt feedback. This helped confirm what I was experiencing:
However, I didn't see any documentation related to that behavior in the docs I linked to in my previous comment (unless I overlooked something), leaving me to learn about it only through trial and error. |
@morganney we are still working on the documentation for flat config. That's a big task for v9.0.0. |
@nzakas It seems like it ought to be possible to allow users to specify a For reference, I hit this issue with the no-commented-out-code rule from eslint-plugin-etc, not one of the more banner cases like |
@wycats we don't have the data to provide |
Revisiting this now that flat config has shipped. Unfortunately, there just isn't an easy way to ensure that The recommended approach is to always use const parserOptions = context.languageOptions?.parserOptions ?? context.parserOptions; |
Environment
Environment Info:
Node version: v18.10.0
npm version: v8.19.2
Local ESLint version: v8.27.0 (Currently used)
Global ESLint version: Not found
Operating System: darwin 21.6.0
What parser are you using?
@typescript-eslint/parser
What did you do?
I used new ESLint configuration file
eslint.config.js
with some plugins, and noticed that some plugin rules do not work correctly when used with a non-default parser. For example, theno-default-export
rule ineslint-plugin-import
does not report errors at all only when used witheslint.config.js
and non-default parser such as@typescript-eslint/parser
.I found that this is because
parserOptions
in context object passed to rule creator functions becomes empty in that case. Theno-default-export
rule checksparserOptions.sourceType
on rule creation, and skips running if thesourceType
is notmodule
.https://github.com/import-js/eslint-plugin-import/blob/48e8130a9f33afd0f9d06635c25d4f1df4d63340/src/rules/no-default-export.js#L15-L18
I created a reproduction repository: https://github.com/ryym/eslint-flat-config-ts-parser-issue
In this repo I created a local ESLint rule that just prints
context.parserOptions
and used it in four patterns:npm run old-config
....eslintrc.js
with default parsernpm run old-config-ts
....eslintrc.js
with@typescript-plugin/parser
parsernpm run new-config
...eslint.config.js
with default parsernpm run new-config-ts
...eslint.config.js
with@typescript-plugin/parser
parserYou can see the output is an empty object only in
new-config-ts
.reproduction flow and output
npm install
npm run check
I also confirmed that this occurs with
@babel/eslint-parser
as well.What did you expect to happen?
ESLint rules using
context.parserOptions
internally work in any combination of config file format and parser.What actually happened?
As I wrote before, a rule using
context.parserOptions
do not work with new flat configeslint.config.js
with non-default parser such-as@typescript-eslint/parser
,@babel-eslint/parser
. This is becauseparserOptions
becomes an empty object only in that case.Participation
Additional comments
No response
The text was updated successfully, but these errors were encountered: