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: [flat config] Caching is not supported when parser is an object #16875
Comments
Hi @liuweiGL, thanks for opening this issue. I ran with the default ESLint // eslint.config.js
module.exports = [
{
files: ['**/*.js', '**/*.mjs'],
rules: {
"semi": "error",
"no-unused-vars": "error"
},
}
] npx eslint --print-config eslint.config.js
// output
{
"languageOptions": {
"ecmaVersion": "latest",
"sourceType": "module",
"parser": "@/espree",
"parserOptions": {},
"globals": {}
},
"plugins": [
"@"
],
"rules": {
"semi": [
2
],
"no-unused-vars": [
2
]
}
} But with language options with the babel parser I got an error. const babelParser = require('@babel/eslint-parser')
module.exports = [
{
files: ['**/*.js', '**/*.mjs'],
rules: {
"semi": "error",
"no-unused-vars": "error"
},
languageOptions: {
parser: babelParser
}
}
] npx eslint --print-config eslint.config.js
// output
Oops! Something went wrong! :(
ESLint: 8.33.0
Error: Caching is not supported when parser is an object.
at Object.value (/Users/joelmathew/WebProjects/experimenting-with-eslint/node_modules/eslint/lib/config/flat-config-array.js:188:27)
at JSON.stringify (<anonymous>)
at Object.execute (/Users/joelmathew/WebProjects/experimenting-with-eslint/node_modules/eslint/lib/cli.js:387:27)
at async main (/Users/joelmathew/WebProjects/experimenting-with-eslint/node_modules/eslint/bin/eslint.js:135:24) This error might have something to do with the @babel/eslint-parser, I'm not quite sure. Let's wait for someone from the core team to help us out here. |
Currently eslint seems to only support string parser eslint/lib/config/flat-config-array.js Lines 147 to 161 in 3bd380d
eslint/lib/config/flat-config-array.js Lines 184 to 195 in 3bd380d
|
Yes, this is a known limitation of the flat config format right now. We just need to decide how to implement caching for objects. The most obvious choice is to ask parsers to implement some form of naming, perhaps like this: parser = {
meta: {
name: "foo",
version: "1.1.1",
}
}; |
TSC Summary: We haven't yet implemented caching for object parsers or processors. To do this, we need a way to identify these objects uniquely. The proposal is to ask parsers to implement some identifying properties, such as: parser = {
meta: {
name: "foo",
version: "1.1.1",
}
}; TSC Question: How do we want to identify object parsers and processors for caching? |
I came across the same issue with eslint-plugin-jsonc. I could work around the issue with a runtime plugin. // Prior (non flat config) -----------------------------------
module.exports = {
extends: ["plugin:jsonc/recommended-with-jsonc"],
parser: "@babel/eslint-parser",
overrides: [
{
files: ["*.json", "*.json5", "*.jsonc"],
parser: "jsonc-eslint-parser",
},
],
};
// Flat config ------------------------------------------------
import jsonc from "eslint-plugin-jsonc";
export default [{
files: ["**/*.json", "**/*.jsonc", "**/*.json5"],
plugins: {
jsonc
},
languageOptions: {
parser: 'jsonc/jsonc-eslint-parser' // <- Error because jsonc.parsers['jsonc-eslint-parser'] does not exists
},
rules: {...}
}]; The initial idea to bypass the languageOptions: {
parser: await import('jsonc-eslint-parser') // <- Error must be string. Object not implemented yet
} To work aroud this, I defined a runtime plugin of the same name import {default as jsonc, parseForESLint, configs} from "eslint-plugin-jsonc";
const parsers = {
'jsonc-eslint-parser': {
parseForESLint
}
}
export default [{
files: ["**/*.json", "**/*.jsonc", "**/*.json5"],
plugins: {
jsonc: { ...jsonc, parsers}
/* same as
jsonc: {
parsers: {
'jsonc-eslint-parser': {
parseForESLint
}
}
} */
},
languageOptions: {
parser: 'jsonc/jsonc-eslint-parser'
},
rules: {...}
}]; Not sure if this is a valid approach, but it seems to work so far. |
Great that you found a workaround! We discussed the |
Working on this. |
This implements a check for a 'meta' key on parsers and processors that contains information to help flat config serialize these objects for use with caching and also --print-config on the command line. Fixes #16875
* feat: Serialize parsers/processors in flat config This implements a check for a 'meta' key on parsers and processors that contains information to help flat config serialize these objects for use with caching and also --print-config on the command line. Fixes #16875 * Add support for non-meta properties * Fix edge case
Environment
What parser are you using?
@babel/eslint-parser
What did you do?
eslint.config.js
What did you expect to happen?
Print the config detail
What actually happened?
Participation
Additional comments
https://eslint.org/blog/2022/08/new-config-system-part-2/#custom-parsers-and-parser-options-are-mostly-the-same
The text was updated successfully, but these errors were encountered: