构建 React 应用程序时不同的 ESLint 结果

Tom*_*bet 8 typescript reactjs eslint package.json eslintrc

我是 React 开发的初学者。
我正在尝试解决项目的所有错误/警告,但在开发环境和生产环境之间得到不同的结果。我在配置上没有对它们做出任何区别。

运行npm run lint给我这个输出: npm run lint

运行npm run build给我这个输出: npm run build

我得到不同的 ESLint 输出是否正常?

这是我的 package.json:

{
  "name": "immersion-dashboard",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@reduxjs/toolkit": "^1.5.1",
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.3.2",
    "@testing-library/user-event": "^7.1.2",
    "@types/jest": "^24.0.0",
    "@types/node": "^12.0.0",
    "@types/react": "^16.9.0",
    "@types/react-dom": "^16.9.0",
    "@types/react-redux": "^7.1.7",
    "bootstrap": "^5.1.3",
    "react": "^17.0.2",
    "react-bootstrap": "^2.0.2",
    "react-dom": "^17.0.2",
    "react-redux": "^7.2.0",
    "react-scripts": "4.0.3",
    "typescript": "~4.1.5"
  },
  "devDependencies": {
    "@typescript-eslint/eslint-plugin": "^5.4.0",
    "@typescript-eslint/parser": "^5.4.0",
    "cross-env": "^7.0.3",
    "eslint": "^7.32.0",
    "eslint-config-airbnb": "^19.0.0",
    "eslint-config-prettier": "^8.3.0",
    "eslint-import-resolver-typescript": "^2.5.0",
    "eslint-plugin-import": "^2.25.3",
    "eslint-plugin-jest": "^25.2.4",
    "eslint-plugin-jsx-a11y": "^6.5.1",
    "eslint-plugin-react": "^7.27.0",
    "eslint-plugin-react-hooks": "^4.3.0",
    "husky": "^7.0.4",
    "lint-staged": "^12.0.2",
    "prettier": "^2.4.1"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "test:staged": "cross-env CI=true react-scripts test --env=jsdom --passWithNoTests",
    "eject": "react-scripts eject",
    "typescript": "tsc --project tsconfig.json --noEmit",
    "prettier": "prettier . --write",
    "lint": "eslint . --ext .ts --ext .tsx --fix",
    "lint-staged": "lint-staged"
  },
  "lint-staged": {
    "*.{ts,tsx,json,css}": "prettier --write",
    "*.{ts,tsx}": [
      "eslint --fix",
      "npm run test:staged"
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

和我的 .eslintrc.json:

{
  "env": {
    "browser": true,
    "es2021": true,
    "jest/globals": true
  },
  "extends": [
    "eslint:recommended",
    "plugin:react/recommended",
    "plugin:@typescript-eslint/recommended",
    "airbnb",
    "prettier"
  ],
  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "ecmaFeatures": {
      "jsx": true
    },
    "ecmaVersion": 12,
    "sourceType": "module"
  },
  "plugins": ["react", "@typescript-eslint", "jest"],
  "rules": {
    "import/extensions": [
      "error",
      "ignorePackages",
      {
        "ts": "never",
        "tsx": "never"
      }
    ],
    "react/jsx-filename-extension": [
      "warn",
      {
        "extensions": [".tsx"]
      }
    ],
    "no-use-before-define": "off",
    "@typescript-eslint/no-use-before-define": ["error"],
    "no-param-reassign": [
      "error",
      {
        "props": false
      }
    ],
    "no-console": "off",
    "@typescript-eslint/no-unused-vars": [
      "error",
      {
        "argsIgnorePattern": "^_",
        "varsIgnorePattern": "^_"
      }
    ]
  },
  "settings": {
    "import/resolver": {
      "typescript": {}
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*rán 19

恐怕您正在运行两个 ESLint 实例,每个实例具有不同的配置,让我解释一下原因。

创建 React 应用程序 ESLint

CRA已经为您设置了 ESLint(除其他外):

在幕后,我们使用 webpack、Babel、ESLint 和其他令人惊叹的项目来为您的应用程序提供支持。

start在 CRA 中,当您运行或命令时, ESLint 会在内部安装并运行build,因此您在开发应用程序或构建最终包时会在控制台中看到输出。您还可以在编辑器中获取 lint 输出。CRA 已经包含 ESLint 的默认配置,其中包括几个插件。

您的项目 ESLint

您已在项目中单独安装了 ESLint,并在您自己的.eslintrc.json. 那绝对没问题!这是检查项目的常用方法。运行此 ESLint 的方式是通过lint添加到脚本中的命令。


因此,当您运行start或 时build,您将使用 CRA 的 ESLint 实例和配置对项目进行 linting,但是当您运行时,lint您将使用 ESLint 实例和配置对项目进行 linting。他们的配置不匹配 100%,因此报告了不同的错误。

您可以通过运行检查是否安装了两个 ESLint 实例npm ls eslint,您将看到如下内容: npm 依赖关系树显示为项目和 CRA 安装的 ESLint

在那里您可以看到一个直接eslint依赖项(您手动安装的依赖项)和另一个eslint所属项react-scripts(由 CRA 作为子依赖项安装的依赖项)。

你怎么解决这个问题?

基本上你有两个选择:

  1. 删除 ESLint 并自定义 CRA ESLinteslint您可以从项目中卸载依赖项、删除自定义.eslintrc.json扩展 CRA ESLint config。这必须通过eslintConfig您的package.json. 您不需要将所有内容都放在那里,.eslintrc.json因为其中大部分内容已被 CRA 配置涵盖。这个选项的缺点是 1) 你不能按需对你的代码进行 lint,因为npm run lintCRA 不允许你这样做,2) 你被绑定到 CRA 使用的 ESLint 插件版本(例如,他们正在使用eslint-plugin-testing-libraryv3,但最新的是v5,不能使用)。
  2. 忽略 CRA 中的 ESLint(推荐)。这就是我通常做的事情。您可以选择退出 CRA 内置 ESLint 实例。为此,您需要.env在项目的根目录中创建一个文件,然后放入DISABLE_ESLINT_PLUGIN=true其中。之后,CRA 不会在运行start或 时对您的代码进行 lint 检测build,因此由您决定何时使用lint命令对其进行 lint 检测。理想情况下,您将lint在 CI 中运行该命令,并在每次使用lint-staged提交文件时在本地运行该命令(这对您来说可能听起来不太熟悉,如果您需要帮助来设置其中任何一个,请告诉我),除了即时获取通过代码编辑器反馈 ESLint 错误(设置 VSCode 或 WebStorm 应该非常简单)。

我希望这对你有帮助,如果你还有什么想讨论的,请告诉我!