ESLint 损坏:带有建议的规则必须将 `meta.hasSuggestions` 属性 设置为 `true`

ESLint broken: Rules with suggestions must set the `meta.hasSuggestions` property to `true`

我正在使用 VSCode,当我将行 'react-hooks/exhaustive-deps': 'warn' 添加到我的 .eslintrc.js 时,我在 ESLint 输出中得到以下内容:

Rules with suggestions must set the `meta.hasSuggestions` property to `true`. Occurred while linting
C:\Users\filepath.jsx:72 Rule: "react-hooks/exhaustive-deps"

这会破坏所有其他 linting。我尝试将以下内容添加到我的 .eslintrc.js:

meta: {
    hasSuggestions: true
},

这给了我以下错误:

UnhandledPromiseRejectionWarning: Error: ESLint configuration in .eslintrc.js is invalid:
    - Unexpected top-level property "meta".

如有任何帮助,我们将不胜感激。

这是我的。eslintrc.js:

module.exports = {
    env: {
        browser: true,
        es2021: true,
    },
    extends: ['eslint:recommended', 'plugin:react/recommended', 'plugin:react-hooks/recommended'],
    settings: {
        'react': {
            'version': 'detect'
        }
    },
    parserOptions: {
        ecmaFeatures: {
            jsx: true,
        },
        ecmaVersion: 13,
        sourceType: 'module',
    },
    plugins: ['react-hooks', 'react'],
    rules: {
        'react/prop-types': [0],
        quotes: ['error', 'single'],
        semi: [1],
        'react/jsx-indent': [1],
        'no-multi-spaces': [1],
        'indent': [2],
        'react/jsx-newline': [2, { prevent: true }],
        'no-trailing-spaces': [1],
        'no-multiple-empty-lines': [1, { max: 1 }],
        'space-infix-ops': [1],
        'object-curly-spacing': [1, 'always'],
        'comma-spacing': [1],
        'react-hooks/rules-of-hooks': 'error',
        'react/self-closing-comp': 1,
        'react/jsx-closing-tag-location': 1,
        'no-unreachable': 1,
        'react-hooks/exhaustive-deps': 'warn'
    },
};

这是我的 package.json:

{
  "name": "app",
  "private": true,
  "dependencies": {
    "@babel/preset-react": "^7.14.5",
    "@emotion/react": "^11.4.1",
    "@emotion/styled": "^11.3.0",
    "@mui/icons-material": "^5.0.4",
    "@mui/material": "^5.0.3",
    "@mui/styles": "^5.0.1",
    "@rails/actioncable": "^6.1.4-1",
    "@rails/activestorage": "^6.1.4-1",
    "@rails/ujs": "^6.1.4-1",
    "@rails/webpacker": "^6.0.0-rc.5",
    "babel-plugin-macros": "^3.1.0",
    "prop-types": "^15.7.2",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-on-rails": "12.3.0",
    "react-player": "^2.9.0",
    "turbolinks": "^5.2.0",
    "webpack": "^5.53.0",
    "webpack-cli": "^4.8.0"
  },
  "version": "0.1.0",
  "babel": {
    "presets": [
      "./node_modules/@rails/webpacker/package/babel/preset.js"
    ]
  },
  "browserslist": [
    "defaults"
  ],
  "devDependencies": {
    "@webpack-cli/serve": "^1.6.0",
    "eslint": "^8.0.0",
    "eslint-plugin-react": "^7.26.1",
    "eslint-plugin-react-hooks": "^4.2.0",
    "webpack-dev-server": "^4.3.1"
  }
}

ESLint 8.0.0 附带 breaking change for rules that provide suggestions。如果您使用的规则在此更改后尚未更新以起作用,则您无法将任何内容放入您的 .eslintrc.js 中以使其起作用。

你能做什么:

  • 使用 ESLint 7,直到插件更新为使用 ESLint 8。
  • eslint-plugin-react-hooks 的情况下,违规规则已经更新(检查 this line on GitHub),只是此后没有稳定版本的软件包。然而,每天都有 alpha 版本,在撰写本文时最新版本是 4.2.1-alpha-c3a19e5af-20211014。如果你真的需要 ESLint 8 和这个插件,你可以使用 alpha 版本,直到下一个稳定版本出来。

我必须删除规则 "react/require-default-props": "off", 才能解决这个问题。不是一个完整的修复,但它现在适用于 react v18