Skip to content

GitLab

  • Projects
  • Groups
  • Snippets
  • Help
    • Loading...
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
T treasure
  • Project overview
    • Project overview
    • Details
    • Activity
    • Releases
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 12
    • Issues 12
    • List
    • Boards
    • Labels
    • Service Desk
    • Milestones
  • Merge requests 0
    • Merge requests 0
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Operations
    • Operations
    • Incidents
    • Environments
  • Packages & Registries
    • Packages & Registries
    • Container Registry
  • Analytics
    • Analytics
    • CI/CD
    • Repository
    • Value Stream
  • Wiki
    • Wiki
  • External wiki
    • External wiki
  • Snippets
    • Snippets
  • Members
    • Members
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • FE
  • treasure
  • Issues
  • #144

Closed
Open
Created Mar 27, 2023 by liuyajun@liuyajunMaintainer

VSCode 中保存时格式化代码出现反复缩进的问题

背景

vscode 保存项目时,eslint 进行格式化代码后会出现反复缩进的问题

image

原因

VSCode 同时使用了 ESLint 插件和 Prettier 插件,导致保存时同时进行了 eslint 格式化和 prettier 格式化。

解决方法

  1. 关闭 Prettier 插件
  2. 修改 vscode 配置
    • 关闭全局的保存格式化配置:"editor.formatOnSave": false,
    • 开启只使用 eslint 格式化
      "editor.codeActionsOnSave": {
        "source.fixAll": false,
        "source.fixAll.eslint": true,
        "source.fixAll.stylelint": true,
      },

项目统一配置

为了防止其他项目的开发者出现问题,建议统一项目配置,将配置代码保存到项目的 .vscode/settings.json 文件内,上传到 git

.vscode/settings.json

{
  "editor.codeActionsOnSave": {
    "source.fixAll": false,
    "source.fixAll.eslint": true,
    "source.fixAll.stylelint": true,
  },
  "editor.quickSuggestions": {
    "other": true,
    "comments": true,
    "strings": true
  },
  "editor.tabSize": 2,
  "editor.formatOnSave": false, // 如果开启,保存时的 eslint 无法正常使用

  // eslint 保存文件时是否自动修复
  "eslint.alwaysShowStatus": true,
  "eslint.codeActionsOnSave.mode": "all", // all 一次性修复所有可能的问题,problems 只修复当前的问题(逐个修复)
  "eslint.validate": [
    "javascript",
    "javascriptreact",
    "vue-html",
    "html",
    "vue",
    "wpy",
    "wxml",
    "wxss",
    "cjson",
    "wxs",
    "typescript",
    "typescriptreact",
    // {
    //   "language": "vue-html",
    //   "autoFix": true
    // },
    // {
    //   "language": "html",
    //   "autoFix": true
    // },
    // {
    //   "language": "vue", // 保存vue文件时是否自动修复,基于 eslint.autoFixOnSave 设置
    //   "autoFix": true
    // }
  ],

}
Edited Mar 27, 2023 by liuyajun
Assignee
Assign to
None
Milestone
None
Assign milestone
Time tracking