如何将Prettier Config添加到Eslint配置中?

发布于 2025-02-10 20:52:43 字数 2643 浏览 1 评论 0原文

请注意,我不想在我的JS项目中使用的分号。

youtube视频

我尝试将其禁用在.eslintrc中。 CJS文件,但是奇怪的是,semi:0无效地禁用缺少;的警告。但是,semi:false确实禁用了VSCODE中的警告。

但是,我的问题似乎与漂亮的问题有关,因此我将Prettier配置添加到我的eslint文件中,并使用semi:semi:false在其中工作。但是,当我运行npm运行lint时,我会收到以下错误:

“在此处输入图像说明”

我的config

/* eslint-env node */
require("@rushstack/eslint-patch/modern-module-resolution")

module.exports = {
  root: true,
  extends: [
    "plugin:vue/vue3-essential",
    "eslint:recommended",
    "@vue/eslint-config-prettier",
  ],
  rules: {
    semi: 0,
  },
  prettier: {
    rules: {
      semi: false
    },
  },
  overrides: [
    {
      files: ["cypress/e2e/**.{cy,spec}.{js,ts,jsx,tsx}"],
      extends: ["plugin:cypress/recommended"],
    },
  ],
};

如果我删除该漂亮的块注意,我将再次获取警告在我的代码中:

“在此处输入图像描述”

我的package.json文件(VUE创建)

{
  "name": "moonholdings.xyz",
  "version": "0.0.0",
  "scripts": {
    "dev": "vite",
    "build": "run-p type-check build-only",
    "preview": "vite preview --port 4173",
    "test:unit": "vitest --environment jsdom",
    "test:e2e": "start-server-and-test preview http://127.0.0.1:4173/ 'cypress open --e2e'",
    "test:e2e:ci": "start-server-and-test preview http://127.0.0.1:4173/ 'cypress run --e2e'",
    "build-only": "vite build",
    "prettier": "prettier --write .",
    "lint": "eslint . && prettier --check ."
  },
  "dependencies": {
    "pinia": "^2.0.14",
    "vue": "^3.2.36",
    "vue-router": "^4.0.15"
  },
  "devDependencies": {
    "@rushstack/eslint-patch": "^1.1.0",
    "@vitejs/plugin-vue": "^2.3.3",
    "@vitejs/plugin-vue-jsx": "^1.3.10",
    "@vue/eslint-config-prettier": "^7.0.0",
    "@vue/test-utils": "^2.0.0",
    "cypress": "^10.0.2",
    "eslint": "^8.5.0",
    "eslint-plugin-cypress": "^2.12.1",
    "eslint-plugin-vue": "^9.0.0",
    "jsdom": "^19.0.0",
    "npm-run-all": "^4.1.5",
    "prettier": "^2.5.1",
    "sass": "^1.53.0",
    "start-server-and-test": "^1.14.0",
    "vite": "^2.9.9",
    "vitest": "^0.13.0",
    "vue-tsc": "^0.35.2"
  }
}

Note, I do not want semicolons used in my JS project.

YouTube video

I tried disabling it in the .eslintrc.cjs file, however the strange thing is that semi: 0 did not work to disable the warnings over missing ;. However semi: false did disable the warnings in VScode.

However it seemed my problem was related to prettier, so I added the prettier config to my eslint file and used semi: false in there it worked. But when I run npm run lint I get the following error:

enter image description here

My config

/* eslint-env node */
require("@rushstack/eslint-patch/modern-module-resolution")

module.exports = {
  root: true,
  extends: [
    "plugin:vue/vue3-essential",
    "eslint:recommended",
    "@vue/eslint-config-prettier",
  ],
  rules: {
    semi: 0,
  },
  prettier: {
    rules: {
      semi: false
    },
  },
  overrides: [
    {
      files: ["cypress/e2e/**.{cy,spec}.{js,ts,jsx,tsx}"],
      extends: ["plugin:cypress/recommended"],
    },
  ],
};

If I remove that prettier block note I will get the warnings again in my code:

enter image description here

My package.json file (created by Vue)

{
  "name": "moonholdings.xyz",
  "version": "0.0.0",
  "scripts": {
    "dev": "vite",
    "build": "run-p type-check build-only",
    "preview": "vite preview --port 4173",
    "test:unit": "vitest --environment jsdom",
    "test:e2e": "start-server-and-test preview http://127.0.0.1:4173/ 'cypress open --e2e'",
    "test:e2e:ci": "start-server-and-test preview http://127.0.0.1:4173/ 'cypress run --e2e'",
    "build-only": "vite build",
    "prettier": "prettier --write .",
    "lint": "eslint . && prettier --check ."
  },
  "dependencies": {
    "pinia": "^2.0.14",
    "vue": "^3.2.36",
    "vue-router": "^4.0.15"
  },
  "devDependencies": {
    "@rushstack/eslint-patch": "^1.1.0",
    "@vitejs/plugin-vue": "^2.3.3",
    "@vitejs/plugin-vue-jsx": "^1.3.10",
    "@vue/eslint-config-prettier": "^7.0.0",
    "@vue/test-utils": "^2.0.0",
    "cypress": "^10.0.2",
    "eslint": "^8.5.0",
    "eslint-plugin-cypress": "^2.12.1",
    "eslint-plugin-vue": "^9.0.0",
    "jsdom": "^19.0.0",
    "npm-run-all": "^4.1.5",
    "prettier": "^2.5.1",
    "sass": "^1.53.0",
    "start-server-and-test": "^1.14.0",
    "vite": "^2.9.9",
    "vitest": "^0.13.0",
    "vue-tsc": "^0.35.2"
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

水晶透心 2025-02-17 20:52:43

在.eslintrc文件中添加带有更漂亮的设置的“漂亮/漂亮”规则:

"rules": {
        "react/react-in-jsx-scope": "off",
        "prettier/prettier": [
            "error",
            {
              "trailingComma": "all",
              "tabWidth": 12,
              "semi": false,
              "singleQuote": true,
              "bracketSpacing": true,
              "eslintIntegration": true,
              "printWidth": 120
            }
          ]
    }

Add "prettier/prettier" rule with prettier settings to your .eslintrc file:

"rules": {
        "react/react-in-jsx-scope": "off",
        "prettier/prettier": [
            "error",
            {
              "trailingComma": "all",
              "tabWidth": 12,
              "semi": false,
              "singleQuote": true,
              "bracketSpacing": true,
              "eslintIntegration": true,
              "printWidth": 120
            }
          ]
    }
偏爱你一生 2025-02-17 20:52:43

弄清楚了,需要禁用vscode 中的 eslint扩展名,它覆盖了我的配置文件。此外,不需要此扩展名。

Figured it out, needed to disable the ESlint extension in VScode, it was overriding my config files. Also this extension isn't needed.

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文