简体   繁体   English

使用插件 eslint-plugin-jsx-a11y 运行全局 ESLint

[英]Run global ESLint with plugin eslint-plugin-jsx-a11y

I'm trying to run a global installation of ESLint against a single file, using a specified path to the configuration file:我正在尝试使用配置文件的指定路径对单个文件运行 ESLint 的全局安装:

eslint FileToCheck.jsx --config "../path/to/config/.eslintrc.js"

but I'm getting the error但我收到错误

ESLint couldn't find the plugin "eslint-plugin-jsx-a11y". ESLint 找不到插件“eslint-plugin-jsx-a11y”。 This can happen for a couple different reasons:这可能有几个不同的原因:

  1. If ESLint is installed globally, then make sure eslint-plugin-jsx-a11y is also installed globally.如果 ESLint 是全局安装的,那么请确保 eslint-plugin-jsx-a11y 也是全局安装的。 A globally-installed ESLint cannot find a locally-installed plugin.全局安装的 ESLint 找不到本地安装的插件。

  2. If ESLint is installed locally, then it's likely that the plugin isn't installed correctly.如果 ESLint 安装在本地,那么很可能是插件没有正确安装。 Try reinstalling by running the following:尝试通过运行以下命令重新安装:

    npm i eslint-plugin-jsx-a11y@latest --save-dev npm 我 eslint-plugin-jsx-a11y@latest --save-dev

So it seems like #1 is applicable and I need to install eslint-plugin-jsx-a11y globally.所以看起来#1 是适用的,我需要全局安装eslint-plugin-jsx-a11y I try to do this with我尝试这样做

yarn global add eslint-plugin-jsx-a11y

and rerun the original ESLint command, but it fails with the same error.并重新运行原始的 ESLint 命令,但它失败并出现相同的错误。 I noticed during the yarn global add that some of the output said我在yarn global add期间注意到一些输出说

"eslint-plugin-jsx-a11y@6.0.2" has no binaries “eslint-plugin-jsx-a11y@6.0.2”没有二进制文件

Indeed, when I check ~/AppData/Local/Yarn/bin I do not find any binaries for that plugin (though I do for ESLint).事实上,当我检查 ~/AppData/Local/Yarn/bin 时,我没有找到该插件的任何二进制文件(尽管我为 ESLint 找到了)。

How can I make ESLint run globally with this plugin?如何使用此插件让 ESLint 全局运行? A good answer will not tell me just to install it locally, but will actually answer the question given - how this can be accomplished with globally installed ESLint and plugins.一个好的答案不会告诉我只是在本地安装它,而是会实际回答给出的问题 - 如何通过全局安装的 ESLint 和插件来实现这一点。

Packages I have installed globally with yarn:我使用纱线全局安装的软件包:

  • eslint eslint
  • babel-core babel 核心
  • babel-eslint babel-eslint
  • eslint-plugin-import eslint 插件导入
  • eslint-plugin-react eslint 插件反应
  • eslint-plugin-jsx-a11y eslint-plugin-jsx-a11y
  • eslint-config-airbnb eslint-config-airbnb

Here is my .eslintrc.js, which may or may not be relevant:这是我的 .eslintrc.js,它可能相关也可能不相关:

