在 ES Lint 中为 React Native 应用程序将 WebSocket 定义为全局变量

Define WebSocket as a global in ES Lint for a React Native app

我收到以下 eslint 错误:

42:21  error  'WebSocket' is not defined  no-undef

您无法从 react-native 导入 WebSocket,因为它是全局的,但是当我将 WebSocket 作为全局变量添加到我的 .eslintrc.yml 时,它不会改变错误的结果:

globals:
   WebSocket: true

如何在 ES Lint 中为 React Native 应用将 WebSocket 定义为全局?

这可以修复吗?目前我的 .eslintrc 看起来像这样:

env:
  browser: false
  es6: true
  commonjs: true
  node: true
extends: 'airbnb'
parser: babel-eslint
globals:
    WebSocket: true
parserOptions:
  ecmaFeatures:
    experimentalObjectRestSpread: true
    jsx: true
  sourceType: module
plugins:
  - react
  - react-native
rules:
  indent:
    - error
    - tab
    - {"SwitchCase": 1}
  linebreak-style:
    - error
    - unix
  quotes:
    - error
    - double
  semi:
    - error
    - never
  no-tabs: off
  max-len: off
  no-console: off
  no-plusplus: off
  global-require: off
  import/no-unresolved: off
  import/extensions: off
  class-methods-use-this: off
  react/jsx-no-bind: off
  react/forbid-prop-types: off
  react/prefer-stateless-function: off
  react/jsx-indent: [2, 'tab']
  react/jsx-indent-props: [2, 'tab']
  react/jsx-filename-extension: [1, { extensions: ['.js', '.jsx'] }]
  react/jsx-uses-react: error
  react/jsx-uses-vars: error
  react-native/no-unused-styles: 2
  react-native/split-platform-components: 2
  react-native/no-inline-styles: off
  react-native/no-color-literals: off

我可以使用内联注释摆脱它

/* globals WebSocket:true */

另外,当我没有从 airbnb eslint 继承时,但我无法弄清楚 Airbnb 中的哪个 lint 规则负责阻止它。

根据您提供的信息,您的配置文件似乎由于某种原因未被提取。 globals 的配置看起来正确并且应该可以工作。为了弄清楚发生了什么,您应该做两件事。首先,您可以 运行 eslint --print-config path_to_some_js_file 查看 ESLint 解析所有依赖项和级联后您的配置的外观。该配置很可能不会声明 globals。之后,您可以 运行 eslint --debug path_to_file 查看 ESLint 使用的所有配置文件。如果您的文件未被包含,请检查所有其他配置文件并验证它们中没有 root: true (这将阻止 ESLint 合并父目录中的配置)。有关 CLI 标志的更多信息,您可以查看 ESLint documentation