нарушение eslint-plugin-react, TypeError: Ключ «плагины»: Ключ «0»: Ожидается объект
Ошибка Eslint при запуске с использованием рекомендуемой конфигурации из eslint-plugin-react. Попытка следовать разделу (новый: eslint.config.js) здесь https://github.com/jsx-eslint/eslint-plugin-react .
> Oops! Something went wrong! :(
>
> ESLint: 8.45.0
>
> TypeError: Key "plugins": Key "0": Expected an object.
> at Object.validate (/home/kris3579/personalProjects/mal-ranker/node_modules/eslint/lib/config/flat-config-schema.js:315:23)
> at ObjectSchema.validate (/home/kris3579/personalProjects/mal-ranker/node_modules/@humanwhocodes/object-schema/src/object-schema.js:218:35)
> at /home/kris3579/personalProjects/mal-ranker/node_modules/@humanwhocodes/object-schema/src/object-schema.js:171:18
> at Array.reduce (<anonymous>)
> at ObjectSchema.merge (/home/kris3579/personalProjects/mal-ranker/node_modules/@humanwhocodes/object-schema/src/object-schema.js:169:24)
> at /home/kris3579/personalProjects/mal-ranker/node_modules/@humanwhocodes/config-array/api.js:916:42
> at Array.reduce (<anonymous>)
> at FlatConfigArray.getConfig (/home/kris3579/personalProjects/mal-ranker/node_modules/@humanwhocodes/config-array/api.js:915:39)
> at FlatConfigArray.isFileIgnored (/home/kris3579/personalProjects/mal-ranker/node_modules/@humanwhocodes/config-array/api.js:943:15)
> at /home/kris3579/personalProjects/mal-ranker/node_modules/eslint/lib/eslint/eslint-helpers.js:312:49
>
eslint.config.js
const js = require('@eslint/js');
const react = require('eslint-plugin-react');
module.exports = [
js.configs.recommended,
react.configs.recommended,
{
files: ["**/*.js", "**/*.mjs"],
plugins: {
react,
},
}
];
Я попытался использовать более явный метод const actRecommended = require('eslint-plugin-react/configs/recommended');' из документации та же ошибка.
пакет.json
{
"name": "placeholder",
"version": "0.7",
"private": true,
"dependencies": {
"@reduxjs/toolkit": "^1.9.5",
"dotenv": "16.3.1",
"js-cookie": "^3.0.5",
"prop-types": "^15.8.1",
"react": "18.2.0",
"react-dom": "^18.2.0",
"react-redux": "^8.1.1",
"react-router-dom": "^6.14.1",
"react-scripts": "5.0.1",
"react-table": "^7.8.0",
"styled-components": "^6.0.4",
"superagent": "8.0.9"
},
"devDependencies": {
"@eslint/js": "^8.45.0",
"eslint": "^8.45.0",
"eslint-plugin-react": "^7.32.2"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"lint": "eslint './**/*.js'"
},
}
Также добавлен global.d.ts после появления предупреждения «Не удалось найти файл объявления для модуля «eslint-plugin-react», ошибка не изменилась.
declare module 'eslint-plugin-react'
2 ответа
Это связано с несовместимостью классических.eslintrc
конфигурация стиля и новая конфигурация Flateslint.config.js
. См. раздел «Более мощные и настраиваемые плагины» . Большинство авторов плагинов не перенесли свои плагины для поддержки новой плоской конфигурации, поэтому мы здесь.
См. соответствующую проблему, обсуждающую эту проблему: https://github.com/eslint/eslint/issues/17355.
На данный момент вы можете изменитьeslint-plugin-react
самостоятельно или лучше откройте PR, чтобы обеспечить необходимые изменения.
eslint.config.js (то же самое, если использовать CJS)
import react from 'eslint-plugin-react'
// Begin fix
react.configs.recommended.plugins = { react }
react.configs.recommended.languageOptions = {
parserOptions: react.configs.recommended.parserOptions
}
delete react.configs.recommended.parserOptions
// End fix
export default [
react.configs.recommended,
// Now add your own config
{
languageOptions: {},
files: ['**/*.js'],
plugins: {
react
},
rules: {}
}
]
Согласно документации, плагины должны быть в массиве.
const js = require('@eslint/js');
const react = require('eslint-plugin-react');
module.exports = [
js.configs.recommended,
react.configs.recommended,
{
files: ["**/*.js", "**/*.mjs"],
plugins: ['react'], // Change this to an array
}
];