module.exports = {
  'extends': 'airbnb',
  'plugins': [
    'react',
    'jsx-a11y',
    'import'
  ],

  'env': {
    'browser': true
  },

  'parser': 'babel-eslint',

  'rules': {
    'prefer-template': 'error',
    'comma-dangle': ['error', 'always-multiline'],
    'import/no-extraneous-dependencies': 'off',
    'react/prop-types': 'off',
    'react/jsx-no-bind': 'off',
    'jsx-a11y/no-static-element-interactions': 'off',
    'jsx-a11y/no-noninteractive-element-interactions': 'off',
    'jsx-a11y/alt-text': 'off',
    'jsx-a11y/no-autofocus': 'off',
    'eqeqeq': ['error', 'always', { 'null': 'ignore' }],
    'no-use-before-define': ['error', { 'functions': false }],
    'func-style': ['error', 'declaration', { 'allowArrowFunctions': true }],
    'no-console': 'off',
    'no-alert': 'off',
    'no-continue': 'off',
    'no-param-reassign': ['error', { 'props': false }],
    'no-plusplus': ['error', { 'allowForLoopAfterthoughts': true }],
    'one-var-declaration-per-line': ['error', 'initializations'],
    'one-var': 'off', // Not needed because of one-var-declaration-per-line
    'indent': ['error', 2, {
      'FunctionDeclaration': { 'parameters': 'first' },
      'SwitchCase': 1
    }],
    'no-restricted-syntax': [
      'error',
      {
        selector: 'ForInStatement',
        message: 'for..in loops iterate over the entire prototype chain, which is virtually never what you want. Use Object.{keys,values,entries}, and iterate over the resulting array.',
      },
      {
        selector: 'LabeledStatement',
        message: 'Labels are a form of GOTO; using them makes code confusing and hard to maintain and understand.',
      },
      {
        selector: 'WithStatement',
        message: '`with` is disallowed in strict mode because it makes code impossible to predict and optimize.',
      },
    ],
  }
};

if (process.env.FEATURE_FLAGS) {
  const flags = Object.keys(JSON.parse(process.env.FEATURE_FLAGS));

  module.exports.globals = flags.reduce(function (flagConfig, flag) {
    flagConfig[flag] = false;
    return flagConfig;
  }, {});
}

This issue is caused by the changes to the way ESLint loads packages between versions 5 and 6. See here for details .此问题是由于 ESLint 在版本 5 和 6 之间加载包的方式发生了变化。有关详细信息,请参阅此处

Plugins and shareable configs are no longer affected by ESLint's location插件和可共享配置不再受 ESLint 位置的影响

Previously, ESLint loaded plugins relative to the location of the ESLint package itself.以前,ESLint 加载插件是相对于 ESLint 包本身的位置。 As a result, we suggested that users with global ESLint installations should also install plugins globally, and users with local ESLint installations should install plugins locally.因此,我们建议全局安装 ESLint 的用户也应该全局安装插件,本地安装 ESLint 的用户应该在本地安装插件。 However, due to a design bug, this strategy caused ESLint to randomly fail to load plugins and shareable configs under certain circumstances, particularly when using package management tools like lerna and Yarn Plug n' Play.然而,由于设计错误,这种策略导致 ESLint 在某些情况下随机无法加载插件和可共享配置,尤其是在使用 lerna 和 Yarn Plug n' Play 等包管理工具时。

As a rule of thumb: With ESLint v6, plugins should always be installed locally, even if ESLint was installed globally.根据经验:使用 ESLint v6,插件应该始终安装在本地,即使 ESLint 是全局安装的。 More precisely, ESLint v6 resolves plugins relative to the end user's project by default, and always resolves shareable configs and parsers relative to the location of the config file that imports them.更准确地说,ESLint v6 默认解析与最终用户项目相关的插件,并且始终解析与导入它们的配置文件的位置相关的可共享配置和解析器。

To address: If you use a global installation of ESLint (eg installed with npm install eslint --global ) along with plugins, you should install those plugins locally in the projects where you run ESLint.要解决:如果您使用 ESLint 的全局安装(例如使用npm install eslint --global )以及插件,您应该在运行 ESLint 的项目中本地安装这些插件。 If your config file extends shareable configs and/or parsers, you should ensure that those packages are installed as dependencies of the project containing the config file.如果您的配置文件扩展了可共享的配置和/或解析器,您应该确保这些包作为包含配置文件的项目的依赖项安装。

If you use a config file located outside of a local project (with the --config flag), consider installing the plugins as dependencies of that config file, and setting the --resolve-plugins-relative-to flag to the location of the config file.如果您使用位于本地项目之外的配置文件(带有--config标志),请考虑将插件安装为该配置文件的依赖项,并将--resolve-plugins-relative-to标志设置为配置文件。

There's a fair bit of discussion around this, but the current state of affairs (late 2019) seems to be that there are no good solutions at this point other that rolling back to eslint@5.x :( Watch this issue for more details.对此有很多讨论,但目前的事态(2019 年末)似乎除了回滚到eslint@5.x ,目前还没有好的解决方案 :( 观看此问题以获取更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM