误报 eslint(no-unused-vars)
Posted
技术标签:
【中文标题】误报 eslint(no-unused-vars)【英文标题】:False Positive eslint(no-unused-vars) 【发布时间】:2019-07-06 16:20:15 【问题描述】:在这里你可以看到我有一个非常简单的 react 组件,但是 eslint 规则给了我一个误报。
配置详情
package.json
"devDependencies":
"eslint": "^5.13.0",
"eslint-plugin-react": "^7.12.4",
"eslint-plugin-react-hooks": "^1.0.1",
.eslintrc.js
module.exports =
"env":
"browser": true,
"es6": true
,
"extends": "eslint:recommended",
"parserOptions":
"ecmaFeatures":
"jsx": true
,
"ecmaVersion": 2018,
"sourceType": "module"
,
"plugins": [
"react",
"react-hooks"
],
"rules":
"indent": [
"error",
2
],
"linebreak-style": [
"error",
"unix"
],
"quotes": [
"error",
"single"
],
"semi": [
"error",
"never"
],
"react-hooks/rules-of-hooks": "error"
,
"settings":
"react":
"createClass": "createReactClass", // Regex for Component Factory to use, default to "createReactClass"
"pragma": "React", // Pragma to use, default to "React"
"version": "detect", // React version. "detect" automatically picks the version you have installed. You can also use `16.0`, `16.3`, etc, if you want to override the detected value.
,
"linkComponents": [
// Components used as alternatives to <a> for linking, eg. <Link to= url />
"Hyperlink",
"name": "Link", "linkAttribute": "to"
]
;
【问题讨论】:
这能回答你的问题吗? ESLint with React gives `no-unused-vars` errors 【参考方案1】:默认情况下,ESLint 不知道 JSX 中变量使用的 React 语义。为了解决这个问题,我可以启用 react/jsx-uses-vars 规则或从 plugin:react/recommended 配置扩展。我选择了后者。
"extends": ["eslint:recommended", "plugin:react/recommended"]
【讨论】:
以上是关于误报 eslint(no-unused-vars)的主要内容,如果未能解决你的问题,请参考以下文章
ESLint 抱怨从未使用过局部状态变量 no-unused-vars
带有 React 的 ESLint 给出了 `no-unused-vars` 错误
ESLint - 为 TypeScript 配置“no-unused-vars”
@typescript-eslint/eslint-plugin 错误:'Route' 已定义但从未使用过(no-unused-vars)