【问题标题】:How can I make eslint/vscode-eslint stop linting .eslintrc?如何让 eslint/vscode-eslint 停止 linting .eslintrc?
【发布时间】:2019-03-15 04:25:14
【问题描述】:

问题

我的 VS Code PROBLEMS 窗格中有一堆来自 eslint 的“问题”,抱怨我的 .eslintrc 的各种 JSON 问题。在 VS Code 之外对目录进行 Linting 不会向我显示 .eslintrc 问题。我的.eslintrc 和.eslintignore 文件位于我的工作区目录中(与我的.vscode 目录位置相同。

我尝试过的事情

我尝试将以下条目添加到我的 .eslintignore 文件中:

  1. **/.*
  2. **/.eslintrc
  3. .eslintrc
  4. ./.eslintrc

但它们都不能阻止 VS Code 在文件编辑器和问题窗格中显示错误。

我已将.eslintrc 添加到 VS Code 中的排除文件设置中,然后在问题窗格中激活了排除文件过滤器,但我不想从 EXPLORER 窗格中隐藏该文件。

问题

如何强制 VS Code/ESLint/vscode-eslint/罪魁祸首在 VS Code 中进行 linting 时忽略我的 .eslintrc?

.eslintrc

{
  extends: 'airbnb',
  parser: 'babel-eslint',
  plugins: [
    'react',
    'jest',
  ],
  env: {
    browser: true,
    node: true,
    es6: true,
    jest/globals: true,
  },
  globals: {
    LOGGING_ENDPOINT: false,
    $: false,
    beautify: false,
    testContext: false,
    page: false,
  },
  settings: {
    react: {
      pragma: 'h',
    },
  },
  rules: {
    import/no-extraneous-dependencies: 'off',
    import/no-unresolved: ['error', { ignore: ['^react$', '^react-dom$'] }],
    import/extensions: 'off',
    react/react-in-jsx-scope: 'off',
    no-underscore-dangle: 'off',
    react/no-danger: 'off',
    no-unused-vars: ['error', { varsIgnorePattern: 'React' }],
    react/require-default-props: 'off',
    function-paren-newline: 'off',
    import/no-named-as-default: 'off',
    object-curly-newline: 'off',
    jest/no-focused-tests: 'error',
  },
}

谢谢!

解决方案

问题是我的.eslintrc 是一种奇怪的 JSON 格式,当 ESLint 读得很好时,VS Code 告诉我所有的问题。一旦我将其正确格式化为 JSON,VS Code 就不再抱怨了。

【问题讨论】:

  • 在.eslintignore文件中添加以上三个条目后,你是否重启了eslint?
  • @A.Lamansky 我尝试重新启动 VS Code 并在 ESLint 输出通道中看到服务器已停止然后再次启动,但 .eslintrc 仍然被 linted(在文件和问题窗格)。
  • 在我的开发环境中使用第一个 glob 模式 (**/.*) 对我有用。您的 .eslintignore 是否位于您的根目录中?如果在子文件夹下,eslint 可能找不到。
  • 它在我的根目录中(与我的.eslintrc 相同)。我也只是尝试将代码目录添加到忽略并引入 linter 错误。使用代码目录条目,我看不到错误。在保存忽略文件没有带有 linter 错误的代码目录的排除条目后,我看到错误显示而无需重新启动。所以忽略工作但由于某种原因没有忽略.eslintrc。
  • 经过进一步调查,似乎 eslint 默认自动忽略点文件(任何以 . 开头的文件,喜欢 .eslintrc),至少根据 2018 年的这个 github 问题:github.com/eslint/eslint/issues/10341。然后我意识到你的.eslintignore 配置对我“有效”的原因是因为我的 eslint 安装已经忽略了我的点文件。这让我想知道......您的配置中是否有一些设置明确覆盖这个明显的“默认”,甚至是无意的?

标签: javascript eslint eslintrc eslintignore


【解决方案1】:

我认为您的问题与 VS Code 而非 ESLint 最相关。

.eslintrc 文件用于为 ESLint 设置规则和配置。

您可以打开 VS Code 用户设置或工作区设置(Ctrl+Shift+P > 首选项:打开用户设置),以及添加一些规则,以便 VS Code 可以忽略某些文件,例如

  "eslint.options": {
    "extensions": [".js", ".jsx"]
  },
  "eslint.validate": [
    "javascript",
    "javascriptreact",
    "typescript",
    "typescriptreact"
  ],

查看有关vscode-eslint 扩展的更多详细信息。


另外,如果您想在保存文件时添加自动修复,您可以将以下内容添加到您的用户/工作区设置中:

  "files.autoSave": "off",
  "editor.formatOnSave": true,
  "eslint.autoFixOnSave": true,
  "editor.codeActionsOnSave": {
    "source.fixAll": true,
    "source.organizeImports": true
  },

【讨论】:

  • 哦,伙计....我想我刚刚意识到了这个问题。我认为这不是 ESLint...这是 VS Code 内置的 JSON linting...??‍♂️
  • 我会接受你的回答,因为你会指引我正确的方向。然而实际的问题是我的.eslintrc 是一种奇怪的 JSON 格式,虽然 ESLint 读得很好,但 VS Code 却告诉我所有的问题。一旦我将其正确格式化为 JSON,VS Code 就不再抱怨了。感谢您的宝贵时间!
猜你喜欢
  • 2020-12-28
  • 1970-01-01
  • 2018-06-22
  • 2020-11-22
  • 2021-11-12
  • 2015-09-16
  • 2020-03-07
  • 2016-07-08
  • 2019-06-05
相关资源
最近更新 更多