javascript - 在 ReactJS 箭头函数类属性上防止 ESLint "no-undef"

标签 javascript reactjs babeljs eslint babel-eslint

我正在尝试让 @babel/plugin-proposal-class-properties@babel/eslint-parser 和 ESLint 一起工作。我有很多这样写的类方法:

class MyClass extends PureComponent {
  ...
  someProperty = () => { ... } // < ESLint: 'someProperty' is not defined.(no-undef)
  ...
}

它们确实有效并且代码确实运行了。除了 ESLint 仍然认为它们是错误的。

我知道这个问题已经被问过好几次了(比如 here ),但我已经被困了好几天了,没有一个答案似乎有帮助。

我的.eslintrc 文件:

{
  "extends": [
    "eslint:recommended",
    "plugin:react/recommended",
    "airbnb"
  ],
  "parser": "@babel/eslint-parser", // < I do use the babel parser
  "parserOptions":{
    "ecmaFeatures": {
      "experimentalObjectRestSpread": true
    },
    "babelOptions": {
      "plugins": [
        "@babel/plugin-proposal-class-properties" // < tried putting my plugin here
      ]
    }
  },
  "plugins": [
    "react",
    "jsx-a11y",
    "import",
    // "@babel" < "ESLint: Error: Failed to load plugin @babel: Cannot find module 'eslint-plugin-@babel'"
  ],
  "rules": {
    "react/jsx-filename-extension": 0,
    "no-plusplus": 0,
    "comma-dangle": [2, "never"],
    "indent": ["error", 2, { "SwitchCase": 1 }],
    "react/jsx-indent": ["error", 2],
    "react/jsx-indent-props": ["error", 2],
    "max-len": [2, 300],
    "semi": [2, "never"],
    "no-underscore-dangle": 0,
    "no-script-url": 0,
    "no-extend-native": ["error", { "exceptions": ["String"] }],
    "no-restricted-syntax": ["error", "ForOfStatement", "LabeledStatement", "WithStatement"],
    "import/no-unresolved": [2, { "ignore" : [ "electron" ] }]
  }
}

我的 babel.config.json 文件:

{
  "presets": [["@babel/preset-env", { "modules": false }], "@babel/preset-react"],
  "plugins": [
    "babel-plugin-add-module-exports",
    "babel-plugin-async-to-promises",
    "babel-plugin-syntax-async-functions",
    "@babel/plugin-proposal-function-bind",
    "@babel/plugin-transform-modules-commonjs",
    "@babel/plugin-proposal-class-properties"
  ],
  "env": {
    "production": {
      "presets": ["react-optimize"],
      "plugins": [
        "babel-plugin-transform-remove-console",
        "babel-plugin-transform-remove-debugger",
        "babel-plugin-dev-expression"
      ]
    },
    "dev": {
      "presets": []
    },
    "test": {
      "plugins": [
        ["webpack-loaders", { "config": "webpack.config.node.js", "verbose": false }]
      ]
    }
  }
}

包版本:

@babel/core: 7.12.10
@babel/eslint-parser: 7.12.1
@babel/eslint-plugin: 7.12.1
@babel/plugin-proposal-class-properties: 7.12.1
eslint: ^3.19.0

最佳答案

以防万一有人遇到这个问题,我最终将 eslint 更新到最新版本(当时是 7.18.0),我的问题就消失了。

关于javascript - 在 ReactJS 箭头函数类属性上防止 ESLint "no-undef",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65813069/

相关文章:

javascript - Node.Select() 在 RadTreeView 中触发 Node_Click() 事件?

reactjs - 用于 Web 的 React 向上滑动面板

javascript - 使用 angularJS 过滤器的 Ionic 应用程序非常慢

javascript - 添加负像素时获得透明度

reactjs - 无法在 monorepo 中设置绝对模块导入

typescript - 如何使用 Bable 7 和 Webpack 4 为 IE11 正确构建带有导入包的 Typescript?

javascript - Webpack: npm start "Module not found: Error: Can' t resolve ..."移动 webpack/react 文件的位置后

javascript - Babel 7 失败,单个插件显示 "Duplicate plugin/preset detected."

javascript - 循环遍历具有相似类的元素

javascript - 从 setState() 更新 View 不​​会按预期触发重新渲染