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

Flo*_*ood 45 reactjs eslint eslintrc react-hooks

我正在使用 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"
Run Code Online (Sandbox Code Playgroud)

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

meta: {
    hasSuggestions: true
},
Run Code Online (Sandbox Code Playgroud)

这给了我以下错误:

UnhandledPromiseRejectionWarning: Error: ESLint configuration in .eslintrc.js is invalid:
    - Unexpected top-level property "meta".
Run Code Online (Sandbox Code Playgroud)

任何帮助,将不胜感激。

这是我的 .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'
    },
};
Run Code Online (Sandbox Code Playgroud)

这是我的 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"
  }
}

Run Code Online (Sandbox Code Playgroud)

Voj*_*nad 58

ESLint 8.0.0对提供建议的规则进行了重大更改。如果您使用在此更改后尚未更新的规则,则无法在 .eslintrc.js 中放入任何内容以使其正常工作。

你可以做什么:

  • 使用 ESLint 7,直到插件更新为可与 ESLint 8 配合使用。
  • 如果是eslint-plugin-react-hooks,则违规规则已被更新(请在 GitHub 上查看此行),只是此后一直没有该包的稳定版本。然而,每天都有 alpha 版本,在撰写本文时最新版本是4.2.1-alpha-c3a19e5af-20211014. 如果您确实需要 ESLint 8 和此插件,您可以使用 alpha 版本,直到下一个稳定版本出现。

  • `npm install -D eslint-plugin-react-hooks@next` 目前可以使用。 (12认同)
  • 此处反应问题:https://github.com/facebook/react/issues/22545 (2认同)